forked from elinorbgr/ai-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibltpy.cpp
115 lines (104 loc) · 3.29 KB
/
libltpy.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <lttoolbox/fst_processor.h>
#include <lttoolbox/lt_locale.h>
class Analyser {
public:
Analyser(const std::string & analyserpath, const std::string & generatorpath) throw(exception);
std::wstring analyse(std::wstring const &word);
std::wstring generate(std::wstring const &word);
private:
FSTProcessor fst_analyser;
FSTProcessor fst_generator;
};
Analyser::Analyser(const std::string & analyserpath, const std::string & generatorpath) throw(exception) {
FILE * file = fopen(analyserpath.c_str(), "r");
if (!file) {
std::cerr << "Couldn't open analyser file " << analyserpath << std::endl;
throw exception();
}
fst_analyser.load(file);
fclose(file);
fst_analyser.setCaseSensitiveMode(false);
fst_analyser.setDictionaryCaseMode(true);
fst_analyser.initBiltrans();
file = fopen(generatorpath.c_str(), "r");
if (!file) {
std::cerr << "Couldn't open analyser file " << analyserpath << std::endl;
throw exception();
}
fst_generator.load(file);
fclose(file);
fst_generator.setCaseSensitiveMode(false);
fst_generator.setDictionaryCaseMode(true);
fst_generator.initBiltrans();
}
std::wstring Analyser::analyse(std::wstring const &word) {
std::pair <std::wstring,int> analysis = fst_analyser.biltransWithQueue(word, false);
// The 'false' means we require no ^ or $ in input/output
if (analysis.second == 0) {
return analysis.first;
}
else {
// a partial match:
return L"@"+word;
}
}
std::wstring Analyser::generate(std::wstring const &word) {
std::pair <std::wstring,int> analysis = fst_generator.biltransWithQueue(word, false);
// The 'false' means we require no ^ or $ in input/output
if (analysis.second == 0) {
return analysis.first;
}
else {
// a partial match:
return L"@"+word;
}
}
extern "C" std::wstring * analyse(Analyser * a, const wchar_t * word) {
// It seems Python can only send wchar_t*, but we need a wstring
size_t wlen = wcslen(word);
if (wlen == 0) {
// avoid a bug in biltransWithQueue:
return 0;
}
std::wstring inputString = L"";
for (size_t i = 0; i < wlen; i++) {
inputString.append(1, word[i]);
}
std::wstring * out = new wstring(a->analyse(inputString));
return out;
}
extern "C" std::wstring * generate(Analyser * a, const wchar_t * word) {
// It seems Python can only send wchar_t*, but we need a wstring
size_t wlen = wcslen(word);
if (wlen == 0) {
// avoid a bug in biltransWithQueue:
return 0;
}
std::wstring inputString = L"";
for (size_t i = 0; i < wlen; i++) {
inputString.append(1, word[i]);
}
std::wstring * out = new wstring(a->generate(inputString));
return out;
}
extern "C" void free_analyses(std::wstring * analyses) {
delete analyses;
}
extern "C" Analyser * init(const char ** error, const char * path_a, const char * path_b) {
LtLocale::tryToSetLocale();
Analyser * a = 0;
try {
a = new Analyser(path_a, path_b);
}
catch (exception & e) {
delete a;
a = 0;
*error = e.what();
return 0;
}
*error = 0;
return a;
}
extern "C" void terminate(Analyser * a) {
delete a;
}