-
Notifications
You must be signed in to change notification settings - Fork 3
/
gofasttext.cpp
52 lines (39 loc) · 1.31 KB
/
gofasttext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "gofasttext.h"
#include <cstring>
#include "fastText/src/fasttext.h"
using namespace fasttext;
struct membuf : std::streambuf
{
membuf(char* begin, char* end) {
this->setg(begin, begin, end);
}
};
void * GoFastTextInit(char * path)
{
FastText* ft_ptr = new FastText();
ft_ptr->loadModel(std::string(path));
return (void*) ft_ptr;
}
void GoFastTextFree(void * ft)
{
FastText* ft_ptr = reinterpret_cast<FastText*>(ft);
delete ft_ptr;
}
go_fast_text_pair_t* GoFastTextPredict(void * ft, char * word, int k, float threshold, int *result_length)
{
FastText* ft_ptr = reinterpret_cast<FastText*>(ft);
membuf sbuf(word, word + sizeof(word));
std::istream in(&sbuf);
std::vector<std::pair<real, std::string>> predictions;
ft_ptr->predictLine(in, predictions, k, threshold);
int result_size = predictions.size();
go_fast_text_pair_t* pairsArray = (go_fast_text_pair_t*) malloc(result_size * sizeof(go_fast_text_pair_t));
for (uint i = 0; i < uint(predictions.size()); i++){
const std::string::size_type label_size = predictions[i].second.size();
pairsArray[i].label = new char[label_size + 1];
memcpy(pairsArray[i].label, predictions[i].second.c_str(), label_size + 1);
pairsArray[i].prob = predictions[i].first;
}
*result_length = result_size;
return pairsArray;
}