natural 0.1.27 → 0.2.1
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/LICENSE.txt +0 -0
- package/Makefile +0 -0
- package/README.md +237 -24
- package/benchmarks/index.js +1 -1
- package/benchmarks/metaphone.js +0 -0
- package/benchmarks/soundex.js +0 -0
- package/examples/classification/basic.js +0 -0
- package/examples/classification/recall.js +0 -0
- package/examples/classification/store.js +0 -0
- package/examples/inflection/count.js +0 -0
- package/examples/inflection/noun.js +0 -0
- package/examples/inflection/noun_attach.js +0 -0
- package/examples/phonetics/compare.js +0 -0
- package/examples/phonetics/sounds_like.js +0 -0
- package/examples/phonetics/tokenize_and_phoneticize.js +0 -0
- package/examples/stemming/stem_corpus.js +0 -0
- package/examples/stemming/stem_word.js +0 -0
- package/examples/tfidf/array_example.js +0 -0
- package/examples/tfidf/multiple_terms.js +0 -0
- package/examples/tfidf/tfidf_example.js +0 -0
- package/index.js +0 -0
- package/io_spec/test_data/tfidf/one +0 -0
- package/io_spec/test_data/tfidf/two +0 -0
- package/io_spec/tfidf_spec.js +0 -0
- package/io_spec/wordnet_spec.js +0 -0
- package/lib/natural/analyzers/sentence_analyzer.js +16 -13
- package/lib/natural/classifiers/bayes_classifier.js +6 -2
- package/lib/natural/classifiers/classifier.js +0 -0
- package/lib/natural/classifiers/logistic_regression_classifier.js +0 -0
- package/lib/natural/distance/dice_coefficient.js +0 -0
- package/lib/natural/distance/jaro-winkler_distance.js +13 -8
- package/lib/natural/distance/levenshtein_distance.js +0 -0
- package/lib/natural/index.js +8 -0
- package/lib/natural/inflectors/count_inflector.js +0 -0
- package/lib/natural/inflectors/form_set.js +0 -0
- package/lib/natural/inflectors/fr/count_inflector.js +0 -0
- package/lib/natural/inflectors/fr/noun_inflector.js +0 -0
- package/lib/natural/inflectors/ja/noun_inflector.js +0 -0
- package/lib/natural/inflectors/noun_inflector.js +0 -0
- package/lib/natural/inflectors/present_verb_inflector.js +1 -0
- package/lib/natural/inflectors/singular_plural_inflector.js +1 -2
- package/lib/natural/ngrams/ngrams.js +45 -8
- package/lib/natural/ngrams/ngrams_zh.js +83 -0
- package/lib/natural/normalizers/normalizer.js +30 -38
- package/lib/natural/normalizers/normalizer_ja.js +26 -8
- package/lib/natural/normalizers/normalizer_no.js +59 -0
- package/lib/natural/phonetics/double_metaphone.js +9 -4
- package/lib/natural/phonetics/metaphone.js +6 -1
- package/lib/natural/phonetics/phonetic.js +0 -0
- package/lib/natural/phonetics/soundex.js +11 -7
- package/lib/natural/spellcheck/spellcheck.js +80 -0
- package/lib/natural/stemmers/lancaster_rules.js +4 -4
- package/lib/natural/stemmers/lancaster_stemmer.js +0 -0
- package/lib/natural/stemmers/porter_stemmer.js +90 -63
- package/lib/natural/stemmers/porter_stemmer_es.js +22 -23
- package/lib/natural/stemmers/porter_stemmer_fr.js +377 -0
- package/lib/natural/stemmers/porter_stemmer_no.js +155 -0
- package/lib/natural/stemmers/porter_stemmer_pt.js +302 -0
- package/lib/natural/stemmers/stemmer.js +0 -0
- package/lib/natural/stemmers/stemmer_fr.js +58 -0
- package/lib/natural/stemmers/stemmer_no.js +61 -0
- package/lib/natural/stemmers/stemmer_pt.js +65 -0
- package/lib/natural/stemmers/token.js +163 -0
- package/lib/natural/tfidf/tfidf.js +50 -19
- package/lib/natural/tokenizers/aggressive_tokenizer.js +0 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_es.js +0 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_fr.js +36 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_nl.js +0 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_no.js +39 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_pt.js +0 -0
- package/lib/natural/tokenizers/regexp_tokenizer.js +0 -0
- package/lib/natural/tokenizers/tokenizer.js +2 -2
- package/lib/natural/tokenizers/treebank_word_tokenizer.js +0 -0
- package/lib/natural/trie/trie.js +47 -0
- package/lib/natural/util/bag.js +50 -0
- package/lib/natural/util/edge_weighted_digraph.js +112 -0
- package/lib/natural/util/longest_path_tree.js +99 -0
- package/lib/natural/util/shortest_path_tree.js +100 -0
- package/lib/natural/util/stopwords.js +0 -0
- package/lib/natural/util/stopwords_fr.js +194 -0
- package/lib/natural/util/stopwords_nl.js +43 -0
- package/lib/natural/util/stopwords_no.js +42 -0
- package/lib/natural/util/stopwords_pt.js +136 -0
- package/lib/natural/util/topological.js +98 -0
- package/lib/natural/wordnet/data_file.js +0 -0
- package/lib/natural/wordnet/index_file.js +0 -0
- package/lib/natural/wordnet/wordnet.js +0 -0
- package/lib/natural/wordnet/wordnet_file.js +0 -0
- package/package.json +2 -2
- package/spec/aggressive_tokenizer_fr_spec.js +101 -0
- package/spec/aggressive_tokenizer_pt_spec.js +0 -0
- package/spec/aggressive_tokenizer_spec.js +0 -0
- package/spec/bayes_classifier_spec.js +12 -0
- package/spec/classifier_spec.js +84 -0
- package/spec/count_inflector_fr_spec.js +0 -0
- package/spec/count_inflector_spec.js +0 -0
- package/spec/dice_coefficient_spec.js +0 -0
- package/spec/double_metaphone_spec.js +10 -1
- package/spec/jaro-winkler_spec.js +0 -0
- package/spec/lancaster_stemmer_spec.js +10 -1
- package/spec/levenshtein_spec.js +0 -0
- package/spec/logistic_regression_classifier_spec.js +0 -0
- package/spec/longest_path_tree_spec.js +102 -0
- package/spec/metaphone_spec.js +10 -0
- package/spec/ngram_spec.js +61 -0
- package/spec/ngram_zh_spec.js +122 -0
- package/spec/normalizer_ja_spec.js +9 -3
- package/spec/normalizer_no_spec.js +111 -0
- package/spec/normalizer_spec.js +26 -5
- package/spec/noun_inflector_fr_spec.js +0 -0
- package/spec/noun_inflector_ja_spec.js +0 -0
- package/spec/noun_inflector_spec.js +0 -0
- package/spec/porter_stemmer_fr_spec.js +101 -0
- package/spec/porter_stemmer_no_spec.js +143 -0
- package/spec/porter_stemmer_pt_spec.js +50 -0
- package/spec/porter_stemmer_spec.js +49 -0
- package/spec/present_verb_inflector_spec.js +3 -0
- package/spec/remove_diacritics_spec.js +31 -0
- package/spec/sentence_analyzer_spec.js +248 -0
- package/spec/shortest_path_tree_spec.js +132 -0
- package/spec/soundex_spec.js +13 -9
- package/spec/spellcheck_spec.js +92 -0
- package/spec/stemmer_token_spec.js +161 -0
- package/spec/test_data/snowball_fr.txt +20402 -0
- package/spec/test_data/snowball_no.txt +20628 -0
- package/spec/test_data/snowball_pt.txt +32016 -0
- package/spec/test_data/tfdif_testcorpus.txt +0 -0
- package/spec/test_data/tfidf_teststopwords.txt +0 -0
- package/spec/test_data/wordnet_mini.sql +0 -0
- package/spec/tfidf_spec.js +153 -28
- package/spec/tokenizer_ja_spec.js +0 -0
- package/spec/treebank_word_tokenizer_spec.js +0 -0
- package/spec/trie_spec.js +27 -3
|
@@ -26,4 +26,252 @@ describe('sentence analyzer', function() {
|
|
|
26
26
|
it('should load', function() {
|
|
27
27
|
var analyzer = new SentenceAnalyzer(null, function() { });
|
|
28
28
|
});
|
|
29
|
+
|
|
30
|
+
it('should determine PP and SP, given a POS', function () {
|
|
31
|
+
var sentenceTags = [
|
|
32
|
+
{token: 'The', pos: 'DT'},
|
|
33
|
+
{token: 'angry', pos: 'JJ'},
|
|
34
|
+
{token: 'bear', pos: 'NN'},
|
|
35
|
+
{token: 'chased', pos: 'VB'},
|
|
36
|
+
{token: 'the', pos: 'DT'},
|
|
37
|
+
{token: 'frightened', pos: 'JJ'},
|
|
38
|
+
{token: 'little', pos: 'JJ'},
|
|
39
|
+
{token: 'squirrel', pos: 'NN'}
|
|
40
|
+
];
|
|
41
|
+
new SentenceAnalyzer({tags: sentenceTags}, function (analyzer) {
|
|
42
|
+
analyzer.part(function (part) {
|
|
43
|
+
var posTags = part.posObj.tags;
|
|
44
|
+
for (var tagNum = 0; tagNum < posTags.length; tagNum++) {
|
|
45
|
+
var posTag = posTags[tagNum];
|
|
46
|
+
if (posTag.token === 'angry') {
|
|
47
|
+
expect(posTag.spos).toEqual('SP');
|
|
48
|
+
} else if (posTag.token === 'squirrel') {
|
|
49
|
+
expect(posTag.spos).toEqual('PP');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
expect(analyzer.subjectToString().trim()).toEqual('The angry bear');
|
|
53
|
+
expect(analyzer.predicateToString().trim()).toEqual('chased the frightened little squirrel');
|
|
54
|
+
expect(analyzer.toString().trim()).toEqual('The angry bear chased the frightened little squirrel');
|
|
55
|
+
expect(analyzer.implicitYou()).toEqual(false);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should determine PP and SP given a POS that begins with a verb', function () {
|
|
61
|
+
var sentenceTags = [
|
|
62
|
+
{token: 'Vote', pos: 'VB'},
|
|
63
|
+
{token: 'for', pos: 'IN'},
|
|
64
|
+
{token: 'me', pos: 'PRP'}
|
|
65
|
+
];
|
|
66
|
+
new SentenceAnalyzer({tags: sentenceTags}, function (analyzer) {
|
|
67
|
+
analyzer.part(function (part) {
|
|
68
|
+
var posTags = part.posObj.tags;
|
|
69
|
+
for (var tagNum = 0; tagNum < posTags.length; tagNum++) {
|
|
70
|
+
var posTag = posTags[tagNum];
|
|
71
|
+
if (posTag.token === 'Vote') {
|
|
72
|
+
expect(posTag.spos).toEqual('PP');
|
|
73
|
+
} else if (posTag.token === 'me') {
|
|
74
|
+
expect(posTag.pp).toEqual(true);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// Adds implicit you.
|
|
78
|
+
var lastTagNum = posTags.length - 1;
|
|
79
|
+
expect(posTags[lastTagNum].token).toEqual('You');
|
|
80
|
+
expect(posTags[lastTagNum].pos).toEqual('PRP');
|
|
81
|
+
expect(posTags[lastTagNum].added).toEqual(true);
|
|
82
|
+
expect(analyzer.implicitYou()).toEqual(true);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should look for EX before VB', function () {
|
|
88
|
+
var sentenceTags = [
|
|
89
|
+
{token: 'There', pos: 'EX'},
|
|
90
|
+
{token: 'is', pos: 'VB'},
|
|
91
|
+
{token: 'a', pos: 'DT'},
|
|
92
|
+
{token: 'house', pos: 'NN'},
|
|
93
|
+
{token: 'in', pos: 'IN'},
|
|
94
|
+
{token: 'the', pos: 'DT'},
|
|
95
|
+
{token: 'valley', pos: 'DT'}
|
|
96
|
+
];
|
|
97
|
+
new SentenceAnalyzer({tags: sentenceTags}, function (analyzer) {
|
|
98
|
+
analyzer.part(function (part) {
|
|
99
|
+
var posTags = part.posObj.tags;
|
|
100
|
+
for (var tagNum = 0; tagNum < posTags.length; tagNum++) {
|
|
101
|
+
var posTag = posTags[tagNum];
|
|
102
|
+
if (posTag.token === 'There') {
|
|
103
|
+
expect(posTag.spos).toEqual('SP');
|
|
104
|
+
} else if (posTag.token === 'is') {
|
|
105
|
+
expect(posTag.spos).toEqual('SP');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
expect(analyzer.subjectToString().trim()).toEqual('There is a house');
|
|
109
|
+
expect(analyzer.predicateToString().trim()).toEqual('');
|
|
110
|
+
expect(analyzer.toString().trim()).toEqual('There is a house in the valley');
|
|
111
|
+
expect(analyzer.implicitYou()).toEqual(false);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('#type', function () {
|
|
117
|
+
|
|
118
|
+
it('should determine a command without punctuation', function () {
|
|
119
|
+
var sentenceTags = [
|
|
120
|
+
{token: 'Vote', pos: 'VB'},
|
|
121
|
+
{token: 'for', pos: 'IN'},
|
|
122
|
+
{token: 'me', pos: 'PRP'}
|
|
123
|
+
];
|
|
124
|
+
var punct = function () {
|
|
125
|
+
return [];
|
|
126
|
+
};
|
|
127
|
+
new SentenceAnalyzer({tags: sentenceTags, punct: punct}, function (analyzer) {
|
|
128
|
+
analyzer.part(function () {
|
|
129
|
+
analyzer.type(function (type) {
|
|
130
|
+
expect(analyzer.senType).toEqual('COMMAND');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should determine an interrogative beginning with who', function () {
|
|
137
|
+
var sentenceTags = [
|
|
138
|
+
{token: 'Who', pos: 'WP'},
|
|
139
|
+
{token: 'voted', pos: 'VB'}
|
|
140
|
+
];
|
|
141
|
+
var punct = function () {
|
|
142
|
+
return [];
|
|
143
|
+
};
|
|
144
|
+
new SentenceAnalyzer({tags: sentenceTags, punct: punct}, function (analyzer) {
|
|
145
|
+
analyzer.part(function () {
|
|
146
|
+
analyzer.type(function (type) {
|
|
147
|
+
expect(analyzer.senType).toEqual('INTERROGATIVE');
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should determine an interrogative ending with a personal pronoun', function () {
|
|
154
|
+
var sentenceTags = [
|
|
155
|
+
{token: 'Should', pos: 'MD'},
|
|
156
|
+
{token: 'we', pos: 'PRP'}
|
|
157
|
+
];
|
|
158
|
+
var punct = function () {
|
|
159
|
+
return '';
|
|
160
|
+
};
|
|
161
|
+
new SentenceAnalyzer({tags: sentenceTags, punct: punct}, function (analyzer) {
|
|
162
|
+
analyzer.part(function () {
|
|
163
|
+
analyzer.type(function (type) {
|
|
164
|
+
expect(analyzer.senType).toEqual('INTERROGATIVE');
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should classify other sentences as unknown', function () {
|
|
171
|
+
var sentenceTags = [
|
|
172
|
+
{token: 'I', pos: 'PRP'},
|
|
173
|
+
{token: 'am', pos: 'VB'},
|
|
174
|
+
{token: 'unsure', pos: 'JJ'}
|
|
175
|
+
];
|
|
176
|
+
var punct = function () {
|
|
177
|
+
return '';
|
|
178
|
+
};
|
|
179
|
+
new SentenceAnalyzer({tags: sentenceTags, punct: punct}, function (analyzer) {
|
|
180
|
+
analyzer.part(function () {
|
|
181
|
+
analyzer.type(function (type) {
|
|
182
|
+
expect(analyzer.senType).toEqual('UNKNOWN');
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should determine an interrogative ending with a question mark', function () {
|
|
189
|
+
var sentenceTags = [
|
|
190
|
+
{token: 'Do', pos: 'VB'},
|
|
191
|
+
{token: 'I', pos: 'PRP'},
|
|
192
|
+
{token: 'care', pos: 'VB'}
|
|
193
|
+
];
|
|
194
|
+
var punct = function () {
|
|
195
|
+
return [{token: '?', pos: '.'}];
|
|
196
|
+
};
|
|
197
|
+
new SentenceAnalyzer({tags: sentenceTags, punct: punct}, function (analyzer) {
|
|
198
|
+
analyzer.part(function () {
|
|
199
|
+
analyzer.type(function (type) {
|
|
200
|
+
expect(analyzer.senType).toEqual('INTERROGATIVE');
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('should determine a command ending in an exclamation point', function () {
|
|
207
|
+
var sentenceTags = [
|
|
208
|
+
{token: 'Vote', pos: 'VB'},
|
|
209
|
+
{token: 'for', pos: 'IN'},
|
|
210
|
+
{token: 'me', pos: 'PRP'}
|
|
211
|
+
];
|
|
212
|
+
var punct = function () {
|
|
213
|
+
return [{token: '!', pos: '.'}];
|
|
214
|
+
};
|
|
215
|
+
new SentenceAnalyzer({tags: sentenceTags, punct: punct}, function (analyzer) {
|
|
216
|
+
analyzer.part(function () {
|
|
217
|
+
analyzer.type(function (type) {
|
|
218
|
+
expect(analyzer.senType).toEqual('COMMAND');
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should determine an exclamation ending in an exclamation point', function () {
|
|
225
|
+
var sentenceTags = [
|
|
226
|
+
{token: 'We', pos: 'PRP'},
|
|
227
|
+
{token: 'like', pos: 'VB'},
|
|
228
|
+
{token: 'sheep', pos: 'NN'}
|
|
229
|
+
];
|
|
230
|
+
var punct = function () {
|
|
231
|
+
return [{token: '!', pos: '.'}];
|
|
232
|
+
};
|
|
233
|
+
new SentenceAnalyzer({tags: sentenceTags, punct: punct}, function (analyzer) {
|
|
234
|
+
analyzer.part(function () {
|
|
235
|
+
analyzer.type(function (type) {
|
|
236
|
+
expect(analyzer.senType).toEqual('EXCLAMATORY');
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('should determine a command ending with .', function () {
|
|
243
|
+
var sentenceTags = [
|
|
244
|
+
{token: 'Vote', pos: 'VB'},
|
|
245
|
+
{token: 'for', pos: 'IN'},
|
|
246
|
+
{token: 'me', pos: 'PRP'}
|
|
247
|
+
];
|
|
248
|
+
var punct = function () {
|
|
249
|
+
return [{token: '.', pos: '.'}];
|
|
250
|
+
};
|
|
251
|
+
new SentenceAnalyzer({tags: sentenceTags, punct: punct}, function (analyzer) {
|
|
252
|
+
analyzer.part(function () {
|
|
253
|
+
analyzer.type(function (type) {
|
|
254
|
+
expect(analyzer.senType).toEqual('COMMAND');
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('should determine a declaration ending with a .', function () {
|
|
261
|
+
var sentenceTags = [
|
|
262
|
+
{token: 'We', pos: 'PRP'},
|
|
263
|
+
{token: 'like', pos: 'VB'},
|
|
264
|
+
{token: 'sheep', pos: 'NN'}
|
|
265
|
+
];
|
|
266
|
+
var punct = function () {
|
|
267
|
+
return [{token: '.', pos: '.'}];
|
|
268
|
+
};
|
|
269
|
+
new SentenceAnalyzer({tags: sentenceTags, punct: punct}, function (analyzer) {
|
|
270
|
+
analyzer.part(function () {
|
|
271
|
+
type = analyzer.type();
|
|
272
|
+
expect(type).toEqual('DECLARATIVE');
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
});
|
|
29
277
|
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2014, Lee Wenzhu
|
|
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
|
+
'use strict';
|
|
23
|
+
|
|
24
|
+
var SPT = require('../lib/natural/util/shortest_path_tree'),
|
|
25
|
+
Topological = require('../lib/natural/util/topological'),
|
|
26
|
+
EdgeWeightedDigraph = require('../lib/natural/util/edge_weighted_digraph');
|
|
27
|
+
|
|
28
|
+
describe("shortest path tree", function() {
|
|
29
|
+
var digraph = new EdgeWeightedDigraph();
|
|
30
|
+
digraph.add(5,4,0.35);
|
|
31
|
+
digraph.add(4,7,0.37);
|
|
32
|
+
digraph.add(5,7,0.28);
|
|
33
|
+
digraph.add(5,1,0.32);
|
|
34
|
+
digraph.add(4,0,0.38);
|
|
35
|
+
digraph.add(0,2,0.26);
|
|
36
|
+
digraph.add(3,7,0.39);
|
|
37
|
+
digraph.add(1,3,0.29);
|
|
38
|
+
digraph.add(7,2,0.34);
|
|
39
|
+
digraph.add(6,2,0.40);
|
|
40
|
+
digraph.add(3,6,0.52);
|
|
41
|
+
digraph.add(6,0,0.58);
|
|
42
|
+
digraph.add(6,4,0.93);
|
|
43
|
+
describe("edge weighted digraph normal operations", function() {
|
|
44
|
+
it("should get the numbers of vertexs", function() {
|
|
45
|
+
expect(digraph.v()).toBe(8);
|
|
46
|
+
});
|
|
47
|
+
it("should get the numbers of edges", function() {
|
|
48
|
+
expect(digraph.e()).toBe(13);
|
|
49
|
+
});
|
|
50
|
+
it("should print all item in digraph", function() {
|
|
51
|
+
expect(digraph.toString()).toBe('0 -> 2, 0.26\n' +
|
|
52
|
+
'1 -> 3, 0.29\n' +
|
|
53
|
+
'3 -> 7, 0.39\n' +
|
|
54
|
+
'3 -> 6, 0.52\n' +
|
|
55
|
+
'4 -> 7, 0.37\n' +
|
|
56
|
+
'4 -> 0, 0.38\n' +
|
|
57
|
+
'5 -> 4, 0.35\n' +
|
|
58
|
+
'5 -> 7, 0.28\n' +
|
|
59
|
+
'5 -> 1, 0.32\n' +
|
|
60
|
+
'6 -> 2, 0.4\n' +
|
|
61
|
+
'6 -> 0, 0.58\n' +
|
|
62
|
+
'6 -> 4, 0.93\n' +
|
|
63
|
+
'7 -> 2, 0.34');
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("topo sort for digraph", function() {
|
|
68
|
+
it("should sort all the vertexs", function() {
|
|
69
|
+
var topoSort = new Topological(digraph);
|
|
70
|
+
expect(topoSort.isDAG()).toBe(true);
|
|
71
|
+
expect(topoSort.order()).toEqual([ 5, 1, 3, 6, 4, 7, 0, 2 ]);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("shortest path tree normal operations", function() {
|
|
76
|
+
var lpt = new SPT(digraph, 5);
|
|
77
|
+
expect(lpt.hasPathTo(0)).toBe(true);
|
|
78
|
+
expect(lpt.hasPathTo(1)).toBe(true);
|
|
79
|
+
expect(lpt.hasPathTo(2)).toBe(true);
|
|
80
|
+
expect(lpt.hasPathTo(3)).toBe(true);
|
|
81
|
+
expect(lpt.hasPathTo(4)).toBe(true);
|
|
82
|
+
expect(lpt.hasPathTo(5)).toBe(false);
|
|
83
|
+
expect(lpt.hasPathTo(6)).toBe(true);
|
|
84
|
+
expect(lpt.hasPathTo(7)).toBe(true);
|
|
85
|
+
expect(lpt.pathTo(0)).toEqual([ 5, 4, 0 ]);
|
|
86
|
+
expect(lpt.pathTo(1)).toEqual([ 5, 1 ]);
|
|
87
|
+
expect(lpt.pathTo(2)).toEqual([ 5, 7, 2 ]);
|
|
88
|
+
expect(lpt.pathTo(3)).toEqual([ 5, 1, 3 ]);
|
|
89
|
+
expect(lpt.pathTo(4)).toEqual([ 5, 4 ]);
|
|
90
|
+
expect(lpt.pathTo(5)).toEqual([ ]);
|
|
91
|
+
expect(lpt.pathTo(6)).toEqual([ 5, 1, 3, 6 ]);
|
|
92
|
+
expect(lpt.pathTo(7)).toEqual([ 5, 7 ]);
|
|
93
|
+
expect(lpt.getDistTo(0)).toBe(0.73);
|
|
94
|
+
expect(lpt.getDistTo(1)).toBe(0.32);
|
|
95
|
+
expect(lpt.getDistTo(2)).toBe(0.62);
|
|
96
|
+
expect(lpt.getDistTo(3)).toBe(0.61);
|
|
97
|
+
expect(lpt.getDistTo(4)).toBe(0.35);
|
|
98
|
+
expect(lpt.getDistTo(5)).toBe(0);
|
|
99
|
+
expect(lpt.getDistTo(6)).toBe(1.13);
|
|
100
|
+
expect(lpt.getDistTo(7)).toBe(0.28);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Check the issue #150
|
|
105
|
+
**/
|
|
106
|
+
describe("lightest weight path", function(){
|
|
107
|
+
it("should find the lightest weight path", function(){
|
|
108
|
+
var digraph = new EdgeWeightedDigraph();
|
|
109
|
+
|
|
110
|
+
digraph.add(1,3,0.29);
|
|
111
|
+
digraph.add(1,6,0);
|
|
112
|
+
digraph.add(3,6,0);
|
|
113
|
+
|
|
114
|
+
var spt = new SPT(digraph, 1);
|
|
115
|
+
var path = spt.pathTo(6);
|
|
116
|
+
expect(path).toEqual([1,6]);
|
|
117
|
+
});
|
|
118
|
+
it("should find the lightest weight path", function(){
|
|
119
|
+
var digraph = new EdgeWeightedDigraph();
|
|
120
|
+
|
|
121
|
+
digraph.add(1,3,-1);
|
|
122
|
+
digraph.add(1,6,0);
|
|
123
|
+
digraph.add(3,6,0);
|
|
124
|
+
|
|
125
|
+
var spt = new SPT(digraph, 1);
|
|
126
|
+
var path = spt.pathTo(6);
|
|
127
|
+
var hasPath = spt.hasPathTo(6);
|
|
128
|
+
expect(hasPath).toBe(true);
|
|
129
|
+
expect(path).toEqual([1,3,6]);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
});
|
package/spec/soundex_spec.js
CHANGED
|
@@ -65,15 +65,6 @@ describe('soundex', function() {
|
|
|
65
65
|
it('sound condense sequences', function() {
|
|
66
66
|
expect(soundex.condense('11222556')).toBe('1256');
|
|
67
67
|
});
|
|
68
|
-
|
|
69
|
-
it('sound condense sequences around H and W', function() {
|
|
70
|
-
expect(soundex.condense('113w355')).toBe('135');
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('sound drop irrelevant H and W', function() {
|
|
74
|
-
expect(soundex.condense('phonetics')).toBe('ponetics');
|
|
75
|
-
expect(soundex.condense('towering')).toBe('toering');
|
|
76
|
-
});
|
|
77
68
|
});
|
|
78
69
|
|
|
79
70
|
describe('padRight0', function() {
|
|
@@ -132,6 +123,19 @@ describe('soundex', function() {
|
|
|
132
123
|
expect('phonetics'.phonetics()).toBe('P532');
|
|
133
124
|
expect('PHONETICS'.phonetics()).toBe('P532');
|
|
134
125
|
});
|
|
126
|
+
|
|
127
|
+
it('issue 221 -- inital vowels that duplicate cons codes', function() {
|
|
128
|
+
soundex.attach();
|
|
129
|
+
expect('Lloyd'.phonetics()).toBe('L300');
|
|
130
|
+
expect('Pfister'.phonetics()).toBe('P236');
|
|
131
|
+
|
|
132
|
+
expect('manhattan'.phonetics()).toBe('M535');
|
|
133
|
+
expect('Lukasiewicz'.phonetics()).toBe('L222');
|
|
134
|
+
|
|
135
|
+
expect('Gauss'.phonetics()).toBe('G200');
|
|
136
|
+
expect('Tymczak'.phonetics()).toBe('T522');
|
|
137
|
+
expect('Jackson'.phonetics()).toBe('J250');
|
|
138
|
+
});
|
|
135
139
|
|
|
136
140
|
it('should tokenize and return string phonetics with string patch', function() {
|
|
137
141
|
soundex.attach();
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
var Spellcheck = require('../lib/natural/spellcheck/spellcheck.js');
|
|
4
|
+
|
|
5
|
+
describe('spellcheck', function() {
|
|
6
|
+
|
|
7
|
+
describe('distance 1 corrections', function() {
|
|
8
|
+
var spellcheck = new Spellcheck(['something']);
|
|
9
|
+
|
|
10
|
+
it('should fix deletions', function() {
|
|
11
|
+
expect(spellcheck.getCorrections('smething')).toContain('something');
|
|
12
|
+
});
|
|
13
|
+
it('should fix insertions', function() {
|
|
14
|
+
expect(spellcheck.getCorrections('somethhing')).toContain('something');
|
|
15
|
+
});
|
|
16
|
+
it('should fix transpositions', function() {
|
|
17
|
+
expect(spellcheck.getCorrections('somehting')).toContain('something');
|
|
18
|
+
});
|
|
19
|
+
it('should fix replacements', function() {
|
|
20
|
+
expect(spellcheck.getCorrections('sometzing')).toContain('something');
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe('distance 2 corrections', function() {
|
|
25
|
+
var spellcheck = new Spellcheck(['something']);
|
|
26
|
+
|
|
27
|
+
it('should fix a deletion then a transposition', function() {
|
|
28
|
+
expect(spellcheck.getCorrections('smtehing', 2)).toContain('something');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('edits', function() {
|
|
33
|
+
var spellcheck = new Spellcheck([]);
|
|
34
|
+
var edits = spellcheck.edits('ab');
|
|
35
|
+
|
|
36
|
+
it('should compute insertions', function() {
|
|
37
|
+
var insertions = ['xab', 'axb', 'abx'];
|
|
38
|
+
for(var i in insertions) {
|
|
39
|
+
expect(edits.indexOf(insertions[i])).toBeGreaterThan(-1);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
it('should compute deletions', function() {
|
|
43
|
+
var deletions = ['a', 'b'];
|
|
44
|
+
for(var i in deletions) {
|
|
45
|
+
expect(edits.indexOf(deletions[i])).toBeGreaterThan(-1);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
it('should compute transpositions', function() {
|
|
49
|
+
var transpositions = ['ba']
|
|
50
|
+
for(var i in transpositions) {
|
|
51
|
+
expect(edits.indexOf(transpositions[i])).toBeGreaterThan(-1);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
it('should compute replacements', function() {
|
|
55
|
+
var replacements = ['zb', 'af']
|
|
56
|
+
for(var i in replacements) {
|
|
57
|
+
expect(edits.indexOf(replacements[i])).toBeGreaterThan(-1);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('edits up to distance', function() {
|
|
63
|
+
var spellcheck = new Spellcheck([]);
|
|
64
|
+
|
|
65
|
+
it('should correctly compute edits at distance', function() {
|
|
66
|
+
var edits = spellcheck.editsWithMaxDistance('abc', 2);
|
|
67
|
+
expect(edits[1].indexOf('abzc')).toBeGreaterThan(-1); // 1 insertion
|
|
68
|
+
expect(edits[2].indexOf('a')).toBeGreaterThan(-1); // 2 deletions
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('boolean spellcheck', function() {
|
|
73
|
+
var spellcheck = new Spellcheck(['cat']);
|
|
74
|
+
|
|
75
|
+
it('should consider cat a word', function() {
|
|
76
|
+
expect(spellcheck.isCorrect('cat')).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should not consider dog a word', function() {
|
|
80
|
+
expect(spellcheck.isCorrect('dog')).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('special cases', function() {
|
|
85
|
+
var spellcheck = new Spellcheck(['cat']);
|
|
86
|
+
|
|
87
|
+
it('should return the input word as the most probable correction if it is already correct', function() {
|
|
88
|
+
expect(spellcheck.getCorrections('cat').indexOf('cat')).toBe(0);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2014, 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
|
+
var StemmerToken = require('../lib/natural/stemmers/token');
|
|
24
|
+
|
|
25
|
+
describe('stemmer token', function () {
|
|
26
|
+
|
|
27
|
+
it('should receive a string', function () {
|
|
28
|
+
var string = 'test',
|
|
29
|
+
token = new StemmerToken(string);
|
|
30
|
+
|
|
31
|
+
expect(token.string).toBe(string);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should hold the original token string', function () {
|
|
35
|
+
var string = 'test',
|
|
36
|
+
token = new StemmerToken(string);
|
|
37
|
+
|
|
38
|
+
expect(token.original).toBe(string);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should replace all instances of a string', function () {
|
|
42
|
+
var string = 'tester',
|
|
43
|
+
token = new StemmerToken(string);
|
|
44
|
+
|
|
45
|
+
token.replaceAll('e', 'a');
|
|
46
|
+
expect(token.string).toBe('tastar');
|
|
47
|
+
|
|
48
|
+
token.replaceAll('ar', 'er');
|
|
49
|
+
expect(token.string).toBe('taster');
|
|
50
|
+
|
|
51
|
+
token.replaceAll('r', '');
|
|
52
|
+
expect(token.string).toBe('taste');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should allow vowels to be set', function () {
|
|
56
|
+
var vowels = 'aeiou',
|
|
57
|
+
vowelsArray = vowels.split(''),
|
|
58
|
+
token = new StemmerToken('');
|
|
59
|
+
|
|
60
|
+
token.usingVowels(vowels);
|
|
61
|
+
expect(token.vowels).toBe(vowels);
|
|
62
|
+
|
|
63
|
+
token.usingVowels(vowelsArray);
|
|
64
|
+
expect(token.vowels).toBe(vowelsArray);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should check for vowels', function () {
|
|
68
|
+
var vowels = 'aeiou',
|
|
69
|
+
token = new StemmerToken('test');
|
|
70
|
+
|
|
71
|
+
token.usingVowels(vowels);
|
|
72
|
+
|
|
73
|
+
expect(token.hasVowelAtIndex(0)).toBe(false);
|
|
74
|
+
expect(token.hasVowelAtIndex(1)).toBe(true);
|
|
75
|
+
expect(token.hasVowelAtIndex(99)).toBe(false);
|
|
76
|
+
|
|
77
|
+
token.usingVowels(vowels.split(''));
|
|
78
|
+
|
|
79
|
+
expect(token.hasVowelAtIndex(0)).toBe(false);
|
|
80
|
+
expect(token.hasVowelAtIndex(1)).toBe(true);
|
|
81
|
+
expect(token.hasVowelAtIndex(99)).toBe(false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should find the next vowel', function () {
|
|
85
|
+
var token = new StemmerToken('tester').usingVowels('aeiou');
|
|
86
|
+
|
|
87
|
+
expect(token.nextVowelIndex(0)).toBe(1);
|
|
88
|
+
expect(token.nextVowelIndex(1)).toBe(1);
|
|
89
|
+
expect(token.nextVowelIndex(2)).toBe(4);
|
|
90
|
+
expect(token.nextVowelIndex(5)).toBe(6);
|
|
91
|
+
expect(token.nextVowelIndex(99)).toBe(6);
|
|
92
|
+
expect(token.nextVowelIndex(-1)).toBe(6);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should find the next consonant', function () {
|
|
96
|
+
var token = new StemmerToken('testee').usingVowels('aeiou');
|
|
97
|
+
|
|
98
|
+
expect(token.nextConsonantIndex(0)).toBe(0);
|
|
99
|
+
expect(token.nextConsonantIndex(1)).toBe(2);
|
|
100
|
+
expect(token.nextConsonantIndex(5)).toBe(6);
|
|
101
|
+
expect(token.nextConsonantIndex(99)).toBe(6);
|
|
102
|
+
expect(token.nextConsonantIndex(-1)).toBe(6);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should mark regions', function() {
|
|
106
|
+
var token = new StemmerToken('tester');
|
|
107
|
+
|
|
108
|
+
token.markRegion('test', 1);
|
|
109
|
+
expect(token.regions.test).toBe(1);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should mark regions with a callback', function() {
|
|
113
|
+
var token = new StemmerToken('tester'),
|
|
114
|
+
context = { value: 99 };
|
|
115
|
+
|
|
116
|
+
token.markRegion('test', 1, function (arg) { return arg; });
|
|
117
|
+
expect(token.regions.test).toBe(1);
|
|
118
|
+
|
|
119
|
+
token.markRegion('test', [1], function (arg) { return arg; });
|
|
120
|
+
expect(token.regions.test).toBe(1);
|
|
121
|
+
|
|
122
|
+
token.markRegion('test', [1, 1], function (a1, a2) { return a1 + a2; });
|
|
123
|
+
expect(token.regions.test).toBe(2);
|
|
124
|
+
|
|
125
|
+
token.markRegion('test', null, function () { return this.string.length; });
|
|
126
|
+
expect(token.regions.test).toBe(6);
|
|
127
|
+
|
|
128
|
+
token.markRegion('test', null, function () { return this.value; }, context);
|
|
129
|
+
expect(token.regions.test).toBe(99);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should check for suffixes', function() {
|
|
133
|
+
var token = new StemmerToken('tester');
|
|
134
|
+
|
|
135
|
+
expect(token.hasSuffix('er')).toBe(true);
|
|
136
|
+
expect(token.hasSuffix('st')).toBe(false);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should check for suffixes within a region', function() {
|
|
140
|
+
var token = new StemmerToken('tester').markRegion('region', 2);
|
|
141
|
+
|
|
142
|
+
expect(token.hasSuffixInRegion('st', 'region')).toBe(false);
|
|
143
|
+
expect(token.hasSuffixInRegion('ster', 'region')).toBe(true);
|
|
144
|
+
expect(token.hasSuffixInRegion('ester', 'region')).toBe(false);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should replace the suffix within a region', function() {
|
|
148
|
+
var t1 = new StemmerToken('tester').markRegion('region', 4),
|
|
149
|
+
t2 = new StemmerToken('tester').markRegion('region', 0);
|
|
150
|
+
|
|
151
|
+
t1.replaceSuffixInRegion('ter', '<s>', 'region');
|
|
152
|
+
expect(t1.string).toBe('tester');
|
|
153
|
+
|
|
154
|
+
t1.replaceSuffixInRegion('er', '<s>', 'region');
|
|
155
|
+
expect(t1.string).toBe('test<s>');
|
|
156
|
+
|
|
157
|
+
t2.replaceSuffixInRegion('protester', '<s>', 'region');
|
|
158
|
+
expect(t2.string).toBe('tester');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
});
|