Last active
April 16, 2018 08:44
-
-
Save tondrej/9194816c48ef794b60622e04ec746894 to your computer and use it in GitHub Desktop.
FastText flattened C-style API examples (DLL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FASTTEXT_API BOOL FASTTEXT_CC fasttext_new(FastText** ft) | |
{ | |
return ExceptionBoundary([&]() -> BOOL | |
{ | |
*ft = new FastText(); | |
return true; | |
}); | |
} | |
FASTTEXT_API BOOL FASTTEXT_CC fasttext_release(FastText* ft) | |
{ | |
return ExceptionBoundary([&]() -> BOOL | |
{ | |
CHECK_HANDLE(ft); | |
ft->~FastText(); | |
return true; | |
}); | |
} | |
typedef BOOL(FASTTEXT_CC *fasttext_nn_callback)(char* word, real score, void* data); | |
FASTTEXT_API BOOL FASTTEXT_CC fasttext_nn2(FastText* ft, Matrix* mx, char** positive, char** negative, int count, fasttext_nn_callback callback, void* data) | |
{ | |
return ExceptionBoundary([&]() -> BOOL | |
{ | |
CHECK_HANDLE(ft); | |
CHECK_HANDLE(mx); | |
std::set<std::string> banSet; | |
std::vector<std::pair<real, std::string>> results; | |
Vector buffer(ft->getDimension()), query(ft->getDimension()); | |
std::string word_; | |
while (true) | |
{ | |
if (!*positive || **positive == '\0') | |
break; | |
word_ = std::string(*positive++); | |
banSet.insert(word_); | |
ft->getWordVector(buffer, word_); | |
query.addVector(buffer, 1.0); | |
} | |
while (true) | |
{ | |
if (!*negative || **negative == '\0') | |
break; | |
word_ = std::string(*negative++); | |
banSet.insert(word_); | |
ft->getWordVector(buffer, word_); | |
query.addVector(buffer, -1.0); | |
} | |
ft->findNN(*mx, query, count, banSet, results); | |
for (auto& pair : results) { | |
int b = callback((char*)pair.second.c_str(), pair.first, data); | |
if (b != 0) break; | |
} | |
return true; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment