/* ************************************************************************* */ Ordering Ordering::ColamdConstrainedFirst(const VariableIndex& variableIndex, const std::vector<Key>& constrainFirst, bool forceOrder) { gttic(Ordering_COLAMDConstrainedFirst); const int none = -1; size_t n = variableIndex.size(); std::vector<int> cmember(n, none); // Build a mapping to look up sorted Key indices by Key FastMap<Key, size_t> keyIndices; size_t j = 0; for (auto key_factors: variableIndex) keyIndices.insert(keyIndices.end(), make_pair(key_factors.first, j++)); // If at least some variables are not constrained to be last, constrain the // ones that should be constrained. int group = 0; for (Key key: constrainFirst) { cmember[keyIndices.at(key)] = group; if (forceOrder) ++group; } if (!forceOrder && !constrainFirst.empty()) ++group; for(int& c: cmember) if (c == none) c = group; return Ordering::ColamdConstrained(variableIndex, cmember); }
/* ************************************************************************* */ Ordering Ordering::ColamdConstrainedLast(const VariableIndex& variableIndex, const std::vector<Key>& constrainLast, bool forceOrder) { gttic(Ordering_COLAMDConstrainedLast); size_t n = variableIndex.size(); std::vector<int> cmember(n, 0); // Build a mapping to look up sorted Key indices by Key // TODO(frank): think of a way to not build this FastMap<Key, size_t> keyIndices; size_t j = 0; for (auto key_factors: variableIndex) keyIndices.insert(keyIndices.end(), make_pair(key_factors.first, j++)); // If at least some variables are not constrained to be last, constrain the // ones that should be constrained. int group = (constrainLast.size() != n ? 1 : 0); for (Key key: constrainLast) { cmember[keyIndices.at(key)] = group; if (forceOrder) ++group; } return Ordering::ColamdConstrained(variableIndex, cmember); }
bool is_terminal_label(const Label label) { assert(open_label_list); assert(label != NO_LABEL); assert(label < label_list.size()); assert(constituent_set.exists(label) == !terminal_set.exists(label)); return terminal_set.exists(label); }
/* ************************************************************************* */ JointMarginal Marginals::jointMarginalInformation(const std::vector<Key>& variables) const { // If 2 variables, we can use the BayesTree::joint function, otherwise we // have to use sequential elimination. if(variables.size() == 1) { Matrix info = marginalInformation(variables.front()); std::vector<size_t> dims; dims.push_back(info.rows()); Ordering indices; indices.insert(variables.front(), 0); return JointMarginal(info, dims, indices); } else { // Obtain requested variables as ordered indices vector<Index> indices(variables.size()); for(size_t i=0; i<variables.size(); ++i) { indices[i] = ordering_[variables[i]]; } // Compute joint marginal factor graph. GaussianFactorGraph jointFG; if(variables.size() == 2) { if(factorization_ == CHOLESKY) jointFG = *bayesTree_.joint(indices[0], indices[1], EliminatePreferCholesky); else if(factorization_ == QR) jointFG = *bayesTree_.joint(indices[0], indices[1], EliminateQR); } else { if(factorization_ == CHOLESKY) jointFG = *GaussianSequentialSolver(graph_, false).jointFactorGraph(indices); else if(factorization_ == QR) jointFG = *GaussianSequentialSolver(graph_, true).jointFactorGraph(indices); } // Build map from variable keys to position in factor graph variables, // which are sorted in index order. Ordering variableConversion; { // First build map from index to key FastMap<Index,Key> usedIndices; for(size_t i=0; i<variables.size(); ++i) usedIndices.insert(make_pair(indices[i], variables[i])); // Next run over indices in sorted order size_t slot = 0; typedef pair<Index,Key> Index_Key; BOOST_FOREACH(const Index_Key& index_key, usedIndices) { variableConversion.insert(index_key.second, slot); ++ slot; } } // Get dimensions from factor graph std::vector<size_t> dims(indices.size(), 0); BOOST_FOREACH(Key key, variables) { dims[variableConversion[key]] = values_.at(key).dim(); }
/* ************************************************************************* */ Ordering Ordering::ColamdConstrained(const VariableIndex& variableIndex, const FastMap<Key, int>& groups) { gttic(Ordering_COLAMDConstrained); size_t n = variableIndex.size(); std::vector<int> cmember(n, 0); // Build a mapping to look up sorted Key indices by Key FastMap<Key, size_t> keyIndices; size_t j = 0; for (auto key_factors: variableIndex) keyIndices.insert(keyIndices.end(), make_pair(key_factors.first, j++)); // Assign groups typedef FastMap<Key, int>::value_type key_group; for(const key_group& p: groups) { // FIXME: check that no groups are skipped cmember[keyIndices.at(p.first)] = p.second; } return Ordering::ColamdConstrained(variableIndex, cmember); }
/* ************************************************************************* */ Ordering Ordering::ColamdConstrainedLast(const VariableIndex& variableIndex, const std::vector<Key>& constrainLast, bool forceOrder) { gttic(Ordering_COLAMDConstrainedLast); size_t n = variableIndex.size(); std::vector<int> cmember(n, 0); // Build a mapping to look up sorted Key indices by Key FastMap<Key, size_t> keyIndices; size_t j = 0; BOOST_FOREACH(const VariableIndex::value_type key_factors, variableIndex) keyIndices.insert(keyIndices.end(), make_pair(key_factors.first, j++)); // If at least some variables are not constrained to be last, constrain the // ones that should be constrained. int group = (constrainLast.size() != n ? 1 : 0); BOOST_FOREACH(Key key, constrainLast) { cmember[keyIndices.at(key)] = group; if (forceOrder) ++group; }
static void posclass_init() { assert(!posclass_is_init); posclass_map.clear(); _all_posclass.clear(); unsigned skipcnt = 0; for (unsigned i = 0; i < tag_to_class_cnt; i++) { // Skip labels not in the vocabulary. if (!is_label_string(tag_to_class[i][0])) { Debug::log(1) << "Skipping unknown label " << tag_to_class[i][0] << " in posclass_init()\n"; skipcnt++; continue; } Label tag = string_to_label(tag_to_class[i][0]); assert(is_terminal_label(tag)); Posclass c = string_to_posclass(tag_to_class[i][1]); assert(posclass_list[c] == tag_to_class[i][1]); assert(posclass_list[c] != ""); posclass_map.insert(tag, c); } posclass_map.lock(); for (unsigned i = 0; i < posclass_cnt; i++) { if (posclass_list[i] != "") _all_posclass.push_back(i); if (posclass_list[i] == "::N") _Posclass_N = i; else if (posclass_list[i] == "::NP") _Posclass_NP = i; } posclass_is_init = true; }
int main( int argc, char * argv[] ) { Process process; Session session1, session2; FastMapEditor * editor; FastMap * hashmap; string fname = "/cpp_fastmap_get"; string hname = fname + "/test"; const char * key = "My Key"; const char * data = "My Data"; uint32_t length; char buffer[50]; psoObjectDefinition mapDef = { PSO_FAST_MAP, 0, 0, 0 }; psoKeyFieldDefinition keys = { "MyKey", PSO_KEY_VARBINARY, 20 }; psoFieldDefinition fields[1] = { { "Field_1", PSO_VARCHAR, {10} } }; try { if ( argc > 1 ) { process.Init( argv[1], argv[0] ); } else { process.Init( "10701", argv[0] ); } } catch( pso::Exception exc ) { cerr << "Test failed in init phase, error = " << exc.Message() << endl; cerr << "Is the server running?" << endl; return 1; } try { session1.Init(); session2.Init(); session2.CreateFolder( fname ); DataDefinition dataDefObj( session2, "cpp_fastmap_get", PSO_DEF_PHOTON_ODBC_SIMPLE, (unsigned char *)fields, sizeof(psoFieldDefinition) ); KeyDefinition keyDefObj( session2, "cpp_fastmap_get", PSO_DEF_PHOTON_ODBC_SIMPLE, (unsigned char *)&keys, sizeof(psoKeyFieldDefinition) ); session2.CreateMap( hname, mapDef, dataDefObj, keyDefObj ); session2.Commit(); hashmap = new FastMap( session1, hname ); editor = new FastMapEditor( session2, hname ); editor->Insert( key, strlen(key), data, strlen(data) ); } catch( pso::Exception exc ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } // Invalid arguments to tested function. try { // No commit yet. hashmap->Get( key, strlen(key), buffer, 50, length ); // Should never come here cerr << "Test failed - line " << __LINE__ << endl; return 1; } catch( pso::Exception exc ) { if ( exc.ErrorCode() != PSO_NO_SUCH_ITEM ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } } try { editor->Close(); session2.Commit(); // Commit the editions session1.Commit(); // Refresh session1 (and hashmap) } catch( pso::Exception exc ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } try { hashmap->Get( NULL, strlen(key), buffer, 50, length ); // Should never come here cerr << "Test failed - line " << __LINE__ << endl; return 1; } catch( pso::Exception exc ) { if ( exc.ErrorCode() != PSO_NULL_POINTER ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } } try { hashmap->Get( key, 0, buffer, 50, length ); // Should never come here cerr << "Test failed - line " << __LINE__ << endl; return 1; } catch( pso::Exception exc ) { if ( exc.ErrorCode() != PSO_INVALID_LENGTH ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } } try { hashmap->Get( key, strlen(key), NULL, 50, length ); // Should never come here cerr << "Test failed - line " << __LINE__ << endl; return 1; } catch( pso::Exception exc ) { if ( exc.ErrorCode() != PSO_NULL_POINTER ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } } try { hashmap->Get( key, strlen(key), buffer, 2, length ); // Should never come here cerr << "Test failed - line " << __LINE__ << endl; return 1; } catch( pso::Exception exc ) { if ( exc.ErrorCode() != PSO_INVALID_LENGTH ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } } // End of invalid args. This call should succeed. try { hashmap->Get( key, strlen(key), buffer, 50, length ); } catch( pso::Exception exc ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } if ( memcmp( buffer, data, 7 ) != 0 ) { cerr << "Test failed - line " << __LINE__ << endl; return 1; } return 0; }
void label_frequency_init() { _label_freq_map.insert(Label_ADJP(), 0.021609); _label_freq_map.insert(Label_ADVP(), 0.0364083); _label_freq_map.insert(Label_CONJP(), 0.000107241); _label_freq_map.insert(Label_FRAG(), 0.0020054); _label_freq_map.insert(Label_INTJ(), 0.000557653); _label_freq_map.insert(Label_LST(), 0.000160861); _label_freq_map.insert(Label_NAC(), 0.000139413); /// \bug This will be broken depending on whether we're using NPBs or not // _label_freq_map.insert(Label_NP(), 0.357198); // w/o NPB // _label_freq_map.insert(Label_NP(), 0.0775364); // w/ NPB // _label_freq_map.insert(Label_NPB(), 0.279904); _label_freq_map.insert(Label_NX(), 0.000589825); _label_freq_map.insert(Label_PP(), 0.0881842); _label_freq_map.insert(Label_PRN(), 0.00154427); _label_freq_map.insert(Label_QP(), 0.0127724); _label_freq_map.insert(Label_RRC(), 2.14482e-05); _label_freq_map.insert(Label_S(), 0.140453); _label_freq_map.insert(Label_SBAR(), 0.0185205); _label_freq_map.insert(Label_SBARQ(), 0.00102951); _label_freq_map.insert(Label_SINV(), 0.00170513); _label_freq_map.insert(Label_SQ(), 0.00164079); _label_freq_map.insert(Label_TOP(), 0.109107); _label_freq_map.insert(Label_UCP(), 0.000353895); _label_freq_map.insert(Label_VP(), 0.199125); _label_freq_map.insert(Label_WHADJP(), 9.65168e-05); _label_freq_map.insert(Label_WHADVP(), 0.00163006); _label_freq_map.insert(Label_WHNP(), 0.00412877); _label_freq_map.insert(Label_WHPP(), 8.57927e-05); _label_freq_map.insert(Label_X(), 0.000825755); _label_freq_map.lock(); _label_freq_is_init = true; }
int main( int argc, char * argv[] ) { Process process; Session session; FastMap * map; string fname = "/cpp_fastmap_fielddefODBC"; string hname = fname + "/test"; size_t len; psoObjectDefinition mapDef = { PSO_FAST_MAP, 0, 0, 0 }; DataDefBuilderODBC fieldDef( 5 ); KeyDefBuilderODBC keyDef( 3 ); DataDefinition * returnedDataDef; KeyDefinition * returnedKeyDef; unsigned char * buffer; try { fieldDef.AddField( "field1", 6, PSO_TINYINT, 0, 0, 0 ); fieldDef.AddField( "field2", 6, PSO_INTEGER, 0, 0, 0 ); fieldDef.AddField( "field3", 6, PSO_CHAR, 30, 0, 0 ); fieldDef.AddField( "field4", 6, PSO_SMALLINT, 0, 0, 0 ); fieldDef.AddField( "field5", 6, PSO_LONGVARBINARY, 0, 0, 0 ); keyDef.AddKeyField( "keyfield1", 9, PSO_KEY_INTEGER, 0 ); keyDef.AddKeyField( "keyfield2", 9, PSO_KEY_CHAR, 30 ); keyDef.AddKeyField( "keyfield3", 9, PSO_KEY_LONGVARBINARY, 0 ); } catch( pso::Exception exc ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } try { if ( argc > 1 ) { process.Init( argv[1], argv[0] ); } else { process.Init( "10701", argv[0] ); } } catch( pso::Exception exc ) { cerr << "Test failed in init phase, error = " << exc.Message() << endl; cerr << "Is the server running?" << endl; return 1; } try { session.Init(); session.CreateFolder( fname ); DataDefinition dataDefObj( session, "cpp_fastmap_datadef_odbc", PSO_DEF_PHOTON_ODBC_SIMPLE, fieldDef.GetDefinition(), fieldDef.GetDefLength() ); KeyDefinition keyDefObj( session, "cpp_fastmap_datadef_odbc", PSO_DEF_PHOTON_ODBC_SIMPLE, keyDef.GetDefinition(), keyDef.GetDefLength() ); session.CreateMap( hname, mapDef, dataDefObj, keyDefObj ); map = new FastMap( session, hname ); } catch( pso::Exception exc ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } try { returnedDataDef = map->GetDataDefinition(); returnedKeyDef = map->GetKeyDefinition(); } catch( pso::Exception exc ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } try { len = 5 * sizeof(psoFieldDefinition); buffer = new unsigned char [len]; if ( len != returnedDataDef->GetLength() ) { cerr << "Test failed - line " << __LINE__ << endl; return 1; } returnedDataDef->GetDefinition( buffer, len ); if ( memcmp( fieldDef.GetDefinition(), buffer, len ) != 0 ) { cerr << "Test failed - line " << __LINE__ << endl; return 1; } delete [] buffer; len = 3 * sizeof(psoKeyFieldDefinition); buffer = new unsigned char [len]; if ( len != returnedKeyDef->GetLength() ) { cerr << "Test failed - line " << __LINE__ << endl; return 1; } returnedKeyDef->GetDefinition( buffer, len ); if ( memcmp( keyDef.GetDefinition(), buffer, len ) != 0 ) { cerr << "Test failed - line " << __LINE__ << endl; return 1; } } catch( pso::Exception exc ) { cerr << "Test failed - line " << __LINE__ << ", error = " << exc.Message() << endl; return 1; } return 0; }
/* ************************************************************************* */ FastMap<Key, size_t> Ordering::invert() const { FastMap<Key, size_t> inverted; for (size_t pos = 0; pos < this->size(); ++pos) inverted.insert(make_pair((*this)[pos], pos)); return inverted; }
/// \todo Make some assertion about # of constit. labels, and/or that /// they are the lowest numbered ones? void read_labels(const string filename) { assert(!open_label_list); ifstream fin(filename.c_str()); assert(fin.is_open()); _all_labels.clear(); _all_constituent_labels.clear(); _all_terminal_labels.clear(); _max_label = 0; label_list.clear(); label_map.clear(); terminal_set.clear(); constituent_set.clear(); unsigned i, is_terminal, cnt; string label; while(!fin.eof()) { fin >> i >> ws >> is_terminal >> ws >> cnt >> label >> ws; if (i == NO_LABEL) label = ""; label_list.push_back(label); assert(label_list.at(i) == label); label_map[label] = i; // FIXME: Don't use [] operator if (i != NO_LABEL) { _all_labels.push_back(i); if (is_terminal) { terminal_set.insert(i, true); _all_terminal_labels.push_back(i); } else { constituent_set.insert(i, true); _all_constituent_labels.push_back(i); } if (i > _max_label) _max_label = i+1; } } assert(label_list.at(NO_LABEL) == ""); assert(label_map[""] == NO_LABEL); // FIXME: Don't use [] operator assert(label_list.size() == label_map.size()); // assert(label_map.size() == terminal_set.size() + constituent_set.size() + 1); terminal_set.lock(); constituent_set.lock(); /* Debug::log(1) << "Read " << constituent_set.size() << " constituents, " << \ terminal_set.size() << " terminals from '" << filename << "'\n"; */ fin.close(); open_label_list = true; if (is_label_string("ADJP")) _Label_ADJP = string_to_label("ADJP"); if (is_label_string("ADVP")) _Label_ADVP = string_to_label("ADVP"); if (is_label_string("AUX")) _Label_AUX = string_to_label("AUX"); if (is_label_string("AUXG")) _Label_AUXG = string_to_label("AUXG"); if (is_label_string("CC")) _Label_CC = string_to_label("CC"); if (is_label_string("CD")) _Label_CD = string_to_label("CD"); if (is_label_string("COLON")) _Label_COLON = string_to_label(":"); if (is_label_string("COMMA")) _Label_COMMA = string_to_label(","); if (is_label_string("CONJP")) _Label_CONJP = string_to_label("CONJP"); if (is_label_string("DOLLAR")) _Label_DOLLAR = string_to_label("$"); if (is_label_string("DT")) _Label_DT = string_to_label("DT"); if (is_label_string("EX")) _Label_EX = string_to_label("EX"); if (is_label_string("FRAG")) _Label_FRAG = string_to_label("FRAG"); if (is_label_string("FW")) _Label_FW = string_to_label("FW"); if (is_label_string("HASH")) _Label_HASH = string_to_label("#"); if (is_label_string("IN")) _Label_IN = string_to_label("IN"); if (is_label_string("INTJ")) _Label_INTJ = string_to_label("INTJ"); if (is_label_string("JJ")) _Label_JJ = string_to_label("JJ"); if (is_label_string("JJR")) _Label_JJR = string_to_label("JJR"); if (is_label_string("JJS")) _Label_JJS = string_to_label("JJS"); if (is_label_string("LS")) _Label_LS = string_to_label("LS"); if (is_label_string("LST")) _Label_LST = string_to_label("LST"); if (is_label_string("MD")) _Label_MD = string_to_label("MD"); if (is_label_string("NAC")) _Label_NAC = string_to_label("NAC"); if (is_label_string("NN")) _Label_NN = string_to_label("NN"); if (is_label_string("NNP")) _Label_NNP = string_to_label("NNP"); if (is_label_string("NNPS")) _Label_NNPS = string_to_label("NNPS"); if (is_label_string("NNS")) _Label_NNS = string_to_label("NNS"); if (is_label_string("NP")) _Label_NP = string_to_label("NP"); if (is_label_string("NPB")) _Label_NPB = string_to_label("NPB"); if (is_label_string("NX")) _Label_NX = string_to_label("NX"); if (is_label_string("POS")) _Label_POS = string_to_label("POS"); if (is_label_string("PP")) _Label_PP = string_to_label("PP"); if (is_label_string("PRN")) _Label_PRN = string_to_label("PRN"); if (is_label_string("PRP")) _Label_PRP = string_to_label("PRP"); if (is_label_string("PRPP")) _Label_PRPP = string_to_label("PRP$"); if (is_label_string("PRT")) _Label_PRT = string_to_label("PRT"); if (is_label_string("QP")) _Label_QP = string_to_label("QP"); if (is_label_string("RB")) _Label_RB = string_to_label("RB"); if (is_label_string("RBR")) _Label_RBR = string_to_label("RBR"); if (is_label_string("RBS")) _Label_RBS = string_to_label("RBS"); if (is_label_string("RP")) _Label_RP = string_to_label("RP"); if (is_label_string("RRC")) _Label_RRC = string_to_label("RRC"); if (is_label_string("S")) _Label_S = string_to_label("S"); if (is_label_string("SBAR")) _Label_SBAR = string_to_label("SBAR"); if (is_label_string("SBARQ")) _Label_SBARQ = string_to_label("SBARQ"); if (is_label_string("SINV")) _Label_SINV = string_to_label("SINV"); if (is_label_string("SQ")) _Label_SQ = string_to_label("SQ"); if (is_label_string("SYM")) _Label_SYM = string_to_label("SYM"); if (is_label_string("TO")) _Label_TO = string_to_label("TO"); if (is_label_string("TOP")) _Label_TOP = string_to_label("TOP"); if (is_label_string("UCP")) _Label_UCP = string_to_label("UCP"); if (is_label_string("UH")) _Label_UH = string_to_label("UH"); if (is_label_string("VB")) _Label_VB = string_to_label("VB"); if (is_label_string("VBD")) _Label_VBD = string_to_label("VBD"); if (is_label_string("VBG")) _Label_VBG = string_to_label("VBG"); if (is_label_string("VBN")) _Label_VBN = string_to_label("VBN"); if (is_label_string("VBP")) _Label_VBP = string_to_label("VBP"); if (is_label_string("VBZ")) _Label_VBZ = string_to_label("VBZ"); if (is_label_string("VP")) _Label_VP = string_to_label("VP"); if (is_label_string("WDT")) _Label_WDT = string_to_label("WDT"); if (is_label_string("WHADJP")) _Label_WHADJP = string_to_label("WHADJP"); if (is_label_string("WHADVP")) _Label_WHADVP = string_to_label("WHADVP"); if (is_label_string("WHNP")) _Label_WHNP = string_to_label("WHNP"); if (is_label_string("WHPP")) _Label_WHPP = string_to_label("WHPP"); if (is_label_string("WP")) _Label_WP = string_to_label("WP"); if (is_label_string("X")) _Label_X = string_to_label("X"); }