-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcld2_emscripten.cc
65 lines (51 loc) · 1.62 KB
/
cld2_emscripten.cc
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
// cld2_emscripten.cc - Embind bindings for exposing functionality to WASM
#include <tuple>
#include <emscripten/bind.h>
#include "lang_script.h"
#include "../public/compact_lang_det.h"
using namespace emscripten;
typedef struct {
int lang;
std::string langCode;
bool isReliable;
} LanguageDetectionResult;
LanguageDetectionResult DetectLanguageWrapper(const std::string &text) {
CLD2::Language lang;
int percent3[3];
double normalized_score3[3];
CLD2::ResultChunkVector resultchunkvector;
int text_bytes;
bool is_reliable;
int valid_prefix_bytes;
lang = CLD2::ExtDetectLanguageSummaryCheckUTF8(
text.c_str(),
text.size(),
true,
nullptr,
0,
nullptr,
percent3,
normalized_score3,
&resultchunkvector,
&text_bytes,
&is_reliable,
&valid_prefix_bytes
);
return {lang, CLD2::LanguageCode(lang), is_reliable};
}
std::string GetBuildVersion() {
return CLD2::DetectLanguageVersion();
}
std::string GetLanguageName(int lang) {
return CLD2::LanguageName(static_cast<CLD2::Language>(lang));
}
EMSCRIPTEN_BINDINGS(cld2Module) {
constant("UNKNOWN_LANGUAGE_ID", static_cast<int>(CLD2::UNKNOWN_LANGUAGE));
value_object<LanguageDetectionResult>("LanguageDetectionResult")
.field("langId", &LanguageDetectionResult::lang)
.field("langCode", &LanguageDetectionResult::langCode)
.field("isReliable", &LanguageDetectionResult::isReliable);
function("getVersion", &GetBuildVersion);
function("getLanguageName", &GetLanguageName);
function("detectLanguage", &DetectLanguageWrapper);
}