-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
55 lines (47 loc) · 1.2 KB
/
index.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
50
51
52
53
54
55
var _ = require("lodash");
var WordNet = require("node-wordnet");
var wordnet = new WordNet({cache:true});
var isLetter = function(c) {
return c.toLowerCase() != c.toUpperCase();
};
var _findForms = function(input) {
var i, wordforms = [], word, _ref, reducer;
reducer = function(list) {
return _.reduce(list, function(memo, word){
return (memo.length < word.length) ? memo : word;
});
};
if (isLetter(input.charAt(0))) {
return wordnet.validFormsAsync(input).then(function(results) {
if (results && results.length !== 0) {
for (i = 0; i < results.length; i++ ) {
_ref = results[i].split('#');
word = _ref[0];
wordforms.push(word);
}
return reducer(_.uniq(wordforms));
} else {
return [input];
}
});
} else {
return [input];
}
};
exports.lemmatize = function(input, cb) {
var lookup;
if(_.isString(input)) {
lookup = [input]; // Assuming a single word.
} else if (_.isArray(input)) {
lookup = input;
} else {
lookup = [];
}
return Promise.all(lookup.map(_findForms)).then(function(res) {
res = _.flatten(res);
if (cb) {
cb(null, res);
}
return res;
});
};