This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
spell.js
49 lines (40 loc) · 1.54 KB
/
spell.js
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
var xml2js = require('xml2js'),
request = require('request'),
_ = require('underscore'),
lib = require('./lib');
var Checker = function(config){
this.config = _.extend({
ignoreDupes: false,
ignoreDigits: true,
ignoreCaps: true,
ignoreHtml: true,
ignoreCode: true,
language: 'en',
threshold: 0
}, config);
};
Checker.prototype.check = function(s, callback){
var text = lib.cleanText(s);
request({
uri: 'http://www.google.com/tbproxy/spell?lang=' + this.config.language + '&hl=' + this.config.language,
method: 'POST',
body: lib.reqXml(text, this.config)
}, function(err, res, body){
if (err || res.statusCode !== 200) {
if (typeof callback === 'function') { callback(err); }
} else {
var parser = new xml2js.Parser({ attrkey: 'attrs', charkey: 'chars' });
parser.parseString(body, function(err, result){
result = lib.formatResult(result, text);
// Remove suggestions below confidence threshold
var threshold = this.config.threshold;
result.suggestions = result.suggestions.reduce(function(acc, x){
if (x.confidence >= threshold) { acc.push(x); }
return acc;
}, []);
if (typeof callback === 'function') { callback(undefined, result); }
}.bind(this));
}
}.bind(this));
};
module.exports.Checker = exports.Checker = Checker;