Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit fed2854

Browse files
committed
Add Mac impl for bulk spell-checking function
This function will return an array of character ranges, indicating where *all* of the misspelled words are in a given string.
1 parent 93e0db1 commit fed2854

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

lib/spellchecker.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ var isMisspelled = function() {
2929
return defaultSpellcheck.isMisspelled.apply(defaultSpellcheck, arguments);
3030
};
3131

32+
var checkSpelling = function() {
33+
ensureDefaultSpellCheck();
34+
35+
return defaultSpellcheck.checkSpelling.apply(defaultSpellcheck, arguments);
36+
};
37+
3238
var add = function() {
3339
ensureDefaultSpellCheck();
3440

@@ -64,6 +70,7 @@ module.exports = {
6470
setDictionary: setDictionary,
6571
add: add,
6672
isMisspelled: isMisspelled,
73+
checkSpelling: checkSpelling,
6774
getAvailableDictionaries: getAvailableDictionaries,
6875
getCorrectionsForMisspelling: getCorrectionsForMisspelling,
6976
Spellchecker: Spellchecker

spec/spellchecker-spec.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ describe "SpellChecker", ->
1111
it "throws an exception when no word specified", ->
1212
expect(-> SpellChecker.isMisspelled()).toThrow()
1313

14+
describe ".checkSpelling(string)", ->
15+
it "returns an array of character ranges of misspelled words", ->
16+
string = "cat caat dog dooog"
17+
18+
expect(SpellChecker.checkSpelling(string)).toEqual [
19+
{start: 4, end: 8},
20+
{start: 13, end: 18},
21+
]
22+
1423
describe ".getCorrectionsForMisspelling(word)", ->
1524
it "returns an array of possible corrections", ->
1625
corrections = SpellChecker.getCorrectionsForMisspelling('worrd')

src/main.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <vector>
12
#include "nan.h"
23
#include "spellchecker.h"
34

@@ -49,6 +50,29 @@ class Spellchecker : public Nan::ObjectWrap {
4950
info.GetReturnValue().Set(Nan::New(that->impl->IsMisspelled(word)));
5051
}
5152

53+
static NAN_METHOD(CheckSpelling) {
54+
Nan::HandleScope scope;
55+
if (info.Length() < 1) {
56+
return Nan::ThrowError("Bad argument");
57+
}
58+
59+
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
60+
String::Utf8Value text(info[0]);
61+
62+
std::vector<MisspelledRange> misspelled_ranges = that->impl->CheckSpelling(*text, text.length());
63+
64+
Local<Array> result = Nan::New<Array>();
65+
std::vector<MisspelledRange>::const_iterator iter = misspelled_ranges.begin();
66+
for (; iter != misspelled_ranges.end(); ++iter) {
67+
Local<Object> misspelled_range = Nan::New<Object>();
68+
misspelled_range->Set(Nan::New("start").ToLocalChecked(), Nan::New<Number>(iter->start));
69+
misspelled_range->Set(Nan::New("end").ToLocalChecked(), Nan::New<Number>(iter->end));
70+
result->Set(iter - misspelled_ranges.begin(), misspelled_range);
71+
}
72+
73+
info.GetReturnValue().Set(result);
74+
}
75+
5276
static NAN_METHOD(Add) {
5377
Nan::HandleScope scope;
5478
if (info.Length() < 1) {
@@ -127,6 +151,7 @@ class Spellchecker : public Nan::ObjectWrap {
127151
Nan::SetMethod(tpl->InstanceTemplate(), "getAvailableDictionaries", Spellchecker::GetAvailableDictionaries);
128152
Nan::SetMethod(tpl->InstanceTemplate(), "getCorrectionsForMisspelling", Spellchecker::GetCorrectionsForMisspelling);
129153
Nan::SetMethod(tpl->InstanceTemplate(), "isMisspelled", Spellchecker::IsMisspelled);
154+
Nan::SetMethod(tpl->InstanceTemplate(), "checkSpelling", Spellchecker::CheckSpelling);
130155
Nan::SetMethod(tpl->InstanceTemplate(), "add", Spellchecker::Add);
131156

132157
exports->Set(Nan::New("Spellchecker").ToLocalChecked(), tpl->GetFunction());

src/spellchecker.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
namespace spellchecker {
88

9+
struct MisspelledRange {
10+
size_t start;
11+
size_t end;
12+
};
13+
914
class SpellcheckerImplementation {
1015
public:
1116
virtual bool SetDictionary(const std::string& language, const std::string& path) = 0;
@@ -17,6 +22,8 @@ class SpellcheckerImplementation {
1722
// Returns true if the word is misspelled.
1823
virtual bool IsMisspelled(const std::string& word) = 0;
1924

25+
virtual std::vector<MisspelledRange> CheckSpelling(const char *text, size_t length) = 0;
26+
2027
// Adds a new word to the dictionary.
2128
// NB: When using Hunspell, this will not modify the .dic file; custom words must be added each
2229
// time the spellchecker is created. Use a custom dictionary file.

src/spellchecker_mac.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class MacSpellchecker : public SpellcheckerImplementation {
1717
std::vector<std::string> GetAvailableDictionaries(const std::string& path);
1818
std::vector<std::string> GetCorrectionsForMisspelling(const std::string& word);
1919
bool IsMisspelled(const std::string& word);
20+
std::vector<MisspelledRange> CheckSpelling(const char *text, size_t length);
2021
void Add(const std::string& word);
2122

2223
private:

src/spellchecker_mac.mm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@
5151
return result;
5252
}
5353

54+
std::vector<MisspelledRange> MacSpellchecker::CheckSpelling(const char *text, size_t length) {
55+
std::vector<MisspelledRange> result;
56+
57+
@autoreleasepool {
58+
NSString* string = [NSString stringWithUTF8String:text];
59+
NSArray *misspellings = [this->spellChecker checkString:string range:NSMakeRange(0, length) types:NSTextCheckingTypeSpelling options:nil inSpellDocumentWithTag:0 orthography:nil wordCount:nil];
60+
for (NSTextCheckingResult *misspelling in misspellings) {
61+
MisspelledRange range;
62+
range.start = misspelling.range.location;
63+
range.end = misspelling.range.location + misspelling.range.length;
64+
result.push_back(range);
65+
}
66+
}
67+
68+
return result;
69+
}
70+
5471
void MacSpellchecker::Add(const std::string& word) {
5572
@autoreleasepool {
5673
NSString* newWord = [NSString stringWithUTF8String:word.c_str()];

0 commit comments

Comments
 (0)