natural 0.1.28 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +7 -0
- package/.npmignore +4 -11
- package/README.md +190 -50
- package/bayes_classifier.json +1 -0
- package/lib/natural/analyzers/sentence_analyzer.js +8 -5
- package/lib/natural/brill_pos_tagger/data/Dutch/README.txt +2 -0
- package/lib/natural/brill_pos_tagger/data/Dutch/brill_CONTEXTRULES.jg +285 -0
- package/lib/natural/brill_pos_tagger/data/Dutch/brill_LEXICON.jg +11699 -0
- package/lib/natural/brill_pos_tagger/data/English/README.txt +8 -0
- package/lib/natural/brill_pos_tagger/data/English/lexicon_from_posjs.json +297827 -0
- package/lib/natural/brill_pos_tagger/data/English/tr_from_brill_paper.txt +10 -0
- package/lib/natural/brill_pos_tagger/data/English/tr_from_posjs.txt +18 -0
- package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tagger.js +136 -0
- package/lib/natural/brill_pos_tagger/lib/Predicate.js +410 -0
- package/lib/natural/brill_pos_tagger/lib/TF_Parser.js +717 -0
- package/lib/natural/brill_pos_tagger/lib/TF_Parser.pegjs +55 -0
- package/lib/natural/brill_pos_tagger/lib/TransformationRule.js +49 -0
- package/lib/natural/classifiers/bayes_classifier.js +6 -5
- package/lib/natural/classifiers/classifier.js +3 -2
- package/lib/natural/classifiers/logistic_regression_classifier.js +9 -5
- package/lib/natural/distance/dice_coefficient.js +22 -12
- package/lib/natural/distance/jaro-winkler_distance.js +13 -8
- package/lib/natural/index.js +6 -0
- package/lib/natural/inflectors/present_verb_inflector.js +1 -0
- package/lib/natural/ngrams/ngrams.js +6 -0
- package/lib/natural/normalizers/normalizer_ja.js +26 -8
- package/lib/natural/phonetics/double_metaphone.js +9 -4
- package/lib/natural/phonetics/metaphone.js +4 -0
- package/lib/natural/phonetics/soundex.js +11 -7
- package/lib/natural/spellcheck/spellcheck.js +80 -0
- package/lib/natural/stemmers/porter_stemmer.js +90 -62
- package/lib/natural/stemmers/porter_stemmer_fr.js +8 -3
- package/lib/natural/stemmers/porter_stemmer_pt.js +302 -0
- package/lib/natural/stemmers/stemmer.js +31 -6
- package/lib/natural/stemmers/stemmer_pt.js +65 -0
- package/lib/natural/stemmers/token.js +163 -0
- package/lib/natural/tfidf/tfidf.js +49 -18
- package/lib/natural/tokenizers/regexp_tokenizer.js +3 -3
- package/{examples/inflection/noun.js → lib/natural/tokenizers/sentence_tokenizer.js} +14 -10
- package/lib/natural/tokenizers/tokenizer_case.js +58 -0
- package/lib/natural/util/bag.js +1 -0
- package/lib/natural/util/stopwords_pt.js +136 -0
- package/lib/natural/wordnet/wordnet.js +2 -2
- package/lib/natural/wordnet/wordnet_file.js +1 -1
- package/lr_classifier.json +1 -0
- package/package.json +7 -4
- package/package.json~ +66 -0
- package/benchmarks/index.js +0 -32
- package/benchmarks/metaphone.js +0 -27
- package/benchmarks/soundex.js +0 -27
- package/examples/classification/basic.js +0 -35
- package/examples/classification/recall.js +0 -28
- package/examples/classification/store.js +0 -37
- package/examples/inflection/count.js +0 -35
- package/examples/inflection/noun_attach.js +0 -33
- package/examples/phonetics/compare.js +0 -39
- package/examples/phonetics/sounds_like.js +0 -40
- package/examples/phonetics/tokenize_and_phoneticize.js +0 -54
- package/examples/stemming/stem_corpus.js +0 -29
- package/examples/stemming/stem_word.js +0 -26
- package/examples/tfidf/array_example.js +0 -34
- package/examples/tfidf/multiple_terms.js +0 -33
- package/examples/tfidf/tfidf_example.js +0 -44
- package/io_spec/test_data/tfidf/one +0 -1
- package/io_spec/test_data/tfidf/two +0 -1
- package/io_spec/tfidf_spec.js +0 -50
- package/io_spec/wordnet_spec.js +0 -81
- package/spec/aggressive_tokenizer_es_spec.js +0 -36
- package/spec/aggressive_tokenizer_fr_spec.js +0 -101
- package/spec/aggressive_tokenizer_nl_spec.js +0 -36
- package/spec/aggressive_tokenizer_pt_spec.js +0 -56
- package/spec/aggressive_tokenizer_spec.js +0 -56
- package/spec/bayes_classifier_spec.js +0 -173
- package/spec/classifier_spec.js +0 -91
- package/spec/count_inflector_fr_spec.js +0 -44
- package/spec/count_inflector_spec.js +0 -73
- package/spec/dice_coefficient_spec.js +0 -26
- package/spec/double_metaphone_spec.js +0 -743
- package/spec/jaro-winkler_spec.js +0 -53
- package/spec/lancaster_stemmer_spec.js +0 -73
- package/spec/levenshtein_spec.js +0 -66
- package/spec/logistic_regression_classifier_spec.js +0 -122
- package/spec/longest_path_tree_spec.js +0 -102
- package/spec/metaphone_spec.js +0 -247
- package/spec/ngram_spec.js +0 -130
- package/spec/ngram_zh_spec.js +0 -122
- package/spec/normalizer_ja_spec.js +0 -154
- package/spec/normalizer_no_spec.js +0 -111
- package/spec/normalizer_spec.js +0 -118
- package/spec/noun_inflector_fr_spec.js +0 -476
- package/spec/noun_inflector_ja_spec.js +0 -112
- package/spec/noun_inflector_spec.js +0 -270
- package/spec/porter_stemmer_fr_spec.js +0 -101
- package/spec/porter_stemmer_it_spec.js +0 -44
- package/spec/porter_stemmer_no_spec.js +0 -143
- package/spec/porter_stemmer_ru_spec.js +0 -63
- package/spec/porter_stemmer_spec.js +0 -137
- package/spec/present_verb_inflector_spec.js +0 -125
- package/spec/sentence_analyzer_spec.js +0 -29
- package/spec/shortest_path_tree_spec.js +0 -132
- package/spec/soundex_spec.js +0 -154
- package/spec/stemmer_ja_spec.js +0 -66
- package/spec/test_data/snowball_fr.txt +0 -20402
- package/spec/test_data/snowball_it.txt +0 -35482
- package/spec/test_data/snowball_no.txt +0 -20628
- package/spec/test_data/tfdif_testcorpus.txt +0 -7
- package/spec/test_data/tfidf_document1.txt +0 -1
- package/spec/test_data/tfidf_document2.txt +0 -1
- package/spec/test_data/tfidf_document3.txt +0 -1
- package/spec/test_data/tfidf_document4.txt +0 -1
- package/spec/test_data/tfidf_teststopwords.txt +0 -1
- package/spec/test_data/wordnet_mini.sql +0 -95
- package/spec/tfidf_spec.js +0 -198
- package/spec/tokenizer_ja_spec.js +0 -58
- package/spec/transliterator_ja_spec.js +0 -103
- package/spec/treebank_word_tokenizer_spec.js +0 -36
- package/spec/trie_spec.js +0 -206
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2014, Ismaël Héry
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
|
12
|
+
all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
module.exports = function () {
|
|
24
|
+
'use strict';
|
|
25
|
+
|
|
26
|
+
var Stemmer = this,
|
|
27
|
+
stopwords = require('../util/stopwords_pt'),
|
|
28
|
+
Tokenizer = require('../tokenizers/aggressive_tokenizer_pt');
|
|
29
|
+
|
|
30
|
+
Stemmer.stem = function (token) {
|
|
31
|
+
return token;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
Stemmer.addStopWords = function (word) {
|
|
35
|
+
stopwords.words.push(word);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
Stemmer.addStopWords = function (words) {
|
|
39
|
+
stopwords.words = stopwords.words.concat(words);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
Stemmer.tokenizeAndStem = function(text, keepStops) {
|
|
43
|
+
var stemmedTokens = [];
|
|
44
|
+
|
|
45
|
+
var tokenStemmer = function (token) {
|
|
46
|
+
if (keepStops || stopwords.words.indexOf(token.toLowerCase()) === -1) {
|
|
47
|
+
stemmedTokens.push(Stemmer.stem(token));
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
new Tokenizer().tokenize(text).forEach(tokenStemmer);
|
|
52
|
+
|
|
53
|
+
return stemmedTokens;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
Stemmer.attach = function () {
|
|
57
|
+
String.prototype.stem = function () {
|
|
58
|
+
return Stemmer.stem(this);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
String.prototype.tokenizeAndStem = function (keepStops) {
|
|
62
|
+
return Stemmer.tokenizeAndStem(this, keepStops);
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2015, Luís Rodrigues
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
|
12
|
+
all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
module.exports = (function () {
|
|
24
|
+
'use strict';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Stemmer token constructor.
|
|
28
|
+
*
|
|
29
|
+
* @param {String} string Token string.
|
|
30
|
+
*/
|
|
31
|
+
var Token = function (string) {
|
|
32
|
+
this.vowels = '';
|
|
33
|
+
this.regions = {};
|
|
34
|
+
this.string = string;
|
|
35
|
+
this.original = string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Set vowels.
|
|
40
|
+
*
|
|
41
|
+
* @param {String|Array} vowels List of vowels.
|
|
42
|
+
* @return {Token} Token instance.
|
|
43
|
+
*/
|
|
44
|
+
Token.prototype.usingVowels = function (vowels) {
|
|
45
|
+
this.vowels = vowels;
|
|
46
|
+
return this;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Marks a region by defining its starting index or providing a callback
|
|
51
|
+
* function that does.
|
|
52
|
+
*
|
|
53
|
+
* @param {String} region Region name.
|
|
54
|
+
* @param {Array|Number} args Callback arguments or region start index.
|
|
55
|
+
* @param {Function} callback Function that determines the start index (optional).
|
|
56
|
+
* @param {Object} context Callback context (optional, defaults to this).
|
|
57
|
+
* @return {Token} Token instance.
|
|
58
|
+
*/
|
|
59
|
+
Token.prototype.markRegion = function (region, args, callback, context) {
|
|
60
|
+
if (typeof callback === 'function') {
|
|
61
|
+
this.regions[region] = callback.apply(context || this, [].concat(args));
|
|
62
|
+
|
|
63
|
+
} else if (!isNaN(args)) {
|
|
64
|
+
this.regions[region] = args;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return this;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Replaces all instances of a string with another.
|
|
72
|
+
*
|
|
73
|
+
* @param {String} find String to be replaced.
|
|
74
|
+
* @param {String} replace Replacement string.
|
|
75
|
+
* @return {Token} Token instance.
|
|
76
|
+
*/
|
|
77
|
+
Token.prototype.replaceAll = function (find, replace) {
|
|
78
|
+
this.string = this.string.split(find).join(replace);
|
|
79
|
+
return this;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Replaces the token suffix if in a region.
|
|
84
|
+
*
|
|
85
|
+
* @param {String} suffix Suffix to replace.
|
|
86
|
+
* @param {String} replace Replacement string.
|
|
87
|
+
* @param {String} region Region name.
|
|
88
|
+
* @return {Token} Token instance.
|
|
89
|
+
*/
|
|
90
|
+
Token.prototype.replaceSuffixInRegion = function (suffix, replace, region) {
|
|
91
|
+
var suffixes = [].concat(suffix);
|
|
92
|
+
for (var i = 0; i < suffixes.length; i++) {
|
|
93
|
+
if (this.hasSuffixInRegion(suffixes[i], region)) {
|
|
94
|
+
this.string = this.string.slice(0, -suffixes[i].length) + replace;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return this;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Determines whether the token has a vowel at the provided index.
|
|
103
|
+
*
|
|
104
|
+
* @param {Integer} index Character index.
|
|
105
|
+
* @return {Boolean} Whether the token has a vowel at the provided index.
|
|
106
|
+
*/
|
|
107
|
+
Token.prototype.hasVowelAtIndex = function (index) {
|
|
108
|
+
return this.vowels.indexOf(this.string[index]) !== -1;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Finds the next vowel in the token.
|
|
113
|
+
*
|
|
114
|
+
* @param {Integer} start Starting index offset.
|
|
115
|
+
* @return {Integer} Vowel index, or the end of the string.
|
|
116
|
+
*/
|
|
117
|
+
Token.prototype.nextVowelIndex = function (start) {
|
|
118
|
+
var index = (start >= 0 && start < this.string.length) ? start : this.string.length;
|
|
119
|
+
while (index < this.string.length && !this.hasVowelAtIndex(index)) {
|
|
120
|
+
index++;
|
|
121
|
+
}
|
|
122
|
+
return index;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Finds the next consonant in the token.
|
|
127
|
+
*
|
|
128
|
+
* @param {Integer} start Starting index offset.
|
|
129
|
+
* @return {Integer} Consonant index, or the end of the string.
|
|
130
|
+
*/
|
|
131
|
+
Token.prototype.nextConsonantIndex = function (start) {
|
|
132
|
+
var index = (start >= 0 && start < this.string.length) ? start : this.string.length;
|
|
133
|
+
while (index < this.string.length && this.hasVowelAtIndex(index)) {
|
|
134
|
+
index++;
|
|
135
|
+
}
|
|
136
|
+
return index;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Determines whether the token has the provided suffix.
|
|
141
|
+
* @param {String} suffix Suffix to match.
|
|
142
|
+
* @return {Boolean} Whether the token string ends in suffix.
|
|
143
|
+
*/
|
|
144
|
+
Token.prototype.hasSuffix = function (suffix) {
|
|
145
|
+
return this.string.slice(-suffix.length) === suffix;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Determines whether the token has the provided suffix within the specified
|
|
150
|
+
* region.
|
|
151
|
+
*
|
|
152
|
+
* @param {String} suffix Suffix to match.
|
|
153
|
+
* @param {String} region Region name.
|
|
154
|
+
* @return {Boolean} Whether the token string ends in suffix.
|
|
155
|
+
*/
|
|
156
|
+
Token.prototype.hasSuffixInRegion = function (suffix, region) {
|
|
157
|
+
var regionStart = this.regions[region] || 0,
|
|
158
|
+
suffixStart = this.string.length - suffix.length;
|
|
159
|
+
return this.hasSuffix(suffix) && suffixStart >= regionStart;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
return Token;
|
|
163
|
+
})();
|
|
@@ -28,13 +28,13 @@ var _ = require("underscore")._,
|
|
|
28
28
|
|
|
29
29
|
function buildDocument(text, key) {
|
|
30
30
|
var stopOut;
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
if(typeof text === 'string') {
|
|
33
33
|
text = tokenizer.tokenize(text.toLowerCase());
|
|
34
34
|
stopOut = true;
|
|
35
35
|
} else if(!_.isArray(text)) {
|
|
36
|
-
return text;
|
|
37
36
|
stopOut = false;
|
|
37
|
+
return text;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
return text.reduce(function(document, term) {
|
|
@@ -63,12 +63,33 @@ function TfIdf(deserialized) {
|
|
|
63
63
|
this._idfCache = {};
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
// backwards compatibility for < node 0.10
|
|
67
|
+
function isEncoding(encoding) {
|
|
68
|
+
if (typeof Buffer.isEncoding !== 'undefined')
|
|
69
|
+
return Buffer.isEncoding(encoding);
|
|
70
|
+
switch ((encoding + '').toLowerCase()) {
|
|
71
|
+
case 'hex':
|
|
72
|
+
case 'utf8':
|
|
73
|
+
case 'utf-8':
|
|
74
|
+
case 'ascii':
|
|
75
|
+
case 'binary':
|
|
76
|
+
case 'base64':
|
|
77
|
+
case 'ucs2':
|
|
78
|
+
case 'ucs-2':
|
|
79
|
+
case 'utf16le':
|
|
80
|
+
case 'utf-16le':
|
|
81
|
+
case 'raw':
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
66
87
|
module.exports = TfIdf;
|
|
67
88
|
TfIdf.tf = tf;
|
|
68
89
|
|
|
69
90
|
TfIdf.prototype.idf = function(term, force) {
|
|
70
91
|
|
|
71
|
-
// Lookup the term in the New term-IDF caching,
|
|
92
|
+
// Lookup the term in the New term-IDF caching,
|
|
72
93
|
// this will cut search times down exponentially on large document sets.
|
|
73
94
|
if(this._idfCache[term] && this._idfCache.hasOwnProperty(term) && force !== true)
|
|
74
95
|
return this._idfCache[term];
|
|
@@ -77,7 +98,7 @@ TfIdf.prototype.idf = function(term, force) {
|
|
|
77
98
|
return count + (documentHasTerm(term, document) ? 1 : 0);
|
|
78
99
|
}, 0);
|
|
79
100
|
|
|
80
|
-
var idf = Math.log((this.documents.length) / (docsWithTerm));
|
|
101
|
+
var idf = 1 + Math.log((this.documents.length) / ( 1 + docsWithTerm ));
|
|
81
102
|
|
|
82
103
|
// Add the idf to the term cache and return it
|
|
83
104
|
this._idfCache[term] = idf;
|
|
@@ -92,13 +113,12 @@ TfIdf.prototype.addDocument = function(document, key, restoreCache) {
|
|
|
92
113
|
// make sure the cache is invalidated when new documents arrive
|
|
93
114
|
if(restoreCache === true) {
|
|
94
115
|
for(var term in this._idfCache) {
|
|
95
|
-
// invoking idf with the force option set will
|
|
116
|
+
// invoking idf with the force option set will
|
|
96
117
|
// force a recomputation of the idf, and it will
|
|
97
118
|
// automatically refresh the cache value.
|
|
98
119
|
this.idf(term, true);
|
|
99
120
|
}
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
121
|
+
} else {
|
|
102
122
|
this._idfCache = {};
|
|
103
123
|
}
|
|
104
124
|
};
|
|
@@ -107,15 +127,17 @@ TfIdf.prototype.addDocument = function(document, key, restoreCache) {
|
|
|
107
127
|
// Otherwise, the cache will just be wiped clean
|
|
108
128
|
TfIdf.prototype.addFileSync = function(path, encoding, key, restoreCache) {
|
|
109
129
|
if(!encoding)
|
|
110
|
-
encoding = '
|
|
111
|
-
|
|
112
|
-
|
|
130
|
+
encoding = 'utf8';
|
|
131
|
+
if(!isEncoding(encoding))
|
|
132
|
+
throw new Error('Invalid encoding: ' + encoding);
|
|
133
|
+
|
|
134
|
+
var document = fs.readFileSync(path, encoding);
|
|
113
135
|
this.documents.push(buildDocument(document, key));
|
|
114
136
|
|
|
115
137
|
// make sure the cache is invalidated when new documents arrive
|
|
116
138
|
if(restoreCache === true) {
|
|
117
139
|
for(var term in this._idfCache) {
|
|
118
|
-
// invoking idf with the force option set will
|
|
140
|
+
// invoking idf with the force option set will
|
|
119
141
|
// force a recomputation of the idf, and it will
|
|
120
142
|
// automatically refresh the cache value.
|
|
121
143
|
this.idf(term, true);
|
|
@@ -128,12 +150,14 @@ TfIdf.prototype.addFileSync = function(path, encoding, key, restoreCache) {
|
|
|
128
150
|
|
|
129
151
|
TfIdf.prototype.tfidf = function(terms, d) {
|
|
130
152
|
var _this = this;
|
|
131
|
-
|
|
153
|
+
|
|
132
154
|
if(!_.isArray(terms))
|
|
133
155
|
terms = tokenizer.tokenize(terms.toString().toLowerCase());
|
|
134
156
|
|
|
135
157
|
return terms.reduce(function(value, term) {
|
|
136
|
-
|
|
158
|
+
var idf = _this.idf(term);
|
|
159
|
+
idf = idf === Infinity ? 0 : idf;
|
|
160
|
+
return value + (tf(term, _this.documents[d]) * idf);
|
|
137
161
|
}, 0.0);
|
|
138
162
|
};
|
|
139
163
|
|
|
@@ -142,21 +166,28 @@ TfIdf.prototype.listTerms = function(d) {
|
|
|
142
166
|
|
|
143
167
|
for(var term in this.documents[d]) {
|
|
144
168
|
if(term != '__key')
|
|
145
|
-
|
|
169
|
+
terms.push({term: term, tfidf: this.tfidf(term, d)});
|
|
146
170
|
}
|
|
147
171
|
|
|
148
|
-
return terms.sort(function(x, y) { return y.tfidf - x.tfidf });
|
|
149
|
-
}
|
|
172
|
+
return terms.sort(function(x, y) { return y.tfidf - x.tfidf; });
|
|
173
|
+
};
|
|
150
174
|
|
|
151
175
|
TfIdf.prototype.tfidfs = function(terms, callback) {
|
|
152
176
|
var tfidfs = new Array(this.documents.length);
|
|
153
|
-
|
|
177
|
+
|
|
154
178
|
for(var i = 0; i < this.documents.length; i++) {
|
|
155
179
|
tfidfs[i] = this.tfidf(terms, i);
|
|
156
|
-
|
|
180
|
+
|
|
157
181
|
if(callback)
|
|
158
182
|
callback(i, tfidfs[i], this.documents[i].__key);
|
|
159
183
|
}
|
|
160
184
|
|
|
161
185
|
return tfidfs;
|
|
162
186
|
};
|
|
187
|
+
|
|
188
|
+
// Define a tokenizer other than the default "WordTokenizer"
|
|
189
|
+
TfIdf.prototype.setTokenizer = function(t) {
|
|
190
|
+
if(!_.isFunction(t.tokenize))
|
|
191
|
+
throw new Error('Expected a valid Tokenizer');
|
|
192
|
+
tokenizer = t;
|
|
193
|
+
};
|
|
@@ -62,7 +62,7 @@ exports.RegexpTokenizer = RegexpTokenizer;
|
|
|
62
62
|
*
|
|
63
63
|
*/
|
|
64
64
|
var WordTokenizer = function(options) {
|
|
65
|
-
this._pattern =
|
|
65
|
+
this._pattern = /[^A-Za-zА-Яа-я0-9_]+/;
|
|
66
66
|
RegexpTokenizer.call(this,options)
|
|
67
67
|
};
|
|
68
68
|
|
|
@@ -74,11 +74,11 @@ exports.WordTokenizer = WordTokenizer;
|
|
|
74
74
|
* non-alphabetic characters. E.g.:
|
|
75
75
|
*
|
|
76
76
|
* >>> WordPunctTokenizer().tokenize("She said 'hello'.")
|
|
77
|
-
* [
|
|
77
|
+
* ["She","said","'","hello","'","."]
|
|
78
78
|
*
|
|
79
79
|
*/
|
|
80
80
|
var WordPunctTokenizer = function(options) {
|
|
81
|
-
this._pattern = new RegExp(/(\w
|
|
81
|
+
this._pattern = new RegExp(/(\w+|[а-я0-9_]+|\.|\!|\'|\"")/i);
|
|
82
82
|
RegexpTokenizer.call(this,options)
|
|
83
83
|
};
|
|
84
84
|
|
|
@@ -20,18 +20,22 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
20
20
|
THE SOFTWARE.
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
|
-
var
|
|
24
|
-
|
|
23
|
+
var Tokenizer = require('./tokenizer'),
|
|
24
|
+
util = require('util');
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
var SentenceTokenizer = function() {
|
|
27
|
+
Tokenizer.call(this);
|
|
28
|
+
};
|
|
29
|
+
util.inherits(SentenceTokenizer, Tokenizer);
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
SentenceTokenizer.prototype.tokenize = function(text) {
|
|
32
|
+
// break string up in to sentences based on punctation and quotation marks
|
|
33
|
+
var tokens = text.match(/([\"\'\‘\“\'\"\[\(\{\⟨][^\.\?\!]+[\.\?\!][\"\'\’\”\'\"\]\)\}\⟩]|[^\.\?\!]+[\.\?\!])\s?/g);
|
|
31
34
|
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
// remove unecessary white space
|
|
36
|
+
tokens = tokens.map(Function.prototype.call, String.prototype.trim);
|
|
34
37
|
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
return this.trim(tokens);
|
|
39
|
+
};
|
|
37
40
|
|
|
41
|
+
module.exports = SentenceTokenizer;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2011, Chris Umbel, Alex Langberg
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
|
12
|
+
all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
var Tokenizer = require('./tokenizer'),
|
|
24
|
+
util = require('util'),
|
|
25
|
+
CaseTokenizer = function() {
|
|
26
|
+
Tokenizer.call(this);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
util.inherits(CaseTokenizer, Tokenizer);
|
|
30
|
+
|
|
31
|
+
CaseTokenizer.prototype.attach = function() {
|
|
32
|
+
var self = this;
|
|
33
|
+
|
|
34
|
+
String.prototype.tokenize = function(preserveApostrophe) {
|
|
35
|
+
return self.tokenize(this, preserveApostrophe);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Idea from Seagull: http://stackoverflow.com/a/26482650
|
|
40
|
+
CaseTokenizer.prototype.tokenize = function(text, preserveApostrophe) {
|
|
41
|
+
var whitelist = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
|
|
42
|
+
var lower = text.toLowerCase();
|
|
43
|
+
var upper = text.toUpperCase();
|
|
44
|
+
var result = '';
|
|
45
|
+
var i;
|
|
46
|
+
|
|
47
|
+
for (i = 0; i < lower.length; ++i) {
|
|
48
|
+
if (lower[i] !== upper[i] || whitelist.indexOf(lower[i]) > -1 || (text[i] === '\'' && preserveApostrophe)) {
|
|
49
|
+
result += text[i];
|
|
50
|
+
} else {
|
|
51
|
+
result += ' ';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return this.trim(result.replace(/\s+/g, ' ').split(' '));
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
module.exports = CaseTokenizer;
|
package/lib/natural/util/bag.js
CHANGED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2011, Luís Rodrigues
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in
|
|
12
|
+
all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
// a list of commonly used words that have little meaning and can be excluded
|
|
24
|
+
// from analysis.
|
|
25
|
+
var words = [
|
|
26
|
+
'a',
|
|
27
|
+
'à',
|
|
28
|
+
'ao',
|
|
29
|
+
'aos',
|
|
30
|
+
'aquela',
|
|
31
|
+
'aquelas',
|
|
32
|
+
'aquele',
|
|
33
|
+
'aqueles',
|
|
34
|
+
'aquilo',
|
|
35
|
+
'as',
|
|
36
|
+
'às',
|
|
37
|
+
'até',
|
|
38
|
+
'com',
|
|
39
|
+
'como',
|
|
40
|
+
'da',
|
|
41
|
+
'das',
|
|
42
|
+
'de',
|
|
43
|
+
'dela',
|
|
44
|
+
'delas',
|
|
45
|
+
'dele',
|
|
46
|
+
'deles',
|
|
47
|
+
'depois',
|
|
48
|
+
'do',
|
|
49
|
+
'dos',
|
|
50
|
+
'e',
|
|
51
|
+
'ela',
|
|
52
|
+
'elas',
|
|
53
|
+
'ele',
|
|
54
|
+
'eles',
|
|
55
|
+
'em',
|
|
56
|
+
'entre',
|
|
57
|
+
'essa',
|
|
58
|
+
'essas',
|
|
59
|
+
'esse',
|
|
60
|
+
'esses',
|
|
61
|
+
'esta',
|
|
62
|
+
'estas',
|
|
63
|
+
'este',
|
|
64
|
+
'estes',
|
|
65
|
+
'eu',
|
|
66
|
+
'isso',
|
|
67
|
+
'isto',
|
|
68
|
+
'já',
|
|
69
|
+
'lhe',
|
|
70
|
+
'lhes',
|
|
71
|
+
'mais',
|
|
72
|
+
'mas',
|
|
73
|
+
'me',
|
|
74
|
+
'mesmo',
|
|
75
|
+
'meu',
|
|
76
|
+
'meus',
|
|
77
|
+
'minha',
|
|
78
|
+
'minhas',
|
|
79
|
+
'muito',
|
|
80
|
+
'muitos',
|
|
81
|
+
'na',
|
|
82
|
+
'não',
|
|
83
|
+
'nas',
|
|
84
|
+
'nem',
|
|
85
|
+
'no',
|
|
86
|
+
'nos',
|
|
87
|
+
'nós',
|
|
88
|
+
'nossa',
|
|
89
|
+
'nossas',
|
|
90
|
+
'nosso',
|
|
91
|
+
'nossos',
|
|
92
|
+
'num',
|
|
93
|
+
'nuns',
|
|
94
|
+
'numa',
|
|
95
|
+
'numas',
|
|
96
|
+
'o',
|
|
97
|
+
'os',
|
|
98
|
+
'ou',
|
|
99
|
+
'para',
|
|
100
|
+
'pela',
|
|
101
|
+
'pelas',
|
|
102
|
+
'pelo',
|
|
103
|
+
'pelos',
|
|
104
|
+
'por',
|
|
105
|
+
'quais',
|
|
106
|
+
'qual',
|
|
107
|
+
'quando',
|
|
108
|
+
'que',
|
|
109
|
+
'quem',
|
|
110
|
+
'se',
|
|
111
|
+
'sem',
|
|
112
|
+
'seu',
|
|
113
|
+
'seus',
|
|
114
|
+
'só',
|
|
115
|
+
'sua',
|
|
116
|
+
'suas',
|
|
117
|
+
'também',
|
|
118
|
+
'te',
|
|
119
|
+
'teu',
|
|
120
|
+
'teus',
|
|
121
|
+
'tu',
|
|
122
|
+
'tua',
|
|
123
|
+
'tuas',
|
|
124
|
+
'um',
|
|
125
|
+
'uma',
|
|
126
|
+
'umas',
|
|
127
|
+
'você',
|
|
128
|
+
'vocês',
|
|
129
|
+
'vos',
|
|
130
|
+
'vosso',
|
|
131
|
+
'vossos',
|
|
132
|
+
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '_'
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
// tell the world about the noise words.
|
|
136
|
+
exports.words = words;
|
|
@@ -137,9 +137,9 @@ function WordNet(dataDir) {
|
|
|
137
137
|
|
|
138
138
|
if (!dataDir) {
|
|
139
139
|
try {
|
|
140
|
-
var WNdb = require('
|
|
140
|
+
var WNdb = require('wordnet-db');
|
|
141
141
|
} catch(e) {
|
|
142
|
-
console.error("Please 'npm install
|
|
142
|
+
console.error("Please 'npm install wordnet-db' before using WordNet module or specify a dict directory.");
|
|
143
143
|
throw e;
|
|
144
144
|
}
|
|
145
145
|
dataDir = WNdb.path;
|
|
@@ -37,7 +37,7 @@ function appendLineChar(fd, pos, buffPos, buff, callback) {
|
|
|
37
37
|
console.log(err);
|
|
38
38
|
else {
|
|
39
39
|
if(buff[buffPos] == 10 || buffPos == buff.length)
|
|
40
|
-
callback(buff.slice(0, buffPos).toString('
|
|
40
|
+
callback(buff.slice(0, buffPos).toString('UTF-8'));
|
|
41
41
|
else {
|
|
42
42
|
appendLineChar(fd, pos + 1, buffPos + 1, buff, callback);
|
|
43
43
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"classifier":{"examples":{"computing":[[1,1,0,0,0,0,0,0,0],[0,0,1,1,0,0,0,0,0],[0,0,0,1,1,1,0,0,0]],"literature":[[0,0,1,0,0,0,1,0,0],[0,0,0,0,0,0,1,1,0],[0,0,0,0,0,0,1,0,1]]},"features":[],"featurePositions":{},"maxFeaturePosition":0,"classifications":["computing","literature"],"exampleCount":6,"theta":[{"elements":[2.0057203693422903,2.0057203693422903,0.0904766662626176,3.447364098636818,0.9696521506748157,0.9696521506748157,-4.840706080886848,-1.2267353995937322,-1.2267353995937322]},{"elements":[-2.00572036934229,-2.00572036934229,-0.09047666626261795,-3.447364098636819,-0.9696521506748155,-0.9696521506748155,4.84070608088685,1.2267353995937313,1.2267353995937313]}]},"docs":[{"label":"computing","text":["fix","box"]},{"label":"computing","text":["write","code"]},{"label":"computing","text":["nasti","script","code"]},{"label":"literature","text":["write","book"]},{"label":"literature","text":["read","book"]},{"label":"literature","text":["studi","book"]}],"features":{"fix":1,"box":1,"write":2,"code":2,"nasti":1,"script":1,"book":3,"read":1,"studi":1},"stemmer":{},"lastAdded":6,"events":{"domain":null,"_events":{},"_maxListeners":10}}
|