natural 0.5.6 → 0.6.3

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 (80) hide show
  1. package/.dockerignore +2 -0
  2. package/.idea/misc.xml +6 -0
  3. package/.idea/modules.xml +8 -0
  4. package/.idea/natural.iml +12 -0
  5. package/.idea/vcs.xml +6 -0
  6. package/.idea/workspace.xml +511 -0
  7. package/.travis.yml +8 -1
  8. package/README.md +331 -40
  9. package/bayes_classifier.json +1 -1
  10. package/classifier.json +418 -0
  11. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tagger.js +8 -9
  12. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tester.js +5 -5
  13. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Trainer.js +10 -6
  14. package/lib/natural/brill_pos_tagger/lib/Corpus.js +21 -16
  15. package/lib/natural/brill_pos_tagger/lib/Lexicon.js +22 -13
  16. package/lib/natural/brill_pos_tagger/lib/Predicate.js +2 -2
  17. package/lib/natural/brill_pos_tagger/lib/RuleTemplate.js +2 -2
  18. package/lib/natural/brill_pos_tagger/lib/RuleTemplates.js +132 -146
  19. package/lib/natural/brill_pos_tagger/lib/Sentence.js +41 -0
  20. package/lib/natural/brill_pos_tagger/lib/TransformationRule.js +10 -11
  21. package/lib/natural/classifiers/classifier.js +5 -3
  22. package/lib/natural/classifiers/maxent/Classifier.js +125 -0
  23. package/lib/natural/classifiers/maxent/Context.js +33 -0
  24. package/lib/natural/classifiers/maxent/Distribution.js +145 -0
  25. package/lib/natural/classifiers/maxent/Element.js +34 -0
  26. package/lib/natural/classifiers/maxent/Feature.js +88 -0
  27. package/lib/natural/classifiers/maxent/FeatureSet.js +67 -0
  28. package/lib/natural/classifiers/maxent/GISScaler.js +148 -0
  29. package/lib/natural/classifiers/maxent/POS/ME_Corpus.js +56 -0
  30. package/lib/natural/classifiers/maxent/POS/ME_Sentence.js +73 -0
  31. package/lib/natural/classifiers/maxent/POS/POS_Element.js +172 -0
  32. package/lib/natural/classifiers/maxent/Sample.js +141 -0
  33. package/lib/natural/classifiers/maxent/SimpleExample/SE_Element.js +49 -0
  34. package/lib/natural/distance/hamming_distance.js +52 -0
  35. package/lib/natural/distance/jaro-winkler_distance.js +16 -6
  36. package/lib/natural/distance/levenshtein_distance.js +2 -2
  37. package/lib/natural/index.js +21 -2
  38. package/lib/natural/inflectors/noun_inflector.js +5 -3
  39. package/lib/natural/ngrams/ngrams.js +83 -25
  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/data/kata-dasar.json +29935 -0
  58. package/lib/natural/stemmers/indonesian/prefix_rules.js +10 -9
  59. package/lib/natural/stemmers/indonesian/stemmer_id.js +16 -15
  60. package/lib/natural/stemmers/indonesian/suffix_rules.js +2 -2
  61. package/lib/natural/stemmers/porter_stemmer_es.js +265 -199
  62. package/lib/natural/stemmers/porter_stemmer_fr.js +1 -1
  63. package/lib/natural/stemmers/porter_stemmer_nl.js +362 -0
  64. package/lib/natural/stemmers/stemmer_es.js +18 -12
  65. package/lib/natural/stemmers/stemmer_nl.js +64 -0
  66. package/lib/natural/tfidf/tfidf.js +27 -4
  67. package/lib/natural/tokenizers/aggressive_tokenizer_sv.js +1 -1
  68. package/lib/natural/tokenizers/aggressive_tokenizer_vi.js +37 -0
  69. package/lib/natural/tokenizers/orthography_matchers.js +33 -0
  70. package/lib/natural/tokenizers/regexp_tokenizer.js +24 -4
  71. package/lib/natural/tokenizers/sentence_tokenizer.js +1 -1
  72. package/lib/natural/wordnet/wordnet_file.js +7 -1
  73. package/lr_classifier.json +1 -1
  74. package/package.json +35 -23
  75. package/.npmignore +0 -4
  76. package/.vscode/temp.sql +0 -0
  77. package/Makefile +0 -38
  78. package/data/kata-dasar.txt +0 -29933
  79. package/package-lock.json +0 -79
  80. package/tests/stemmer_id_test.js +0 -361
