int main(int argc, char *argv[]) { string model_fn; string input_fn("-"); string output_fn; int opt; while ((opt = getopt(argc, argv, "i:o:m:"))) { switch (opt) { case 'i': input_fn = optarg; break; case 'o': output_fn = optarg; break; case 'm': output_fn = optarg; break; } } istream* is; ifstream ifs; if (input_fn == "-") { is = &cin; } else { ifs.open(input_fn.c_str()); if (!ifs) { cerr << "failed to open input file: " << input_fn << endl; return -1; } is = &ifs; } char model_args[PATH_MAX]; snprintf(model_args, sizeof(model_args), "-m %s", model_fn.c_str()); CRFPP::Tagger *tagger = CRFPP::createTagger(model_args); if (!tagger) { cerr << "failed to create tagger: " << CRFPP::getTaggerError() << endl; return -1; } ofstream output(output_fn.c_str()); if (!output) { cerr << "failed to open output file: " << output_fn << endl; return -1; } string line; while (getline(*is, line, '\n')) { if (line.empty()) continue; tagger->clear(); segment(tagger, line, output); } }
void CRF::classify(const FunctionCallbackInfo<Value>& args){ Isolate* isolate = args.GetIsolate(); CRF* obj = (CRF *) ObjectWrap::Unwrap<CRF>(args.Holder()); v8::Persistent<v8::External, v8::CopyablePersistentTraits<v8::External> > tagger = obj->tagger; v8::Local<v8::External> handle = v8::Local<v8::External>::New(isolate, tagger); CRFPP::Tagger *a = (CRFPP::Tagger *) handle->Value(); if (a==NULL) { fprintf(stderr,"No tagger. Exiting...\n"); return; } a->clear(); Local<Array> arr = Local<Array>::Cast(args[0]); int size = arr->Length(); for(int i=0;i<size;i++){ Local<Value> element = arr->Get(i); char* c = get(element); a->add(c); // each iteration, free memory free(c); c = NULL; } bool isParsed = a->parse(); if ( isParsed == false) { fprintf(stderr,"Failed to parse.\n"); } Local<Array> solutions = Array::New(isolate, a->nbest()); unsigned int count = 0; while (count < a->nbest()) { Local<Array> s = Array::New(isolate, a->ysize()); for (size_t i = 0; i < a->size(); ++i) { s -> Set(i,Local<Value>(String::NewFromUtf8(isolate, a->y2(i)))); } solutions -> Set(count, s); a->next(); count++; } args.GetReturnValue().Set(solutions); };
Handle<Value> CRF::classify(const Arguments& args){ HandleScope scope; Local<Array> arr = Local<Array>::Cast(args[0]); CRF* obj = ObjectWrap::Unwrap<CRF>(args.This()); CRFPP::Tagger *a = *(obj->tagger); a->clear(); int size = arr->Length(); for(int i=0;i<size;i++){ Local<Value> element = arr->Get(i); char* c = get(element); a->add(c); // each iteration, free memory free(c); c = NULL; } a->parse(); Local<Array> solutions = Array::New(a->nbest()); unsigned int count = 0; while (count < a->nbest()) { Local<Array> s = Array::New(a->ysize()); for (size_t i = 0; i < a->size(); ++i) { s -> Set(i,Local<Value>(String::New(a->y2(i)))); } solutions -> Set(count, s); a->next(); count++; } return scope.Close(solutions); };