Add get_word_id(char* word) method to library
Change-Id: Idf0701248ccdf0ecc1c2265d215fbe582510ea96
diff --git a/src/collocatordb.cc b/src/collocatordb.cc
index 691f820..352efd3 100644
--- a/src/collocatordb.cc
+++ b/src/collocatordb.cc
@@ -327,6 +327,8 @@
void readVocab(string fname);
string getWord(uint32_t w1);
+ uint64_t getWordId(const char *word) const;
+
CollocatorDB(const char *db_name, bool read_only);
// public interface of CollocatorDB.
@@ -794,6 +796,14 @@
return _vocab[w1].word;
}
+uint64_t rocksdb::CollocatorDB::getWordId(const char *word) const {
+ for (uint64_t i = 0; i < _vocab.size(); i++) {
+ if (strcmp(_vocab[i].word.c_str(), word) == 0)
+ return i;
+ }
+ return 0;
+}
+
string rocksdb::CollocatorDB::collocators2json(uint32_t w1, vector<Collocator> collocators) {
ostringstream s;
int i = 0;
@@ -883,6 +893,10 @@
return strdup(db->getWord(w).c_str());
}
+ DLL_EXPORT uint64_t get_word_id(COLLOCATORS *db, char *word) {
+ return db->getWordId(word);
+ }
+
DLL_EXPORT void read_vocab(COLLOCATORS *db, char *fname) {
std::string fName(fname);
db->readVocab(fName);
diff --git a/src/collocatordb.h b/src/collocatordb.h
index 0eed8d8..d601902 100644
--- a/src/collocatordb.h
+++ b/src/collocatordb.h
@@ -128,4 +128,6 @@
extern char *get_word(COLLOCATORDB *db, uint32_t w1);
extern void read_vocab(COLLOCATORDB *db, char *fname);
extern char *get_version();
+extern uint64_t get_word_id(COLLOCATORDB *db, const char *word);
+
#endif