Пример #1
0
void clustering(string filename) {
	Mat image = imread(filename);

	Preprocessor* pr = new Preprocessor(image);
    pr->preprocess(10);

    Mat cover;

    if(!pr->warpObject()) {
    	cout << "#WARP-UNSUCCESSFUL" << endl;
    	cover = image.clone();
    	image.release();
    	Size newSize = (cover.size().width > cover.size().height) ? Size(1024, 768) : Size(768, 1024);
    	resize(cover, cover, newSize);
    	cout << "using the whole image..." << endl;
    } else {
    	cover = pr->WarpedCover();
    	string nf = prependStringToFileName("to_swt_", filename);
    	imwrite(nf, cover);
    	cout << "#BEGIN-TO-SWT" << endl;
    	cout << nf << endl;
    	cout << "#END-TO-SWT" << endl;
    }

	cout << "#BEGIN-CLUSTERVECTOR" << endl;
	cout << Preprocessor::calculateLAB(40, cover) << endl;
	cout << "#END-CLUSTERVECTOR" << endl;
}
Пример #2
0
TEST(segmentor_unittest, preprocess3) {
  Preprocessor preprocessor;

  std::vector<std::string> raw_forms;
  std::vector<std::string> forms;
  std::vector<int> chartypes;
  int retval = preprocessor.preprocess("中文mix单词", raw_forms, forms, chartypes);
  EXPECT_EQ(retval, 5);

  EXPECT_EQ(raw_forms.size(), 5);
  EXPECT_STREQ(raw_forms[0].c_str(), "中");
  EXPECT_STREQ(raw_forms[1].c_str(), "文");
  EXPECT_STREQ(raw_forms[2].c_str(), "mix");
  EXPECT_STREQ(raw_forms[3].c_str(), "单");
  EXPECT_STREQ(raw_forms[4].c_str(), "词");

  EXPECT_EQ(forms.size(), 5);
  EXPECT_STREQ(forms[0].c_str(), "中");
  EXPECT_STREQ(forms[1].c_str(), "文");
  EXPECT_STREQ(forms[2].c_str(), "_eng_");
  EXPECT_STREQ(forms[3].c_str(), "单");
  EXPECT_STREQ(forms[4].c_str(), "词");

  EXPECT_EQ(chartypes.size(), 5);
  EXPECT_EQ(chartypes[0], CHAR_OTHER);
  EXPECT_EQ(chartypes[1], CHAR_OTHER|preprocessor.HAS_ENG_ON_RIGHT);
  EXPECT_EQ(chartypes[2], Preprocessor::CHAR_ENG);
  EXPECT_EQ(chartypes[3], CHAR_OTHER|preprocessor.HAS_ENG_ON_LEFT);
  EXPECT_EQ(chartypes[4], CHAR_OTHER);
}
Пример #3
0
TEST(segmentor_unittest, preprocess2) {
  Preprocessor preprocessor;

  std::vector<std::string> raw_forms;
  std::vector<std::string> forms;
  std::vector<int> chartypes;
  int retval = preprocessor.preprocess("中文 单  词", raw_forms, forms, chartypes);
  EXPECT_EQ(retval, 4);

  EXPECT_EQ(raw_forms.size(), 4);
  EXPECT_STREQ(raw_forms[0].c_str(), "中");
  EXPECT_STREQ(raw_forms[1].c_str(), "文");
  EXPECT_STREQ(raw_forms[2].c_str(), "单");
  EXPECT_STREQ(raw_forms[3].c_str(), "词");

  EXPECT_EQ(forms.size(), 4);
  EXPECT_STREQ(forms[0].c_str(), "中");
  EXPECT_STREQ(forms[1].c_str(), "文");
  EXPECT_STREQ(forms[2].c_str(), "单");
  EXPECT_STREQ(forms[3].c_str(), "词");

  EXPECT_EQ(chartypes.size(), 4);
  EXPECT_EQ(chartypes[0], CHAR_OTHER);
  EXPECT_EQ(chartypes[1], CHAR_OTHER|preprocessor.HAS_SPACE_ON_RIGHT);
  EXPECT_EQ(chartypes[2], 
      CHAR_OTHER|preprocessor.HAS_SPACE_ON_LEFT|preprocessor.HAS_SPACE_ON_RIGHT);
  EXPECT_EQ(chartypes[3], CHAR_OTHER|preprocessor.HAS_SPACE_ON_LEFT);
}
Пример #4
0
TEST(segmentor_unittest, preprocess5) {
  Preprocessor preprocessor;

  std::vector<std::string> raw_forms;
  std::vector<std::string> forms;
  std::vector<int> chartypes;
  int retval = preprocessor.preprocess("Python日报 2015-05-12 http://t.cn/RAsCOoK",
      raw_forms, forms, chartypes);
  EXPECT_EQ(retval, 5);

  EXPECT_EQ(raw_forms.size(), 5);
  EXPECT_STREQ(raw_forms[0].c_str(), "Python");
  EXPECT_STREQ(raw_forms[3].c_str(), "2015-05-12");
  EXPECT_STREQ(raw_forms[4].c_str(), "http://t.cn/RAsCOoK");

  EXPECT_EQ(forms.size(), 5);
  EXPECT_STREQ(forms[0].c_str(), "_eng_");
  EXPECT_STREQ(forms[3].c_str(), "_eng_");
  EXPECT_STREQ(forms[4].c_str(), "_uri_");

  EXPECT_EQ(chartypes.size(), 5);
  EXPECT_EQ(chartypes[0], Preprocessor::CHAR_ENG);
  EXPECT_EQ(chartypes[2], CHAR_OTHER|preprocessor.HAS_SPACE_ON_RIGHT);
  EXPECT_EQ(chartypes[3],
      Preprocessor::CHAR_ENG|preprocessor.HAS_SPACE_ON_LEFT|preprocessor.HAS_SPACE_ON_RIGHT);
  EXPECT_EQ(chartypes[4], Preprocessor::CHAR_URI|preprocessor.HAS_SPACE_ON_LEFT);
}
Пример #5
0
TEST(segmentor_unittest, preprocess4) {
  Preprocessor preprocessor;

  std::vector<std::string> raw_forms;
  std::vector<std::string> forms;
  std::vector<int> chartypes;
  int retval = preprocessor.preprocess("i like reading", raw_forms, forms, chartypes);
  EXPECT_EQ(retval, 3);

  EXPECT_EQ(raw_forms.size(), 3);
  EXPECT_STREQ(raw_forms[0].c_str(), "i");
  EXPECT_STREQ(raw_forms[1].c_str(), "like");
  EXPECT_STREQ(raw_forms[2].c_str(), "reading");

  EXPECT_EQ(forms.size(), 3);
  EXPECT_STREQ(forms[0].c_str(), "_eng_");
  EXPECT_STREQ(forms[1].c_str(), "_eng_");
  EXPECT_STREQ(forms[2].c_str(), "_eng_");

  EXPECT_EQ(chartypes.size(), 3);
  EXPECT_EQ(chartypes[0], Preprocessor::CHAR_ENG|preprocessor.HAS_SPACE_ON_RIGHT);
  EXPECT_EQ(chartypes[1],
      Preprocessor::CHAR_ENG|preprocessor.HAS_SPACE_ON_LEFT|preprocessor.HAS_SPACE_ON_RIGHT);
  EXPECT_EQ(chartypes[2], Preprocessor::CHAR_ENG|preprocessor.HAS_SPACE_ON_LEFT);
}
int Tester::test(string name, bool withoutTokens) {
    try {
        Parser parser;
        Preprocessor preproc;
        preproc.setIncludeDirectory("");
        vector<Token*> tokens = parser.parse(preproc.preprocess(name + ".cmm", "", "../the_best_compilator_ever/cmmlibs/"));

        std::ofstream log;
        log.open ("../the_best_compilator_ever/tester/log.txt",  std::ios_base::app);

        log << "======TESTING::" + name + "\n\n---TOKENS:";

         string output = CodeGenerator::generate(tokens);

         if (withoutTokens){
            log << "KEY : WithoutTokens\nOutput is ";
            log << (output != "" ? "not " : "") << "empty\n";
         } else {
            log << output << '\n';
         }

         if (output != "") {
             fileFromString(name + ".asm", output);

             log << "-----EXECUTION\n";

             string yasm = exec("yasm -felf64 -dgwarf2 "+ name + ".asm -o  "+ name + ".o");
             log << (yasm == "" ? "YASM OK" : "YASM FAILED") << "\n";
             string gcc = (exec("gcc "+ name + ".o -o " + name));
             log << (gcc == "" ? "GCC OK" : "GCC FAILED") << "\n";
             log << "Execution is finished\n";
         } else {
             std::cout << "\n------Parser output is empty, " + name + "\n";
             return 1;
         }
    }  catch (ParsingException& ignored) {
        std::cout << "\n------Exception " + std::string(ignored.what()) + "\n";
        return 1;
    }

    return 0;
}
Пример #7
0
TEST(segmentor_unittest, preprocess6) {
  Preprocessor preprocessor;

  std::vector<std::string> raw_forms;
  std::vector<std::string> forms;
  std::vector<int> chartypes;
  int retval = preprocessor.preprocess("套头衫/Jacquad Sweater",
      raw_forms, forms, chartypes);
  EXPECT_EQ(retval, 6);

  EXPECT_EQ(raw_forms.size(), 6);
  EXPECT_STREQ(raw_forms[3].c_str(), "/");
  EXPECT_STREQ(raw_forms[4].c_str(), "Jacquad");
  EXPECT_STREQ(raw_forms[5].c_str(), "Sweater");

  EXPECT_EQ(chartypes.size(), 6);
  EXPECT_EQ(chartypes[3], CHAR_PUNC|preprocessor.HAS_ENG_ON_RIGHT);
  EXPECT_EQ(chartypes[4], Preprocessor::CHAR_ENG|preprocessor.HAS_SPACE_ON_RIGHT);
  EXPECT_EQ(chartypes[5], Preprocessor::CHAR_ENG|preprocessor.HAS_SPACE_ON_LEFT);
}
Пример #8
0
void recognizing(string filename, vector<uint> ids) {

	Mat image = imread(filename);

	Recognizer* rcg = new Recognizer(0.70f, 0.065f);
	Preprocessor* pr = new Preprocessor(image);
    pr->preprocess(10);

    Mat cover;

    if(!pr->warpObject()) {
    	cout << "#WARP-UNSUCCESSFUL" << endl;
    	cover = image.clone();
    	image.release();
    	Size newSize = (cover.size().width > cover.size().height) ? Size(1024, 768) : Size(768, 1024);
    	resize(cover, cover, newSize);
    	cout << "using the whole image..." << endl;
    } else {
    	cover = pr->WarpedCover();
    }

    Mat trainingImage;

    for(size_t i = 0; i < ids.size(); ++i) {
    	cout << "training " << i << endl;
    	rcg->train(ids.at(i));
	}

	if(rcg->recognize(Preprocessor::kuwaharaNagaoFilter(cover)))
	{
   		Book result = rcg->LastResult();
        cout << "#BEGIN-RESULT " << result.Id() << " #END-RESULT";
    } else {
    	cout << "#BEGIN-RESULT null #END-RESULT";
    }
    cout << endl;
}
Пример #9
0
int
main(int argc, char* argv[])
{
    vector<string> cppArgs;
    //bool preprocess;
    //string include;
    //string output;
    //string dllExport;
    //bool impl;
    //bool depend;
    //bool debug;
    //bool ice;
    //bool checksum;
    //bool stream;
    //bool caseSensitive;

    string inputFilename;

#ifdef false
    po::variables_map vm;

    //try
    //{

    po::options_description hidden("Hidden options");
    hidden.add_options()
        ("input-file", po::value< vector<string> >(), "input file")
        ;


    po::options_description allowed("Allowed options");
    allowed.add_options()
        ("help", "produce help message")
        ("output", po::value<std::string>(), "output file name")
        ;

    po::options_description cmdline("Command line options");
    cmdline.add(allowed).add(hidden);

    po::positional_options_description p;
    p.add("input-file", -1);

    po::store(po::command_line_parser(argc, argv).
              options(cmdline).positional(p).run(), vm);

    po::notify(vm);

    if (vm.count("help"))
    {
        cout << allowed << "\n";
        return 1;
    }

    if ( vm.count("input-file"))
    {
        vector<string> sv = vm["input-file"].as<vector<string> >();
        //for (vector<string>::iterator i=sv.begin(); i!=sv.end(); i++)
        //{
        //cout << *i << "\n";
        //}

        inputFilename = sv[0];
    }

    if (vm.count("output"))
    {
        cout << "Output filename is " << vm["output"].as<std::string>() << "\n";
    }

    //}
    //catch(...)
    //{
    //cout << "CRAP!\n";
    //}


    cout << "Input file is \"" << inputFilename << "\"\n";

#endif

    IceUtilInternal::Options opts;
    opts.addOpt("h", "help");
    opts.addOpt("I", "", IceUtilInternal::Options::NeedArg, "",
                IceUtilInternal::Options::Repeat);


    vector<string> args;
    try
    {
        args = opts.parse(argc, argv);
    }
    catch(const IceUtilInternal::BadOptException& e)
    {
        cerr << argv[0] << ": " << e.reason << endl;
        usage(argv[0]);
        return EXIT_FAILURE;
    }

    if(opts.isSet("h") || opts.isSet("help"))
    {
        usage(argv[0]);
        return EXIT_SUCCESS;
    }

    vector<string> includePaths;
    includePaths = opts.argVec("I");
    for (vector<string>::const_iterator i=includePaths.begin(); i!=includePaths.end(); ++i)
    {
        cppArgs.push_back("-I" + Preprocessor::normalizeIncludePath(*i));
    }

    if(args.empty())
    {
        cerr << argv[0] << ": no input file" << endl;
        usage(argv[0]);
        return EXIT_FAILURE;
    }

    int status = EXIT_SUCCESS;

    vector<string>::const_iterator i = args.begin();
    std::string filename = *i;
#if ICE_INT_VERSION / 100 < 304
    Preprocessor* icecpp = new Preprocessor(argv[0], *i, cppArgs);
#else
    PreprocessorPtr icecpp = Preprocessor::create(argv[0], *i, cppArgs);
#endif

    FILE* cppHandle = icecpp->preprocess(true);

    if (cppHandle == 0)
    {
        return EXIT_FAILURE;
    }

//#if ICE_INT_VERSION / 100 < 304
    bool ignRedefs = false;
    bool all = false;
    bool allowIcePrefix = false;
    bool caseSensitive = true;
    //StringList defaultGlobalMetadata;
    UnitPtr u = Unit::createUnit(ignRedefs, all, allowIcePrefix, caseSensitive);
//#else
//      UnitPtr u = Unit::createUnit(false, false, false);
//#endif

    int parseStatus = u->parse(*i, cppHandle, false);

    if ( parseStatus == EXIT_FAILURE )
    {
        cerr << "Parsing failed";
        exit(0);
    }

    if(!icecpp->close())
    {
        u->destroy();
        return EXIT_FAILURE;
    }


    BasicClassLinkHandler linker;
    RstGen rstGen(linker);
    u->visit(&rstGen, true);

    u->destroy();

    return status;
}
Пример #10
0
int main(int argc, char *argv[])
{
    using namespace std;
    bool print_tokens = false, print_ast = false, no_run = false; 
    // Refer whether tokens and ast need to be printed. 
    int name_count = 0;
    // Count the filename inputed.
    string filename;
    // Save the name of file.
    string raw;

    if (argc < 2)
    {
        // A correct call of rune must given 2 or more args;
        show_help();
        return -2;
    }
    else
    {
        for (int i = 1; i < argc; ++i)
        {
            if (argv[i][0] == '-')
            {
                switch(argv[i][1])
                {
                case 't':
                    print_tokens = true;
                    break;
                case 'a':
                    print_ast = true;
                    break;
                case 'n':
                    no_run = true;
                    break;
                default:
                    cout << "unrecognized command line option \'"
                         << argv[i] << '\'';  
                    show_help();
                    return -2;
                }
            }
            else
            {
                filename = argv[i];
                name_count++;
            }
        }
        if (name_count != 1)
        {
            // if more than 1 filenames were inputed, show help and exit.
            show_help();
            return -2;
        }
    }
    raw = read_all_from_file(filename);
    if (raw.size() == 0)
    {
        // **There is a pitfall.
        // **I'm not sure how to get known the existance of file.
        // **So, deal as same when file not exist and empty.
        cout << "file not exist or nothing contained in file.";
        return -3;
    }
    try
    {
        Preprocessor pre;
        vector<string> lines = pre.preprocess(raw);
        // Raw string preprocessed to decommented lines.
        Scanner scn;
        vector<Token> tokens = scn.scan(lines);
        // Scan the lines to a vector of Tokens.
        // Token is defined in "tokenEnum.h".
        if (print_tokens)
        {
            show_tokens(tokens);
        }
        // print Tokens if has option "-tokens".
        Parser psr;
        auto ast = psr.parse(tokens);
        // Parse the Tokens to an Abstract Syntax Tree.
        // AST is defined in "syntexTreeEnum.h".
        if (print_ast)
        {
            cout << "SYNTAX TREE:\n";
            psr.show_tree(ast);
        }
        // print AST if has option "-ast".
        if (!no_run)
        {
            Interpreter itp;
            itp.execute(ast);
            // Interprete the AST.
        }
    } 
    catch (string msg)
    {
        cout << msg << endl;
        return -1;
        // print error message.
    }
	return 0;
}