natural 0.1.27 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/.editorconfig +7 -0
  2. package/LICENSE.txt +0 -0
  3. package/Makefile +0 -0
  4. package/README.md +237 -24
  5. package/benchmarks/index.js +1 -1
  6. package/benchmarks/metaphone.js +0 -0
  7. package/benchmarks/soundex.js +0 -0
  8. package/examples/classification/basic.js +0 -0
  9. package/examples/classification/recall.js +0 -0
  10. package/examples/classification/store.js +0 -0
  11. package/examples/inflection/count.js +0 -0
  12. package/examples/inflection/noun.js +0 -0
  13. package/examples/inflection/noun_attach.js +0 -0
  14. package/examples/phonetics/compare.js +0 -0
  15. package/examples/phonetics/sounds_like.js +0 -0
  16. package/examples/phonetics/tokenize_and_phoneticize.js +0 -0
  17. package/examples/stemming/stem_corpus.js +0 -0
  18. package/examples/stemming/stem_word.js +0 -0
  19. package/examples/tfidf/array_example.js +0 -0
  20. package/examples/tfidf/multiple_terms.js +0 -0
  21. package/examples/tfidf/tfidf_example.js +0 -0
  22. package/index.js +0 -0
  23. package/io_spec/test_data/tfidf/one +0 -0
  24. package/io_spec/test_data/tfidf/two +0 -0
  25. package/io_spec/tfidf_spec.js +0 -0
  26. package/io_spec/wordnet_spec.js +0 -0
  27. package/lib/natural/analyzers/sentence_analyzer.js +16 -13
  28. package/lib/natural/classifiers/bayes_classifier.js +6 -2
  29. package/lib/natural/classifiers/classifier.js +0 -0
  30. package/lib/natural/classifiers/logistic_regression_classifier.js +0 -0
  31. package/lib/natural/distance/dice_coefficient.js +0 -0
  32. package/lib/natural/distance/jaro-winkler_distance.js +13 -8
  33. package/lib/natural/distance/levenshtein_distance.js +0 -0
  34. package/lib/natural/index.js +8 -0
  35. package/lib/natural/inflectors/count_inflector.js +0 -0
  36. package/lib/natural/inflectors/form_set.js +0 -0
  37. package/lib/natural/inflectors/fr/count_inflector.js +0 -0
  38. package/lib/natural/inflectors/fr/noun_inflector.js +0 -0
  39. package/lib/natural/inflectors/ja/noun_inflector.js +0 -0
  40. package/lib/natural/inflectors/noun_inflector.js +0 -0
  41. package/lib/natural/inflectors/present_verb_inflector.js +1 -0
  42. package/lib/natural/inflectors/singular_plural_inflector.js +1 -2
  43. package/lib/natural/ngrams/ngrams.js +45 -8
  44. package/lib/natural/ngrams/ngrams_zh.js +83 -0
  45. package/lib/natural/normalizers/normalizer.js +30 -38
  46. package/lib/natural/normalizers/normalizer_ja.js +26 -8
  47. package/lib/natural/normalizers/normalizer_no.js +59 -0
  48. package/lib/natural/phonetics/double_metaphone.js +9 -4
  49. package/lib/natural/phonetics/metaphone.js +6 -1
  50. package/lib/natural/phonetics/phonetic.js +0 -0
  51. package/lib/natural/phonetics/soundex.js +11 -7
  52. package/lib/natural/spellcheck/spellcheck.js +80 -0
  53. package/lib/natural/stemmers/lancaster_rules.js +4 -4
  54. package/lib/natural/stemmers/lancaster_stemmer.js +0 -0
  55. package/lib/natural/stemmers/porter_stemmer.js +90 -63
  56. package/lib/natural/stemmers/porter_stemmer_es.js +22 -23
  57. package/lib/natural/stemmers/porter_stemmer_fr.js +377 -0
  58. package/lib/natural/stemmers/porter_stemmer_no.js +155 -0
  59. package/lib/natural/stemmers/porter_stemmer_pt.js +302 -0
  60. package/lib/natural/stemmers/stemmer.js +0 -0
  61. package/lib/natural/stemmers/stemmer_fr.js +58 -0
  62. package/lib/natural/stemmers/stemmer_no.js +61 -0
  63. package/lib/natural/stemmers/stemmer_pt.js +65 -0
  64. package/lib/natural/stemmers/token.js +163 -0
  65. package/lib/natural/tfidf/tfidf.js +50 -19
  66. package/lib/natural/tokenizers/aggressive_tokenizer.js +0 -0
  67. package/lib/natural/tokenizers/aggressive_tokenizer_es.js +0 -0
  68. package/lib/natural/tokenizers/aggressive_tokenizer_fr.js +36 -0
  69. package/lib/natural/tokenizers/aggressive_tokenizer_nl.js +0 -0
  70. package/lib/natural/tokenizers/aggressive_tokenizer_no.js +39 -0
  71. package/lib/natural/tokenizers/aggressive_tokenizer_pt.js +0 -0
  72. package/lib/natural/tokenizers/regexp_tokenizer.js +0 -0
  73. package/lib/natural/tokenizers/tokenizer.js +2 -2
  74. package/lib/natural/tokenizers/treebank_word_tokenizer.js +0 -0
  75. package/lib/natural/trie/trie.js +47 -0
  76. package/lib/natural/util/bag.js +50 -0
  77. package/lib/natural/util/edge_weighted_digraph.js +112 -0
  78. package/lib/natural/util/longest_path_tree.js +99 -0
  79. package/lib/natural/util/shortest_path_tree.js +100 -0
  80. package/lib/natural/util/stopwords.js +0 -0
  81. package/lib/natural/util/stopwords_fr.js +194 -0
  82. package/lib/natural/util/stopwords_nl.js +43 -0
  83. package/lib/natural/util/stopwords_no.js +42 -0
  84. package/lib/natural/util/stopwords_pt.js +136 -0
  85. package/lib/natural/util/topological.js +98 -0
  86. package/lib/natural/wordnet/data_file.js +0 -0
  87. package/lib/natural/wordnet/index_file.js +0 -0
  88. package/lib/natural/wordnet/wordnet.js +0 -0
  89. package/lib/natural/wordnet/wordnet_file.js +0 -0
  90. package/package.json +2 -2
  91. package/spec/aggressive_tokenizer_fr_spec.js +101 -0
  92. package/spec/aggressive_tokenizer_pt_spec.js +0 -0
  93. package/spec/aggressive_tokenizer_spec.js +0 -0
  94. package/spec/bayes_classifier_spec.js +12 -0
  95. package/spec/classifier_spec.js +84 -0
  96. package/spec/count_inflector_fr_spec.js +0 -0
  97. package/spec/count_inflector_spec.js +0 -0
  98. package/spec/dice_coefficient_spec.js +0 -0
  99. package/spec/double_metaphone_spec.js +10 -1
  100. package/spec/jaro-winkler_spec.js +0 -0
  101. package/spec/lancaster_stemmer_spec.js +10 -1
  102. package/spec/levenshtein_spec.js +0 -0
  103. package/spec/logistic_regression_classifier_spec.js +0 -0
  104. package/spec/longest_path_tree_spec.js +102 -0
  105. package/spec/metaphone_spec.js +10 -0
  106. package/spec/ngram_spec.js +61 -0
  107. package/spec/ngram_zh_spec.js +122 -0
  108. package/spec/normalizer_ja_spec.js +9 -3
  109. package/spec/normalizer_no_spec.js +111 -0
  110. package/spec/normalizer_spec.js +26 -5
  111. package/spec/noun_inflector_fr_spec.js +0 -0
  112. package/spec/noun_inflector_ja_spec.js +0 -0
  113. package/spec/noun_inflector_spec.js +0 -0
  114. package/spec/porter_stemmer_fr_spec.js +101 -0
  115. package/spec/porter_stemmer_no_spec.js +143 -0
  116. package/spec/porter_stemmer_pt_spec.js +50 -0
  117. package/spec/porter_stemmer_spec.js +49 -0
  118. package/spec/present_verb_inflector_spec.js +3 -0
  119. package/spec/remove_diacritics_spec.js +31 -0
  120. package/spec/sentence_analyzer_spec.js +248 -0
  121. package/spec/shortest_path_tree_spec.js +132 -0
  122. package/spec/soundex_spec.js +13 -9
  123. package/spec/spellcheck_spec.js +92 -0
  124. package/spec/stemmer_token_spec.js +161 -0
  125. package/spec/test_data/snowball_fr.txt +20402 -0
  126. package/spec/test_data/snowball_no.txt +20628 -0
  127. package/spec/test_data/snowball_pt.txt +32016 -0
  128. package/spec/test_data/tfdif_testcorpus.txt +0 -0
  129. package/spec/test_data/tfidf_teststopwords.txt +0 -0
  130. package/spec/test_data/wordnet_mini.sql +0 -0
  131. package/spec/tfidf_spec.js +153 -28
  132. package/spec/tokenizer_ja_spec.js +0 -0
  133. package/spec/treebank_word_tokenizer_spec.js +0 -0
  134. package/spec/trie_spec.js +27 -3
