natural 0.5.4 → 0.6.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 (82) hide show
  1. package/.dockerignore +2 -0
  2. package/.npmignore +1 -0
  3. package/.travis.yml +1 -1
  4. package/README.md +412 -89
  5. package/bayes_classifier.json +1 -1
  6. package/classifier.json +418 -0
  7. package/data/kata-dasar.txt +29933 -0
  8. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tagger.js +8 -9
  9. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tester.js +5 -5
  10. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Trainer.js +10 -6
  11. package/lib/natural/brill_pos_tagger/lib/Corpus.js +21 -16
  12. package/lib/natural/brill_pos_tagger/lib/Lexicon.js +22 -13
  13. package/lib/natural/brill_pos_tagger/lib/Predicate.js +2 -2
  14. package/lib/natural/brill_pos_tagger/lib/RuleTemplate.js +2 -2
  15. package/lib/natural/brill_pos_tagger/lib/RuleTemplates.js +132 -146
  16. package/lib/natural/brill_pos_tagger/lib/Sentence.js +41 -0
  17. package/lib/natural/brill_pos_tagger/lib/TF_Parser.js +83 -76
  18. package/lib/natural/brill_pos_tagger/lib/TF_Parser.pegjs +19 -4
  19. package/lib/natural/brill_pos_tagger/lib/TransformationRule.js +10 -11
  20. package/lib/natural/classifiers/classifier.js +15 -8
  21. package/lib/natural/classifiers/maxent/Classifier.js +125 -0
  22. package/lib/natural/classifiers/maxent/Context.js +33 -0
  23. package/lib/natural/classifiers/maxent/Distribution.js +145 -0
  24. package/lib/natural/classifiers/maxent/Element.js +34 -0
  25. package/lib/natural/classifiers/maxent/Feature.js +88 -0
  26. package/lib/natural/classifiers/maxent/FeatureSet.js +67 -0
  27. package/lib/natural/classifiers/maxent/GISScaler.js +148 -0
  28. package/lib/natural/classifiers/maxent/POS/ME_Corpus.js +56 -0
  29. package/lib/natural/classifiers/maxent/POS/ME_Sentence.js +73 -0
  30. package/lib/natural/classifiers/maxent/POS/POS_Element.js +172 -0
  31. package/lib/natural/classifiers/maxent/Sample.js +141 -0
  32. package/lib/natural/classifiers/maxent/SimpleExample/SE_Element.js +49 -0
  33. package/lib/natural/distance/hamming_distance.js +52 -0
  34. package/lib/natural/distance/jaro-winkler_distance.js +16 -6
  35. package/lib/natural/distance/levenshtein_distance.js +164 -11
  36. package/lib/natural/index.js +26 -5
  37. package/lib/natural/inflectors/noun_inflector.js +15 -13
  38. package/lib/natural/ngrams/ngrams.js +83 -25
  39. package/lib/natural/normalizers/normalizer_sv.js +41 -0
  40. package/lib/natural/sentiment/Basque/senticon_eu.json +21557 -0
  41. package/lib/natural/sentiment/Catalan/senticon_ca.json +36352 -0
  42. package/lib/natural/sentiment/Dutch/negations_du.json +3 -0
  43. package/lib/natural/sentiment/Dutch/pattern-sentiment-nl.json +39650 -0
  44. package/lib/natural/sentiment/English/negations_en.json +3 -0
  45. package/lib/natural/sentiment/English/pattern-sentiment-en.json +16024 -0
  46. package/lib/natural/sentiment/English/senticon_en.json +124197 -0
  47. package/lib/natural/sentiment/French/pattern-sentiment-fr.json +40906 -0
  48. package/lib/natural/sentiment/Galician/senticon_gl.json +24427 -0
  49. package/lib/natural/sentiment/Italian/pattern-sentiment-it.json +24522 -0
  50. package/lib/natural/sentiment/SentimentAnalyzer.js +116 -0
  51. package/lib/natural/sentiment/Spanish/afinnShortSortedSpanish.json +1655 -0
  52. package/lib/natural/sentiment/Spanish/negations_es.json +3 -0
  53. package/lib/natural/sentiment/Spanish/senticon_es.json +56722 -0
  54. package/lib/natural/sentiment/tools/README.md +31 -0
  55. package/lib/natural/sentiment/tools/XmlParser4PatternData.js +31 -0
  56. package/lib/natural/sentiment/tools/sentimentXmlParser.js +30 -0
  57. package/lib/natural/stemmers/indonesian/base_stemmer_id.js +86 -0
  58. package/lib/natural/stemmers/indonesian/prefix_rules.js +1027 -0
  59. package/lib/natural/stemmers/indonesian/removal.js +49 -0
  60. package/lib/natural/stemmers/indonesian/stemmer_id.js +307 -0
  61. package/lib/natural/stemmers/indonesian/suffix_rules.js +75 -0
  62. package/lib/natural/stemmers/porter_stemmer_es.js +265 -199
  63. package/lib/natural/stemmers/porter_stemmer_fr.js +1 -1
  64. package/lib/natural/stemmers/porter_stemmer_nl.js +362 -0
  65. package/lib/natural/stemmers/porter_stemmer_sv.js +99 -0
  66. package/lib/natural/stemmers/stemmer_es.js +18 -12
  67. package/lib/natural/stemmers/stemmer_nl.js +64 -0
  68. package/lib/natural/stemmers/stemmer_sv.js +61 -0
  69. package/lib/natural/tfidf/tfidf.js +27 -4
  70. package/lib/natural/tokenizers/aggressive_tokenizer_id.js +44 -0
  71. package/lib/natural/tokenizers/aggressive_tokenizer_sv.js +40 -0
  72. package/lib/natural/tokenizers/aggressive_tokenizer_vi.js +37 -0
  73. package/lib/natural/tokenizers/orthography_matchers.js +33 -0
  74. package/lib/natural/tokenizers/regexp_tokenizer.js +23 -3
  75. package/lib/natural/tokenizers/sentence_tokenizer.js +1 -1
  76. package/lib/natural/util/stopwords_id.js +119 -0
  77. package/lib/natural/util/stopwords_no.js +1 -1
  78. package/lib/natural/util/stopwords_sv.js +70 -0
  79. package/lib/natural/wordnet/wordnet_file.js +7 -1
  80. package/lr_classifier.json +1 -1
  81. package/package-lock.json +378 -0
  82. package/package.json +29 -21
