Permalink
Browse files

Added local dictionary support

  • Loading branch information...
1 parent b55315f commit 9181e7a4a90c5dede3d5ee1d9bae63d80e2ca7f2 @tstone committed Jun 24, 2012
Showing with 31 additions and 3 deletions.
  1. +14 −0 README.md
  2. +13 −1 src/checker.js
  3. +4 −2 src/result.js
View
@@ -34,6 +34,7 @@ Usage
ignoreCode: Completely remove all <code> tags. // (default: true)
language: Language to check against. // (default: 'en')
threshold: Filter suggestions based on Google's confidence level. // (default: 0)
+ dictionary: String (file path) or array of strings (word list)
### Doing the Check
@@ -70,3 +71,16 @@ In the above example, the suggestion returned would look like this:
words: [ 'example', 'ex maple', 'ex-maple', 'exampled', 'examples' ],
context: 'This is an [exmaple]' }
+### Custom Dictionary
+
+It's possible to have a custom dictionary with domain-specific or user-added words. The `dictionary` configuration option of checker takes either a path to a file or an array of strings.
+
+For a dictionary file, the words should be `\n` terminated, one on each line.
+
+Example:
+
+ var checker = new googlespell.Checker({ dictionary: [ 'js' ]});
+ checker.check('This is written in js.', function(err, result){
+ // result.suggestions === [];
+ });
+
View
@@ -3,7 +3,8 @@ var xml2js = require('xml2js'),
request = require('request'),
_ = require('underscore'),
lib = require('./lib'),
- Result = require('./result');
+ Result = require('./result'),
+ fs = require('fs');
var Checker = function(config){
this.config = _.extend({
@@ -15,6 +16,17 @@ var Checker = function(config){
language: 'en',
threshold: 0
}, config);
+
+ // Setup dictionary
+ if (config.dictionary) {
+ if (Array.isArray(config.dictionary)) {
+ this.config.dictionary = config.dictionary;
+ } else {
+ this.config.dictionary = fs.readFileSync(config.dictionary, 'utf8').split('\n');
+ }
+ } else {
+ this.config.dictionary = [];
+ }
};
Checker.prototype.check = function(s, callback){
View
@@ -22,6 +22,7 @@ var Result = function(base, source, config) { this.__init(base, source, config);
cls.__parseSuggestions = function(xs){
var suggestions = [];
var source = this.source;
+ var dict = this.config.dictionary;
var threshold = this.config.threshold;
var sugObj = function(o) {
@@ -34,12 +35,13 @@ var Result = function(base, source, config) { this.__init(base, source, config);
sug.context = lib.getContext(source, sug.word, sug.offset, 50, 30);
return sug;
}.bind(this);
+
var addSug = function(x) {
var sug = sugObj(x);
- if (sug.confidence >= threshold) {
+ if (dict.indexOf(sug.word) === -1 && sug.confidence >= threshold) {
suggestions.push(sug);
}
- };
+ }.bind(this);
Array.isArray(xs) ? xs.forEach(addSug) : addSug(xs);
return suggestions;

0 comments on commit 9181e7a

Please sign in to comment.