natural 6.11.1 → 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.
Files changed (116) hide show
  1. package/.env +9 -7
  2. package/.eslintrc.json +13 -0
  3. package/.github/workflows/node.js.yml +57 -19
  4. package/lib/natural/analyzers/SenType.ts +30 -0
  5. package/lib/natural/analyzers/index.d.ts +6 -10
  6. package/lib/natural/brill_pos_tagger/index.d.ts +23 -14
  7. package/lib/natural/brill_pos_tagger/index.js +1 -1
  8. package/lib/natural/classifiers/bayes_classifier.js +6 -1
  9. package/lib/natural/classifiers/classifier.js +19 -32
  10. package/lib/natural/classifiers/classifier_train_parallel.js +7 -5
  11. package/lib/natural/classifiers/index.d.ts +36 -15
  12. package/lib/natural/classifiers/logistic_regression_classifier.js +7 -2
  13. package/lib/natural/classifiers/maxent/Classifier.js +5 -3
  14. package/lib/natural/distance/index.d.ts +11 -14
  15. package/lib/natural/inflectors/count_inflector.js +18 -21
  16. package/lib/natural/inflectors/fr/count_inflector.js +18 -19
  17. package/lib/natural/inflectors/index.d.ts +12 -3
  18. package/lib/natural/inflectors/index.js +3 -0
  19. package/lib/natural/ngrams/index.d.ts +5 -5
  20. package/lib/natural/normalizers/index.d.ts +24 -1
  21. package/lib/natural/normalizers/index.js +4 -1
  22. package/lib/natural/normalizers/normalizer_ja.js +109 -72
  23. package/lib/natural/phonetics/dm_soundex.js +52 -51
  24. package/lib/natural/phonetics/double_metaphone.js +397 -399
  25. package/lib/natural/phonetics/index.d.ts +42 -12
  26. package/lib/natural/phonetics/metaphone.js +134 -157
  27. package/lib/natural/phonetics/phonetic.js +5 -29
  28. package/lib/natural/phonetics/soundex.js +45 -54
  29. package/lib/natural/sentiment/index.d.ts +6 -6
  30. package/lib/natural/spellcheck/index.d.ts +2 -0
  31. package/lib/natural/stemmers/index.d.ts +43 -1
  32. package/lib/natural/stemmers/index.js +1 -0
  33. package/lib/natural/stemmers/token.js +131 -132
  34. package/lib/natural/tfidf/index.d.ts +7 -7
  35. package/lib/natural/tfidf/tfidf.js +7 -1
  36. package/lib/natural/tokenizers/index.d.ts +2 -2
  37. package/lib/natural/trie/index.d.ts +1 -1
  38. package/lib/natural/util/index.d.ts +8 -0
  39. package/lib/natural/util/index.js +2 -0
  40. package/lib/natural/util/storage/File.js +22 -0
  41. package/lib/natural/util/storage/Memcached.js +22 -0
  42. package/lib/natural/util/storage/MongoDB.js +31 -3
  43. package/lib/natural/util/storage/Postgres.js +49 -32
  44. package/lib/natural/util/storage/Redis.js +22 -0
  45. package/lib/natural/util/storage/StorageBackend.js +23 -0
  46. package/lib/natural/util/storage/index.d.ts +39 -0
  47. package/lib/natural/util/storage/index.js +24 -0
  48. package/package.json +22 -9
  49. package/ts_spec/MaxEntClassifier_spec.ts +130 -0
  50. package/ts_spec/SentimentAnalyzer_spec.ts +232 -0
  51. package/ts_spec/WordPunctTokenizer_spec.ts +57 -0
  52. package/ts_spec/aggressive_tokenizer_de_spec.ts +36 -0
  53. package/ts_spec/aggressive_tokenizer_es_spec.ts +39 -0
  54. package/ts_spec/aggressive_tokenizer_fr_spec.ts +111 -0
  55. package/ts_spec/aggressive_tokenizer_hi_spec.ts +70 -0
  56. package/ts_spec/aggressive_tokenizer_nl_spec.ts +39 -0
  57. package/ts_spec/aggressive_tokenizer_pt_spec.ts +48 -0
  58. package/ts_spec/aggressive_tokenizer_spec.ts +57 -0
  59. package/ts_spec/aggressive_tokenizer_sv_spec.ts +52 -0
  60. package/ts_spec/aggressive_tokenizer_vi_spec.ts +48 -0
  61. package/ts_spec/bayes_classifier_spec.ts +165 -0
  62. package/ts_spec/brill_pos_tagger_spec.ts +79 -0
  63. package/ts_spec/brill_pos_trainer_spec.ts +107 -0
  64. package/ts_spec/carry_stemmer_fr_spec.ts +97 -0
  65. package/ts_spec/classifier_spec.ts +104 -0
  66. package/ts_spec/count_inflector_fr_spec.ts +47 -0
  67. package/ts_spec/count_inflector_spec.ts +76 -0
  68. package/ts_spec/damerau_levenshtein_spec.ts +53 -0
  69. package/ts_spec/dice_coefficient_spec.ts +37 -0
  70. package/ts_spec/double_metaphone_spec.ts +739 -0
  71. package/ts_spec/hamming_distance_spec.ts +66 -0
  72. package/ts_spec/jaro-winkler_spec.ts +79 -0
  73. package/ts_spec/lancaster_stemmer_spec.ts +63 -0
  74. package/ts_spec/levenshtein_spec.ts +107 -0
  75. package/ts_spec/logistic_regression_classifier_spec.ts +93 -0
  76. package/ts_spec/longest_path_tree_spec.ts +111 -0
  77. package/ts_spec/metaphone_spec.ts +258 -0
  78. package/ts_spec/ngram_spec.ts +145 -0
  79. package/ts_spec/ngram_zh_spec.ts +124 -0
  80. package/ts_spec/normalizer_ja_spec.ts +165 -0
  81. package/ts_spec/normalizer_no_spec.ts +112 -0
  82. package/ts_spec/normalizer_spec.ts +141 -0
  83. package/ts_spec/noun_inflector_fr_spec.ts +472 -0
  84. package/ts_spec/noun_inflector_ja_spec.ts +116 -0
  85. package/ts_spec/noun_inflector_spec.ts +369 -0
  86. package/ts_spec/orthography_tokenizer_spec.ts +47 -0
  87. package/ts_spec/porter_stemmer_de_spec.ts +53 -0
  88. package/ts_spec/porter_stemmer_es_spec.ts +63 -0
  89. package/ts_spec/porter_stemmer_fr_spec.ts +103 -0
  90. package/ts_spec/porter_stemmer_it_spec.ts +56 -0
  91. package/ts_spec/porter_stemmer_nl_spec.ts +73 -0
  92. package/ts_spec/porter_stemmer_no_spec.ts +148 -0
  93. package/ts_spec/porter_stemmer_pt_spec.ts +50 -0
  94. package/ts_spec/porter_stemmer_ru_spec.ts +67 -0
  95. package/ts_spec/porter_stemmer_spec.ts +202 -0
  96. package/ts_spec/porter_stemmer_sv_spec.ts +41 -0
  97. package/ts_spec/present_verb_inflector_spec.ts +127 -0
  98. package/ts_spec/remove_diacritics_spec.ts +30 -0
  99. package/ts_spec/sentence_analyzer_spec.ts +293 -0
  100. package/ts_spec/sentence_tokenizer_parser_spec.ts +200 -0
  101. package/ts_spec/sentence_tokenizer_spec.ts +189 -0
  102. package/ts_spec/shortest_path_tree_spec.ts +141 -0
  103. package/ts_spec/soundex_spec.ts +162 -0
  104. package/ts_spec/spellcheck_spec.ts +112 -0
  105. package/ts_spec/stemmer_id_spec.ts +374 -0
  106. package/ts_spec/stemmer_ja_spec.ts +69 -0
  107. package/ts_spec/stemmer_token_spec.ts +163 -0
  108. package/ts_spec/tfidf_spec.ts +275 -0
  109. package/ts_spec/tokenizer_case_spec.ts +242 -0
  110. package/ts_spec/tokenizer_ja_spec.ts +63 -0
  111. package/ts_spec/transliterator_ja_spec.ts +105 -0
  112. package/ts_spec/treebank_word_tokenizer_spec.ts +38 -0
  113. package/ts_spec/trie_spec.ts +197 -0
  114. package/tsconfig.json +5 -3
  115. package/example.txt +0 -1
  116. package/f2f5d4bc-daa0-4392-8981-9614cd0c802e.json +0 -1
