forked from atom/node-spellchecker
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.cc
228 lines (173 loc) · 6.76 KB
/
main.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <vector>
#include "nan.h"
#include "spellchecker.h"
using Nan::ObjectWrap;
using namespace spellchecker;
using namespace v8;
namespace {
class Spellchecker : public Nan::ObjectWrap {
SpellcheckerImplementation* impl;
v8::Persistent<v8::Value> dictData;
static NAN_METHOD(New) {
Nan::HandleScope scope;
Spellchecker* that = new Spellchecker();
that->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
static NAN_METHOD(SetDictionary) {
Nan::HandleScope scope;
if (info.Length() < 1) {
return Nan::ThrowError("Bad argument");
}
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
bool has_contents = false;
if (info.Length() > 1) {
if (!node::Buffer::HasInstance(info[1])) {
return Nan::ThrowError("SetDictionary 2nd argument must be a Buffer");
}
// NB: We must guarantee that we pin this Buffer
that->dictData.Reset(info.GetIsolate(), info[1]);
has_contents = true;
}
Nan::Utf8String utf8_value(info[0]);
int len = utf8_value.length();
std::string language(*utf8_value, len);
bool result;
if (has_contents) {
result = that->impl->SetDictionaryToContents(
(unsigned char*)node::Buffer::Data(info[1]),
node::Buffer::Length(info[1]));
} else {
result = that->impl->SetDictionary(language);
}
info.GetReturnValue().Set(Nan::New(result));
}
static NAN_METHOD(IsMisspelled) {
Nan::HandleScope scope;
if (info.Length() < 1) {
return Nan::ThrowError("Bad argument");
}
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
Nan::Utf8String utf8_value(info[0]);
int len = utf8_value.length();
std::string word(*utf8_value, len);
info.GetReturnValue().Set(Nan::New(that->impl->IsMisspelled(word)));
}
static NAN_METHOD(CheckSpelling) {
Nan::HandleScope scope;
if (info.Length() < 1) {
return Nan::ThrowError("Bad argument");
}
Local<String> string = Local<String>::Cast(info[0]);
if (!string->IsString()) {
return Nan::ThrowError("Bad argument");
}
Local<Array> result = Nan::New<Array>();
info.GetReturnValue().Set(result);
if (string->Length() == 0) {
return;
}
std::vector<uint16_t> text(string->Length() + 1);
string->Write(info.GetIsolate(), reinterpret_cast<uint16_t *>(text.data()));
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
std::vector<MisspelledRange> misspelled_ranges = that->impl->CheckSpelling(text.data(), text.size());
std::vector<MisspelledRange>::const_iterator iter = misspelled_ranges.begin();
for (; iter != misspelled_ranges.end(); ++iter) {
size_t index = iter - misspelled_ranges.begin();
uint32_t start = iter->start, end = iter->end;
Local<Object> misspelled_range = Nan::New<Object>();
Nan::Set(misspelled_range, Nan::New("start").ToLocalChecked(), Nan::New<Integer>(start));
Nan::Set(misspelled_range, Nan::New("end").ToLocalChecked(), Nan::New<Integer>(end));
Nan::Set(result, index, misspelled_range);
}
}
static NAN_METHOD(Add) {
Nan::HandleScope scope;
if (info.Length() < 1) {
return Nan::ThrowError("Bad argument");
}
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
Nan::Utf8String utf8_value(info[0]);
int len = utf8_value.length();
std::string word(*utf8_value, len);
that->impl->Add(word);
return;
}
static NAN_METHOD(Remove) {
Nan::HandleScope scope;
if (info.Length() < 1) {
return Nan::ThrowError("Bad argument");
}
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
Nan::Utf8String utf8_value(info[0]);
int len = utf8_value.length();
std::string word(*utf8_value, len);
that->impl->Remove(word);
return;
}
static NAN_METHOD(GetAvailableDictionaries) {
Nan::HandleScope scope;
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
std::string path = ".";
if (info.Length() > 0) {
Nan::Utf8String utf8_value(info[0]);
int len = utf8_value.length();
std::string path(*utf8_value, len);
}
std::vector<std::string> dictionaries =
that->impl->GetAvailableDictionaries(path);
Local<Array> result = Nan::New<Array>(dictionaries.size());
for (size_t i = 0; i < dictionaries.size(); ++i) {
const std::string& dict = dictionaries[i];
Nan::Set(result, i, Nan::New(dict.data(), dict.size()).ToLocalChecked());
}
info.GetReturnValue().Set(result);
}
static NAN_METHOD(GetCorrectionsForMisspelling) {
Nan::HandleScope scope;
if (info.Length() < 1) {
return Nan::ThrowError("Bad argument");
}
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
// Getting the length and passing it to the constructor is faster
// than calculating it in the string constructor
Nan::Utf8String utf8_value(info[0]);
int len = utf8_value.length();
std::string word(*utf8_value, len);
std::vector<std::string> corrections =
that->impl->GetCorrectionsForMisspelling(word);
Local<Array> result = Nan::New<Array>(corrections.size());
for (size_t i = 0; i < corrections.size(); ++i) {
const std::string& word = corrections[i];
Nan::MaybeLocal<String> val = Nan::New<String>(word.data(), word.size());
Nan::Set(result, i, val.ToLocalChecked());
}
info.GetReturnValue().Set(result);
}
Spellchecker() {
impl = SpellcheckerFactory::CreateSpellchecker();
}
// actual destructor
virtual ~Spellchecker() {
delete impl;
}
public:
static void Init(Local<Object> exports) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(Spellchecker::New);
tpl->SetClassName(Nan::New<String>("Spellchecker").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetMethod(tpl->InstanceTemplate(), "setDictionary", Spellchecker::SetDictionary);
Nan::SetMethod(tpl->InstanceTemplate(), "getAvailableDictionaries", Spellchecker::GetAvailableDictionaries);
Nan::SetMethod(tpl->InstanceTemplate(), "getCorrectionsForMisspelling", Spellchecker::GetCorrectionsForMisspelling);
Nan::SetMethod(tpl->InstanceTemplate(), "isMisspelled", Spellchecker::IsMisspelled);
Nan::SetMethod(tpl->InstanceTemplate(), "checkSpelling", Spellchecker::CheckSpelling);
Nan::SetMethod(tpl->InstanceTemplate(), "add", Spellchecker::Add);
Nan::SetMethod(tpl->InstanceTemplate(), "remove", Spellchecker::Remove);
Nan::Set(exports, Nan::New("Spellchecker").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
}
};
void Init(Local<Object> exports, Local<Object> module) {
Spellchecker::Init(exports);
}
} // namespace
NODE_MODULE(spellchecker, Init)