Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FastText flattened C-style API examples (DLL)
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