void CRF::New(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = args.GetIsolate(); if (args.IsConstructCall()) { // Invoked as constructor: `new CRF(...)` CRF* obj = new CRF(); CRFPP::Tagger* tag = CRFPP::createTagger(get(args[0])); if(!tag){ isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, (const char *) CRFPP::getTaggerError()))); return; } v8::Local<v8::External> handle = v8::External::New(isolate, tag); v8::Persistent<v8::External, v8::CopyablePersistentTraits<v8::External> > tagger(isolate, handle); obj -> tagger = tagger; obj->Wrap(args.This()); args.GetReturnValue().Set(args.This()); } else { const int argc = 1; Local<Value> argv[argc] = { args[0] }; Local<Function> cons = Local<Function>::New(isolate, constructor); args.GetReturnValue().Set(cons->NewInstance(argc, argv)); } }
VectorFactorsExtractor::VectorFactorsExtractor(const CRF& i_crf, vector<double>* o_parameters) { o_parameters->clear(); const Factors& factors = i_crf.GetParameters(); const vector<double>& initialWeights = factors.GetInitialWeights(); const vector<double>& finalWeights = factors.GetFinalWeights(); const vector<double>& defaultWeights = factors.GetDefaultWeights(); numberOfLabels = i_crf.GetOutputAlphabet().Size(); numberOfFeatures = i_crf.GetInputAlphabet().Size(); numberOfCliques = i_crf.GetWeightsAlphabet().Size(); // Insert intial, final and default weights dimension = 2 * numberOfLabels + numberOfCliques; o_parameters->resize(dimension); std::copy(initialWeights.begin(), initialWeights.end(), o_parameters->begin()); std::copy(finalWeights.begin(), finalWeights.end(), o_parameters->begin() + numberOfLabels); std::copy(defaultWeights.begin(), defaultWeights.end(), o_parameters->begin() + 2 * numberOfLabels); cliqueFeatureConverter.resize(numberOfCliques); const vector<unordered_map<int, double> >& cliquesData = factors.GetWeights(); // Iterate over cliques for (size_t cliqueIndex = 0; cliqueIndex < cliquesData.size(); ++cliqueIndex) { unordered_map<int, int> featureToPositionInVector; const unordered_map<int, double>& currentCliquesData = cliquesData[cliqueIndex]; for (auto iter = currentCliquesData.begin(); iter != currentCliquesData.end(); ++iter) { o_parameters->push_back(iter->second); featureToPositionInVector[iter->first] = dimension; ++dimension; } cliqueFeatureConverter[cliqueIndex] = std::move(featureToPositionInVector); printf("\rVectorFactorsExtractor evaluated %zu cliques", cliqueIndex); } }
Handle<Value> CRF::New(const Arguments& args) { HandleScope scope; if (!args.IsConstructCall()) { return ThrowException(Exception::TypeError( String::New("Use the new operator to create instances of this object.")) ); } if (args.Length() < 1) { return ThrowException(Exception::TypeError( String::New("First argument must be the path to the model"))); } // Creates a new instance object of this type and wraps it. CRF* obj = new CRF(); CRFPP::Tagger* tag = CRFPP::createTagger(get(args[0])); if(!tag){ return ThrowException(Exception::TypeError( String::New(CRFPP::getTaggerError()))); } obj -> tagger = Persistent<CRFPP::Tagger>(tag); obj->Wrap(args.This()); return args.This(); }
void CRFBinaryWriter::Write(const CRF& crf) const { std::ofstream out(file, std::ios::binary); Tools::SetLocale(out); if (!out) { std::wcout << L"Can't write to " << Tools::ConvertStringToWstring(file) << L" " << file.size() << std::endl; exit(1); } // Printing alphabets; crf.GetInputAlphabet().Print(out); crf.GetOutputAlphabet().Print(out); crf.GetWeightsAlphabet().Print(out); // Printing States; int stateSize = crf.GetNumStates(); out.write((char*)&stateSize, sizeof(int)); for (int stateIndex = 0; stateIndex < stateSize; stateIndex++) { const State& state = crf.GetStates()[stateIndex]; double initialWeight = crf.GetInitialWeights()[stateIndex]; out.write((char*)&initialWeight, sizeof(double)); double finalWeight = crf.GetFinalWeights()[stateIndex]; out.write((char*)&finalWeight, sizeof(double)); int numDestinations = state.GetNumDestinations(); out.write((char*)&numDestinations, sizeof(int)); for (int destIndex = 0; destIndex < numDestinations; ++destIndex) { out.write((char*)&state.GetDestinations()[destIndex], sizeof(int)); int weightIndexesLength = state.GetWeightsIndexes()[destIndex].size(); out.write((char*)&weightIndexesLength, sizeof(int)); for (int weightIndex = 0; weightIndex < weightIndexesLength; ++weightIndex) { out.write((char*)&state.GetWeightsIndexes()[destIndex][weightIndex], sizeof(int)); } } } // Printing weights; const Factors& parameters = crf.GetParameters(); for (int widx = 0; widx < parameters.GetWeightAlphabet().Size(); ++widx) { double defaultWeight = parameters.GetDefaultWeights()[widx]; out.write((char*)&defaultWeight, sizeof(double)); const unordered_map<int, double>& transitionWeights = parameters.GetWeights()[widx]; int size = transitionWeights.size(); out.write((char*)&size, sizeof(int)); for (auto item : transitionWeights) { int index = item.first; double value = item.second; out.write((char*)&index, sizeof(int)); out.write((char*)&value, sizeof(double)); } } out.close(); }