@@ -0,0 +1,39 @@
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 { AggressiveTokenizerEs } from 'lib/natural'
26
+ const tokenizer = new AggressiveTokenizerEs()
27
+
28
+ describe('aggressive_tokenizer_es', function () {
29
+ it('should tokenize strings', function () {
30
+ expect(tokenizer.tokenize('hola yo me llamo eduardo y esudié ingeniería')).toEqual(['hola', 'yo', 'me', 'llamo', 'eduardo', 'y', 'esudié', 'ingeniería'])
31
+ })
32
+
33
+ /*
34
+ it('should tokenize strings via attached string method', function() {
35
+ tokenizer.attach();
36
+ expect('hola yo me llamo eduardo y esudié ingeniería'.tokenize()).toEqual(['hola', 'yo', 'me', 'llamo', 'eduardo', 'y', 'esudié', 'ingeniería']);
37
+ });
38
+ */
39
+ })
@@ -0,0 +1,111 @@
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 SOFTWARE
21
+ */
22
+
23
+ 'use strict'
24
+
25
+ import { AggressiveTokenizerFr } from 'lib/natural'
26
+ const tokenizer = new AggressiveTokenizerFr()
27
+
28
+ const text = "Affectueusement surnommé « Gabo » dans toute l'Amérique latine, le Colombien Gabriel Garcia Marquez, prix Nobel de littérature 1982, l'un des plus grands écrivains du XXe siècle, est mort À son domicile de Mexico jeudi 17 avril. Il était âgé de 87 ans. Son Œuvre a été traduite dans toutes les langues ou presque, et vendue à quelque 50 millions d'exemplaires."
29
+
30
+ const tokenized = ['Affectueusement',
31
+ 'surnommé',
32
+ 'Gabo',
33
+ 'dans',
34
+ 'toute',
35
+ 'l',
36
+ 'Amérique',
37
+ 'latine',
38
+ 'le',
39
+ 'Colombien',
40
+ 'Gabriel',
41
+ 'Garcia',
42
+ 'Marquez',
43
+ 'prix',
44
+ 'Nobel',
45
+ 'de',
46
+ 'littérature',
47
+ '1982',
48
+ 'l',
49
+ 'un',
50
+ 'des',
51
+ 'plus',
52
+ 'grands',
53
+ 'écrivains',
54
+ 'du',
55
+ 'XXe',
56
+ 'siècle',
57
+ 'est',
58
+ 'mort',
59
+ 'À',
60
+ 'son',
61
+ 'domicile',
62
+ 'de',
63
+ 'Mexico',
64
+ 'jeudi',
65
+ '17',
66
+ 'avril',
67
+ 'Il',
68
+ 'était',
69
+ 'âgé',
70
+ 'de',
71
+ '87',
72
+ 'ans',
73
+ 'Son',
74
+ 'Œuvre',
75
+ 'a',
76
+ 'été',
77
+ 'traduite',
78
+ 'dans',
79
+ 'toutes',
80
+ 'les',
81
+ 'langues',
82
+ 'ou',
83
+ 'presque',
84
+ 'et',
85
+ 'vendue',
86
+ 'à',
87
+ 'quelque',
88
+ '50',
89
+ 'millions',
90
+ 'd',
91
+ 'exemplaires']
92
+
93
+ describe('aggressive_tokenizer_fr', function () {
94
+ it('should tokenize strings', function () {
95
+ expect(tokenizer.tokenize(text)).toEqual(tokenized)
96
+ })
97
+
98
+ it('should handle hyphens in words correctly', function () {
99
+ const sentence = 'Des sous-pages dans le sous-bois de la ville de Paris'
100
+ const res = tokenizer.tokenize(sentence)
101
+ const expectedRes = ['Des', 'sous-pages', 'dans', 'le', 'sous-bois', 'de', 'la', 'ville', 'de', 'Paris']
102
+ expect(res).toEqual(expectedRes)
103
+ })
104
+
105
+ /*
106
+ it('should tokenize strings via attached string method', function() {
107
+ tokenizer.attach();
108
+ expect(text.tokenize()).toEqual(tokenized);
109
+ });
110
+ */
111
+ })
@@ -0,0 +1,70 @@
1
+ /*
2
+ file aggressive_tokenizer_hi_spec.js , located at spec\aggressive_tokenizer_hi_spec.js is licensed as follows:
3
+ - (The MIT License)
4
+ - Copyright (c) 2023 Mukesh Singh Bisht
5
+
6
+ Permission is hereby granted, free of charge, to any person or entity obtaining a copy
7
+ of file aggressive_tokenizer_hi_spec.js and its content(the "Software"), to deal in the
8
+ Software without restriction, including without limitation the rights to use, copy,
9
+ modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
10
+ and to permit persons to whom the Software is furnished to do so, subject to the following
11
+ conditions:
12
+
13
+ 1. The above copyright notice and this permission notice shall be included in all copies
14
+ or substantial portions of the Software.
15
+
16
+ 2. Proper credit must be given to the original author Mukesh Singh Bisht, along with the
17
+ date of authorship specified as July 23, 2023, in any usage, distribution, or
18
+ modification of the Software.
19
+
20
+ THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22
+ PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23
+ FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR
24
+ OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25
+ DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ 'use strict'
29
+
30
+ import { AggressiveTokenizerHi } from 'lib/natural'
31
+ const tokenizer = new AggressiveTokenizerHi()
32
+
33
+ describe('aggressive_tokenizer_hi', function () {
34
+ it('should tokenize strings', function () {
35
+ const string = 'स्वतंत्रता दिवस की हार्दिक शुभकामनाएं'
36
+ const expectedArray = ['स्वतंत्रता', 'दिवस', 'की', 'हार्दिक', 'शुभकामनाएं']
37
+ expect(tokenizer.tokenize(string)).toEqual(expectedArray)
38
+ })
39
+ it('should tokenize strings including english words', function () {
40
+ const string = 'स्वतंत्रता दिवस की हार्दिक शुभकामनाएं congrats mukesh'
41
+ const expectedArray = ['स्वतंत्रता', 'दिवस', 'की', 'हार्दिक', 'शुभकामनाएं', 'congrats', 'mukesh']
42
+ expect(tokenizer.tokenize(string)).toEqual(expectedArray)
43
+ })
44
+
45
+ it('should swallow viram(stop) symbols', function () {
46
+ const string = 'स्वतंत्रता दिवस की हार्दिक शुभकामनाएं । congrats mukesh'
47
+ const expectedArray = ['स्वतंत्रता', 'दिवस', 'की', 'हार्दिक', 'शुभकामनाएं', 'congrats', 'mukesh']
48
+ expect(tokenizer.tokenize(string)).toEqual(expectedArray)
49
+ })
50
+ it('should swallow dirgh viram(stop) symbols', function () {
51
+ const string = 'राजा बाजीराव ॥ '
52
+ const expectedArray = ['राजा', 'बाजीराव']
53
+ expect(tokenizer.tokenize(string)).toEqual(expectedArray)
54
+ })
55
+ it('should swallow trailing off symbols', function () {
56
+ const string = 'राजा बाजीराव ...'
57
+ const expectedArray = ['राजा', 'बाजीराव']
58
+ expect(tokenizer.tokenize(string)).toEqual(expectedArray)
59
+ })
60
+ it('should swallow question mark symbols', function () {
61
+ const string = 'राजा बाजीराव ?'
62
+ const expectedArray = ['राजा', 'बाजीराव']
63
+ expect(tokenizer.tokenize(string)).toEqual(expectedArray)
64
+ })
65
+ it('should swallow comma mark symbols', function () {
66
+ const string = 'राजा, बाजीराव'
67
+ const expectedArray = ['राजा', 'बाजीराव']
68
+ expect(tokenizer.tokenize(string)).toEqual(expectedArray)
69
+ })
70
+ })
@@ -0,0 +1,39 @@
1
+ /*
2
+ Copyright (c) 2011, Chris Umbel, Martijn de Boer
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 { AggressiveTokenizerNl } from 'lib/natural'
26
+ const tokenizer = new AggressiveTokenizerNl()
27
+
28
+ describe('aggressive_tokenizer_nl', function () {
29
+ it('should tokenize strings', function () {
30
+ expect(tokenizer.tokenize('\'s Morgens is het nog erg koud, vertelde de weerman over een van de radio\'s')).toEqual(['\'s', 'Morgens', 'is', 'het', 'nog', 'erg', 'koud', 'vertelde', 'de', 'weerman', 'over', 'een', 'van', 'de', 'radio\'s'])
31
+ })
32
+
33
+ it('should handle hyphens in words correctly', function () {
34
+ const sentence = 'clearing-systeem front-office-automatisering christelijk-historisch mond-op-mond, kant-en-klaar, kruidje-roer-me-niet, doe-het-zelver'
35
+ const res = tokenizer.tokenize(sentence)
36
+ const expectedRes = ['clearing-systeem', 'front-office-automatisering', 'christelijk-historisch', 'mond-op-mond', 'kant-en-klaar', 'kruidje-roer-me-niet', 'doe-het-zelver']
37
+ expect(res).toEqual(expectedRes)
38
+ })
39
+ })
@@ -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
+ })