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,48 @@
|
|
|
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 softwé and associated documentation files (the "Softwé"), to deal
|
|
6
|
+
in the Softwé without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Softwé, and to permit persons to whom the Softwé 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 Softwé.
|
|
13
|
+
|
|
14
|
+
THE SOFTWé 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 SOFTWé OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
THE SOFTWé.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
'use strict'
|
|
24
|
+
|
|
25
|
+
import { AggressiveTokenizerPt } from 'lib/natural'
|
|
26
|
+
const tokenizer = new AggressiveTokenizerPt()
|
|
27
|
+
|
|
28
|
+
describe('aggressive_tokenizer_pt', function () {
|
|
29
|
+
it('should tokenize strings', function () {
|
|
30
|
+
expect(tokenizer.tokenize('isso é coração')).toEqual(['isso', 'é', 'coração'])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should swallow punctuation', function () {
|
|
34
|
+
expect(tokenizer.tokenize('isso é coração, no')).toEqual(['isso', 'é', 'coração', 'no'])
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should swallow final punctuation', function () {
|
|
38
|
+
expect(tokenizer.tokenize('isso é coração, no?')).toEqual(['isso', 'é', 'coração', 'no'])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should swallow initial punctuation', function () {
|
|
42
|
+
expect(tokenizer.tokenize('.isso é coração, no')).toEqual(['isso', 'é', 'coração', 'no'])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should swallow duplicate punctuation', function () {
|
|
46
|
+
expect(tokenizer.tokenize('eu vou... pause')).toEqual(['eu', 'vou', 'pause'])
|
|
47
|
+
})
|
|
48
|
+
})
|
|
@@ -0,0 +1,57 @@
|
|
|
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 { AggressiveTokenizer } from 'lib/natural'
|
|
26
|
+
const tokenizer = new AggressiveTokenizer()
|
|
27
|
+
|
|
28
|
+
describe('aggressive_tokenizer', function () {
|
|
29
|
+
it('should tokenize strings', function () {
|
|
30
|
+
expect(tokenizer.tokenize('these are things')).toEqual(['these', 'are', 'things'])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should swallow punctuation', function () {
|
|
34
|
+
expect(tokenizer.tokenize('these are things, no')).toEqual(['these', 'are', 'things', 'no'])
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should swallow final punctuation', function () {
|
|
38
|
+
expect(tokenizer.tokenize('these are things, no?')).toEqual(['these', 'are', 'things', 'no'])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should swallow initial punctuation', function () {
|
|
42
|
+
expect(tokenizer.tokenize('.these are things, no')).toEqual(['these', 'are', 'things', 'no'])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should swallow duplicate punctuation', function () {
|
|
46
|
+
expect(tokenizer.tokenize('i shal... pause')).toEqual(['i', 'shal', 'pause'])
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('should remove underscores', function () {
|
|
50
|
+
expect(tokenizer.tokenize('_ hi_this_is_a_test_case_ for__removing___underscores_')).toEqual(['hi', 'this', 'is', 'a', 'test', 'case', 'for', 'removing', 'underscores'])
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('should handle the use of hyphen inside words (issue #656)', function () {
|
|
54
|
+
expect(tokenizer.tokenize('links text-based opposed image-based links/CTA\'s')).toEqual(
|
|
55
|
+
['links', 'text-based', 'opposed', 'image-based', 'links/CTA\'s'])
|
|
56
|
+
})
|
|
57
|
+
})
|
|
@@ -0,0 +1,52 @@
|
|
|
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 { AggressiveTokenizerSv } from 'lib/natural'
|
|
26
|
+
const tokenizer = new AggressiveTokenizerSv()
|
|
27
|
+
|
|
28
|
+
describe('aggressive_tokenizer_sv', function () {
|
|
29
|
+
it('should tokenize strings', function () {
|
|
30
|
+
expect(tokenizer.tokenize('Ett tu tre')).toEqual(['Ett', 'tu', 'tre'])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should swallow punctuation', function () {
|
|
34
|
+
expect(tokenizer.tokenize('Ett, tu, tre')).toEqual(['Ett', 'tu', 'tre'])
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should swallow final punctuation', function () {
|
|
38
|
+
expect(tokenizer.tokenize('Ett, tu, tre?')).toEqual(['Ett', 'tu', 'tre'])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should swallow initial punctuation', function () {
|
|
42
|
+
expect(tokenizer.tokenize('.Ett, tu, tre')).toEqual(['Ett', 'tu', 'tre'])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should swallow duplicate punctuation', function () {
|
|
46
|
+
expect(tokenizer.tokenize('Ett, tu... tre')).toEqual(['Ett', 'tu', 'tre'])
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('should not split on hyphen or Swedish letters', function () {
|
|
50
|
+
expect(tokenizer.tokenize('It-bolaget ägs av en 30-åring')).toEqual(['It-bolaget', 'ägs', 'av', 'en', '30-åring'])
|
|
51
|
+
})
|
|
52
|
+
})
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2018, Hugo W.L. ter Doest
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this softwé and associated documentation files (the "Softwé"), to deal
|
|
6
|
+
in the Softwé without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Softwé, and to permit persons to whom the Softwé 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 Softwé.
|
|
13
|
+
|
|
14
|
+
THE SOFTWé 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 SOFTWé OR THE USE OR OTHER DEALINGS IN
|
|
20
|
+
THE SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
'use strict'
|
|
24
|
+
|
|
25
|
+
import { AggressiveTokenizerVi } from 'lib/natural'
|
|
26
|
+
const tokenizer = new AggressiveTokenizerVi()
|
|
27
|
+
|
|
28
|
+
describe('aggressive_tokenizer_vi', function () {
|
|
29
|
+
it('should tokenize strings', function () {
|
|
30
|
+
expect(tokenizer.tokenize('Tôi bị lạc')).toEqual(['Tôi', 'bị', 'lạc'])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should swallow final punctuation', function () {
|
|
34
|
+
expect(tokenizer.tokenize('Làm ơn đợi một lát!')).toEqual(['Làm', 'ơn', 'đợi', 'một', 'lát'])
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should swallow final punctuation', function () {
|
|
38
|
+
expect(tokenizer.tokenize('Tôi đang tìm John.')).toEqual(['Tôi', 'đang', 'tìm', 'John'])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should swallow initial punctuation', function () {
|
|
42
|
+
expect(tokenizer.tokenize('.Đi thẳng, sau đó rẽ trái / phải')).toEqual(['Đi', 'thẳng', 'sau', 'đó', 'rẽ', 'trái', 'phải'])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should swallow duplicate punctuation', function () {
|
|
46
|
+
expect(tokenizer.tokenize('.Xin giữ máy!')).toEqual(['Xin', 'giữ', 'máy'])
|
|
47
|
+
})
|
|
48
|
+
})
|
|
@@ -0,0 +1,165 @@
|
|
|
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, PorterStemmer } from 'lib/natural'
|
|
26
|
+
|
|
27
|
+
function setupClassifier (): BayesClassifier {
|
|
28
|
+
const classifier = new BayesClassifier()
|
|
29
|
+
classifier.addDocument(['fix', 'box'], 'computing')
|
|
30
|
+
classifier.addDocument(['write', 'code'], 'computing')
|
|
31
|
+
classifier.addDocument(['script', 'code'], 'computing')
|
|
32
|
+
classifier.addDocument(['write', 'book'], 'literature')
|
|
33
|
+
classifier.addDocument(['read', 'book'], 'literature')
|
|
34
|
+
classifier.addDocument(['study', 'book'], 'literature')
|
|
35
|
+
return classifier
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('bayes classifier', function () {
|
|
39
|
+
describe('classifier', function () {
|
|
40
|
+
it('should classify with arrays', function () {
|
|
41
|
+
const classifier = setupClassifier()
|
|
42
|
+
classifier.train()
|
|
43
|
+
expect(classifier.classify(['bug', 'code'])).toBe('computing')
|
|
44
|
+
expect(classifier.classify(['read', 'thing'])).toBe('literature')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('should classify with parallel training', function () {
|
|
48
|
+
const classifier = setupClassifier()
|
|
49
|
+
// Check for parallel method
|
|
50
|
+
if (classifier.Threads !== null) {
|
|
51
|
+
classifier.trainParallel(2, function (err) {
|
|
52
|
+
if (err !== null) {
|
|
53
|
+
console.log(err)
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
expect(classifier.classify(['bug', 'code'])).toBe('computing')
|
|
57
|
+
expect(classifier.classify(['read', 'thing'])).toBe('literature')
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('should classify with parallel batched training', function () {
|
|
63
|
+
const classifier = setupClassifier()
|
|
64
|
+
// Check for parallel method
|
|
65
|
+
if (classifier.Threads !== null) {
|
|
66
|
+
classifier.on('doneTraining', function () {
|
|
67
|
+
expect(classifier.classify(['bug', 'code'])).toBe('computing')
|
|
68
|
+
expect(classifier.classify(['read', 'thing'])).toBe('literature')
|
|
69
|
+
// asyncSpecDone();
|
|
70
|
+
})
|
|
71
|
+
classifier.trainParallelBatches({ numThreads: 2, batchSize: 2 })
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('should provide all classification scores', function () {
|
|
76
|
+
const classifier = setupClassifier()
|
|
77
|
+
classifier.train()
|
|
78
|
+
|
|
79
|
+
expect(classifier.getClassifications('i write code')[0].label).toBe('computing')
|
|
80
|
+
expect(classifier.getClassifications('i write code')[1].label).toBe('literature')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
function setupClassifierWithSentences (): BayesClassifier {
|
|
84
|
+
const classifier = new BayesClassifier()
|
|
85
|
+
classifier.addDocument('i fixed the box', 'computing')
|
|
86
|
+
classifier.addDocument('i write code', 'computing')
|
|
87
|
+
classifier.addDocument('nasty script code', 'computing')
|
|
88
|
+
classifier.addDocument('write a book', 'literature')
|
|
89
|
+
classifier.addDocument('read a book', 'literature')
|
|
90
|
+
classifier.addDocument('study the books', 'literature')
|
|
91
|
+
return classifier
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
it('should classify with strings', function () {
|
|
95
|
+
const classifier = setupClassifierWithSentences()
|
|
96
|
+
classifier.train()
|
|
97
|
+
expect(classifier.classify('a bug in the code')).toBe('computing')
|
|
98
|
+
expect(classifier.classify('read all the books')).toBe('literature')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should classify and re-classify after document-removal', function () {
|
|
102
|
+
const classifier = new BayesClassifier()
|
|
103
|
+
const classifications = new Map<string, number>()
|
|
104
|
+
|
|
105
|
+
// Add some good/bad docs and train
|
|
106
|
+
classifier.addDocument('foo bar baz', 'good')
|
|
107
|
+
classifier.addDocument('qux zooby', 'bad')
|
|
108
|
+
classifier.addDocument('asdf qwer', 'bad')
|
|
109
|
+
classifier.train()
|
|
110
|
+
|
|
111
|
+
expect(classifier.classify('foo')).toBe('good')
|
|
112
|
+
expect(classifier.classify('qux')).toBe('bad')
|
|
113
|
+
|
|
114
|
+
// Remove one of the bad docs, retrain
|
|
115
|
+
classifier.removeDocument('qux zooby', 'bad')
|
|
116
|
+
classifier.retrain()
|
|
117
|
+
|
|
118
|
+
// Simple `classify` will still return a single result, even if
|
|
119
|
+
// ratio for each side is equal -- have to compare actual values in
|
|
120
|
+
// the classifications, should be equal since qux is unclassified
|
|
121
|
+
const arr = classifier.getClassifications('qux')
|
|
122
|
+
let item: { label: string, value: number }
|
|
123
|
+
for (let i = 0, ii = arr.length; i < ii; i++) {
|
|
124
|
+
item = arr[i]
|
|
125
|
+
classifications.set(item.label, item.value)
|
|
126
|
+
}
|
|
127
|
+
expect(classifications.get('good')).toEqual(classifications.get('bad'))
|
|
128
|
+
|
|
129
|
+
// Re-classify as good, retrain
|
|
130
|
+
classifier.addDocument('qux zooby', 'good')
|
|
131
|
+
classifier.retrain()
|
|
132
|
+
|
|
133
|
+
// Should now be good, original docs should be unaffected
|
|
134
|
+
expect(classifier.classify('foo')).toBe('good')
|
|
135
|
+
expect(classifier.classify('qux')).toBe('good')
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('should serialize and deserialize a working classifier', function () {
|
|
139
|
+
const classifier = setupClassifierWithSentences()
|
|
140
|
+
const objAsString = JSON.stringify(classifier)
|
|
141
|
+
const obj: Record<string, unknown> = JSON.parse(objAsString)
|
|
142
|
+
const newClassifier = BayesClassifier.restore(obj)
|
|
143
|
+
|
|
144
|
+
newClassifier.addDocument('kick a ball', 'sports')
|
|
145
|
+
newClassifier.addDocument('hit some balls', 'sports')
|
|
146
|
+
newClassifier.addDocument('kick and punch', 'sports')
|
|
147
|
+
|
|
148
|
+
newClassifier.train()
|
|
149
|
+
|
|
150
|
+
expect(newClassifier.classify('a bug in the code')).toBe('computing')
|
|
151
|
+
expect(newClassifier.classify('read all the books')).toBe('literature')
|
|
152
|
+
expect(newClassifier.classify('kick butt')).toBe('sports')
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
it('should accept an optional smoothing parameter for the Bayesian estimates', function () {
|
|
156
|
+
const defaultClassifier = new BayesClassifier()
|
|
157
|
+
const newClassifier1 = new BayesClassifier(PorterStemmer)
|
|
158
|
+
const newClassifier2 = new BayesClassifier(PorterStemmer, 0.1)
|
|
159
|
+
|
|
160
|
+
expect(defaultClassifier.classifier.smoothing).toBe(1.0)
|
|
161
|
+
expect(newClassifier1.classifier.smoothing).toBe(1.0)
|
|
162
|
+
expect(newClassifier2.classifier.smoothing).toBe(0.1)
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
})
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Unit test for Brill's POS Tagger: test against the pos module
|
|
3
|
+
Copyright (C) 2019 Hugo W.L. ter Doest
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
This program is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict'
|
|
20
|
+
|
|
21
|
+
import { WordTokenizer, Lexicon, RuleSet, BrillPOSTagger } from 'lib/natural'
|
|
22
|
+
import type { Sentence, TagResults } from 'lib/natural'
|
|
23
|
+
|
|
24
|
+
import * as englishSentences from '../spec/test_data/NYT-20150205-picassos-granddaughter-plans-to-sell-art-worrying-the-market.json'
|
|
25
|
+
|
|
26
|
+
import jsonData from '../spec/test_data/NYT-20150205-picassos-granddaughter-plans_expected_tag_results.json'
|
|
27
|
+
|
|
28
|
+
import * as dutchSentences from '../spec/test_data/Volkskrant-20150205-Knot-geldpers-aanzetten-is-paardenmiddel-voor-half-procent-inflatie.json'
|
|
29
|
+
|
|
30
|
+
const englishTagResults: TagResults = jsonData as TagResults
|
|
31
|
+
const DEBUG = false
|
|
32
|
+
|
|
33
|
+
// Compares two tagged sentences. First one is in the old POSJS format, i.e.
|
|
34
|
+
// an array of two position arrays. The second one is a Sentence object
|
|
35
|
+
// that holds an array of objects for each position {token: "string", tag: "string"}
|
|
36
|
+
function compareTaggedSentences (sentenceInOldFormat: string[][], sentenceInNewFormat: Sentence): boolean {
|
|
37
|
+
let equal = true
|
|
38
|
+
sentenceInOldFormat.forEach(function (wordPlusTag: string[], index: number) {
|
|
39
|
+
equal = equal &&
|
|
40
|
+
(wordPlusTag[1] === sentenceInNewFormat.taggedWords[index].tag)
|
|
41
|
+
DEBUG && console.log(wordPlusTag[1] + ' ' + sentenceInNewFormat.taggedWords[index].tag)
|
|
42
|
+
})
|
|
43
|
+
return equal
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe('Brill\'s POS Tagger', function () {
|
|
47
|
+
let brillPOSTagger: BrillPOSTagger
|
|
48
|
+
let lexicon: Lexicon
|
|
49
|
+
let ruleSet: RuleSet
|
|
50
|
+
const tokenizer = new WordTokenizer()
|
|
51
|
+
|
|
52
|
+
it('should process an English newspaper article just like the dariusk/pos-js module', function () {
|
|
53
|
+
lexicon = new Lexicon('EN', 'NN')
|
|
54
|
+
expect(lexicon.nrEntries()).toBeGreaterThan(0)
|
|
55
|
+
ruleSet = new RuleSet('EN')
|
|
56
|
+
expect(ruleSet.nrRules()).toBeGreaterThan(0)
|
|
57
|
+
brillPOSTagger = new BrillPOSTagger(lexicon, ruleSet)
|
|
58
|
+
|
|
59
|
+
englishSentences.sentences.forEach(function (sentence: string, index: number) {
|
|
60
|
+
const tokenizedSentence = tokenizer.tokenize(sentence)
|
|
61
|
+
const taggedSentence = brillPOSTagger.tag(tokenizedSentence)
|
|
62
|
+
expect(compareTaggedSentences(englishTagResults.results[index], taggedSentence)).toBe(true)
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('should tag a Dutch news paper article', function () {
|
|
67
|
+
lexicon = new Lexicon('DU', 'N')
|
|
68
|
+
expect(lexicon.nrEntries()).toBeGreaterThan(0)
|
|
69
|
+
const ruleSet = new RuleSet('DU')
|
|
70
|
+
expect(ruleSet.nrRules()).toBeGreaterThan(0)
|
|
71
|
+
brillPOSTagger = new BrillPOSTagger(lexicon, ruleSet)
|
|
72
|
+
|
|
73
|
+
dutchSentences.sentences.forEach(function (sentence, index) {
|
|
74
|
+
const tokenizedSentence = tokenizer.tokenize(sentence)
|
|
75
|
+
const taggedSentence = brillPOSTagger.tag(tokenizedSentence)
|
|
76
|
+
expect(tokenizedSentence.length).toEqual(taggedSentence.taggedWords.length)
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
})
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Unit test for Brill's POS Trainer
|
|
3
|
+
Copyright (C) 2019 Hugo W.L. ter Doest
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
This program is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict'
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
Corpus,
|
|
23
|
+
ruleTemplates,
|
|
24
|
+
RuleTemplate,
|
|
25
|
+
Sentence,
|
|
26
|
+
BrillPOSTrainer,
|
|
27
|
+
BrillPOSTagger,
|
|
28
|
+
BrillPOSTester
|
|
29
|
+
} from 'lib/natural'
|
|
30
|
+
|
|
31
|
+
import type {
|
|
32
|
+
Lexicon,
|
|
33
|
+
RuleSet
|
|
34
|
+
} from 'lib/natural'
|
|
35
|
+
|
|
36
|
+
// Import a JSON file with the brown corpus
|
|
37
|
+
import brownCorpus from '../spec/test_data/browntag_nolines_excerpt.json'
|
|
38
|
+
|
|
39
|
+
const DEBUG = false
|
|
40
|
+
const JSON_FLAG = 2
|
|
41
|
+
|
|
42
|
+
function selectRuleTemplates (templateNames: string[]): RuleTemplate[] {
|
|
43
|
+
const templates: RuleTemplate[] = []
|
|
44
|
+
templateNames.forEach(function (name: string) {
|
|
45
|
+
if (ruleTemplates[name] !== undefined) {
|
|
46
|
+
const template = new RuleTemplate(name, ruleTemplates[name])
|
|
47
|
+
templates.push(template)
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
return templates
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
describe('Brill\'s POS Trainer', function () {
|
|
54
|
+
let corpus = null
|
|
55
|
+
let corpora: Corpus[]
|
|
56
|
+
const percentageTrain = 60
|
|
57
|
+
let trainLexicon: Lexicon
|
|
58
|
+
// Templates consider only tags, no words
|
|
59
|
+
const templateNames = [
|
|
60
|
+
'NEXT-TAG',
|
|
61
|
+
'PREV-TAG',
|
|
62
|
+
'PREV-1-OR-2-OR-3-TAG',
|
|
63
|
+
'PREV-1-OR-2-TAG',
|
|
64
|
+
'NEXT1OR2TAG',
|
|
65
|
+
'NEXT1OR2OR3TAG',
|
|
66
|
+
'SURROUNDTAG',
|
|
67
|
+
'PREV2TAG',
|
|
68
|
+
'NEXT2TAG'
|
|
69
|
+
]
|
|
70
|
+
let templates: RuleTemplate[]
|
|
71
|
+
let trainer = null
|
|
72
|
+
let ruleSet: RuleSet
|
|
73
|
+
|
|
74
|
+
it('should split the corpus in a training and testing corpus', function () {
|
|
75
|
+
corpus = new Corpus(brownCorpus, JSON_FLAG, Sentence)
|
|
76
|
+
DEBUG && console.log('Corpus: ' + JSON.stringify(corpus, null, 2))
|
|
77
|
+
corpora = corpus.splitInTrainAndTest(percentageTrain)
|
|
78
|
+
expect(corpora[0].nrSentences() + corpora[1].nrSentences()).toEqual(corpus.nrSentences())
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('should build a lexicon from the training corpus', function () {
|
|
82
|
+
trainLexicon = corpora[0].buildLexicon()
|
|
83
|
+
// Set default category to noun (NN)
|
|
84
|
+
// and default category for capitalised words to proper noun (NP)
|
|
85
|
+
trainLexicon.setDefaultCategories('NN', 'NP')
|
|
86
|
+
expect(trainLexicon.nrEntries()).not.toEqual(0)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('should set up the rule templates', function () {
|
|
90
|
+
templates = selectRuleTemplates(templateNames)
|
|
91
|
+
expect(templates.length).toEqual(templateNames.length)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('should train on the training corpus to derive transformation rules', function () {
|
|
95
|
+
trainer = new BrillPOSTrainer(1)
|
|
96
|
+
ruleSet = trainer.train(corpora[0], templates, trainLexicon)
|
|
97
|
+
expect(ruleSet.nrRules()).toBeGreaterThan(0)
|
|
98
|
+
expect(trainer.printRulesWithScores()).not.toEqual('')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should test the derived transformation rules on the test corpus', function () {
|
|
102
|
+
const tagger = new BrillPOSTagger(trainLexicon, ruleSet)
|
|
103
|
+
const tester = new BrillPOSTester()
|
|
104
|
+
const scores = tester.test(corpora[1], tagger)
|
|
105
|
+
expect(scores[1]).toBeGreaterThan(0)
|
|
106
|
+
})
|
|
107
|
+
})
|
|
@@ -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
|
+
})
|