natural 6.12.0 → 7.0.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/.env +2 -1
- package/.eslintrc.json +13 -0
- package/.github/workflows/node.js.yml +3 -6
- package/lib/natural/analyzers/SenType.ts +30 -0
- package/lib/natural/analyzers/index.d.ts +6 -10
- package/lib/natural/brill_pos_tagger/index.d.ts +23 -14
- package/lib/natural/brill_pos_tagger/index.js +1 -1
- package/lib/natural/classifiers/bayes_classifier.js +1 -1
- package/lib/natural/classifiers/classifier.js +11 -34
- package/lib/natural/classifiers/classifier_train_parallel.js +7 -5
- package/lib/natural/classifiers/index.d.ts +36 -15
- package/lib/natural/classifiers/maxent/Classifier.js +5 -3
- package/lib/natural/distance/index.d.ts +11 -14
- package/lib/natural/inflectors/count_inflector.js +18 -21
- package/lib/natural/inflectors/fr/count_inflector.js +18 -19
- package/lib/natural/inflectors/index.d.ts +12 -3
- package/lib/natural/inflectors/index.js +3 -0
- package/lib/natural/ngrams/index.d.ts +5 -5
- package/lib/natural/normalizers/index.d.ts +24 -1
- package/lib/natural/normalizers/index.js +4 -1
- package/lib/natural/normalizers/normalizer_ja.js +109 -72
- package/lib/natural/phonetics/dm_soundex.js +52 -51
- package/lib/natural/phonetics/double_metaphone.js +397 -399
- package/lib/natural/phonetics/index.d.ts +42 -12
- package/lib/natural/phonetics/metaphone.js +134 -157
- package/lib/natural/phonetics/phonetic.js +5 -29
- package/lib/natural/phonetics/soundex.js +45 -54
- package/lib/natural/sentiment/index.d.ts +6 -6
- package/lib/natural/spellcheck/index.d.ts +2 -0
- package/lib/natural/stemmers/index.d.ts +43 -1
- package/lib/natural/stemmers/index.js +1 -0
- package/lib/natural/stemmers/token.js +131 -132
- package/lib/natural/tfidf/index.d.ts +7 -7
- package/lib/natural/tfidf/tfidf.js +7 -1
- package/lib/natural/tokenizers/index.d.ts +2 -2
- package/lib/natural/trie/index.d.ts +1 -1
- package/lib/natural/util/index.d.ts +8 -0
- package/lib/natural/util/index.js +2 -0
- package/lib/natural/util/storage/index.d.ts +39 -0
- package/lib/natural/util/storage/index.js +24 -0
- package/package.json +22 -9
- package/ts_spec/MaxEntClassifier_spec.ts +130 -0
- package/ts_spec/SentimentAnalyzer_spec.ts +232 -0
- package/ts_spec/WordPunctTokenizer_spec.ts +57 -0
- package/ts_spec/aggressive_tokenizer_de_spec.ts +36 -0
- package/ts_spec/aggressive_tokenizer_es_spec.ts +39 -0
- package/ts_spec/aggressive_tokenizer_fr_spec.ts +111 -0
- package/ts_spec/aggressive_tokenizer_hi_spec.ts +70 -0
- package/ts_spec/aggressive_tokenizer_nl_spec.ts +39 -0
- package/ts_spec/aggressive_tokenizer_pt_spec.ts +48 -0
- package/ts_spec/aggressive_tokenizer_spec.ts +57 -0
- package/ts_spec/aggressive_tokenizer_sv_spec.ts +52 -0
- package/ts_spec/aggressive_tokenizer_vi_spec.ts +48 -0
- package/ts_spec/bayes_classifier_spec.ts +165 -0
- package/ts_spec/brill_pos_tagger_spec.ts +79 -0
- package/ts_spec/brill_pos_trainer_spec.ts +107 -0
- package/ts_spec/carry_stemmer_fr_spec.ts +97 -0
- package/ts_spec/classifier_spec.ts +104 -0
- package/ts_spec/count_inflector_fr_spec.ts +47 -0
- package/ts_spec/count_inflector_spec.ts +76 -0
- package/ts_spec/damerau_levenshtein_spec.ts +53 -0
- package/ts_spec/dice_coefficient_spec.ts +37 -0
- package/ts_spec/double_metaphone_spec.ts +739 -0
- package/ts_spec/hamming_distance_spec.ts +66 -0
- package/ts_spec/jaro-winkler_spec.ts +79 -0
- package/ts_spec/lancaster_stemmer_spec.ts +63 -0
- package/ts_spec/levenshtein_spec.ts +107 -0
- package/ts_spec/logistic_regression_classifier_spec.ts +93 -0
- package/ts_spec/longest_path_tree_spec.ts +111 -0
- package/ts_spec/metaphone_spec.ts +258 -0
- package/ts_spec/ngram_spec.ts +145 -0
- package/ts_spec/ngram_zh_spec.ts +124 -0
- package/ts_spec/normalizer_ja_spec.ts +165 -0
- package/ts_spec/normalizer_no_spec.ts +112 -0
- package/ts_spec/normalizer_spec.ts +141 -0
- package/ts_spec/noun_inflector_fr_spec.ts +472 -0
- package/ts_spec/noun_inflector_ja_spec.ts +116 -0
- package/ts_spec/noun_inflector_spec.ts +369 -0
- package/ts_spec/orthography_tokenizer_spec.ts +47 -0
- package/ts_spec/porter_stemmer_de_spec.ts +53 -0
- package/ts_spec/porter_stemmer_es_spec.ts +63 -0
- package/ts_spec/porter_stemmer_fr_spec.ts +103 -0
- package/ts_spec/porter_stemmer_it_spec.ts +56 -0
- package/ts_spec/porter_stemmer_nl_spec.ts +73 -0
- package/ts_spec/porter_stemmer_no_spec.ts +148 -0
- package/ts_spec/porter_stemmer_pt_spec.ts +50 -0
- package/ts_spec/porter_stemmer_ru_spec.ts +67 -0
- package/ts_spec/porter_stemmer_spec.ts +202 -0
- package/ts_spec/porter_stemmer_sv_spec.ts +41 -0
- package/ts_spec/present_verb_inflector_spec.ts +127 -0
- package/ts_spec/remove_diacritics_spec.ts +30 -0
- package/ts_spec/sentence_analyzer_spec.ts +293 -0
- package/ts_spec/sentence_tokenizer_parser_spec.ts +200 -0
- package/ts_spec/sentence_tokenizer_spec.ts +189 -0
- package/ts_spec/shortest_path_tree_spec.ts +141 -0
- package/ts_spec/soundex_spec.ts +162 -0
- package/ts_spec/spellcheck_spec.ts +112 -0
- package/ts_spec/stemmer_id_spec.ts +374 -0
- package/ts_spec/stemmer_ja_spec.ts +69 -0
- package/ts_spec/stemmer_token_spec.ts +163 -0
- package/ts_spec/tfidf_spec.ts +275 -0
- package/ts_spec/tokenizer_case_spec.ts +242 -0
- package/ts_spec/tokenizer_ja_spec.ts +63 -0
- package/ts_spec/transliterator_ja_spec.ts +105 -0
- package/ts_spec/treebank_word_tokenizer_spec.ts +38 -0
- package/ts_spec/trie_spec.ts +197 -0
- package/tsconfig.json +5 -3
- package/example.txt +0 -1
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2011, Chris Umbel
|
|
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
|
+
'use strict'
|
|
24
|
+
|
|
25
|
+
import { SentenceTokenizer as Tokenizer } from 'lib/natural'
|
|
26
|
+
const tokenizer = new Tokenizer()
|
|
27
|
+
|
|
28
|
+
describe('sentence_tokenizer', function () {
|
|
29
|
+
it('should tokenize strings and trim whitespace', function () {
|
|
30
|
+
expect(
|
|
31
|
+
tokenizer.tokenize('This is a sentence. This is another sentence.')
|
|
32
|
+
)
|
|
33
|
+
.toEqual([
|
|
34
|
+
'This is a sentence.',
|
|
35
|
+
'This is another sentence.'
|
|
36
|
+
])
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
/*
|
|
40
|
+
it('should tokenize strings via attached string method', function() {
|
|
41
|
+
tokenizer.attach();
|
|
42
|
+
expect(
|
|
43
|
+
'This is a sentence. This is another sentence.'.tokenize()
|
|
44
|
+
).toEqual([
|
|
45
|
+
'This is a sentence.',
|
|
46
|
+
'This is another sentence.'
|
|
47
|
+
]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should tokenize strings via attached string method', function() {
|
|
51
|
+
tokenizer.attach();
|
|
52
|
+
expect(
|
|
53
|
+
'This is a sentence. This is another sentence.'.tokenize()
|
|
54
|
+
).toEqual([
|
|
55
|
+
'This is a sentence.',
|
|
56
|
+
'This is another sentence.'
|
|
57
|
+
]);
|
|
58
|
+
});
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
it('should include quotation marks', function () {
|
|
62
|
+
expect(
|
|
63
|
+
tokenizer.tokenize('"This is a sentence." This is another sentence.')
|
|
64
|
+
).toEqual([
|
|
65
|
+
'"This is a sentence."',
|
|
66
|
+
'This is another sentence.'
|
|
67
|
+
])
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('should include brackets', function () {
|
|
71
|
+
expect(
|
|
72
|
+
tokenizer.tokenize('This is a sentence. [This is another sentence.]')
|
|
73
|
+
).toEqual([
|
|
74
|
+
'This is a sentence.',
|
|
75
|
+
'[This is another sentence.]'
|
|
76
|
+
])
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('should handle repetitive punctuation', function () {
|
|
80
|
+
expect(
|
|
81
|
+
tokenizer.tokenize('I love you!! Do you love me??')
|
|
82
|
+
).toEqual(['I love you!!', 'Do you love me??'
|
|
83
|
+
])
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('should handle repetitive punctuation with space', function () {
|
|
87
|
+
expect(
|
|
88
|
+
tokenizer.tokenize('I love you! ! Do you love me? ?')
|
|
89
|
+
).toEqual(['I love you! !', 'Do you love me? ?'
|
|
90
|
+
])
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('should handle decimal points in numbers', function () {
|
|
94
|
+
expect(
|
|
95
|
+
tokenizer.tokenize('Pi is approximately equal to 3.14.')
|
|
96
|
+
).toEqual([
|
|
97
|
+
'Pi is approximately equal to 3.14.'
|
|
98
|
+
])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should handle periods in email addresses', function () {
|
|
102
|
+
expect(
|
|
103
|
+
tokenizer.tokenize('My email address is batman@example.com.')
|
|
104
|
+
).toEqual([
|
|
105
|
+
'My email address is batman@example.com.'
|
|
106
|
+
])
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('should handle periods in web addresses', function () {
|
|
110
|
+
expect(
|
|
111
|
+
tokenizer.tokenize('My twitter feed can be found at https://twitter.com/user1.')
|
|
112
|
+
).toEqual([
|
|
113
|
+
'My twitter feed can be found at https://twitter.com/user1.'
|
|
114
|
+
])
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('should handle an ellipsis followed by punctuation', function () {
|
|
118
|
+
expect(
|
|
119
|
+
tokenizer.tokenize('Is this the end for our heroes...?')
|
|
120
|
+
).toEqual([
|
|
121
|
+
'Is this the end for our heroes...?'
|
|
122
|
+
])
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('should handle multiple spaces separating sentences', function () {
|
|
126
|
+
expect(
|
|
127
|
+
tokenizer.tokenize('Tune in tomorrow and find out! Same Bat-Time! Same Bat-Channel!')
|
|
128
|
+
).toEqual([
|
|
129
|
+
'Tune in tomorrow and find out!',
|
|
130
|
+
'Same Bat-Time!',
|
|
131
|
+
'Same Bat-Channel!'
|
|
132
|
+
])
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('should handle braces and quotes (issue #591)', function () {
|
|
136
|
+
expect(
|
|
137
|
+
tokenizer.tokenize('Teste. Test test. Test test: “Test.”')
|
|
138
|
+
).toEqual([
|
|
139
|
+
'Teste.',
|
|
140
|
+
'Test test.',
|
|
141
|
+
'Test test: “Test.”'
|
|
142
|
+
])
|
|
143
|
+
expect(
|
|
144
|
+
tokenizer.tokenize('Test Test. Test test, test test (test test) test: “Test.”')
|
|
145
|
+
).toEqual([
|
|
146
|
+
'Test Test.',
|
|
147
|
+
'Test test, test test (test test) test: “Test.”'
|
|
148
|
+
])
|
|
149
|
+
expect(
|
|
150
|
+
tokenizer.tokenize('Test Test. Test test, test (test) test (test test) test: “Test.”')
|
|
151
|
+
).toEqual([
|
|
152
|
+
'Test Test.',
|
|
153
|
+
'Test test, test (test) test (test test) test: “Test.”'
|
|
154
|
+
])
|
|
155
|
+
expect(
|
|
156
|
+
tokenizer.tokenize('Test: Test (test) test “Test.”')
|
|
157
|
+
).toEqual([
|
|
158
|
+
'Test: Test (test) test “Test.”'
|
|
159
|
+
])
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('Should handle text with the ellipsis symbol … (issue #648)', function () {
|
|
163
|
+
expect(
|
|
164
|
+
tokenizer.tokenize('We’re heading for a catastrophic global temperature rise… Fires are blazing from the Amazon to the Arctic.')
|
|
165
|
+
).toEqual([
|
|
166
|
+
'We’re heading for a catastrophic global temperature rise…',
|
|
167
|
+
'Fires are blazing from the Amazon to the Arctic.'
|
|
168
|
+
])
|
|
169
|
+
})
|
|
170
|
+
it('It should handle last sentence without punctuation (issue #648)', function () {
|
|
171
|
+
expect(
|
|
172
|
+
tokenizer.tokenize('We’re heading for a catastrophic global temperature rise. Fires are blazing from the Amazon to the Arctic')
|
|
173
|
+
).toEqual([
|
|
174
|
+
'We’re heading for a catastrophic global temperature rise.',
|
|
175
|
+
'Fires are blazing from the Amazon to the Arctic'
|
|
176
|
+
])
|
|
177
|
+
})
|
|
178
|
+
it('It should handle the example from issue #689 correctly', function () {
|
|
179
|
+
const testInput = `
|
|
180
|
+
This is some test content.
|
|
181
|
+
|
|
182
|
+
We're trying to figure out variations in versions of the package.
|
|
183
|
+
`.trim()
|
|
184
|
+
expect(tokenizer.tokenize(testInput)).toEqual([
|
|
185
|
+
'This is some test content.',
|
|
186
|
+
'We\'re trying to figure out variations in versions of the package.'
|
|
187
|
+
])
|
|
188
|
+
})
|
|
189
|
+
})
|
|
@@ -0,0 +1,141 @@
|
|
|
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
|
+
|
|
23
|
+
'use strict'
|
|
24
|
+
|
|
25
|
+
import {
|
|
26
|
+
ShortestPathTree as SPT,
|
|
27
|
+
Topological,
|
|
28
|
+
EdgeWeightedDigraph
|
|
29
|
+
} from 'lib/natural'
|
|
30
|
+
|
|
31
|
+
describe('shortest path tree', function () {
|
|
32
|
+
const digraph = new EdgeWeightedDigraph()
|
|
33
|
+
digraph.add(5, 4, 0.35)
|
|
34
|
+
digraph.add(4, 7, 0.37)
|
|
35
|
+
digraph.add(5, 7, 0.28)
|
|
36
|
+
digraph.add(5, 1, 0.32)
|
|
37
|
+
digraph.add(4, 0, 0.38)
|
|
38
|
+
digraph.add(0, 2, 0.26)
|
|
39
|
+
digraph.add(3, 7, 0.39)
|
|
40
|
+
digraph.add(1, 3, 0.29)
|
|
41
|
+
digraph.add(7, 2, 0.34)
|
|
42
|
+
digraph.add(6, 2, 0.40)
|
|
43
|
+
digraph.add(3, 6, 0.52)
|
|
44
|
+
digraph.add(6, 0, 0.58)
|
|
45
|
+
digraph.add(6, 4, 0.93)
|
|
46
|
+
describe('edge weighted digraph normal operations', function () {
|
|
47
|
+
it('should get the numbers of vertexs', function () {
|
|
48
|
+
expect(digraph.v()).toBe(8)
|
|
49
|
+
})
|
|
50
|
+
it('should get the numbers of edges', function () {
|
|
51
|
+
expect(digraph.e()).toBe(13)
|
|
52
|
+
})
|
|
53
|
+
it('should print all item in digraph', function () {
|
|
54
|
+
expect(digraph.toString()).toBe('0 -> 2, 0.26\n' +
|
|
55
|
+
'1 -> 3, 0.29\n' +
|
|
56
|
+
'3 -> 7, 0.39\n' +
|
|
57
|
+
'3 -> 6, 0.52\n' +
|
|
58
|
+
'4 -> 7, 0.37\n' +
|
|
59
|
+
'4 -> 0, 0.38\n' +
|
|
60
|
+
'5 -> 4, 0.35\n' +
|
|
61
|
+
'5 -> 7, 0.28\n' +
|
|
62
|
+
'5 -> 1, 0.32\n' +
|
|
63
|
+
'6 -> 2, 0.4\n' +
|
|
64
|
+
'6 -> 0, 0.58\n' +
|
|
65
|
+
'6 -> 4, 0.93\n' +
|
|
66
|
+
'7 -> 2, 0.34')
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
describe('topo sort for digraph', function () {
|
|
71
|
+
it('should sort all the vertexs', function () {
|
|
72
|
+
const topoSort = new Topological(digraph)
|
|
73
|
+
expect(topoSort.isDAG()).toBe(true)
|
|
74
|
+
expect(topoSort.order()).toEqual([5, 1, 3, 6, 4, 7, 0, 2])
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
describe('shortest path tree normal operations', function () {
|
|
79
|
+
const lpt = new SPT(digraph, 5)
|
|
80
|
+
it('should determine existence of paths', function () {
|
|
81
|
+
expect(lpt.hasPathTo(0)).toBe(true)
|
|
82
|
+
expect(lpt.hasPathTo(1)).toBe(true)
|
|
83
|
+
expect(lpt.hasPathTo(2)).toBe(true)
|
|
84
|
+
expect(lpt.hasPathTo(3)).toBe(true)
|
|
85
|
+
expect(lpt.hasPathTo(4)).toBe(true)
|
|
86
|
+
expect(lpt.hasPathTo(5)).toBe(false)
|
|
87
|
+
expect(lpt.hasPathTo(6)).toBe(true)
|
|
88
|
+
expect(lpt.hasPathTo(7)).toBe(true)
|
|
89
|
+
})
|
|
90
|
+
it('should determine paths', function () {
|
|
91
|
+
expect(lpt.pathTo(0)).toEqual([5, 4, 0])
|
|
92
|
+
expect(lpt.pathTo(1)).toEqual([5, 1])
|
|
93
|
+
expect(lpt.pathTo(2)).toEqual([5, 7, 2])
|
|
94
|
+
expect(lpt.pathTo(3)).toEqual([5, 1, 3])
|
|
95
|
+
expect(lpt.pathTo(4)).toEqual([5, 4])
|
|
96
|
+
expect(lpt.pathTo(5)).toEqual([])
|
|
97
|
+
expect(lpt.pathTo(6)).toEqual([5, 1, 3, 6])
|
|
98
|
+
expect(lpt.pathTo(7)).toEqual([5, 7])
|
|
99
|
+
})
|
|
100
|
+
it('should calculate distances', function () {
|
|
101
|
+
expect(lpt.getDistTo(0)).toBe(0.73)
|
|
102
|
+
expect(lpt.getDistTo(1)).toBe(0.32)
|
|
103
|
+
expect(lpt.getDistTo(2)).toBe(0.62)
|
|
104
|
+
expect(lpt.getDistTo(3)).toBe(0.61)
|
|
105
|
+
expect(lpt.getDistTo(4)).toBe(0.35)
|
|
106
|
+
expect(lpt.getDistTo(5)).toBe(0)
|
|
107
|
+
expect(lpt.getDistTo(6)).toBe(1.13)
|
|
108
|
+
expect(lpt.getDistTo(7)).toBe(0.28)
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Check the issue #150
|
|
114
|
+
**/
|
|
115
|
+
describe('lightest weight path', function () {
|
|
116
|
+
it('should find the lightest weight path', function () {
|
|
117
|
+
const digraph = new EdgeWeightedDigraph()
|
|
118
|
+
|
|
119
|
+
digraph.add(1, 3, 0.29)
|
|
120
|
+
digraph.add(1, 6, 0)
|
|
121
|
+
digraph.add(3, 6, 0)
|
|
122
|
+
|
|
123
|
+
const spt = new SPT(digraph, 1)
|
|
124
|
+
const path = spt.pathTo(6)
|
|
125
|
+
expect(path).toEqual([1, 6])
|
|
126
|
+
})
|
|
127
|
+
it('should find the lightest weight path', function () {
|
|
128
|
+
const digraph = new EdgeWeightedDigraph()
|
|
129
|
+
|
|
130
|
+
digraph.add(1, 3, -1)
|
|
131
|
+
digraph.add(1, 6, 0)
|
|
132
|
+
digraph.add(3, 6, 0)
|
|
133
|
+
|
|
134
|
+
const spt = new SPT(digraph, 1)
|
|
135
|
+
const path = spt.pathTo(6)
|
|
136
|
+
const hasPath = spt.hasPathTo(6)
|
|
137
|
+
expect(hasPath).toBe(true)
|
|
138
|
+
expect(path).toEqual([1, 3, 6])
|
|
139
|
+
})
|
|
140
|
+
})
|
|
141
|
+
})
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2011, Chris Umbel
|
|
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
|
+
'use strict'
|
|
24
|
+
|
|
25
|
+
import { SoundEx } from 'lib/natural'
|
|
26
|
+
const soundex = new SoundEx()
|
|
27
|
+
|
|
28
|
+
describe('soundex', function () {
|
|
29
|
+
describe('transformLipps', function () {
|
|
30
|
+
it('should replace B, F, P, and V with 1', function () {
|
|
31
|
+
expect(soundex.transformLipps('bopper')).toBe('1o11er')
|
|
32
|
+
expect(soundex.transformLipps('valumf')).toBe('1alum1')
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
describe('transformThroats', function () {
|
|
37
|
+
it('should replace C, G, J, K, Q, S, X, Z with 2', function () {
|
|
38
|
+
expect(soundex.transformLipps('bopper')).toBe('1o11er')
|
|
39
|
+
expect(soundex.transformLipps('valumf')).toBe('1alum1')
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
describe('transformTongue', function () {
|
|
44
|
+
it('should replace D and T with 3', function () {
|
|
45
|
+
expect(soundex.transformToungue('dat')).toBe('3a3')
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
describe('transformL', function () {
|
|
50
|
+
it('should replace L with 4', function () {
|
|
51
|
+
expect(soundex.transformL('lala')).toBe('4a4a')
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
describe('transformHum', function () {
|
|
56
|
+
it('should replace M and N with 5', function () {
|
|
57
|
+
expect(soundex.transformHum('mummification')).toBe('5u55ificatio5')
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
describe('transformR', function () {
|
|
62
|
+
it('should replace R with 6', function () {
|
|
63
|
+
expect(soundex.transformR('render')).toBe('6ende6')
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
describe('condense', function () {
|
|
68
|
+
it('sound condense sequences', function () {
|
|
69
|
+
expect(soundex.condense('11222556')).toBe('1256')
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe('padRight0', function () {
|
|
74
|
+
it('sound padd zeros on the right to a lenght of 3', function () {
|
|
75
|
+
expect(soundex.padRight0('1')).toBe('100')
|
|
76
|
+
expect(soundex.padRight0('11')).toBe('110')
|
|
77
|
+
expect(soundex.padRight0('111')).toBe('111')
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('should not code the first character', function () {
|
|
82
|
+
expect(soundex.process('render').charAt(0)).toBe('R')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('should pad right with zeros', function () {
|
|
86
|
+
expect(soundex.process('super')).toBe('S160')
|
|
87
|
+
expect(soundex.process('butt')).toBe('B300')
|
|
88
|
+
expect(soundex.process('a')).toBe('A000')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('should pad right with zeros', function () {
|
|
92
|
+
expect(soundex.process('but')).toBe('B300')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('should perform soundex', function () {
|
|
96
|
+
expect(soundex.process('BLACKBERRY')).toBe('B421')
|
|
97
|
+
expect(soundex.process('blackberry')).toBe('B421')
|
|
98
|
+
expect(soundex.process('calculate')).toBe('C424')
|
|
99
|
+
expect(soundex.process('CALCULATE')).toBe('C424')
|
|
100
|
+
expect(soundex.process('fox')).toBe('F200')
|
|
101
|
+
expect(soundex.process('FOX')).toBe('F200')
|
|
102
|
+
expect(soundex.process('jump')).toBe('J510')
|
|
103
|
+
expect(soundex.process('JUMP')).toBe('J510')
|
|
104
|
+
expect(soundex.process('phonetics')).toBe('P532')
|
|
105
|
+
expect(soundex.process('PHONETICS')).toBe('P532')
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('should perform soundex via compare method', function () {
|
|
109
|
+
expect(soundex.compare('ant', 'and')).toBeTruthy()
|
|
110
|
+
expect(soundex.compare('ant', 'anne')).toBeFalsy()
|
|
111
|
+
expect(soundex.compare('band', 'bant')).toBeTruthy()
|
|
112
|
+
expect(soundex.compare('band', 'gand')).toBeFalsy()
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
/*
|
|
116
|
+
it('should perform soundex via attached soundsLike String patch', function() {
|
|
117
|
+
soundex.attach();
|
|
118
|
+
expect('ant'.soundsLike('ant')).toBeTruthy();
|
|
119
|
+
expect('ant'.soundsLike('anne')).toBeFalsy();
|
|
120
|
+
expect('band'.soundsLike('bant')).toBeTruthy();
|
|
121
|
+
expect('band'.soundsLike('gand')).toBeFalsy();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should return string phonetics with string patch', function() {
|
|
125
|
+
soundex.attach();
|
|
126
|
+
expect('phonetics'.phonetics()).toBe('P532');
|
|
127
|
+
expect('PHONETICS'.phonetics()).toBe('P532');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('issue 221 -- inital vowels that duplicate cons codes', function() {
|
|
131
|
+
soundex.attach();
|
|
132
|
+
expect('Lloyd'.phonetics()).toBe('L300');
|
|
133
|
+
expect('Pfister'.phonetics()).toBe('P236');
|
|
134
|
+
|
|
135
|
+
expect('manhattan'.phonetics()).toBe('M535');
|
|
136
|
+
expect('Lukasiewicz'.phonetics()).toBe('L222');
|
|
137
|
+
|
|
138
|
+
expect('Gauss'.phonetics()).toBe('G200');
|
|
139
|
+
expect('Tymczak'.phonetics()).toBe('T522');
|
|
140
|
+
expect('Jackson'.phonetics()).toBe('J250');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('should tokenize and return string phonetics with string patch', function() {
|
|
144
|
+
soundex.attach();
|
|
145
|
+
expect('phonetics'.tokenizeAndPhoneticize()).toEqual(['P532']);
|
|
146
|
+
expect('phonetics jump'.tokenizeAndPhoneticize()).toEqual(['P532', 'J510']);
|
|
147
|
+
expect('phonetics jump calculate'.tokenizeAndPhoneticize()).toEqual(['P532', 'J510', 'C424']);
|
|
148
|
+
});
|
|
149
|
+
*/
|
|
150
|
+
|
|
151
|
+
it('should max out at four characters long by default', function () {
|
|
152
|
+
expect(soundex.process('supercalifragilisticexpialidocious').length).toBe(4)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('should truncate to specified length if maxLength passed', function () {
|
|
156
|
+
expect(soundex.process('supercalifragilisticexpialidocious', 8).length).toBe(8)
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('should handle a maxLength beyond code length', function () {
|
|
160
|
+
expect(soundex.process('JUMP', 8)).toBe('J510')
|
|
161
|
+
})
|
|
162
|
+
})
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2011
|
|
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
|
+
'use strict'
|
|
24
|
+
|
|
25
|
+
import { Spellcheck } from 'lib/natural'
|
|
26
|
+
|
|
27
|
+
describe('spellcheck', function () {
|
|
28
|
+
describe('distance 1 corrections', function () {
|
|
29
|
+
const spellcheck = new Spellcheck(['something'])
|
|
30
|
+
|
|
31
|
+
it('should fix deletions', function () {
|
|
32
|
+
expect(spellcheck.getCorrections('smething')).toContain('something')
|
|
33
|
+
})
|
|
34
|
+
it('should fix insertions', function () {
|
|
35
|
+
expect(spellcheck.getCorrections('somethhing')).toContain('something')
|
|
36
|
+
})
|
|
37
|
+
it('should fix transpositions', function () {
|
|
38
|
+
expect(spellcheck.getCorrections('somehting')).toContain('something')
|
|
39
|
+
})
|
|
40
|
+
it('should fix replacements', function () {
|
|
41
|
+
expect(spellcheck.getCorrections('sometzing')).toContain('something')
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
describe('distance 2 corrections', function () {
|
|
46
|
+
const spellcheck = new Spellcheck(['something'])
|
|
47
|
+
|
|
48
|
+
it('should fix a deletion then a transposition', function () {
|
|
49
|
+
expect(spellcheck.getCorrections('smtehing', 2)).toContain('something')
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('edits', function () {
|
|
54
|
+
const spellcheck = new Spellcheck([])
|
|
55
|
+
const edits = spellcheck.edits('ab')
|
|
56
|
+
|
|
57
|
+
it('should compute insertions', function () {
|
|
58
|
+
const insertions = ['xab', 'axb', 'abx']
|
|
59
|
+
insertions.forEach(insertion => {
|
|
60
|
+
expect(edits.indexOf(insertion)).toBeGreaterThan(-1)
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
it('should compute deletions', function () {
|
|
64
|
+
const deletions = ['a', 'b']
|
|
65
|
+
deletions.forEach(deletion => {
|
|
66
|
+
expect(edits.indexOf(deletion)).toBeGreaterThan(-1)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
it('should compute transpositions', function () {
|
|
70
|
+
const transpositions = ['ba']
|
|
71
|
+
transpositions.forEach(transposition => {
|
|
72
|
+
expect(edits.indexOf(transposition)).toBeGreaterThan(-1)
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
it('should compute replacements', function () {
|
|
76
|
+
const replacements = ['zb', 'af']
|
|
77
|
+
replacements.forEach(replacement => {
|
|
78
|
+
expect(edits.indexOf(replacement)).toBeGreaterThan(-1)
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
describe('edits up to distance', function () {
|
|
84
|
+
const spellcheck = new Spellcheck([])
|
|
85
|
+
|
|
86
|
+
it('should correctly compute edits at distance', function () {
|
|
87
|
+
const edits = spellcheck.editsWithMaxDistance('abc', 2)
|
|
88
|
+
expect(edits[1].indexOf('abzc')).toBeGreaterThan(-1) // 1 insertion
|
|
89
|
+
expect(edits[2].indexOf('a')).toBeGreaterThan(-1) // 2 deletions
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
describe('boolean spellcheck', function () {
|
|
94
|
+
const spellcheck = new Spellcheck(['cat'])
|
|
95
|
+
|
|
96
|
+
it('should consider cat a word', function () {
|
|
97
|
+
expect(spellcheck.isCorrect('cat')).toBe(true)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('should not consider dog a word', function () {
|
|
101
|
+
expect(spellcheck.isCorrect('dog')).toBe(false)
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
describe('special cases', function () {
|
|
106
|
+
const spellcheck = new Spellcheck(['cat'])
|
|
107
|
+
|
|
108
|
+
it('should return the input word as the most probable correction if it is already correct', function () {
|
|
109
|
+
expect(spellcheck.getCorrections('cat').indexOf('cat')).toBe(0)
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
})
|