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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/*-
* Copyright 2023 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lang_detection_fasttext.h"
#ifdef WITH_FASTTEXT
#include "fasttext/fasttext.h"
#include "libserver/cfg_file.h"
#include "libserver/logger.h"
#include "fmt/core.h"
#include <exception>
#include <string>
#include <vector>
#include <sstream>
#include <streambuf>
#endif
#ifdef WITH_FASTTEXT
namespace rspamd::langdet {
class fasttext_langdet {
private:
fasttext::FastText ft;
bool loaded;
struct one_shot_buf : public std::streambuf {
explicit one_shot_buf(const char *in, std::size_t sz) {
auto deconst_in = const_cast<char *>(in);
setg(deconst_in, deconst_in, deconst_in + sz);
}
};
public:
explicit fasttext_langdet(struct rspamd_config *cfg) {
const auto *ucl_obj = cfg->rcl_obj;
const auto *opts_section = ucl_object_find_key(ucl_obj, "options");
if (opts_section) {
const auto *model = ucl_object_find_key(opts_section, "fasttext_langdet_model");
if (model) {
try {
ft.loadModel(ucl_object_tostring(model));
loaded = true;
}
catch (std::exception &e) {
auto err_message = fmt::format("cannot load fasttext model: {}", e.what());
msg_err_config("%s", err_message.c_str());
loaded = false;
}
}
}
}
/* Disallow multiple initialisation */
fasttext_langdet() = delete;
fasttext_langdet(const fasttext_langdet &) = delete;
fasttext_langdet(fasttext_langdet &&) = delete;
~fasttext_langdet() = default;
auto detect_language(const char *in, size_t len, int k) -> std::vector<std::pair<fasttext::real, std::string>> *
{
if (!loaded) {
return nullptr;
}
/* Hack to deal with streams without copies */
one_shot_buf buf{in, len};
auto stream = std::istream{&buf};
auto predictions = new std::vector<std::pair<fasttext::real, std::string>>;
predictions->reserve(k);
auto res = ft.predictLine(stream, *predictions, k, 0.0f);
if (res) {
return predictions;
}
else {
delete predictions;
}
return nullptr;
}
};
}
#endif
/* C API part */
G_BEGIN_DECLS
#define FASTTEXT_MODEL_TO_C_API(p) reinterpret_cast<rspamd::langdet::fasttext_langdet *>(p)
#define FASTTEXT_RESULT_TO_C_API(res) reinterpret_cast<std::vector<std::pair<fasttext::real, std::string>> *>(res)
void* rspamd_lang_detection_fasttext_init(struct rspamd_config *cfg)
{
#ifndef WITH_FASTTEXT
return nullptr;
#else
return (void *)new rspamd::langdet::fasttext_langdet(cfg);
#endif
}
rspamd_fasttext_predict_result_t rspamd_lang_detection_fasttext_detect(void *ud,
const char *in, size_t len, int k)
{
#ifndef WITH_FASTTEXT
return nullptr;
#else
auto *real_model = FASTTEXT_MODEL_TO_C_API(ud);
auto *res = real_model->detect_language(in, len, k);
return (rspamd_fasttext_predict_result_t)res;
#endif
}
void rspamd_lang_detection_fasttext_destroy(void *ud)
{
#ifdef WITH_FASTTEXT
delete FASTTEXT_MODEL_TO_C_API(ud);
#endif
}
const char *
rspamd_lang_detection_fasttext_get_lang(rspamd_fasttext_predict_result_t res)
{
#ifdef WITH_FASTTEXT
auto *real_res = FASTTEXT_RESULT_TO_C_API(res);
if (real_res && !real_res->empty()) {
return real_res->front().second.c_str();
}
#endif
return nullptr;
}
float
rspamd_lang_detection_fasttext_get_prob(rspamd_fasttext_predict_result_t res)
{
#ifdef WITH_FASTTEXT
auto *real_res = FASTTEXT_RESULT_TO_C_API(res);
if (real_res && !real_res->empty()) {
return real_res->front().first;
}
#endif
return 0.0f;
}
void rspamd_fasttext_predict_result_destroy(rspamd_fasttext_predict_result_t res)
{
#ifdef WITH_FASTTEXT
auto *real_res = FASTTEXT_RESULT_TO_C_API(res);
delete real_res;
#endif
}
G_END_DECLS
|