natural 6.12.0 → 7.0.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/.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 +6 -2
- package/lib/natural/tokenizers/index.js +1 -0
- 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,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2020, Hugo W.L. ter Doest
|
|
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 { CarryStemmerFr as stemmer } from 'lib/natural'
|
|
26
|
+
|
|
27
|
+
// const snowBallDict = require('spec/test_data/snowball_fr.json')
|
|
28
|
+
|
|
29
|
+
const tests = [
|
|
30
|
+
{
|
|
31
|
+
input: 'action',
|
|
32
|
+
output: 'ac'
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
input: 'acteur',
|
|
36
|
+
output: 'ac'
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
input: 'actrices',
|
|
40
|
+
output: 'ac'
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
input: 'Dleyton',
|
|
44
|
+
output: 'Dleyton'
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
input: 'volera',
|
|
48
|
+
output: 'vol'
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
const testSentences = [
|
|
53
|
+
{
|
|
54
|
+
input: 'Le petit cheval de manège',
|
|
55
|
+
output: ['pet', 'cheval', 'manèg']
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
describe('Carry stemmer', function () {
|
|
60
|
+
tests.forEach(test => {
|
|
61
|
+
it('stems ' + test.input + ' correctly', function () {
|
|
62
|
+
expect(stemmer.stem(test.input)).toBe(test.output)
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
testSentences.forEach(test => {
|
|
67
|
+
it('tokenizes and stems ' + test.input + ' correctly', function () {
|
|
68
|
+
expect(stemmer.tokenizeAndStem(test.input)).toEqual(test.output)
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
/*
|
|
73
|
+
it('should perform stemming on a lot of words', function() {
|
|
74
|
+
var ok = [];
|
|
75
|
+
var ko = [];
|
|
76
|
+
|
|
77
|
+
Object.keys(snowBallDict).forEach(word => {
|
|
78
|
+
|
|
79
|
+
var stemmed = stemmer.stem(word);
|
|
80
|
+
var expectedStem = snowBallDict[word];
|
|
81
|
+
|
|
82
|
+
if (stemmed === expectedStem) {
|
|
83
|
+
ok.push(word);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
ko.push({
|
|
87
|
+
word: word,
|
|
88
|
+
expected: expectedStem,
|
|
89
|
+
actual: stemmed
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
expect(ko.length).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
*/
|
|
97
|
+
})
|
|
@@ -0,0 +1,104 @@
|
|
|
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 { BayesClassifier } from 'lib/natural'
|
|
26
|
+
|
|
27
|
+
describe('classifier', function () {
|
|
28
|
+
describe('addDocument', function () {
|
|
29
|
+
it('should ignore an empty document', function () {
|
|
30
|
+
const classifier = new BayesClassifier()
|
|
31
|
+
classifier.addDocument('', 'philosophy')
|
|
32
|
+
expect(classifier.docs.length).toBe(0)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('should increment features', function () {
|
|
36
|
+
const classifier = new BayesClassifier()
|
|
37
|
+
classifier.addDocument('foo', '')
|
|
38
|
+
classifier.addDocument('foo', '')
|
|
39
|
+
classifier.addDocument('bar', '')
|
|
40
|
+
classifier.addDocument('bar', '')
|
|
41
|
+
classifier.addDocument('bar', '')
|
|
42
|
+
classifier.addDocument('baz', '')
|
|
43
|
+
expect(classifier.docs.length).toBe(6)
|
|
44
|
+
expect(classifier.features.foo).toBe(2)
|
|
45
|
+
expect(classifier.features.bar).toBe(3)
|
|
46
|
+
expect(classifier.features.baz).toBe(1)
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
describe('Classifier', function () {
|
|
51
|
+
const pushedEvents: any[] = []
|
|
52
|
+
function eventRegister (obj: any): void {
|
|
53
|
+
pushedEvents.push(obj)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function assertEventResults (): void {
|
|
57
|
+
expect(pushedEvents[0].index).toBe(0)
|
|
58
|
+
expect(pushedEvents[0].total).toBe(6)
|
|
59
|
+
expect(pushedEvents.length).toBe(6)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
it('should emit events when documents are added or training is finished', function () {
|
|
63
|
+
const classifier = new BayesClassifier()
|
|
64
|
+
classifier.on('trainedWithDocument', eventRegister)
|
|
65
|
+
classifier.on('doneTraining', assertEventResults)
|
|
66
|
+
|
|
67
|
+
classifier.addDocument('i fixed the box', 'computing')
|
|
68
|
+
classifier.addDocument('i write code', 'computing')
|
|
69
|
+
classifier.addDocument('nasty script code', 'computing')
|
|
70
|
+
classifier.addDocument('write a book', 'literature')
|
|
71
|
+
classifier.addDocument('read a book', 'literature')
|
|
72
|
+
classifier.addDocument('study the books', 'literature')
|
|
73
|
+
|
|
74
|
+
classifier.train()
|
|
75
|
+
classifier.removeListener('trainedWithDocument', eventRegister)
|
|
76
|
+
classifier.removeListener('doneTraining', assertEventResults)
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
describe('Classifier', function () {
|
|
81
|
+
let classifier: BayesClassifier
|
|
82
|
+
beforeEach(function () {
|
|
83
|
+
classifier = new BayesClassifier()
|
|
84
|
+
classifier.addDocument('i fixed the box', 'computing')
|
|
85
|
+
classifier.addDocument('i write code', 'computing')
|
|
86
|
+
classifier.addDocument('write a book', 'literature')
|
|
87
|
+
classifier.addDocument('study the books', 'literature')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('should do nothing if text is not a string', function () {
|
|
91
|
+
expect(classifier.docs.length).toBe(4)
|
|
92
|
+
classifier.removeDocument(['write a book'], 'literature')
|
|
93
|
+
classifier.removeDocument(['study the books'], 'literature')
|
|
94
|
+
expect(classifier.docs.length).toBe(4)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('should do nothing if text is not a match', function () {
|
|
98
|
+
expect(classifier.docs.length).toBe(4)
|
|
99
|
+
classifier.removeDocument('something else', 'literature')
|
|
100
|
+
classifier.removeDocument('another thing', 'literature')
|
|
101
|
+
expect(classifier.docs.length).toBe(4)
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2012, Guillaume Marty
|
|
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 { CountInflectorFr } from 'lib/natural'
|
|
26
|
+
const inflector = new CountInflectorFr()
|
|
27
|
+
|
|
28
|
+
describe('count_inflector', function () {
|
|
29
|
+
it('should handle 1er cases', function () {
|
|
30
|
+
expect(inflector.nth(1)).toBe('1er')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should handle the 2e cases', function () {
|
|
34
|
+
expect(inflector.nth(0)).toBe('0e')
|
|
35
|
+
expect(inflector.nth(2)).toBe('2e')
|
|
36
|
+
expect(inflector.nth(3)).toBe('3e')
|
|
37
|
+
expect(inflector.nth(5)).toBe('5e')
|
|
38
|
+
expect(inflector.nth(11)).toBe('11e')
|
|
39
|
+
expect(inflector.nth(100)).toBe('100e')
|
|
40
|
+
expect(inflector.nth(999)).toBe('999e')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('should handle roman numerals', function () {
|
|
44
|
+
expect(inflector.nth('I')).toBe('Ier')
|
|
45
|
+
expect(inflector.nth('XX')).toBe('XXe')
|
|
46
|
+
})
|
|
47
|
+
})
|
|
@@ -0,0 +1,76 @@
|
|
|
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 { CountInflector } from 'lib/natural'
|
|
26
|
+
const inflector = new CountInflector()
|
|
27
|
+
|
|
28
|
+
describe('count_inflector', function () {
|
|
29
|
+
it('should handle 1st cases', function () {
|
|
30
|
+
expect(inflector.nth(1)).toBe('1st')
|
|
31
|
+
expect(inflector.nth(101)).toBe('101st')
|
|
32
|
+
expect(inflector.nth(11)).not.toBe('11st')
|
|
33
|
+
expect(inflector.nth(111)).not.toBe('111st')
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('should handle the 12th cases', function () {
|
|
37
|
+
expect(inflector.nth(12)).toBe('12th')
|
|
38
|
+
expect(inflector.nth(112)).toBe('112th')
|
|
39
|
+
expect(inflector.nth(1112)).toBe('1112th')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('should handle the 11th cases', function () {
|
|
43
|
+
expect(inflector.nth(11)).toBe('11th')
|
|
44
|
+
expect(inflector.nth(111)).toBe('111th')
|
|
45
|
+
expect(inflector.nth(1111)).toBe('1111th')
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should handle the 13th cases', function () {
|
|
49
|
+
expect(inflector.nth(13)).toBe('13th')
|
|
50
|
+
expect(inflector.nth(113)).toBe('113th')
|
|
51
|
+
expect(inflector.nth(1113)).toBe('1113th')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('should handle the th cases', function () {
|
|
55
|
+
expect(inflector.nth(10)).toBe('10th')
|
|
56
|
+
expect(inflector.nth(4)).toBe('4th')
|
|
57
|
+
expect(inflector.nth(400)).toBe('400th')
|
|
58
|
+
expect(inflector.nth(404)).toBe('404th')
|
|
59
|
+
expect(inflector.nth(5)).toBe('5th')
|
|
60
|
+
expect(inflector.nth(5000)).toBe('5000th')
|
|
61
|
+
expect(inflector.nth(5005)).toBe('5005th')
|
|
62
|
+
expect(inflector.nth(9)).toBe('9th')
|
|
63
|
+
expect(inflector.nth(90009)).toBe('90009th')
|
|
64
|
+
expect(inflector.nth(90000)).toBe('90000th')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('should handle 2nd cases', function () {
|
|
68
|
+
expect(inflector.nth(2)).toBe('2nd')
|
|
69
|
+
expect(inflector.nth(12)).not.toBe('12nd')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('should handle 3rd cases', function () {
|
|
73
|
+
expect(inflector.nth(3)).toBe('3rd')
|
|
74
|
+
expect(inflector.nth(13)).not.toBe('13rd')
|
|
75
|
+
})
|
|
76
|
+
})
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { DamerauLevenshteinDistance, DamerauLevenshteinDistanceSearch } from 'lib/natural'
|
|
4
|
+
|
|
5
|
+
describe('DamerauLevenshtein', function () {
|
|
6
|
+
describe('default', function () {
|
|
7
|
+
it('should be 0 when given equal strings', function () {
|
|
8
|
+
expect(DamerauLevenshteinDistance('test', 'test')).toBe(0)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should calculate 1 for adjacent transposition', function () {
|
|
12
|
+
expect(DamerauLevenshteinDistance('za', 'az')).toBe(1)
|
|
13
|
+
expect(DamerauLevenshteinDistance('Tomato', 'oTmato')).toBe(1)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('should handle custom transposition_cost', function () {
|
|
17
|
+
expect(DamerauLevenshteinDistance('za', 'az', { transposition_cost: 0 })).toBe(0)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should calculate 2 when there are 2 transpositions', function () {
|
|
21
|
+
expect(DamerauLevenshteinDistance('tomato', 'otmaot')).toBe(2)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should calculate 2 for 1 transposition and 1 insertion', function () {
|
|
25
|
+
expect(DamerauLevenshteinDistance('CA', 'ABC')).toBe(2)
|
|
26
|
+
expect(DamerauLevenshteinDistance('a cat', 'a abct')).toBe(2)
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
describe('options.restricted = true', function () {
|
|
31
|
+
const restricted = { restricted: true }
|
|
32
|
+
it('should calculate 0 for equal strings', function () {
|
|
33
|
+
expect(DamerauLevenshteinDistance('identity', 'identity', restricted)).toBe(0)
|
|
34
|
+
})
|
|
35
|
+
it('should calculate 1 for an adjacent transposition', function () {
|
|
36
|
+
expect(DamerauLevenshteinDistance('za', 'az', restricted)).toBe(1)
|
|
37
|
+
})
|
|
38
|
+
it('should not count transposition more than 1 char away', function () {
|
|
39
|
+
expect(DamerauLevenshteinDistance('CA', 'ABC', restricted)).toBe(3)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('should combine search with Damerau', function () {
|
|
44
|
+
const source = 'The RainCoat BookStore'
|
|
45
|
+
const target = 'All the best books are here at the Rain Coats Book Store'
|
|
46
|
+
const result = DamerauLevenshteinDistanceSearch(source, target)
|
|
47
|
+
expect(result).toEqual({
|
|
48
|
+
substring: 'the Rain Coats Book Store',
|
|
49
|
+
distance: 4,
|
|
50
|
+
offset: 31
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { DiceCoefficient as dice } from 'lib/natural'
|
|
4
|
+
import text1 from '../spec/test_data/Wikipedia_EN_FrenchRevolution.json'
|
|
5
|
+
import text2 from '../spec/test_data/Wikipedia_EN_InfluenceOfTheFrenchRevolution.json'
|
|
6
|
+
|
|
7
|
+
describe('dice', function () {
|
|
8
|
+
it('should handle exact matches', function () {
|
|
9
|
+
expect(dice('john', 'john')).toBe(1)
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('should match single character words', function () {
|
|
13
|
+
expect(dice('a', 'a')).toBe(1)
|
|
14
|
+
expect(dice('a', 'b')).toBe(0)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should handle total mis-matches', function () {
|
|
18
|
+
expect(dice('john', 'matt')).toBe(0)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
// Example from http://en.wikipedia.org/wiki/Dice's_coefficient
|
|
22
|
+
it('should handle a typical case', function () {
|
|
23
|
+
expect(dice('night', 'nacht')).toBe(0.25)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('should sanitize case', function () {
|
|
27
|
+
expect(dice('night', 'NIGHT')).toBe(1)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('should sanitize spacing', function () {
|
|
31
|
+
expect(dice('the space', 'the space')).toBe(1)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('should compare complete texts', function () {
|
|
35
|
+
expect(dice(text1.text, text2.text)).toBe(0.7939374395356337)
|
|
36
|
+
})
|
|
37
|
+
})
|