@@ -0,0 +1,418 @@
1
+ {
2
+ "features": {
3
+ "features": [
4
+ {
5
+ "name": "isZero",
6
+ "parameters": [
7
+ "0"
8
+ ],
9
+ "parametersKey": "0",
10
+ "observedExpect": 0.3
11
+ },
12
+ {
13
+ "name": "isOne",
14
+ "parameters": [
15
+ "1"
16
+ ],
17
+ "parametersKey": "1",
18
+ "observedExpect": 0.3
19
+ },
20
+ {
21
+ "name": "Correction feature",
22
+ "parameters": [],
23
+ "parametersKey": "",
24
+ "observedExpect": 0.4
25
+ }
26
+ ],
27
+ "map": {
28
+ "isZero | 0": true,
29
+ "isOne | 1": true,
30
+ "Correction feature | ": true
31
+ }
32
+ },
33
+ "sample": {
34
+ "frequencyOfContext": {
35
+ "\"0\"": 6,
36
+ "\"1\"": 4
37
+ },
38
+ "frequency": {
39
+ "x\"0\"": 3,
40
+ "y\"0\"": 3,
41
+ "x\"1\"": 1,
42
+ "y\"1\"": 3
43
+ },
44
+ "classes": [
45
+ "x",
46
+ "y"
47
+ ],
48
+ "elements": [
49
+ {
50
+ "a": "x",
51
+ "b": {
52
+ "data": "0",
53
+ "key": "\"0\""
54
+ },
55
+ "key": "x\"0\""
56
+ },
57
+ {
58
+ "a": "x",
59
+ "b": {
60
+ "data": "0",
61
+ "key": "\"0\""
62
+ },
63
+ "key": "x\"0\""
64
+ },
65
+ {
66
+ "a": "x",
67
+ "b": {
68
+ "data": "0",
69
+ "key": "\"0\""
70
+ },
71
+ "key": "x\"0\""
72
+ },
73
+ {
74
+ "a": "y",
75
+ "b": {
76
+ "data": "0",
77
+ "key": "\"0\""
78
+ },
79
+ "key": "y\"0\""
80
+ },
81
+ {
82
+ "a": "y",
83
+ "b": {
84
+ "data": "0",
85
+ "key": "\"0\""
86
+ },
87
+ "key": "y\"0\""
88
+ },
89
+ {
90
+ "a": "y",
91
+ "b": {
92
+ "data": "0",
93
+ "key": "\"0\""
94
+ },
95
+ "key": "y\"0\""
96
+ },
97
+ {
98
+ "a": "x",
99
+ "b": {
100
+ "data": "1",
101
+ "key": "\"1\""
102
+ },
103
+ "key": "x\"1\""
104
+ },
105
+ {
106
+ "a": "y",
107
+ "b": {
108
+ "data": "1",
109
+ "key": "\"1\""
110
+ },
111
+ "key": "y\"1\""
112
+ },
113
+ {
114
+ "a": "y",
115
+ "b": {
116
+ "data": "1",
117
+ "key": "\"1\""
118
+ },
119
+ "key": "y\"1\""
120
+ },
121
+ {
122
+ "a": "y",
123
+ "b": {
124
+ "data": "1",
125
+ "key": "\"1\""
126
+ },
127
+ "key": "y\"1\""
128
+ }
129
+ ]
130
+ },
131
+ "scaler": {
132
+ "featureSet": {
133
+ "features": [
134
+ {
135
+ "name": "isZero",
136
+ "parameters": [
137
+ "0"
138
+ ],
139
+ "parametersKey": "0",
140
+ "observedExpect": 0.3
141
+ },
142
+ {
143
+ "name": "isOne",
144
+ "parameters": [
145
+ "1"
146
+ ],
147
+ "parametersKey": "1",
148
+ "observedExpect": 0.3
149
+ },
150
+ {
151
+ "name": "Correction feature",
152
+ "parameters": [],
153
+ "parametersKey": "",
154
+ "observedExpect": 0.4
155
+ }
156
+ ],
157
+ "map": {
158
+ "isZero | 0": true,
159
+ "isOne | 1": true,
160
+ "Correction feature | ": true
161
+ }
162
+ },
163
+ "sample": {
164
+ "frequencyOfContext": {
165
+ "\"0\"": 6,
166
+ "\"1\"": 4
167
+ },
168
+ "frequency": {
169
+ "x\"0\"": 3,
170
+ "y\"0\"": 3,
171
+ "x\"1\"": 1,
172
+ "y\"1\"": 3
173
+ },
174
+ "classes": [
175
+ "x",
176
+ "y"
177
+ ],
178
+ "elements": [
179
+ {
180
+ "a": "x",
181
+ "b": {
182
+ "data": "0",
183
+ "key": "\"0\""
184
+ },
185
+ "key": "x\"0\""
186
+ },
187
+ {
188
+ "a": "x",
189
+ "b": {
190
+ "data": "0",
191
+ "key": "\"0\""
192
+ },
193
+ "key": "x\"0\""
194
+ },
195
+ {
196
+ "a": "x",
197
+ "b": {
198
+ "data": "0",
199
+ "key": "\"0\""
200
+ },
201
+ "key": "x\"0\""
202
+ },
203
+ {
204
+ "a": "y",
205
+ "b": {
206
+ "data": "0",
207
+ "key": "\"0\""
208
+ },
209
+ "key": "y\"0\""
210
+ },
211
+ {
212
+ "a": "y",
213
+ "b": {
214
+ "data": "0",
215
+ "key": "\"0\""
216
+ },
217
+ "key": "y\"0\""
218
+ },
219
+ {
220
+ "a": "y",
221
+ "b": {
222
+ "data": "0",
223
+ "key": "\"0\""
224
+ },
225
+ "key": "y\"0\""
226
+ },
227
+ {
228
+ "a": "x",
229
+ "b": {
230
+ "data": "1",
231
+ "key": "\"1\""
232
+ },
233
+ "key": "x\"1\""
234
+ },
235
+ {
236
+ "a": "y",
237
+ "b": {
238
+ "data": "1",
239
+ "key": "\"1\""
240
+ },
241
+ "key": "y\"1\""
242
+ },
243
+ {
244
+ "a": "y",
245
+ "b": {
246
+ "data": "1",
247
+ "key": "\"1\""
248
+ },
249
+ "key": "y\"1\""
250
+ },
251
+ {
252
+ "a": "y",
253
+ "b": {
254
+ "data": "1",
255
+ "key": "\"1\""
256
+ },
257
+ "key": "y\"1\""
258
+ }
259
+ ]
260
+ },
261
+ "iteration": 3,
262
+ "improvement": 0.008269762368828815,
263
+ "C": 1,
264
+ "featureSums": {
265
+ "x\"0\"": 1,
266
+ "y\"0\"": 0,
267
+ "x\"1\"": 0,
268
+ "y\"1\"": 1
269
+ }
270
+ },
271
+ "p": {
272
+ "alpha": [
273
+ 0.8442857142857142,
274
+ 1.8851785714285711,
275
+ 0.777505117061545
276
+ ],
277
+ "featureSet": {
278
+ "features": [
279
+ {
280
+ "name": "isZero",
281
+ "parameters": [
282
+ "0"
283
+ ],
284
+ "parametersKey": "0",
285
+ "observedExpect": 0.3
286
+ },
287
+ {
288
+ "name": "isOne",
289
+ "parameters": [
290
+ "1"
291
+ ],
292
+ "parametersKey": "1",
293
+ "observedExpect": 0.3
294
+ },
295
+ {
296
+ "name": "Correction feature",
297
+ "parameters": [],
298
+ "parametersKey": "",
299
+ "observedExpect": 0.4
300
+ }
301
+ ],
302
+ "map": {
303
+ "isZero | 0": true,
304
+ "isOne | 1": true,
305
+ "Correction feature | ": true
306
+ }
307
+ },
308
+ "sample": {
309
+ "frequencyOfContext": {
310
+ "\"0\"": 6,
311
+ "\"1\"": 4
312
+ },
313
+ "frequency": {
314
+ "x\"0\"": 3,
315
+ "y\"0\"": 3,
316
+ "x\"1\"": 1,
317
+ "y\"1\"": 3
318
+ },
319
+ "classes": [
320
+ "x",
321
+ "y"
322
+ ],
323
+ "elements": [
324
+ {
325
+ "a": "x",
326
+ "b": {
327
+ "data": "0",
328
+ "key": "\"0\""
329
+ },
330
+ "key": "x\"0\""
331
+ },
332
+ {
333
+ "a": "x",
334
+ "b": {
335
+ "data": "0",
336
+ "key": "\"0\""
337
+ },
338
+ "key": "x\"0\""
339
+ },
340
+ {
341
+ "a": "x",
342
+ "b": {
343
+ "data": "0",
344
+ "key": "\"0\""
345
+ },
346
+ "key": "x\"0\""
347
+ },
348
+ {
349
+ "a": "y",
350
+ "b": {
351
+ "data": "0",
352
+ "key": "\"0\""
353
+ },
354
+ "key": "y\"0\""
355
+ },
356
+ {
357
+ "a": "y",
358
+ "b": {
359
+ "data": "0",
360
+ "key": "\"0\""
361
+ },
362
+ "key": "y\"0\""
363
+ },
364
+ {
365
+ "a": "y",
366
+ "b": {
367
+ "data": "0",
368
+ "key": "\"0\""
369
+ },
370
+ "key": "y\"0\""
371
+ },
372
+ {
373
+ "a": "x",
374
+ "b": {
375
+ "data": "1",
376
+ "key": "\"1\""
377
+ },
378
+ "key": "x\"1\""
379
+ },
380
+ {
381
+ "a": "y",
382
+ "b": {
383
+ "data": "1",
384
+ "key": "\"1\""
385
+ },
386
+ "key": "y\"1\""
387
+ },
388
+ {
389
+ "a": "y",
390
+ "b": {
391
+ "data": "1",
392
+ "key": "\"1\""
393
+ },
394
+ "key": "y\"1\""
395
+ },
396
+ {
397
+ "a": "y",
398
+ "b": {
399
+ "data": "1",
400
+ "key": "\"1\""
401
+ },
402
+ "key": "y\"1\""
403
+ }
404
+ ]
405
+ },
406
+ "aPriorisBeforeNormalisation": {
407
+ "x\"0\"": 0.8442857142857142,
408
+ "y\"0\"": 0.777505117061545,
409
+ "x\"1\"": 0.777505117061545,
410
+ "y\"1\"": 1.8851785714285711
411
+ },
412
+ "aPrioriNormalisationConstant": 11.298413325389037,
413
+ "aPosterioriNormalisationConstants": {
414
+ "\"0\"": 1.6217908313472593,
415
+ "\"1\"": 2.662683688490116
416
+ }
417
+ }
418
+ }
@@ -19,6 +19,7 @@
19
19
  var fs = require("fs");