@@ -0,0 +1,377 @@
1
+ 'use strict';
2
+
3
+ /*
4
+ Copyright (c) 2014, Ismaël Héry
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ */
24
+
25
+ /*
26
+ * Spec for the French Porter Stemmer can be found at:
27
+ * http://snowball.tartarus.org/algorithms/french/stemmer.html
28
+ */
29
+
30
+ var Stemmer = require('./stemmer_fr');
31
+
32
+ var PorterStemmer = new Stemmer();
33
+ module.exports = PorterStemmer;
34
+
35
+ // Export
36
+ PorterStemmer.stem = stem;
37
+
38
+ // Exports for test purpose
39
+ PorterStemmer.prelude = prelude;
40
+ PorterStemmer.regions = regions;
41
+ PorterStemmer.endsinArr = endsinArr;
42
+
43
+ /**
44
+ * Stem a word thanks to Porter Stemmer rules
45
+ * @param {String} token Word to be stemmed
46
+ * @return {String} Stemmed word
47
+ */
48
+ function stem(token) {
49
+ token = prelude(token.toLowerCase());
50
+
51
+ if (token.length == 1)
52
+ return token;
53
+
54
+ var regs = regions(token);
55
+
56
+ var r1_txt, r2_txt, rv_txt;
57
+ r1_txt = token.substring(regs.r1);
58
+ r2_txt = token.substring(regs.r2);
59
+ rv_txt = token.substring(regs.rv);
60
+
61
+ // Step 1
62
+ var beforeStep1 = token;
63
+ var suf, pref2, pref3, letterBefore, letter2Before, i;
64
+ var doStep2a = false;
65
+
66
+ if ((suf = endsinArr(r2_txt, ['ance', 'iqUe', 'isme', 'able', 'iste', 'eux', 'ances', 'iqUes', 'ismes', 'ables', 'istes'])) != '') {
67
+ token = token.slice(0, -suf.length); // delete
68
+ } else if ((suf = endsinArr(token, ['icatrice', 'icateur', 'ication', 'icatrices', 'icateurs', 'ications'])) != '') {
69
+ if (endsinArr(r2_txt, ['icatrice', 'icateur', 'ication', 'icatrices', 'icateurs', 'ications']) != '') {
70
+ token = token.slice(0, -suf.length); // delete
71
+ } else {
72
+ token = token.slice(0, -suf.length) + 'iqU'; // replace by iqU
73
+ }
74
+ } else if ((suf = endsinArr(r2_txt, ['atrice', 'ateur', 'ation', 'atrices', 'ateurs', 'ations'])) != '') {
75
+ token = token.slice(0, -suf.length); // delete
76
+ } else if ((suf = endsinArr(r2_txt, ['logie', 'logies'])) != '') {
77
+ token = token.slice(0, -suf.length) + 'log'; // replace with log
78
+ } else if ((suf = endsinArr(r2_txt, ['usion', 'ution', 'usions', 'utions'])) != '') {
79
+ token = token.slice(0, -suf.length) + 'u'; // replace with u
80
+ } else if ((suf = endsinArr(r2_txt, ['ence', 'ences'])) != '') {
81
+ token = token.slice(0, -suf.length) + 'ent'; // replace with ent
82
+ }
83
+ // ement(s)
84
+ else if ((suf = endsinArr(r1_txt, ['issement', 'issements'])) != '') {
85
+ if (!isVowel(token[token.length - suf.length - 1])) {
86
+ token = token.slice(0, -suf.length); // delete
87
+ r1_txt = token.substring(regs.r1);
88
+ r2_txt = token.substring(regs.r2);
89
+ rv_txt = token.substring(regs.rv);
90
+ }
91
+ } else if ((suf = endsinArr(r2_txt, ['ativement', 'ativements'])) != '') {
92
+ token = token.slice(0, -suf.length); // delete
93
+ } else if ((suf = endsinArr(r2_txt, ['ivement', 'ivements'])) != '') {
94
+ token = token.slice(0, -suf.length); // delete
95
+ } else if ((suf = endsinArr(token, ['eusement', 'eusements'])) != '') {
96
+ if ((suf = endsinArr(r2_txt, ['eusement', 'eusements'])) != '')
97
+ token = token.slice(0, -suf.length); // delete
98
+ else if ((suf = endsinArr(r1_txt, ['eusement', 'eusements'])) != '')
99
+ token = token.slice(0, -suf.length) + 'eux'; // replace by eux
100
+ else if ((suf = endsinArr(rv_txt, ['ement', 'ements'])) != '')
101
+ token = token.slice(0, -suf.length); // delete
102
+ } else if ((suf = endsinArr(r2_txt, ['ablement', 'ablements', 'iqUement', 'iqUements'])) != '') {
103
+ token = token.slice(0, -suf.length); // delete
104
+ } else if ((suf = endsinArr(rv_txt, ['ièrement', 'ièrements', 'Ièrement', 'Ièrements'])) != '') {
105
+ token = token.slice(0, -suf.length) + 'i'; // replace by i
106
+ } else if ((suf = endsinArr(rv_txt, ['ement', 'ements'])) != '') {
107
+ token = token.slice(0, -suf.length); // delete
108
+ }
109
+ // ité(s)
110
+ else if ((suf = endsinArr(token, ['icité', 'icités'])) != '') {
111
+ if (endsinArr(r2_txt, ['icité', 'icités']) != '')
112
+ token = token.slice(0, -suf.length); // delete
113
+ else
114
+ token = token.slice(0, -suf.length) + 'iqU'; // replace by iqU
115
+ } else if ((suf = endsinArr(token, ['abilité', 'abilités'])) != '') {
116
+ if (endsinArr(r2_txt, ['abilité', 'abilités']) != '')
117
+ token = token.slice(0, -suf.length); // delete
118
+ else
119
+ token = token.slice(0, -suf.length) + 'abl'; // replace by abl
120
+ } else if ((suf = endsinArr(r2_txt, ['ité', 'ités'])) != '') {
121
+ token = token.slice(0, -suf.length); // delete if in R2
122
+ } else if ((suf = endsinArr(token, ['icatif', 'icative', 'icatifs', 'icatives'])) != '') {
123
+ if ((suf = endsinArr(r2_txt, ['icatif', 'icative', 'icatifs', 'icatives'])) != '') {
124
+ token = token.slice(0, -suf.length); // delete
125
+ r2_txt = token.substring(regs.r2);
126
+ rv_txt = token.substring(regs.rv);
127
+ }
128
+ if ((suf = endsinArr(r2_txt, ['atif', 'ative', 'atifs', 'atives'])) != '') {
129
+ token = token.slice(0, -suf.length - 2) + 'iqU'; // replace with iqU
130
+ r2_txt = token.substring(regs.r2);
131
+ rv_txt = token.substring(regs.rv);
132
+ }
133
+ } else if ((suf = endsinArr(r2_txt, ['atif', 'ative', 'atifs', 'atives'])) != '') {
134
+ token = token.slice(0, -suf.length); // delete
135
+ } else if ((suf = endsinArr(r2_txt, ['if', 'ive', 'ifs', 'ives'])) != '') {
136
+ token = token.slice(0, -suf.length); // delete
137
+ } else if ((suf = endsinArr(token, ['eaux'])) != '') {
138
+ token = token.slice(0, -suf.length) + 'eau'; // replace by eau
139
+ } else if ((suf = endsinArr(r1_txt, ['aux'])) != '') {
140
+ token = token.slice(0, -suf.length) + 'al'; // replace by al
141
+ } else if ((suf = endsinArr(r2_txt, ['euse', 'euses'])) != '') {
142
+ token = token.slice(0, -suf.length); // delete
143
+ } else if ((suf = endsinArr(r1_txt, ['euse', 'euses'])) != '') {
144
+ token = token.slice(0, -suf.length) + 'eux'; // replace by eux
145
+ } else if ((suf = endsinArr(rv_txt, ['amment'])) != '') {
146
+ token = token.slice(0, -suf.length) + 'ant'; // replace by ant
147
+ doStep2a = true;
148
+ } else if ((suf = endsinArr(rv_txt, ['emment'])) != '') {
149
+ token = token.slice(0, -suf.length) + 'ent'; // replace by ent
150
+ doStep2a = true;
151
+ } else if ((suf = endsinArr(rv_txt, ['ment', 'ments'])) != '') {
152
+ // letter before must be a vowel in RV
153
+ letterBefore = token[token.length - suf.length - 1];
154
+ if (isVowel(letterBefore) && endsin(rv_txt, letterBefore + suf)) {
155
+ token = token.slice(0, -suf.length); // delete
156
+ doStep2a = true;
157
+ }
158
+ }
159
+
160
+ // re compute regions
161
+ r1_txt = token.substring(regs.r1);
162
+ r2_txt = token.substring(regs.r2);
163
+ rv_txt = token.substring(regs.rv);
164
+
165
+ // Step 2a
166
+ var beforeStep2a = token;
167
+ var step2aDone = false;
168
+ if (beforeStep1 === token || doStep2a) {
169
+ step2aDone = true;
170
+ if ((suf = endsinArr(rv_txt, ['îmes', 'ît', 'îtes', 'i', 'ie', 'Ie', 'ies', 'ir', 'ira', 'irai', 'iraIent', 'irais', 'irait', 'iras', 'irent', 'irez', 'iriez', 'irions', 'irons', 'iront', 'is', 'issaIent', 'issais', 'issait', 'issant', 'issante', 'issantes', 'issants', 'isse', 'issent', 'isses', 'issez', 'issiez', 'issions', 'issons', 'it'])) != '') {
171
+ letterBefore = token[token.length - suf.length - 1];
172
+ if (!isVowel(letterBefore) && endsin(rv_txt, letterBefore + suf))
173
+ token = token.slice(0, -suf.length); // delete
174
+ }
175
+ }
176
+
177
+ // Step 2b
178
+ if (step2aDone && token === beforeStep2a) {
179
+ if ((suf = endsinArr(rv_txt, ['é', 'ée', 'ées', 'és', 'èrent', 'er', 'era', 'erai', 'eraIent', 'erais', 'erait', 'eras', 'erez', 'eriez', 'erions', 'erons', 'eront', 'ez', 'iez', 'Iez'])) != '') {
180
+ token = token.slice(0, -suf.length); // delete
181
+ r2_txt = token.substring(regs.r2);
182
+ rv_txt = token.substring(regs.rv);
183
+ } else if ((suf = endsinArr(rv_txt, ['ions'])) != '' && endsinArr(r2_txt, ['ions'])) {
184
+ token = token.slice(0, -suf.length); // delete
185
+ r2_txt = token.substring(regs.r2);
186
+ rv_txt = token.substring(regs.rv);
187
+ }
188
+ // add 'Ie' suffix to pass test for 'évanouie'
189
+ else if ((suf = endsinArr(rv_txt, ['âmes', 'ât', 'âtes', 'a', 'ai', 'aIent', 'ais', 'ait', 'ant', 'ante', 'antes', 'ants', 'as', 'asse', 'assent', 'asses', 'assiez', 'assions'])) != '') {
190
+ token = token.slice(0, -suf.length); // delete
191
+
192
+ letterBefore = token[token.length - 1];
193
+ if (letterBefore === 'e' && endsin(rv_txt, 'e' + suf))
194
+ token = token.slice(0, -1);
195
+
196
+ r2_txt = token.substring(regs.r2);
197
+ rv_txt = token.substring(regs.rv);
198
+ }
199
+ }
200
+
201
+ // Step 3
202
+ if (!(token === beforeStep1)) {
203
+ if (token[token.length - 1] === 'Y')
204
+ token = token.slice(0, -1) + 'i';
205
+ if (token[token.length - 1] === 'ç')
206
+ token = token.slice(0, -1) + 'c';
207
+ } // Step 4
208
+ else {
209
+ letterBefore = token[token.length - 1];
210
+ letter2Before = token[token.length - 2];
211
+
212
+ if (letterBefore === 's' && ['a', 'i', 'o', 'u', 'è', 's'].indexOf(letter2Before) == -1) {
213
+ token = token.slice(0, -1);
214
+ r1_txt = token.substring(regs.r1);
215
+ r2_txt = token.substring(regs.r2);
216
+ rv_txt = token.substring(regs.rv);
217
+ }
218
+
219
+ if ((suf = endsinArr(r2_txt, ['ion'])) != '') {
220
+ letterBefore = token[token.length - suf.length - 1];
221
+ if (letterBefore === 's' || letterBefore === 't') {
222
+ token = token.slice(0, -suf.length); // delete
223
+ r1_txt = token.substring(regs.r1);
224
+ r2_txt = token.substring(regs.r2);
225
+ rv_txt = token.substring(regs.rv);
226
+ }
227
+ }
228
+
229
+ if ((suf = endsinArr(rv_txt, ['ier', 'ière', 'Ier', 'Ière'])) != '') {
230
+ token = token.slice(0, -suf.length) + 'i'; // replace by i
231
+ r1_txt = token.substring(regs.r1);
232
+ r2_txt = token.substring(regs.r2);
233
+ rv_txt = token.substring(regs.rv);
234
+ }
235
+ if ((suf = endsinArr(rv_txt, 'e')) != '') {
236
+ token = token.slice(0, -suf.length); // delete
237
+ r1_txt = token.substring(regs.r1);
238
+ r2_txt = token.substring(regs.r2);
239
+ rv_txt = token.substring(regs.rv);
240
+ }
241
+ if ((suf = endsinArr(rv_txt, 'ë')) != '') {
242
+ if (token.slice(token.length - 3, -1) === 'gu')
243
+ token = token.slice(0, -suf.length); // delete
244
+ }
245
+ }
246
+
247
+ // Step 5
248
+ if ((suf = endsinArr(token, ['enn', 'onn', 'ett', 'ell', 'eill'])) != '') {
249
+ token = token.slice(0, -1); // delete last letter
250
+ }
251
+
252
+ // Step 6
253
+ i = token.length - 1;
254
+ while (i > 0) {
255
+ if (!isVowel(token[i])) {
256
+ i--;
257
+ } else if (i !== token.length - 1 && (token[i] === 'é' || token[i] === 'è')) {
258
+ token = token.substring(0, i) + 'e' + token.substring(i + 1, token.length);
259
+ break;
260
+ } else {
261
+ break;
262
+ }
263
+ }
264
+
265
+ return token.toLowerCase();
266
+
267
+ };
268
+
269
+ /**
270
+ * Compute r1, r2, rv regions as required by french porter stemmer algorithm
271
+ * @param {String} token Word to compute regions on
272
+ * @return {Object} Regions r1, r2, rv as offsets from the begining of the word
273
+ */
274
+ function regions(token) {
275
+ var r1, r2, rv, len;
276
+ var i;
277
+
278
+ r1 = r2 = rv = len = token.length;
279
+
280
+ // R1 is the region after the first non-vowel following a vowel,
281
+ for (var i = 0; i < len - 1 && r1 == len; i++) {
282
+ if (isVowel(token[i]) && !isVowel(token[i + 1])) {
283
+ r1 = i + 2;
284
+ }
285
+ }
286
+ // Or is the null region at the end of the word if there is no such non-vowel.
287
+
288
+ // R2 is the region after the first non-vowel following a vowel in R1
289
+ for (i = r1; i < len - 1 && r2 == len; i++) {
290
+ if (isVowel(token[i]) && !isVowel(token[i + 1])) {
291
+ r2 = i + 2;
292
+ }
293
+ }
294
+ // Or is the null region at the end of the word if there is no such non-vowel.
295
+
296
+ // RV region
297
+ var three = token.slice(0, 3);
298
+ if (isVowel(token[0]) && isVowel(token[1])) {
299
+ rv = 3;
300
+ }
301
+ if (three === 'par' || three == 'col' || three === 'tap')
302
+ rv = 3;
303
+ // the region after the first vowel not at the beginning of the word or null
304
+ else {
305
+ for (i = 1; i < len - 1 && rv == len; i++) {
306
+ if (isVowel(token[i])) {
307
+ rv = i + 1;
308
+ }
309
+ }
310
+ }
311
+
312
+ return {
313
+ r1: r1,
314
+ r2: r2,
315
+ rv: rv
316
+ };
317
+ };
318
+
319
+ /**
320
+ * Pre-process/prepare words as required by french porter stemmer algorithm
321
+ * @param {String} token Word to be prepared
322
+ * @return {String} Prepared word
323
+ */
324
+ function prelude(token) {
325
+ token = token.toLowerCase();
326
+
327
+ var result = '';
328
+ var i = 0;
329
+
330
+ // special case for i = 0 to avoid '-1' index
331
+ if (token[i] === 'y' && isVowel(token[i + 1])) {
332
+ result += token[i].toUpperCase();
333
+ } else {
334
+ result += token[i];
335
+ }
336
+
337
+ for (i = 1; i < token.length; i++) {
338
+ if ((token[i] === 'u' || token[i] === 'i') && isVowel(token[i - 1]) && isVowel(token[i + 1])) {
339
+ result += token[i].toUpperCase();
340
+ } else if (token[i] === 'y' && (isVowel(token[i - 1]) || isVowel(token[i + 1]))) {
341
+ result += token[i].toUpperCase();
342
+ } else if (token[i] === 'u' && token[i - 1] === 'q') {
343
+ result += token[i].toUpperCase();
344
+ } else {
345
+ result += token[i];
346
+ }
347
+ }
348
+
349
+ return result;
350
+ };
351
+
352
+ /**
353
+ * Return longest matching suffixes for a token or '' if no suffix match
354
+ * @param {String} token Word to find matching suffix
355
+ * @param {Array} suffixes Array of suffixes to test matching
356
+ * @return {String} Longest found matching suffix or ''
357
+ */
358
+ function endsinArr(token, suffixes) {
359
+ var i, longest = '';
360
+ for (i = 0; i < suffixes.length; i++) {
361
+ if (endsin(token, suffixes[i]) && suffixes[i].length > longest.length)
362
+ longest = suffixes[i];
363
+ }
364
+
365
+ return longest;
366
+ };
367
+
368
+
369
+ function isVowel(letter) {
370
+ return (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u' || letter == 'y' || letter == 'â' || letter == 'à' || letter == 'ë' ||
371
+ letter == 'é' || letter == 'ê' || letter == 'è' || letter == 'ï' || letter == 'î' || letter == 'ô' || letter == 'û' || letter == 'ù');
372
+ };
373
+
374
+ function endsin(token, suffix) {
375
+ if (token.length < suffix.length) return false;
376
+ return (token.slice(-suffix.length) == suffix);
377
+ };
@@ -0,0 +1,155 @@
1
+ /*
2
+ Copyright (c) 2014, Kristoffer Brabrand
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
+ var Stemmer = require('./stemmer_no');
24
+
25
+ // Get the part of the token after the first non-vowel following a vowel
26
+ function getR1(token) {
27
+ var match = token.match(/[aeiouyæåø]{1}[^aeiouyæåø]([A-Za-z0-9_æøåÆØÅäÄöÖüÜ]+)/);
28
+
29
+ if (match) {
30
+ var preR1Length = match.index + 2;
31
+
32
+ if (preR1Length < 3 && preR1Length > 0) {
33
+ return token.slice(3);
34
+ } else if (preR1Length >= 3) {
35
+ return match[1];
36
+ } else {
37
+ return token;
38
+ }
39
+ }
40
+
41
+ return null;
42
+ }
43
+
44
+ function step1(token) {
45
+ // Perform step 1a-c
46
+ var step1aResult = step1a(token),
47
+ step1bResult = step1b(token),
48
+ step1cResult = step1c(token);
49
+
50
+ // Returne the shortest result string (from 1a, 1b and 1c)
51
+ if (step1aResult.length < step1bResult.length) {
52
+ return (step1aResult.length < step1cResult.length) ? step1aResult : step1cResult;
53
+ } else {
54
+ return (step1bResult.length < step1cResult.length) ? step1bResult : step1cResult;
55
+ }
56
+ }
57
+
58
+ // step 1a as defined for the porter stemmer algorithm.
59
+ function step1a(token) {
60
+ var r1 = getR1(token);
61
+
62
+ if (!r1) {
63
+ return token;
64
+ }
65
+
66
+ var r1Match = r1.match(/(a|e|ede|ande|ende|ane|ene|hetene|en|heten|ar|er|heter|as|es|edes|endes|enes|hetenes|ens|hetens|ers|ets|et|het|ast)$/);
67
+
68
+ if (r1Match) {
69
+ return token.replace(new RegExp(r1Match[1] + '$'), '');
70
+ }
71
+
72
+ return token;
73
+ }
74
+
75
+ // step 1b as defined for the porter stemmer algorithm.
76
+ function step1b(token) {
77
+ var r1 = getR1(token);
78
+
79
+ if (!r1) {
80
+ return token;
81
+ }
82
+
83
+ if (token.match(/(b|c|d|f|g|h|j|l|m|n|o|p|r|t|v|y|z)s$/)) {
84
+ return token.slice(0, -1);
85
+ }
86
+
87
+ if (token.match(/([^aeiouyæåø]k)s$/)) {
88
+ return token.slice(0, -1);
89
+ }
90
+
91
+ return token;
92
+ }
93
+
94
+ // step 1c as defined for the porter stemmer algorithm.
95
+ function step1c(token) {
96
+ var r1 = getR1(token);
97
+
98
+ if (!r1) {
99
+ return token;
100
+ }
101
+
102
+ if (r1.match(/(erte|ert)$/)) {
103
+ return token.replace(/(erte|ert)$/, 'er');
104
+ }
105
+
106
+ return token;
107
+ }
108
+
109
+ // step 2 as defined for the porter stemmer algorithm.
110
+ function step2(token) {
111
+ var r1 = getR1(token);
112
+
113
+ if (!r1) {
114
+ return token;
115
+ }
116
+
117
+ if (r1.match(/(d|v)t$/)) {
118
+ return token.slice(0, -1);
119
+ }
120
+
121
+ return token;
122
+ }
123
+
124
+ // step 3 as defined for the porter stemmer algorithm.
125
+ function step3(token) {
126
+ var r1 = getR1(token);
127
+
128
+ if (!r1)
129
+ return token;
130
+
131
+ var r1Match = r1.match(/(leg|eleg|ig|eig|lig|elig|els|lov|elov|slov|hetslov)$/);
132
+
133
+ if (r1Match) {
134
+ return token.replace(new RegExp(r1Match[1] + '$'), '');
135
+ }
136
+
137
+ return token;
138
+ }
139
+
140
+ var PorterStemmer = new Stemmer();
141
+ module.exports = PorterStemmer;
142
+
143
+ // perform full stemming algorithm on a single word
144
+ PorterStemmer.stem = function(token) {
145
+ return step3(step2(step1(token.toLowerCase()))).toString();
146
+ };
147
+
148
+ //exports for tests
149
+ PorterStemmer.getR1 = getR1;
150
+ PorterStemmer.step1 = step1;
151
+ PorterStemmer.step1a = step1a;
152
+ PorterStemmer.step1b = step1b;
153
+ PorterStemmer.step1c = step1c;
154
+ PorterStemmer.step2 = step2;
155
+ PorterStemmer.step3 = step3;