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.
Files changed (108) hide show
  1. package/.env +2 -1
  2. package/.eslintrc.json +13 -0
  3. package/.github/workflows/node.js.yml +3 -6
  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 +1 -1
  9. package/lib/natural/classifiers/classifier.js +11 -34
  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/maxent/Classifier.js +5 -3
  13. package/lib/natural/distance/index.d.ts +11 -14
  14. package/lib/natural/inflectors/count_inflector.js +18 -21
  15. package/lib/natural/inflectors/fr/count_inflector.js +18 -19
  16. package/lib/natural/inflectors/index.d.ts +12 -3
  17. package/lib/natural/inflectors/index.js +3 -0
  18. package/lib/natural/ngrams/index.d.ts +5 -5
  19. package/lib/natural/normalizers/index.d.ts +24 -1
  20. package/lib/natural/normalizers/index.js +4 -1
  21. package/lib/natural/normalizers/normalizer_ja.js +109 -72
  22. package/lib/natural/phonetics/dm_soundex.js +52 -51
  23. package/lib/natural/phonetics/double_metaphone.js +397 -399
  24. package/lib/natural/phonetics/index.d.ts +42 -12
  25. package/lib/natural/phonetics/metaphone.js +134 -157
  26. package/lib/natural/phonetics/phonetic.js +5 -29
  27. package/lib/natural/phonetics/soundex.js +45 -54
  28. package/lib/natural/sentiment/index.d.ts +6 -6
  29. package/lib/natural/spellcheck/index.d.ts +2 -0
  30. package/lib/natural/stemmers/index.d.ts +43 -1
  31. package/lib/natural/stemmers/index.js +1 -0
  32. package/lib/natural/stemmers/token.js +131 -132
  33. package/lib/natural/tfidf/index.d.ts +7 -7
  34. package/lib/natural/tfidf/tfidf.js +7 -1
  35. package/lib/natural/tokenizers/index.d.ts +2 -2
  36. package/lib/natural/trie/index.d.ts +1 -1
  37. package/lib/natural/util/index.d.ts +8 -0
  38. package/lib/natural/util/index.js +2 -0
  39. package/lib/natural/util/storage/index.d.ts +39 -0
  40. package/lib/natural/util/storage/index.js +24 -0
  41. package/package.json +22 -9
  42. package/ts_spec/MaxEntClassifier_spec.ts +130 -0
  43. package/ts_spec/SentimentAnalyzer_spec.ts +232 -0
  44. package/ts_spec/WordPunctTokenizer_spec.ts +57 -0
  45. package/ts_spec/aggressive_tokenizer_de_spec.ts +36 -0
  46. package/ts_spec/aggressive_tokenizer_es_spec.ts +39 -0
  47. package/ts_spec/aggressive_tokenizer_fr_spec.ts +111 -0
  48. package/ts_spec/aggressive_tokenizer_hi_spec.ts +70 -0
  49. package/ts_spec/aggressive_tokenizer_nl_spec.ts +39 -0
  50. package/ts_spec/aggressive_tokenizer_pt_spec.ts +48 -0
  51. package/ts_spec/aggressive_tokenizer_spec.ts +57 -0
  52. package/ts_spec/aggressive_tokenizer_sv_spec.ts +52 -0
  53. package/ts_spec/aggressive_tokenizer_vi_spec.ts +48 -0
  54. package/ts_spec/bayes_classifier_spec.ts +165 -0
  55. package/ts_spec/brill_pos_tagger_spec.ts +79 -0
  56. package/ts_spec/brill_pos_trainer_spec.ts +107 -0
  57. package/ts_spec/carry_stemmer_fr_spec.ts +97 -0
  58. package/ts_spec/classifier_spec.ts +104 -0
  59. package/ts_spec/count_inflector_fr_spec.ts +47 -0
  60. package/ts_spec/count_inflector_spec.ts +76 -0
  61. package/ts_spec/damerau_levenshtein_spec.ts +53 -0
  62. package/ts_spec/dice_coefficient_spec.ts +37 -0
  63. package/ts_spec/double_metaphone_spec.ts +739 -0
  64. package/ts_spec/hamming_distance_spec.ts +66 -0
  65. package/ts_spec/jaro-winkler_spec.ts +79 -0
  66. package/ts_spec/lancaster_stemmer_spec.ts +63 -0
  67. package/ts_spec/levenshtein_spec.ts +107 -0
  68. package/ts_spec/logistic_regression_classifier_spec.ts +93 -0
  69. package/ts_spec/longest_path_tree_spec.ts +111 -0
  70. package/ts_spec/metaphone_spec.ts +258 -0
  71. package/ts_spec/ngram_spec.ts +145 -0
  72. package/ts_spec/ngram_zh_spec.ts +124 -0
  73. package/ts_spec/normalizer_ja_spec.ts +165 -0
  74. package/ts_spec/normalizer_no_spec.ts +112 -0
  75. package/ts_spec/normalizer_spec.ts +141 -0
  76. package/ts_spec/noun_inflector_fr_spec.ts +472 -0
  77. package/ts_spec/noun_inflector_ja_spec.ts +116 -0
  78. package/ts_spec/noun_inflector_spec.ts +369 -0
  79. package/ts_spec/orthography_tokenizer_spec.ts +47 -0
  80. package/ts_spec/porter_stemmer_de_spec.ts +53 -0
  81. package/ts_spec/porter_stemmer_es_spec.ts +63 -0
  82. package/ts_spec/porter_stemmer_fr_spec.ts +103 -0
  83. package/ts_spec/porter_stemmer_it_spec.ts +56 -0
  84. package/ts_spec/porter_stemmer_nl_spec.ts +73 -0
  85. package/ts_spec/porter_stemmer_no_spec.ts +148 -0
  86. package/ts_spec/porter_stemmer_pt_spec.ts +50 -0
  87. package/ts_spec/porter_stemmer_ru_spec.ts +67 -0
  88. package/ts_spec/porter_stemmer_spec.ts +202 -0
  89. package/ts_spec/porter_stemmer_sv_spec.ts +41 -0
  90. package/ts_spec/present_verb_inflector_spec.ts +127 -0
  91. package/ts_spec/remove_diacritics_spec.ts +30 -0
  92. package/ts_spec/sentence_analyzer_spec.ts +293 -0
  93. package/ts_spec/sentence_tokenizer_parser_spec.ts +200 -0
  94. package/ts_spec/sentence_tokenizer_spec.ts +189 -0
  95. package/ts_spec/shortest_path_tree_spec.ts +141 -0
  96. package/ts_spec/soundex_spec.ts +162 -0
  97. package/ts_spec/spellcheck_spec.ts +112 -0
  98. package/ts_spec/stemmer_id_spec.ts +374 -0
  99. package/ts_spec/stemmer_ja_spec.ts +69 -0
  100. package/ts_spec/stemmer_token_spec.ts +163 -0
  101. package/ts_spec/tfidf_spec.ts +275 -0
  102. package/ts_spec/tokenizer_case_spec.ts +242 -0
  103. package/ts_spec/tokenizer_ja_spec.ts +63 -0
  104. package/ts_spec/transliterator_ja_spec.ts +105 -0
  105. package/ts_spec/treebank_word_tokenizer_spec.ts +38 -0
  106. package/ts_spec/trie_spec.ts +197 -0
  107. package/tsconfig.json +5 -3
  108. package/example.txt +0 -1