20
20
 
21
21
  var TF_Parser = require('./TF_Parser');
22
+ var Sentence = require('./Sentence');
22
23
 
23
24
  function Brill_POS_Tagger(lexicon, ruleSet) {
24
25
  this.lexicon = lexicon;
@@ -30,18 +31,17 @@ function Brill_POS_Tagger(lexicon, ruleSet) {
30
31
  // the word itself followed by its lexical category
31
32
  Brill_POS_Tagger.prototype.tag = function(sentence) {
32
33
  var taggedSentence = this.tagWithLexicon(sentence);
34
+ //console.log(taggedSentence);
33
35
  return this.applyRules(taggedSentence);
34
36
  };
35
37
 
36
38
  Brill_POS_Tagger.prototype.tagWithLexicon = function(sentence) {
37
- var taggedSentence = new Array(sentence.length);
39
+ var taggedSentence = new Sentence();
38
40
 
39
41
  var that = this;
40
42
  sentence.forEach(function(word, index) {
41
- taggedSentence[index] = [];
42
- taggedSentence[index][0] = word;
43
43
  var categories = that.lexicon.tagWord(word);
44
- taggedSentence[index][1] = categories[0];
44
+ taggedSentence.addTaggedWord(word, categories[0]);
45
45
  });
46
46
  return(taggedSentence);
47
47
  };
@@ -50,14 +50,13 @@ Brill_POS_Tagger.prototype.tagWithLexicon = function(sentence) {
50
50
  // taggedSentence is an array of tagged words.
51
51
  // A tagged word is an array consisting of the word itself followed by its lexical category.
52
52
  // Returns an array of tagged words as well
53
- Brill_POS_Tagger.prototype.applyRules = function(taggedSentence) {
54
- // Apply transformation rules
55
- for (var i = 0, size = taggedSentence.length; i < size; i++) {
53
+ Brill_POS_Tagger.prototype.applyRules = function(sentence) {
54
+ for (var i = 0, size = sentence.taggedWords.length; i < size; i++) {
56
55
  this.ruleSet.getRules().forEach(function(rule) {
57
- rule.apply(taggedSentence, i);
56
+ rule.apply(sentence, i);
58
57
  });
59
58
  }
60
- return(taggedSentence);
59
+ return sentence;
61
60
  };
62
61
 
63
62
  module.exports = Brill_POS_Tagger;
@@ -27,16 +27,16 @@ Brill_POS_Tester.prototype.test = function(corpus, tagger) {
27
27
 
28
28
  // Tag the corpus using the tagger
29
29
  corpus.sentences.forEach(function(sentence) {
30
- var s = sentence.map(function(token) {
30
+ var s = sentence.taggedWords.map(function(token) {
31
31
  return token.token;
32
32
  });
33
33
 
34
34
  // Use the lexicon to tag the sentence
35
35
  var taggedSentence = tagger.tagWithLexicon(s);
36
36
  // Count the right tags
37
- sentence.forEach(function(token, i) {
37
+ sentence.taggedWords.forEach(function(token, i) {
38
38
  totalWords++;
39
- if (token.tag === taggedSentence[i][1]) {
39
+ if (token.tag === taggedSentence.taggedWords[i].tag) {
40
40
  correctTagsLexicon++;
41
41
  }
42
42
  });
@@ -44,8 +44,8 @@ Brill_POS_Tester.prototype.test = function(corpus, tagger) {
44
44
  // Use the rule set to tag the sentence
45
45
  var taggedSentenceAfterRules = tagger.applyRules(taggedSentence);
46
46
  // Count the right tags
47
- sentence.forEach(function(token, i) {
48
- if (token.tag === taggedSentenceAfterRules[i][1]) {
47
+ sentence.taggedWords.forEach(function(token, i) {
48
+ if (token.tag === taggedSentenceAfterRules.taggedWords[i].tag) {
49
49
  correctTagsAfterRules++;
50
50
  }
51
51
  });
@@ -27,6 +27,7 @@
27
27
 
28
28
  var TransformationRule = require("./TransformationRule");
29
29
  var RuleSet = require("./RuleSet");
30
+ var Sentence = require('./Sentence');
30
31
 
31
32
  // Training continues as long as there are rules with a positive score
32
33
  // that have not been selected before
@@ -156,7 +157,7 @@ Brill_POS_Trainer.prototype.disconnectSiteFromRule = function(i, j, rule) {
156
157
  // Adjusts the score of the rule at position i, j of the corpus
157
158
  Brill_POS_Trainer.prototype.scoreRule = function(rule, i, j) {
158
159
  //logger.debug("Brill_POS_Trainer.prototype.scoreRule: entry");
159
- var token = this.corpus.sentences[i][j];
160
+ var token = this.corpus.sentences[i].taggedWords[j];
160
161
  var rightTag = token.tag;
161
162
  var oldTag = token.testTag;
162
163
  var newTag = token.newTag;
@@ -198,7 +199,7 @@ Brill_POS_Trainer.prototype.scoreRule = function(rule, i, j) {
198
199
  // Generate positive rules for this given site using templates
199
200
  Brill_POS_Trainer.prototype.generatePositiveRules = function(i, j) {
200
201
  var sentence = this.corpus.sentences[i];
201
- var token = sentence[j];
202
+ var token = sentence.taggedWords[j];
202
203
  // A positive rule should trigger on the currently assigned testTag
203
204
  var oldTag = token.testTag;
204
205
  //logger.debug("Brill_POS_Trainer.prototype.generatePositiveRules: oldTag " + oldTag);
@@ -242,7 +243,7 @@ Brill_POS_Trainer.prototype.scanForPositiveRules = function() {
242
243
  //logger.debug("Brill_POS_Trainer.prototype.scanForPositiveRules: entry");
243
244
  var that = this;
244
245
  this.corpus.sentences.forEach(function(sentence, i) {
245
- sentence.forEach(function(token, j) {
246
+ sentence.taggedWords.forEach(function(token, j) {
246
247
  //logger.debug("Brill_POS_Trainer.prototype.scanForPositiveRules: sentence no " + i);
247
248
  var newRules = that.generatePositiveRules(i, j);
248
249
  newRules.getRules().forEach(function(rule) {
@@ -265,10 +266,13 @@ Brill_POS_Trainer.prototype.scanForSites = function() {
265
266
  if (i % 100 === 0) {
266
267
  //logger.info("Brill_POS_Trainer.prototype.scanForSites: sentence " + i);
267
268
  }
268
- var taggedSentence = sentence.map(function(token) {
269
- return [token.token, token.testTag];
269
+
270
+ var taggedSentence = new Sentence();
271
+ sentence.taggedWords.forEach(function(wordObject) {
272
+ taggedSentence.addTaggedWord(wordObject.token, wordObject.testTag);
270
273
  });
271
- sentence.forEach(function(token, j) {
274
+
275
+ sentence.taggedWords.forEach(function(token, j) {
272
276
  that.positiveRules.getRules().forEach(function(rule) {
273
277
  if (rule.isApplicableAt(sentence, taggedSentence, j)) {
274
278
  that.associateSiteWithRule(i, j, rule);
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  Corpus class for parsing and analysing corpora
3
- Copyright (C) 2017 Hugo W.L. ter Doest
3
+ Copyright (C) 2018 Hugo W.L. ter Doest
4
4
 
5
5
  This program is free software: you can redistribute it and/or modify
6
6
  it under the terms of the GNU General Public License as published by
@@ -16,24 +16,23 @@
16
16
  along with this program. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- //var log4js = require('log4js');
20
- //var logger = log4js.getLogger();
21
- //logger.setLevel('INFO');
22
-
19
+ var Sample = require('../../classifiers/maxent/Sample');
20
+ var ElementClass = require('../../classifiers/maxent/POS/POS_Element');
23
21
  var Lexicon = require('./Lexicon');
22
+
24
23
  const BROWN = 1;
25
24
 
26
25
  // sentences: an array of annotated sentences
27
26
  // A sentence is an array of annotated tokens
28
27
  // A token is an object with (token, tag, testTag, ruleList)
29
- function Corpus(data, typeOfCorpus) {
28
+ function Corpus(data, typeOfCorpus, SentenceClass) {
30
29
  this.wordCount = 0;
31
30
  this.sentences = [];
32
31
  if (data && typeOfCorpus) {
33
32
  // For other types of corpora add a case here and supply a parsing method
34
33
  switch (typeOfCorpus) {
35
34
  case BROWN:
36
- this.parseBrownCorpus(data);
35
+ this.parseBrownCorpus(data, SentenceClass);
37
36
  break;
38
37
  default:
39
38
  // Assume it is an array of tagged sentences
@@ -45,7 +44,7 @@ function Corpus(data, typeOfCorpus) {
45
44
  // data is raw text
46
45
  // A corpus parsing method should split the corpus in sentences each of which
47
46
  // consist of an array of tokens.
48
- Corpus.prototype.parseBrownCorpus = function(data) {
47
+ Corpus.prototype.parseBrownCorpus = function(data, SentenceClass) {
49
48
  var that = this;
50
49
 
51
50
  var lines = data.split('\n');
@@ -53,16 +52,13 @@ Corpus.prototype.parseBrownCorpus = function(data) {
53
52
  var trimmedLine = line.trim();
54
53
  // Only parse lines that contain characters
55
54
  if (trimmedLine != "") {
56
- var taggedSentence = [];
55
+ var taggedSentence = new SentenceClass();
57
56
  var tokens = line.trim().split(/\s+/);
58
57
  tokens.forEach(function (token) {
59
58
  that.wordCount++;
60
59
  // Create a tagged sentences consisting of tokens
61
60
  var wordPlusTag = token.split('_');
62
- taggedSentence.push({
63
- "token": wordPlusTag[0],
64
- "tag": wordPlusTag[1]
65
- });
61
+ taggedSentence.addTaggedWord(wordPlusTag[0], wordPlusTag[1]);
66
62
  });
67
63
 
68
64
  // Add the sentence to the corpus
@@ -96,7 +92,7 @@ Corpus.prototype.splitInTrainAndTest = function(percentageTrain) {
96
92
  };
97
93
 
98
94
  // Analyses the corpus:
99
- // - registers used PS tags
95
+ // - registers used POS tags
100
96
  // - records the frequency of POS tag for each word
101
97
  Corpus.prototype.analyse = function() {
102
98
  this.tagFrequencies = {};
@@ -105,7 +101,7 @@ Corpus.prototype.analyse = function() {
105
101
 
106
102
  var that = this;
107
103
  this.sentences.forEach(function(sentence) {
108
- sentence.forEach(function(token) {
104
+ sentence.taggedWords.forEach(function(token) {
109
105
  that.wordCount++;
110
106
 
111
107
  // Register the tags used in the corpus
@@ -156,7 +152,7 @@ Corpus.prototype.buildLexicon = function() {
156
152
 
157
153
  Corpus.prototype.tag = function(lexicon) {
158
154
  this.sentences.forEach(function(sentence) {
159
- sentence.forEach(function(token) {
155
+ sentence.taggedWords.forEach(function(token) {
160
156
  // tagWord returns a list of categories, take the first category
161
157
  token.testTag = lexicon.tagWord(token.token)[0];
162
158
  });
@@ -171,6 +167,15 @@ Corpus.prototype.nrWords = function() {
171
167
  return this.wordCount;
172
168
  };
173
169
 
170
+ Corpus.prototype.generateFeatures = function() {
171
+ var features = [];
172
+ this.sentences.forEach(function(sentence) {
173
+ features = sentence.generateFeatures(features);
174
+ });
175
+ //console.log(JSON.stringify(features));
176
+ return features;
177
+ };
178
+
174
179
  Corpus.prototype.prettyPrint = function() {
175
180
  this.sentences.forEach(function(sentence, index) {
176
181
  //logger.debug("sentence no " + index + "\n" +