@@ -0,0 +1,49 @@
1
+ /*
2
+ Copyright (c) 2017, Alif Bhaskoro, Andy Librian, R. Kukuh (Reimplemented from https://github.com/sastrawi/sastrawi)
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
+ // a list of commonly used words that have little meaning and can be excluded
24
+ // from analysis.
25
+
26
+ function Removal (original_word, result, removedPart, affixType) {
27
+ this.original_word = original_word;
28
+ this.result = result;
29
+ this.removedPart = removedPart
30
+ this.affixType = affixType;
31
+ }
32
+
33
+ Removal.prototype.getOriginalWord = function() {
34
+ return this.original_word;
35
+ };
36
+
37
+ Removal.prototype.getResult = function() {
38
+ return this.result;
39
+ };
40
+
41
+ Removal.prototype.getRemovedPart = function() {
42
+ return this.removedPart;
43
+ };
44
+
45
+ Removal.prototype.getAffixType = function() {
46
+ return this.affixType;
47
+ };
48
+
49
+ module.exports = Removal;
@@ -0,0 +1,307 @@
1
+ /*
2
+ Copyright (c) 2017, Alif Bhaskoro, Andy Librian, R. Kukuh (Reimplemented from https://github.com/sastrawi/sastrawi)
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 baseStemmer = require('./base_stemmer_id');
24
+ var stemmer = new baseStemmer();
25
+
26
+ // Dictionary
27
+ var dictionary = [];
28
+ loadDictionary();
29
+
30
+ // Rules
31
+ var SuffixRules = require("./suffix_rules");
32
+ var PrefixRules = require("./prefix_rules");
33
+
34
+ var suffix_rules = SuffixRules.rules;
35
+ var prefix_rules = PrefixRules.rules;
36
+
37
+ // Removals
38
+ var removals;
39
+
40
+ // Words
41
+ var original_word;
42
+ var current_word;
43
+
44
+ module.exports = stemmer;
45
+
46
+ // perform full stemming algorithm on a single word
47
+ stemmer.stem = function(token) {
48
+ // Cache stemmer not yet implemented
49
+ // Set to lowercase
50
+ token = token.toLowerCase();
51
+
52
+ //Initiate everything
53
+ removals = [];
54
+
55
+ if(isPlural(token)){
56
+ return stemPluralWord(token);
57
+ }
58
+ else{
59
+ return stemSingularWord(token);
60
+ }
61
+ };
62
+
63
+ // Stem for plural word
64
+ function stemPluralWord(plural_word){
65
+ var matches = plural_word.match(/^(.*)-(.*)$/);
66
+ if(!matches){
67
+ return plural_word;
68
+ }
69
+ words = [matches[1], matches[2]];
70
+
71
+ //malaikat-malaikat-nya -> malaikat malaikat-nya
72
+ suffix = words[1];
73
+ suffixes = ["ku", "mu", "nya", "lah", "kah", "tah", "pun"];
74
+ matches = words[0].match(/^(.*)-(.*)$/);
75
+ if(suffixes.indexOf(suffix) != -1 && matches){
76
+ words[0] = matches[1];
77
+ words[1] = matches[2] + '-' + suffix;
78
+ }
79
+
80
+ //berbalas-balasan -> balas
81
+ rootWord1 = stemSingularWord(words[0]);
82
+ rootWord2 = stemSingularWord(words[1]);
83
+
84
+ //meniru-nirukan -> tiru
85
+ if(!find(words[1]) && rootWord2==words[1]){
86
+ rootWord2 = stemSingularWord("me" + words[1]);
87
+ }
88
+ if(rootWord1==rootWord2){
89
+ return rootWord1;
90
+ }
91
+ else{
92
+ return plural_word;
93
+ }
94
+ }
95
+
96
+ // Stem for singular word
97
+ function stemSingularWord(word){
98
+ original_word = word; // Save the original word for reverting later
99
+ current_word = word;
100
+
101
+ // Step 1
102
+ if(current_word.length>3){
103
+ // Step 2-5
104
+ stemmingProcess();
105
+ }
106
+
107
+ // Step 6
108
+ if(find(current_word)){
109
+ return current_word;
110
+ }
111
+ else{
112
+ return original_word;
113
+ }
114
+ }
115
+
116
+ // Return true if word is in plural form ex: gelas-gelas, else false
117
+ function isPlural(token){
118
+ var matches = token.match(/^(.*)-(ku|mu|nya|lah|kah|tah|pun)$/);
119
+ if(matches){
120
+ return matches[1].search('-') != -1;
121
+ }
122
+ return token.search('-') != -1;
123
+ }
124
+
125
+ // Find certain word in dictionary
126
+ function find(word) {
127
+ return (dictionary.indexOf(word) != -1);
128
+ }
129
+
130
+ function loadDictionary(){
131
+ var fs = require('fs');
132
+ var dirname = __dirname + "/../../../../data/kata-dasar.txt";
133
+ var fin = fs.readFileSync(dirname).toString().split("\n");
134
+ fin.forEach(function (word) {
135
+ if (word) {
136
+ dictionary.push(word.trim());
137
+ }
138
+ });
139
+ }
140
+
141
+ // Stemming from step 2-5
142
+ function stemmingProcess(){
143
+ if(find(current_word))
144
+ return
145
+
146
+ // Confix Stripping
147
+ // Try to remove prefixes first before suffixes if the specification is met
148
+ if(precedenceAdjustmentSpecification(original_word)){
149
+ // Step 4, 5
150
+ removePrefixes();
151
+ if(find(current_word))
152
+ return
153
+
154
+ // Step 2, 3
155
+ removeSuffixes();
156
+ if(find(current_word)){
157
+ return
158
+ }
159
+ else{
160
+ // if the trial is failed, restore the original word
161
+ // and continue to normal rule precedence (suffix first, prefix afterwards)
162
+ current_word = original_word;
163
+ removals = []
164
+ }
165
+ }
166
+
167
+ // Step 2, 3
168
+ removeSuffixes();
169
+ if(find(current_word))
170
+ return
171
+
172
+ // Step 4, 5
173
+ removePrefixes();
174
+ if(find(current_word))
175
+ return
176
+
177
+ //ECS Loop Restore Prefixes
178
+ loopRestorePrefixes();
179
+ }
180
+
181
+ // Remove Suffixes
182
+ function removeSuffixes(){
183
+ for(var i in suffix_rules){
184
+ resultObj = suffix_rules[i](current_word);
185
+
186
+ // Add result to variable
187
+ if(resultObj.removal!=undefined){
188
+ removals.push(resultObj.removal);
189
+ }
190
+ current_word = resultObj.current_word;
191
+
192
+ if(find(current_word))
193
+ return current_word;
194
+ }
195
+ }
196
+
197
+ // Remove Prefixes
198
+ function removePrefixes(){
199
+ for(var i=0; i<3; i++){
200
+ var removalCount = removals.length;
201
+ checkPrefixRules();
202
+ if(find(current_word))
203
+ return current_word;
204
+ }
205
+ }
206
+
207
+ function checkPrefixRules(){
208
+ var removalCount = removals.length;
209
+ var j = 0;
210
+ for(j=0; j<prefix_rules.length; j++){
211
+ resultObj = prefix_rules[j](current_word);
212
+
213
+ // Add result to variable
214
+ if(resultObj.removal!=undefined){
215
+ removals.push(resultObj.removal);
216
+ }
217
+ current_word = resultObj.current_word;
218
+
219
+ if(find(current_word))
220
+ return current_word;
221
+ if(removals.length>removalCount){
222
+ return
223
+ }
224
+ }
225
+ }
226
+
227
+ // Loop Restore Prefixes
228
+ function loopRestorePrefixes(){
229
+ restorePrefix();
230
+
231
+ var reversed_removals = removals.reverse();
232
+ var temp_current_word = current_word;
233
+
234
+ for(var i in reversed_removals){
235
+ current_removal = reversed_removals[i];
236
+
237
+ if(!isSuffixRemovals(current_removal)){
238
+ continue
239
+ }
240
+
241
+ if(current_removal.getRemovedPart() == "kan"){
242
+ current_word = current_removal.getResult() + "k";
243
+
244
+ // Step 4, 5
245
+ removePrefixes();
246
+ if(find(current_word))
247
+ return
248
+ current_word = current_removal.getResult() + "kan";
249
+ }
250
+ else{
251
+ current_word = current_removal.getOriginalWord();
252
+ }
253
+
254
+ // Step 4, 5
255
+ removePrefixes();
256
+ if(find(current_word))
257
+ return
258
+
259
+ current_word = temp_current_word;
260
+ }
261
+ }
262
+
263
+ function isSuffixRemovals(removal){
264
+ var type = removal.getAffixType();
265
+ if(type == "DS" || type == "PP" || type == "P"){
266
+ return true;
267
+ }
268
+ return false;
269
+ }
270
+ function restorePrefix(){
271
+ for(var i=0; i<removals.length; i++){
272
+ current_word = removals[i].getOriginalWord();
273
+ break;
274
+ }
275
+
276
+ for(var i=0; i<removals.length; i++){
277
+ if(removals[i].getAffixType() == "DP"){
278
+ removals.splice(i, 1);
279
+ i--;
280
+ }
281
+ }
282
+ }
283
+
284
+ // Check if word require precedence adjustment or not
285
+ // Adjustment means remove prefix then suffix instead of remove suffix then prefix
286
+ function precedenceAdjustmentSpecification(word){
287
+ var regex_rules = [
288
+ /^be(.*)lah$/,
289
+ /^be(.*)an$/,
290
+ /^me(.*)i$/,
291
+ /^di(.*)i$/,
292
+ /^pe(.*)i$/,
293
+ /^ter(.*)i$/,
294
+ ];
295
+
296
+ for(var i in regex_rules){
297
+ if(word.match(regex_rules[i])){
298
+ return true;
299
+ }
300
+ }
301
+ return false;
302
+ }
303
+
304
+ //exports for tests
305
+ stemmer.isPlural = isPlural;
306
+ stemmer.dictionary = dictionary;
307
+ stemmer.a = suffix_rules[0];
@@ -0,0 +1,75 @@
1
+ /*
2
+ Copyright (c) 2017, Alif Bhaskoro, Andy Librian, R. Kukuh (Reimplemented from https://github.com/sastrawi/sastrawi)
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
+ // a list of commonly used words that have little meaning and can be excluded
24
+ // from analysis.
25
+
26
+ var Removal = require("./removal");
27
+
28
+ SuffixRules = function() {
29
+ var SuffixRules = this;
30
+
31
+ this.removal = undefined;
32
+ this.current_word = undefined;
33
+
34
+ function createResultObject(result, word, type){
35
+ if(result!=word){
36
+ var removedPart = word.replace(result, '');
37
+
38
+ var removal = new Removal(word, result, removedPart, type);
39
+
40
+ this.removal = removal;
41
+ }
42
+ else{
43
+ this.removal = undefined;
44
+ }
45
+ this.current_word = result;
46
+ return this;
47
+ }
48
+
49
+ SuffixRules.RemoveInflectionalParticle = function(word){
50
+ var result = word.replace(/-*(lah|kah|tah|pun)$/, '');
51
+ return createResultObject(result, word, "P");
52
+ }
53
+
54
+ SuffixRules.RemoveInflectionalPossessivePronoun = function(word){
55
+ var result = word.replace(/-*(ku|mu|nya)$/, '');
56
+ return createResultObject(result, word, "PP");
57
+ }
58
+
59
+ SuffixRules.RemoveDerivationalSuffix = function(word){
60
+ var result = word.replace(/(is|isme|isasi|i|kan|an)$/, '');
61
+ return createResultObject(result, word, "DS");
62
+ }
63
+ }
64
+
65
+ module.exports = SuffixRules;
66
+
67
+ // Initalize suffix rules array
68
+ var rules = [];
69
+ var sr = new SuffixRules();
70
+
71
+ rules.push(sr.RemoveInflectionalParticle);
72
+ rules.push(sr.RemoveInflectionalPossessivePronoun);
73
+ rules.push(sr.RemoveDerivationalSuffix);
74
+
75
+ SuffixRules.rules = rules;