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
@@ -20,24 +20,54 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
  THE SOFTWARE.
21
21
  */
22
22
 
23
- export interface Phonetic<T> {
24
- compare: (stringA: string, stringB: string) => boolean
25
- tokenizeAndPhoneticize: (str: string, phoneticsProcessor: Phonetic<T>, keepStops: boolean) => T
23
+ export class Phonetic<T> {
24
+ compare (stringA: string, stringB: string): boolean
25
+ tokenizeAndPhoneticize (str: string, phoneticsProcessor: Phonetic<T>, keepStops: boolean): T
26
26
  }
27
27
 
28
- export let Metaphone: Phonetic<string> & {
29
- process: (token: string, maxLength?: number) => string
28
+ export class Metaphone extends Phonetic<string> {
29
+ dedup (token: string): string
30
+ dropInitialLetters (token: string): string
31
+ dropBafterMAtEnd (token: string): string
32
+ cTransform (token: string): string
33
+ dTransform (token: string): string
34
+ dropG (token: string): string
35
+ transformG (token: string): string
36
+ dropH (token: string): string
37
+ transformCK (token: string): string
38
+ transformPH (token: string): string
39
+ transformQ (token: string): string
40
+ transformS (token: string): string
41
+ transformT (token: string): string
42
+ dropT (token: string): string
43
+ transformV (token: string): string
44
+ transformWH (token: string): string
45
+ dropW (token: string): string
46
+ transformX (token: string): string
47
+ dropY (token: string): string
48
+ transformZ (token: string): string
49
+ dropVowels (token: string): string
50
+ process (token: string, maxLength?: number): string
30
51
  }
31
52
 
32
- export let SoundEx: Phonetic<string> & {
33
- process: (token: string, maxLength?: number) => string
53
+ export class SoundEx extends Phonetic<string> {
54
+ process (token: string, maxLength?: number): string
55
+ transformLipps (token: string): string
56
+ transformThroats (token: string): string
57
+ transformToungue (token: string): string
58
+ transformL (token: string): string
59
+ transformHum (token: string): string
60
+ transformR (token: string): string
61
+ condense (token: string): string
62
+ padRight0 (token: string): string
63
+ transform (token: string): string
34
64
  }
35
65
 
36
- export let DoubleMetaphone: Phonetic<string[]> & {
37
- process: (token: string, maxLength?: number) => string[]
38
- isVowel: (c: string) => string | RegExpMatchArray | null
66
+ export class DoubleMetaphone extends Phonetic<string[]> {
67
+ process (token: string, maxLength?: number): string[]
68
+ isVowel (c: string): string | RegExpMatchArray | null
39
69
  }
40
70
 
41
- export let SoundExDM: Phonetic<string> & {
42
- process: (word: string, codeLength?: number) => string
71
+ export class SoundExDM extends Phonetic<string> {
72
+ process (word: string, codeLength?: number): string
43
73
  }
@@ -22,170 +22,147 @@ THE SOFTWARE.
22
22
 
23
23
  'use strict'
24
24
 
25
- 'use strict'
26
-
27
25
  const Phonetic = require('./phonetic')
28
26
 
29
- function dedup (token) {
30
- return token.replace(/([^c])\1/g, '$1')
31
- }
32
-
33
- function dropInitialLetters (token) {
34
- if (token.match(/^(kn|gn|pn|ae|wr)/)) { return token.substr(1, token.length - 1) }
35
-
36
- return token
37
- }
38
-
39
- function dropBafterMAtEnd (token) {
40
- return token.replace(/mb$/, 'm')
41
- }
42
-
43
- function cTransform (token) {
44
- token = token.replace(/([^s]|^)(c)(h)/g, '$1x$3').trim()
45
-
46
- token = token.replace(/cia/g, 'xia')
47
- token = token.replace(/c(i|e|y)/g, 's$1')
48
- token = token.replace(/c/g, 'k')
49
-
50
- return token
51
- }
52
-
53
- function dTransform (token) {
54
- token = token.replace(/d(ge|gy|gi)/g, 'j$1')
55
- token = token.replace(/d/g, 't')
56
-
57
- return token
58
- }
59
-
60
- function dropG (token) {
61
- token = token.replace(/gh(^$|[^aeiou])/g, 'h$1')
62
- token = token.replace(/g(n|ned)$/g, '$1')
63
-
64
- return token
65
- }
66
-
67
- function transformG (token) {
68
- token = token.replace(/gh/g, 'f')
69
- token = token.replace(/([^g]|^)(g)(i|e|y)/g, '$1j$3')
70
- token = token.replace(/gg/g, 'g')
71
- token = token.replace(/g/g, 'k')
72
-
73
- return token
74
- }
75
-
76
- function dropH (token) {
77
- return token.replace(/([aeiou])h([^aeiou]|$)/g, '$1$2')
78
- }
79
-
80
- function transformCK (token) {
81
- return token.replace(/ck/g, 'k')
82
- }
83
- function transformPH (token) {
84
- return token.replace(/ph/g, 'f')
85
- }
86
-
87
- function transformQ (token) {
88
- return token.replace(/q/g, 'k')
89
- }
90
-
91
- function transformS (token) {
92
- return token.replace(/s(h|io|ia)/g, 'x$1')
93
- }
94
-
95
- function transformT (token) {
96
- token = token.replace(/t(ia|io)/g, 'x$1')
97
- token = token.replace(/th/, '0')
27
+ class Metaphone extends Phonetic {
28
+ dedup (token) {
29
+ return token.replace(/([^c])\1/g, '$1')
30
+ }
98
31
 
99
- return token
100
- }
32
+ dropInitialLetters (token) {
33
+ if (token.match(/^(kn|gn|pn|ae|wr)/)) { return token.substr(1, token.length - 1) }
101
34
 
102
- function dropT (token) {
103
- return token.replace(/tch/g, 'ch')
104
- }
35
+ return token
36
+ }
105
37
 
106
- function transformV (token) {
107
- return token.replace(/v/g, 'f')
108
- }
38
+ dropBafterMAtEnd (token) {
39
+ return token.replace(/mb$/, 'm')
40
+ }
109
41
 
110
- function transformWH (token) {
111
- return token.replace(/^wh/, 'w')
112
- }
42
+ cTransform (token) {
43
+ token = token.replace(/([^s]|^)(c)(h)/g, '$1x$3').trim()
113
44
 
114
- function dropW (token) {
115
- return token.replace(/w([^aeiou]|$)/g, '$1')
45
+ token = token.replace(/cia/g, 'xia')
46
+ token = token.replace(/c(i|e|y)/g, 's$1')
47
+ token = token.replace(/c/g, 'k')
48
+
49
+ return token
50
+ }
51
+
52
+ dTransform (token) {
53
+ token = token.replace(/d(ge|gy|gi)/g, 'j$1')
54
+ token = token.replace(/d/g, 't')
55
+
56
+ return token
57
+ }
58
+
59
+ dropG (token) {
60
+ token = token.replace(/gh(^$|[^aeiou])/g, 'h$1')
61
+ token = token.replace(/g(n|ned)$/g, '$1')
62
+
63
+ return token
64
+ }
65
+
66
+ transformG (token) {
67
+ token = token.replace(/gh/g, 'f')
68
+ token = token.replace(/([^g]|^)(g)(i|e|y)/g, '$1j$3')
69
+ token = token.replace(/gg/g, 'g')
70
+ token = token.replace(/g/g, 'k')
71
+
72
+ return token
73
+ }
74
+
75
+ dropH (token) {
76
+ return token.replace(/([aeiou])h([^aeiou]|$)/g, '$1$2')
77
+ }
78
+
79
+ transformCK (token) {
80
+ return token.replace(/ck/g, 'k')
81
+ }
82
+
83
+ transformPH (token) {
84
+ return token.replace(/ph/g, 'f')
85
+ }
86
+
87
+ transformQ (token) {
88
+ return token.replace(/q/g, 'k')
89
+ }
90
+
91
+ transformS (token) {
92
+ return token.replace(/s(h|io|ia)/g, 'x$1')
93
+ }
94
+
95
+ transformT (token) {
96
+ token = token.replace(/t(ia|io)/g, 'x$1')
97
+ token = token.replace(/th/, '0')
98
+
99
+ return token
100
+ }
101
+
102
+ dropT (token) {
103
+ return token.replace(/tch/g, 'ch')
104
+ }
105
+
106
+ transformV (token) {
107
+ return token.replace(/v/g, 'f')
108
+ }
109
+
110
+ transformWH (token) {
111
+ return token.replace(/^wh/, 'w')
112
+ }
113
+
114
+ dropW (token) {
115
+ return token.replace(/w([^aeiou]|$)/g, '$1')
116
+ }
117
+
118
+ transformX (token) {
119
+ token = token.replace(/^x/, 's')
120
+ token = token.replace(/x/g, 'ks')
121
+ return token
122
+ }
123
+
124
+ dropY (token) {
125
+ return token.replace(/y([^aeiou]|$)/g, '$1')
126
+ }
127
+
128
+ transformZ (token) {
129
+ return token.replace(/z/, 's')
130
+ }
131
+
132
+ dropVowels (token) {
133
+ return token.charAt(0) + token.substr(1, token.length).replace(/[aeiou]/g, '')
134
+ }
135
+
136
+ process (token, maxLength) {
137
+ const maxLengthNew = maxLength || 32
138
+ token = token.toLowerCase()
139
+ token = this.dedup(token)
140
+ token = this.dropInitialLetters(token)
141
+ token = this.dropBafterMAtEnd(token)
142
+ token = this.transformCK(token)
143
+ token = this.cTransform(token)
144
+ token = this.dTransform(token)
145
+ token = this.dropG(token)
146
+ token = this.transformG(token)
147
+ token = this.dropH(token)
148
+ token = this.transformPH(token)
149
+ token = this.transformQ(token)
150
+ token = this.transformS(token)
151
+ token = this.transformX(token)
152
+ token = this.transformT(token)
153
+ token = this.dropT(token)
154
+ token = this.transformV(token)
155
+ token = this.transformWH(token)
156
+ token = this.dropW(token)
157
+ token = this.dropY(token)
158
+ token = this.transformZ(token)
159
+ token = this.dropVowels(token)
160
+
161
+ token.toUpperCase()
162
+ if (token.length >= maxLengthNew) { token = token.substring(0, maxLengthNew) }
163
+
164
+ return token.toUpperCase()
165
+ }
116
166
  }
117
167
 
118
- function transformX (token) {
119
- token = token.replace(/^x/, 's')
120
- token = token.replace(/x/g, 'ks')
121
- return token
122
- }
123
-
124
- function dropY (token) {
125
- return token.replace(/y([^aeiou]|$)/g, '$1')
126
- }
127
-
128
- function transformZ (token) {
129
- return token.replace(/z/, 's')
130
- }
131
-
132
- function dropVowels (token) {
133
- return token.charAt(0) + token.substr(1, token.length).replace(/[aeiou]/g, '')
134
- }
135
-
136
- const Metaphone = new Phonetic()
137
168
  module.exports = Metaphone
138
-
139
- Metaphone.process = function (token, maxLength) {
140
- const maxLengthNew = maxLength || 32
141
- token = token.toLowerCase()
142
- token = dedup(token)
143
- token = dropInitialLetters(token)
144
- token = dropBafterMAtEnd(token)
145
- token = transformCK(token)
146
- token = cTransform(token)
147
- token = dTransform(token)
148
- token = dropG(token)
149
- token = transformG(token)
150
- token = dropH(token)
151
- token = transformPH(token)
152
- token = transformQ(token)
153
- token = transformS(token)
154
- token = transformX(token)
155
- token = transformT(token)
156
- token = dropT(token)
157
- token = transformV(token)
158
- token = transformWH(token)
159
- token = dropW(token)
160
- token = dropY(token)
161
- token = transformZ(token)
162
- token = dropVowels(token)
163
-
164
- token.toUpperCase()
165
- if (token.length >= maxLengthNew) { token = token.substring(0, maxLengthNew) }
166
-
167
- return token.toUpperCase()
168
- }
169
-
170
- // expose functions for testing
171
- Metaphone.dedup = dedup
172
- Metaphone.dropInitialLetters = dropInitialLetters
173
- Metaphone.dropBafterMAtEnd = dropBafterMAtEnd
174
- Metaphone.cTransform = cTransform
175
- Metaphone.dTransform = dTransform
176
- Metaphone.dropG = dropG
177
- Metaphone.transformG = transformG
178
- Metaphone.dropH = dropH
179
- Metaphone.transformCK = transformCK
180
- Metaphone.transformPH = transformPH
181
- Metaphone.transformQ = transformQ
182
- Metaphone.transformS = transformS
183
- Metaphone.transformT = transformT
184
- Metaphone.dropT = dropT
185
- Metaphone.transformV = transformV
186
- Metaphone.transformWH = transformWH
187
- Metaphone.dropW = dropW
188
- Metaphone.transformX = transformX
189
- Metaphone.dropY = dropY
190
- Metaphone.transformZ = transformZ
191
- Metaphone.dropVowels = dropVowels
@@ -26,12 +26,12 @@ const stopwords = require('../util/stopwords')
26
26
  const Tokenizer = require('../tokenizers/aggressive_tokenizer')
27
27
  const tokenizer = new Tokenizer()
28
28
 
29
- module.exports = function () {
30
- this.compare = function (stringA, stringB) {
29
+ class Phonetic {
30
+ compare (stringA, stringB) {
31
31
  return this.process(stringA) === this.process(stringB)
32
32
  }
33
33
 
34
- this.tokenizeAndPhoneticize = function (str, phoneticsProcessor, keepStops) {
34
+ tokenizeAndPhoneticize (str, phoneticsProcessor, keepStops) {
35
35
  const phoneticizedTokens = []
36
36
 
37
37
  tokenizer.tokenize(str).forEach(function (token) {
@@ -42,30 +42,6 @@ module.exports = function () {
42
42
 
43
43
  return phoneticizedTokens
44
44
  }
45
-
46
- /*
47
- this.attach = function () {
48
- const phonetic = this
49
-
50
- String.prototype.soundsLike = function (compareTo) {
51
- return phonetic.compare(this, compareTo)
52
- }
53
-
54
- String.prototype.phonetics = function () {
55
- return phonetic.process(this)
56
- }
57
-
58
- String.prototype.tokenizeAndPhoneticize = function (keepStops) {
59
- const phoneticizedTokens = []
60
-
61
- tokenizer.tokenize(this).forEach(function (token) {
62
- if (keepStops || stopwords.words.indexOf(token) < 0) {
63
- phoneticizedTokens.push(token.phonetics())
64
- }
65
- })
66
-
67
- return phoneticizedTokens
68
- }
69
- }
70
- */
71
45
  }
46
+
47
+ module.exports = Phonetic
@@ -24,60 +24,51 @@ THE SOFTWARE.
24
24
 
25
25
  const Phonetic = require('./phonetic')
26
26
 
27
- function transformLipps (token) {
28
- return token.replace(/[bfpv]/g, '1')
27
+ class SoundEx extends Phonetic {
28
+ process (token, maxLength) {
29
+ token = token.toLowerCase()
30
+ let transformed = this.condense(this.transform(token.substr(1, token.length - 1))) // anything that isn't a digit goes
31
+ // deal with duplicate INITIAL consonant SOUNDS
32
+ transformed = transformed.replace(new RegExp('^' + this.transform(token.charAt(0))), '')
33
+ return token.charAt(0).toUpperCase() + this.padRight0(transformed.replace(/\D/g, '')).substr(0, (maxLength && maxLength - 1) || 3)
34
+ }
35
+
36
+ transformLipps (token) {
37
+ return token.replace(/[bfpv]/g, '1')
38
+ }
39
+
40
+ transformThroats (token) {
41
+ return token.replace(/[cgjkqsxz]/g, '2')
42
+ }
43
+
44
+ transformToungue (token) {
45
+ return token.replace(/[dt]/g, '3')
46
+ }
47
+
48
+ transformL (token) {
49
+ return token.replace(/l/g, '4')
50
+ }
51
+
52
+ transformHum (token) {
53
+ return token.replace(/[mn]/g, '5')
54
+ }
55
+
56
+ transformR (token) {
57
+ return token.replace(/r/g, '6')
58
+ }
59
+
60
+ condense (token) {
61
+ return token.replace(/(\d)?\1+/g, '$1')
62
+ }
63
+
64
+ padRight0 (token) {
65
+ if (token.length < 4) { return token + Array(4 - token.length).join('0') } else { return token }
66
+ }
67
+
68
+ transform (token) {
69
+ return this.transformLipps(this.transformThroats(
70
+ this.transformToungue(this.transformL(this.transformHum(this.transformR(token))))))
71
+ }
29
72
  }
30
73
 
31
- function transformThroats (token) {
32
- return token.replace(/[cgjkqsxz]/g, '2')
33
- }
34
-
35
- function transformToungue (token) {
36
- return token.replace(/[dt]/g, '3')
37
- }
38
-
39
- function transformL (token) {
40
- return token.replace(/l/g, '4')
41
- }
42
-
43
- function transformHum (token) {
44
- return token.replace(/[mn]/g, '5')
45
- }
46
-
47
- function transformR (token) {
48
- return token.replace(/r/g, '6')
49
- }
50
-
51
- function condense (token) {
52
- return token.replace(/(\d)?\1+/g, '$1')
53
- }
54
-
55
- function padRight0 (token) {
56
- if (token.length < 4) { return token + Array(4 - token.length).join('0') } else { return token }
57
- }
58
-
59
- function transform (token) {
60
- return transformLipps(transformThroats(
61
- transformToungue(transformL(transformHum(transformR(token))))))
62
- }
63
-
64
- const SoundEx = new Phonetic()
65
74
  module.exports = SoundEx
66
-
67
- SoundEx.process = function (token, maxLength) {
68
- token = token.toLowerCase()
69
- let transformed = condense(transform(token.substr(1, token.length - 1))) // anything that isn't a digit goes
70
- // deal with duplicate INITIAL consonant SOUNDS
71
- transformed = transformed.replace(new RegExp('^' + transform(token.charAt(0))), '')
72
- return token.charAt(0).toUpperCase() + padRight0(transformed.replace(/\D/g, '')).substr(0, (maxLength && maxLength - 1) || 3)
73
- }
74
-
75
- // export for tests;
76
- SoundEx.transformLipps = transformLipps
77
- SoundEx.transformThroats = transformThroats
78
- SoundEx.transformToungue = transformToungue
79
- SoundEx.transformL = transformL
80
- SoundEx.transformHum = transformHum
81
- SoundEx.transformR = transformR
82
- SoundEx.condense = condense
83
- SoundEx.padRight0 = padRight0
@@ -1,6 +1,8 @@
1
-
2
1
  /*
3
- Copyright (c) 2022, Dylan R. E. Moonfire <https://github.com/dmoonfire>, Emily Marigold Klassen <https://github.com/forivall>, Hugo W.L. ter Doest
2
+ Copyright (c) 2022,
3
+ Dylan R. E. Moonfire <https://github.com/dmoonfire>,
4
+ Emily Marigold Klassen <https://github.com/forivall>,
5
+ Hugo W.L. ter Doest
4
6
 
5
7
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
8
  of this software and associated documentation files (the "Software"), to deal
@@ -21,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
23
  THE SOFTWARE.
22
24
  */
23
25
 
24
- import { Stemmer } from '../stemmers'
26
+ import type { Stemmer } from '../stemmers'
25
27
 
26
28
  export type AfinnLanguageType = 'afinn'
27
29
  export type AfinnLanguage = 'English' | 'Spanish' | 'Portuguese'
@@ -33,8 +35,6 @@ export type PatternLanguageType = 'pattern'
33
35
  export type PatternLanguage = 'Dutch' | 'Italian' | 'English' | 'French' | 'German'
34
36
 
35
37
  export declare class SentimentAnalyzer {
36
- constructor (language: AfinnLanguage, stemmer: Stemmer, type: AfinnLanguageType)
37
- constructor (language: SenticonLanguage, stemmer: Stemmer, type: SenticonLanguageType)
38
- constructor (language: PatternLanguage, stemmer: Stemmer, type: PatternLanguageType)
38
+ constructor (language: string, stemmer: Stemmer | undefined, type: string)
39
39
  getSentiment (words: string[]): number
40
40
  }
@@ -27,4 +27,6 @@ export declare class Spellcheck {
27
27
  constructor (wordlist: string[])
28
28
  isCorrect (word: string): boolean
29
29
  getCorrections (word: string, maxDistance?: number): string[]
30
+ edits (word: string): string[]
31
+ editsWithMaxDistance (word: string, maxDistance: number): string[]
30
32
  }
@@ -20,13 +20,35 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
  THE SOFTWARE.
21
21
  */
22
22
 
23
- declare interface Stemmer {
23
+ declare interface RegionsType {
24
+ r1: number
25
+ r2: number
26
+ rv: number
27
+ }
28
+
29
+ export declare interface Stemmer {
24
30
  stem: (token: string) => string
25
31
  addStopWord: (stopWord: string) => void
26
32
  addStopWords: (moreStopWords: string[]) => void
27
33
  removeStopWord: (stopWord: string) => void
28
34
  removeStopWords: (moreStopWords: string[]) => void
29
35
  tokenizeAndStem: (text: string, keepStops?: boolean) => string[]
36
+ regions: (word: string) => RegionsType
37
+ prelude: (token: string) => string
38
+ endsinArr: (token: string, suffixes: string[]) => string
39
+ categorizeGroups: (token: string) => string
40
+ measure: (token: string) => number
41
+
42
+ // Exposed for testing purposes:
43
+ step1: (token: string) => string
44
+ step1a: (token: string) => string
45
+ step1b: (token: string) => string
46
+ step1c: (token: string) => string
47
+ step2: (token: string) => string
48
+ step3: (token: string) => string
49
+ step4: (token: string) => string
50
+ step5a: (token: string) => string
51
+ step5b: (token: string) => string
30
52
  }
31
53
 
32
54
  export let CarryStemmerFr: Stemmer
@@ -45,3 +67,23 @@ export let PorterStemmerRu: Stemmer
45
67
  export let PorterStemmerSv: Stemmer
46
68
  export let StemmerId: Stemmer
47
69
  export let StemmerJa: Stemmer
70
+
71
+ export declare type TokenCallback = (...args: number[]) => number[] | number
72
+
73
+ export declare class Token {
74
+ vowels: string[] | string
75
+ regions: Record<string, number>
76
+ string: string
77
+ original: string
78
+
79
+ constructor (s: string)
80
+ usingVowels (vowels: string | string[]): Token
81
+ markRegion (region: string, args: number[] | number | null, callback?: TokenCallback, context?: unknown): Token
82
+ replaceAll (find: string, replace: string): Token
83
+ replaceSuffixInRegion (suffix: string, replace: string, region: string): Token
84
+ hasVowelAtIndex (index: number): boolean
85
+ nextVowelIndex (index: number): number
86
+ nextConsonantIndex (index: number): number
87
+ hasSuffix (suffix: string): number
88
+ hasSuffixInRegion (suffix: string, region: string): boolean
89
+ }
@@ -38,3 +38,4 @@ exports.PorterStemmerNl = require('./porter_stemmer_nl')
38
38
  exports.LancasterStemmer = require('./lancaster_stemmer')
39
39
  exports.StemmerJa = require('./stemmer_ja')
40
40
  exports.StemmerId = require('./indonesian/stemmer_id')
41
+ exports.Token = require('./token')