@@ -0,0 +1,127 @@
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
+ import { PresentVerbInflector } from 'lib/natural'
24
+ const inflector = new PresentVerbInflector()
25
+
26
+ describe('present verb inflector', function () {
27
+ describe('singularization', function () {
28
+ it('should singularize regular ES forms', function () {
29
+ expect(inflector.singularize('catch')).toBe('catches')
30
+ expect(inflector.singularize('do')).toBe('does')
31
+ expect(inflector.singularize('go')).toBe('goes')
32
+ })
33
+
34
+ it('should handle [CS]HES forms', function () {
35
+ expect(inflector.singularize('cash')).toBe('cashes')
36
+ expect(inflector.singularize('ach')).toBe('aches')
37
+ })
38
+
39
+ it('should ignore XES forms', function () {
40
+ expect(inflector.singularize('annex')).toBe('annexes')
41
+ })
42
+
43
+ it('should handle SSES forms', function () {
44
+ expect(inflector.singularize('access')).toBe('accesses')
45
+ })
46
+
47
+ it('should ignore ZZES forms', function () {
48
+ expect(inflector.singularize('buzz')).toBe('buzzes')
49
+ })
50
+
51
+ it('should singularize regular S forms', function () {
52
+ expect(inflector.singularize('claim')).toBe('claims')
53
+ expect(inflector.singularize('drink')).toBe('drinks')
54
+ expect(inflector.singularize('become')).toBe('becomes')
55
+ })
56
+
57
+ it('should singularize irregular forms', function () {
58
+ expect(inflector.singularize('are')).toBe('is')
59
+ expect(inflector.singularize('were')).toBe('was')
60
+ expect(inflector.singularize('have')).toBe('has')
61
+ })
62
+
63
+ it('should singularize ies forms', function () {
64
+ expect(inflector.singularize('fly')).toBe('flies')
65
+ expect(inflector.singularize('try')).toBe('tries')
66
+ })
67
+
68
+ it('should handle ambiguous forms', function () {
69
+ expect(inflector.singularize('will')).toBe('will')
70
+ })
71
+ })
72
+
73
+ describe('pluralization', function () {
74
+ it('should pluralize regular ES forms', function () {
75
+ expect(inflector.pluralize('catches')).toBe('catch')
76
+ expect(inflector.pluralize('does')).toBe('do')
77
+ expect(inflector.pluralize('goes')).toBe('go')
78
+ })
79
+
80
+ it('should handle [CS]HES forms', function () {
81
+ expect(inflector.pluralize('cashes')).toBe('cash')
82
+ expect(inflector.pluralize('aches')).toBe('ach')
83
+ })
84
+
85
+ it('should handle XES forms', function () {
86
+ expect(inflector.pluralize('annexes')).toBe('annex')
87
+ })
88
+
89
+ it('should handle SSES forms', function () {
90
+ expect(inflector.pluralize('accesses')).toBe('access')
91
+ })
92
+
93
+ it('should handle ZZES forms', function () {
94
+ expect(inflector.pluralize('buzzes')).toBe('buzz')
95
+ })
96
+
97
+ it('should pluralize regular S forms that done drop e', function () {
98
+ expect(inflector.pluralize('becomes')).toBe('become')
99
+ })
100
+
101
+ it('should pluralize regular S forms', function () {
102
+ expect(inflector.pluralize('drinks')).toBe('drink')
103
+ expect(inflector.pluralize('claims')).toBe('claim')
104
+ })
105
+
106
+ it('should pluralize irregular forms', function () {
107
+ expect(inflector.pluralize('was')).toBe('were')
108
+ expect(inflector.pluralize('is')).toBe('are')
109
+ expect(inflector.pluralize('am')).toBe('are')
110
+ expect(inflector.pluralize('has')).toBe('have')
111
+ })
112
+
113
+ it('should pluralize ies forms', function () {
114
+ expect(inflector.pluralize('flies')).toBe('fly')
115
+ expect(inflector.pluralize('tries')).toBe('try')
116
+ })
117
+
118
+ it('should handle ambiguous forms', function () {
119
+ expect(inflector.pluralize('will')).toBe('will')
120
+ })
121
+ })
122
+
123
+ it('should pluralize and singularize string', function () {
124
+ expect(inflector.pluralize('becomes')).toBe('become')
125
+ expect(inflector.singularize('become')).toBe('becomes')
126
+ })
127
+ })
@@ -0,0 +1,30 @@
1
+ /*
2
+ Copyright (c) 2013, Ken Koch
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
+ import { removeDiacritics } from 'lib/natural'
24
+
25
+ describe('remove_diacritics', function () {
26
+ it('should correctly remove diacritics', function () {
27
+ const originalPhrase = 'piñon ça va über résumé œdipe'
28
+ expect(removeDiacritics(originalPhrase)).toEqual('pinon ca va uber resume oedipe')
29
+ })
30
+ })
@@ -0,0 +1,293 @@
1
+ /*
2
+ Copyright (c) 2011, Rob Ellis, 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
+ /* eslint-disable no-new */
26
+ import { SentenceAnalyzer } from 'lib/natural'
27
+ import { SenType } from 'lib/natural/analyzers/SenType'
28
+ import type {
29
+ TaggedSentence,
30
+ TaggedWord,
31
+ PunctuationFunction,
32
+ PunctuationMapping,
33
+ CallbackFunction
34
+ } from 'lib/natural'
35
+
36
+ declare type Tests = Array<{
37
+ token?: string
38
+ result: string | boolean
39
+ argument?: string
40
+ index?: string
41
+ }>
42
+
43
+ describe('sentence analyzer', function () {
44
+ function testSentenceAnalyzer (sentenceTags: TaggedWord[], tests: Tests, callback: CallbackFunction): void {
45
+ new SentenceAnalyzer({ tags: sentenceTags, punct: function () { return [] } }, function (analyzer) {
46
+ analyzer.part(function (part) {
47
+ const posTags = part.posObj.tags
48
+ for (let tagNum = 0; tagNum < posTags.length; tagNum++) {
49
+ const posTag = posTags[tagNum]
50
+ tests.forEach(t => {
51
+ if (posTag.token === t.token && t.argument !== undefined) {
52
+ expect(posTag[t.argument]).toEqual(t.result)
53
+ }
54
+ })
55
+ }
56
+ const lastTagNum = posTags.length - 1
57
+ tests.forEach(t => {
58
+ if (t.index !== undefined && t.argument !== undefined) {
59
+ expect(posTags[lastTagNum][t.argument]).toEqual(t.result)
60
+ }
61
+ })
62
+ })
63
+ callback(analyzer)
64
+ })
65
+ }
66
+
67
+ it('should determine PP and SP, given a POS', function () {
68
+ const sentenceTags: TaggedWord[] = [
69
+ { token: 'The', pos: 'DT' },
70
+ { token: 'angry', pos: 'JJ' },
71
+ { token: 'bear', pos: 'NN' },
72
+ { token: 'chased', pos: 'VB' },
73
+ { token: 'the', pos: 'DT' },
74
+ { token: 'frightened', pos: 'JJ' },
75
+ { token: 'little', pos: 'JJ' },
76
+ { token: 'squirrel', pos: 'NN' }
77
+ ]
78
+
79
+ const tests: Tests = [
80
+ {
81
+ token: 'angry',
82
+ argument: 'spos',
83
+ result: 'SP'
84
+ },
85
+ {
86
+ token: 'squirrel',
87
+ argument: 'spos',
88
+ result: 'PP'
89
+ }
90
+ ]
91
+
92
+ testSentenceAnalyzer(sentenceTags, tests, function (analyzer) {
93
+ expect(analyzer.subjectToString().trim()).toEqual('The angry bear')
94
+ expect(analyzer.predicateToString().trim()).toEqual('chased the frightened little squirrel')
95
+ expect(analyzer.toString().trim()).toEqual('The angry bear chased the frightened little squirrel')
96
+ expect(analyzer.implicitYou()).toEqual(false)
97
+ })
98
+ })
99
+
100
+ it('should determine PP and SP given a POS that begins with a verb', function () {
101
+ const sentenceTags = [
102
+ { token: 'Vote', pos: 'VB' },
103
+ { token: 'for', pos: 'IN' },
104
+ { token: 'me', pos: 'PRP' }
105
+ ]
106
+
107
+ const tests: Tests = [
108
+ {
109
+ token: 'Vote',
110
+ argument: 'spos',
111
+ result: 'PP'
112
+ },
113
+ {
114
+ token: 'me',
115
+ argument: 'pp',
116
+ result: true
117
+ },
118
+ {
119
+ index: 'LAST',
120
+ argument: 'token',
121
+ result: 'You'
122
+ },
123
+ {
124
+ index: 'LAST',
125
+ argument: 'pos',
126
+ result: 'PRP'
127
+ },
128
+ {
129
+ index: 'LAST',
130
+ argument: 'added',
131
+ result: true
132
+ }
133
+ ]
134
+
135
+ testSentenceAnalyzer(sentenceTags, tests, function (analyzer) {
136
+ expect(analyzer.implicitYou()).toEqual(true)
137
+ })
138
+ })
139
+
140
+ it('should look for EX before VB', function () {
141
+ const sentenceTags = [
142
+ { token: 'There', pos: 'EX' },
143
+ { token: 'is', pos: 'VB' },
144
+ { token: 'a', pos: 'DT' },
145
+ { token: 'house', pos: 'NN' },
146
+ { token: 'in', pos: 'IN' },
147
+ { token: 'the', pos: 'DT' },
148
+ { token: 'valley', pos: 'DT' }
149
+ ]
150
+
151
+ const tests = [
152
+ {
153
+ token: 'There',
154
+ argument: 'spos',
155
+ result: 'SP'
156
+ },
157
+ {
158
+ token: 'is',
159
+ argument: 'spos',
160
+ result: 'SP'
161
+ }
162
+ ]
163
+
164
+ testSentenceAnalyzer(sentenceTags, tests, function (analyzer) {
165
+ expect(analyzer.subjectToString().trim()).toEqual('There is a house')
166
+ expect(analyzer.predicateToString().trim()).toEqual('')
167
+ expect(analyzer.toString().trim()).toEqual('There is a house in the valley')
168
+ expect(analyzer.implicitYou()).toEqual(false)
169
+ })
170
+ })
171
+
172
+ function testSentenceType (args: TaggedSentence, sentenceType: SenType, callback: CallbackFunction | null): void {
173
+ new SentenceAnalyzer(args, function (analyzer) {
174
+ analyzer.part(function () {
175
+ analyzer.type(function (type) {
176
+ expect(analyzer.senType).toEqual(sentenceType)
177
+ })
178
+ })
179
+ if (callback !== null) {
180
+ callback(analyzer)
181
+ }
182
+ })
183
+ }
184
+
185
+ describe('#type', function () {
186
+ it('should determine a command without punctuation', function () {
187
+ const sentenceTags = [
188
+ { token: 'Vote', pos: 'VB' },
189
+ { token: 'for', pos: 'IN' },
190
+ { token: 'me', pos: 'PRP' }
191
+ ]
192
+ const punct: PunctuationFunction = function (): PunctuationMapping {
193
+ return []
194
+ }
195
+
196
+ testSentenceType({ tags: sentenceTags, punct }, SenType.Command, null)
197
+ })
198
+
199
+ it('should determine an interrogative beginning with who', function () {
200
+ const sentenceTags = [
201
+ { token: 'Who', pos: 'WP' },
202
+ { token: 'voted', pos: 'VB' }
203
+ ]
204
+ const punct: PunctuationFunction = function (): PunctuationMapping {
205
+ return []
206
+ }
207
+
208
+ testSentenceType({ tags: sentenceTags, punct }, SenType.Interrogative, null)
209
+ })
210
+
211
+ it('should determine an interrogative ending with a personal pronoun', function () {
212
+ const sentenceTags = [
213
+ { token: 'Should', pos: 'MD' },
214
+ { token: 'we', pos: 'PRP' }
215
+ ]
216
+ const punct: PunctuationFunction = function (): PunctuationMapping {
217
+ return ''
218
+ }
219
+
220
+ testSentenceType({ tags: sentenceTags, punct }, SenType.Interrogative, null)
221
+ })
222
+
223
+ it('should classify other sentences as unknown', function () {
224
+ const sentenceTags = [
225
+ { token: 'I', pos: 'PRP' },
226
+ { token: 'am', pos: 'VB' },
227
+ { token: 'unsure', pos: 'JJ' }
228
+ ]
229
+ const punct: PunctuationFunction = function (): PunctuationMapping {
230
+ return ''
231
+ }
232
+
233
+ testSentenceType({ tags: sentenceTags, punct }, SenType.Unknown, null)
234
+ })
235
+
236
+ it('should determine an interrogative ending with a question mark', function () {
237
+ const sentenceTags = [
238
+ { token: 'Do', pos: 'VB' },
239
+ { token: 'I', pos: 'PRP' },
240
+ { token: 'care', pos: 'VB' }
241
+ ]
242
+ const punct: PunctuationFunction = function (): PunctuationMapping {
243
+ return [{ token: '?', pos: '.' }]
244
+ }
245
+
246
+ testSentenceType({ tags: sentenceTags, punct }, SenType.Interrogative, null)
247
+ })
248
+
249
+ const taggedSentForCommand = [
250
+ { token: 'Vote', pos: 'VB' },
251
+ { token: 'for', pos: 'IN' },
252
+ { token: 'me', pos: 'PRP' }
253
+ ]
254
+
255
+ it('should determine a command ending in an exclamation point', function () {
256
+ const punct: PunctuationFunction = function (): PunctuationMapping {
257
+ return [{ token: '!', pos: '.' }]
258
+ }
259
+
260
+ testSentenceType({ tags: taggedSentForCommand, punct }, SenType.Command, null)
261
+ })
262
+
263
+ it('should determine a command ending with .', function () {
264
+ const punct: PunctuationFunction = function (): PunctuationMapping {
265
+ return [{ token: '.', pos: '.' }]
266
+ }
267
+
268
+ testSentenceType({ tags: taggedSentForCommand, punct }, SenType.Command, null)
269
+ })
270
+
271
+ const taggedSentenceForExclam = [
272
+ { token: 'We', pos: 'PRP' },
273
+ { token: 'like', pos: 'VB' },
274
+ { token: 'sheep', pos: 'NN' }
275
+ ]
276
+
277
+ it('should determine an exclamation ending in an exclamation point', function () {
278
+ const punct: PunctuationFunction = function (): PunctuationMapping {
279
+ return [{ token: '!', pos: '.' }]
280
+ }
281
+
282
+ testSentenceType({ tags: taggedSentenceForExclam, punct }, SenType.Exclamatory, null)
283
+ })
284
+
285
+ it('should determine a declaration ending with a .', function () {
286
+ const punct: PunctuationFunction = function (): PunctuationMapping {
287
+ return [{ token: '.', pos: '.' }]
288
+ }
289
+
290
+ testSentenceType({ tags: taggedSentenceForExclam, punct }, SenType.Declarative, null)
291
+ })
292
+ })
293
+ })
@@ -0,0 +1,200 @@
1
+ /*
2
+ Copyright (c) 2011, 2020 Chris Umbel, 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 { SentenceTokenizerNew as Tokenizer } from 'lib/natural'
26
+ const tokenizer = new Tokenizer()
27
+
28
+ describe('sentence_tokenizer', function () {
29
+ it('should tokenize strings and trim whitespace', function () {
30
+ expect(
31
+ tokenizer.tokenize('This is a sentence. This is another sentence.')
32
+ )
33
+ .toEqual([
34
+ 'This is a sentence.',
35
+ 'This is another sentence.'
36
+ ])
37
+ })
38
+
39
+ /*
40
+ it('should tokenize strings via attached string method', function() {
41
+ tokenizer.attach();
42
+ expect(
43
+ 'This is a sentence. This is another sentence.'.tokenize()
44
+ ).toEqual([
45
+ 'This is a sentence.',
46
+ 'This is another sentence.'
47
+ ]);
48
+ });
49
+
50
+ it('should tokenize strings via attached string method', function() {
51
+ tokenizer.attach();
52
+ expect(
53
+ 'This is a sentence. This is another sentence.'.tokenize()
54
+ ).toEqual([
55
+ 'This is a sentence.',
56
+ 'This is another sentence.'
57
+ ]);
58
+ });
59
+ */
60
+
61
+ it('should include quotation marks', function () {
62
+ expect(
63
+ tokenizer.tokenize('"This is a sentence." This is another sentence.')
64
+ ).toEqual([
65
+ '"This is a sentence."',
66
+ 'This is another sentence.'
67
+ ])
68
+ })
69
+
70
+ it('should include brackets', function () {
71
+ expect(
72
+ tokenizer.tokenize('This is a sentence. [This is another sentence.]')
73
+ ).toEqual([
74
+ 'This is a sentence.',
75
+ '[This is another sentence.]'
76
+ ])
77
+ })
78
+
79
+ it('should handle repetitive punctuation', function () {
80
+ expect(
81
+ tokenizer.tokenize('I love you!! Do you love me??')
82
+ ).toEqual(['I love you!!', 'Do you love me??'
83
+ ])
84
+ })
85
+
86
+ it('should handle repetitive punctuation with space', function () {
87
+ expect(
88
+ tokenizer.tokenize('I love you! ! Do you love me? ?')
89
+ ).toEqual(['I love you! !', 'Do you love me? ?'
90
+ ])
91
+ })
92
+
93
+ it('should handle decimal numbers in sentences', function () {
94
+ expect(
95
+ tokenizer.tokenize('Pi is approximately equal to 3.14.')
96
+ ).toEqual([
97
+ 'Pi is approximately equal to 3.14.'
98
+ ])
99
+ })
100
+
101
+ it('should tokenize text with a number like "1)" present ', function () {
102
+ expect(
103
+ tokenizer.tokenize("This is a sentence that can't 1) be parsed with SentenceTokenizerNew. Here is another sentence.")
104
+ ).toEqual([
105
+ "This is a sentence that can't 1) be parsed with SentenceTokenizerNew.",
106
+ 'Here is another sentence.'
107
+ ])
108
+ })
109
+
110
+ it('should handle periods in email addresses', function () {
111
+ expect(
112
+ tokenizer.tokenize('My email address is batman@example.com.')
113
+ ).toEqual([
114
+ 'My email address is batman@example.com.'
115
+ ])
116
+ })
117
+
118
+ it('should handle periods in web addresses', function () {
119
+ expect(
120
+ tokenizer.tokenize('My twitter feed can be found at https://twitter.com/user1.')
121
+ ).toEqual([
122
+ 'My twitter feed can be found at https://twitter.com/user1.'
123
+ ])
124
+ })
125
+
126
+ it('should handle an ellipsis followed by punctuation', function () {
127
+ expect(
128
+ tokenizer.tokenize('Is this the end for our heroes...?')
129
+ ).toEqual([
130
+ 'Is this the end for our heroes...?'
131
+ ])
132
+ })
133
+
134
+ it('should handle multiple spaces separating sentences', function () {
135
+ expect(
136
+ tokenizer.tokenize('Tune in tomorrow and find out! Same Bat-Time! Same Bat-Channel!')
137
+ ).toEqual([
138
+ 'Tune in tomorrow and find out!',
139
+ 'Same Bat-Time!',
140
+ 'Same Bat-Channel!'
141
+ ])
142
+ })
143
+
144
+ it('should handle a sentence that does not end with punctuation (issue #549))', function () {
145
+ expect(
146
+ tokenizer.tokenize('This is a sentence. But is this also one')
147
+ ).toEqual([
148
+ 'This is a sentence.',
149
+ 'But is this also one'
150
+ ])
151
+ })
152
+
153
+ it('should handle a sentence that contains a quoted phrase (issue #550 but with . and ’ reversed))', function () {
154
+ expect(
155
+ tokenizer.tokenize('This is a sentence. And another where ‘Someone says something’.')
156
+ ).toEqual([
157
+ 'This is a sentence.',
158
+ 'And another where ‘Someone says something’.'
159
+ ])
160
+ })
161
+
162
+ it('should handle sentences with an abbreviation', function () {
163
+ expect(
164
+ tokenizer.tokenize('Acme, Inc. is creating exciting products. Use at your own risk.')
165
+ ).toEqual([
166
+ 'Acme, Inc. is creating exciting products.',
167
+ 'Use at your own risk.'
168
+ ])
169
+ expect(
170
+ tokenizer.tokenize('I need the parts A.S.A.P. please. Send them when they are ready')
171
+ ).toEqual(
172
+ [
173
+ 'I need the parts A.S.A.P. please.',
174
+ 'Send them when they are ready'
175
+ ]
176
+ )
177
+ expect(
178
+ tokenizer.tokenize('I need the parts from Inc.. Send them when they are ready!')
179
+ ).toEqual(
180
+ [
181
+ 'I need the parts from Inc..',
182
+ 'Send them when they are ready!'
183
+ ]
184
+ )
185
+ })
186
+ it('Should correctly parse multiple sentences with a subset of the sentences surrounded with quotes', function () {
187
+ expect(
188
+ tokenizer.tokenize('"All ticketed passengers should now be in the Blue Concourse sleep lounge. Make sure your validation papers are in order. Thank you." The upstairs lounge was not at all grungy.')
189
+ ).toEqual(
190
+ [
191
+ '"',
192
+ 'All ticketed passengers should now be in the Blue Concourse sleep lounge.',
193
+ 'Make sure your validation papers are in order.',
194
+ 'Thank you.',
195
+ '"',
196
+ 'The upstairs lounge was not at all grungy.'
197
+ ]
198
+ )
199
+ })
200
+ })