natural 0.2.1 → 0.4.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 (106) hide show
  1. package/.npmignore +4 -11
  2. package/README.md +156 -41
  3. package/bayes_classifier.json +1 -0
  4. package/lib/natural/brill_pos_tagger/data/Dutch/README.txt +2 -0
  5. package/lib/natural/brill_pos_tagger/data/Dutch/brill_CONTEXTRULES.jg +285 -0
  6. package/lib/natural/brill_pos_tagger/data/Dutch/brill_LEXICON.jg +11699 -0
  7. package/lib/natural/brill_pos_tagger/data/English/README.txt +8 -0
  8. package/lib/natural/brill_pos_tagger/data/English/lexicon_from_posjs.json +297827 -0
  9. package/lib/natural/brill_pos_tagger/data/English/tr_from_brill_paper.txt +10 -0
  10. package/lib/natural/brill_pos_tagger/data/English/tr_from_posjs.txt +18 -0
  11. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tagger.js +136 -0
  12. package/lib/natural/brill_pos_tagger/lib/Predicate.js +410 -0
  13. package/lib/natural/brill_pos_tagger/lib/TF_Parser.js +717 -0
  14. package/lib/natural/brill_pos_tagger/lib/TF_Parser.pegjs +55 -0
  15. package/lib/natural/brill_pos_tagger/lib/TransformationRule.js +49 -0
  16. package/lib/natural/classifiers/bayes_classifier.js +6 -5
  17. package/lib/natural/classifiers/classifier.js +3 -2
  18. package/lib/natural/classifiers/logistic_regression_classifier.js +9 -5
  19. package/lib/natural/distance/dice_coefficient.js +22 -12
  20. package/lib/natural/index.js +6 -0
  21. package/lib/natural/ngrams/ngrams.js +4 -0
  22. package/lib/natural/stemmers/stemmer.js +31 -6
  23. package/lib/natural/tokenizers/regexp_tokenizer.js +3 -3
  24. package/{examples/inflection/noun.js → lib/natural/tokenizers/sentence_tokenizer.js} +14 -10
  25. package/lib/natural/tokenizers/tokenizer_case.js +58 -0
  26. package/lib/natural/util/bag.js +1 -0
  27. package/lib/natural/wordnet/wordnet.js +2 -2
  28. package/lib/natural/wordnet/wordnet_file.js +1 -1
  29. package/lr_classifier.json +1 -0
  30. package/package.json +6 -3
  31. package/package.json~ +66 -0
  32. package/benchmarks/index.js +0 -32
  33. package/benchmarks/metaphone.js +0 -27
  34. package/benchmarks/soundex.js +0 -27
  35. package/examples/classification/basic.js +0 -35
  36. package/examples/classification/recall.js +0 -28
  37. package/examples/classification/store.js +0 -37
  38. package/examples/inflection/count.js +0 -35
  39. package/examples/inflection/noun_attach.js +0 -33
  40. package/examples/phonetics/compare.js +0 -39
  41. package/examples/phonetics/sounds_like.js +0 -40
  42. package/examples/phonetics/tokenize_and_phoneticize.js +0 -54
  43. package/examples/stemming/stem_corpus.js +0 -29
  44. package/examples/stemming/stem_word.js +0 -26
  45. package/examples/tfidf/array_example.js +0 -34
  46. package/examples/tfidf/multiple_terms.js +0 -33
  47. package/examples/tfidf/tfidf_example.js +0 -44
  48. package/io_spec/test_data/tfidf/one +0 -1
  49. package/io_spec/test_data/tfidf/two +0 -1
  50. package/io_spec/tfidf_spec.js +0 -50
  51. package/io_spec/wordnet_spec.js +0 -81
  52. package/spec/aggressive_tokenizer_es_spec.js +0 -36
  53. package/spec/aggressive_tokenizer_fr_spec.js +0 -101
  54. package/spec/aggressive_tokenizer_nl_spec.js +0 -36
  55. package/spec/aggressive_tokenizer_pt_spec.js +0 -56
  56. package/spec/aggressive_tokenizer_spec.js +0 -56
  57. package/spec/bayes_classifier_spec.js +0 -173
  58. package/spec/classifier_spec.js +0 -175
  59. package/spec/count_inflector_fr_spec.js +0 -44
  60. package/spec/count_inflector_spec.js +0 -73
  61. package/spec/dice_coefficient_spec.js +0 -26
  62. package/spec/double_metaphone_spec.js +0 -752
  63. package/spec/jaro-winkler_spec.js +0 -53
  64. package/spec/lancaster_stemmer_spec.js +0 -73
  65. package/spec/levenshtein_spec.js +0 -66
  66. package/spec/logistic_regression_classifier_spec.js +0 -122
  67. package/spec/longest_path_tree_spec.js +0 -102
  68. package/spec/metaphone_spec.js +0 -252
  69. package/spec/ngram_spec.js +0 -130
  70. package/spec/ngram_zh_spec.js +0 -122
  71. package/spec/normalizer_ja_spec.js +0 -160
  72. package/spec/normalizer_no_spec.js +0 -111
  73. package/spec/normalizer_spec.js +0 -140
  74. package/spec/noun_inflector_fr_spec.js +0 -476
  75. package/spec/noun_inflector_ja_spec.js +0 -112
  76. package/spec/noun_inflector_spec.js +0 -270
  77. package/spec/porter_stemmer_fr_spec.js +0 -101
  78. package/spec/porter_stemmer_it_spec.js +0 -44
  79. package/spec/porter_stemmer_no_spec.js +0 -143
  80. package/spec/porter_stemmer_pt_spec.js +0 -50
  81. package/spec/porter_stemmer_ru_spec.js +0 -63
  82. package/spec/porter_stemmer_spec.js +0 -182
  83. package/spec/present_verb_inflector_spec.js +0 -128
  84. package/spec/remove_diacritics_spec.js +0 -31
  85. package/spec/sentence_analyzer_spec.js +0 -277
  86. package/spec/shortest_path_tree_spec.js +0 -132
  87. package/spec/soundex_spec.js +0 -158
  88. package/spec/spellcheck_spec.js +0 -92
  89. package/spec/stemmer_ja_spec.js +0 -66
  90. package/spec/stemmer_token_spec.js +0 -161
  91. package/spec/test_data/snowball_fr.txt +0 -20402
  92. package/spec/test_data/snowball_it.txt +0 -35482
  93. package/spec/test_data/snowball_no.txt +0 -20628
  94. package/spec/test_data/snowball_pt.txt +0 -32016
  95. package/spec/test_data/tfdif_testcorpus.txt +0 -7
  96. package/spec/test_data/tfidf_document1.txt +0 -1
  97. package/spec/test_data/tfidf_document2.txt +0 -1
  98. package/spec/test_data/tfidf_document3.txt +0 -1
  99. package/spec/test_data/tfidf_document4.txt +0 -1
  100. package/spec/test_data/tfidf_teststopwords.txt +0 -1
  101. package/spec/test_data/wordnet_mini.sql +0 -95
  102. package/spec/tfidf_spec.js +0 -323
  103. package/spec/tokenizer_ja_spec.js +0 -58
  104. package/spec/transliterator_ja_spec.js +0 -103
  105. package/spec/treebank_word_tokenizer_spec.js +0 -36
  106. package/spec/trie_spec.js +0 -206
@@ -1,7 +0,0 @@
1
- 50
2
- the:23
3
- a:17
4
- girl:1
5
- moon:1
6
- said:5
7
- phone:2
@@ -1 +0,0 @@
1
- this document is about node.
@@ -1 +0,0 @@
1
- this document is about ruby.
@@ -1 +0,0 @@
1
- this document is about ruby and node.
@@ -1 +0,0 @@
1
- this document is about node. it has node examples
@@ -1 +0,0 @@
1
- moon
@@ -1,95 +0,0 @@
1
- PRAGMA foreign_keys=OFF;
2
- BEGIN TRANSACTION;
3
- CREATE TABLE adjpositions (synsetid integer not null, wordid integer not null, position varchar(2) not null, primary key (synsetid, wordid));
4
- CREATE TABLE adjpositiontypes (position varchar(2) not null, positionname varchar(24) not null, primary key (position));
5
- CREATE TABLE casedwords (casedwordid integer not null, wordid integer not null, cased varchar(80) not null unique, primary key (casedwordid));
6
- CREATE TABLE lexdomains (lexdomainid smallint not null, lexdomainname varchar(32), pos char, primary key (lexdomainid));
7
- CREATE TABLE lexlinks (word1id integer not null, synset1id integer not null, word2id integer not null, synset2id integer not null, linkid smallint not null, primary key (word1id, synset1id, word2id, synset2id, linkid));
8
- CREATE TABLE linktypes (linkid smallint not null, link varchar(50), recurses integer not null, primary key (linkid));
9
- CREATE TABLE morphmaps (morphid integer not null, pos char not null, wordid integer not null, primary key (morphid, pos, wordid));
10
- CREATE TABLE morphs (morphid integer not null, morph varchar(70) not null unique, primary key (morphid));
11
- CREATE TABLE postypes (pos char not null, posname varchar(20) not null, primary key (pos));
12
- CREATE TABLE samples (synsetid integer not null, sampleid smallint not null, sample longvarchar not null, primary key (synsetid, sampleid));
13
- CREATE TABLE semlinks (synset1id integer not null, synset2id integer not null, linkid smallint not null, primary key (synset1id, synset2id, linkid));
14
- CREATE TABLE senses (wordid integer not null, synsetid integer not null, casedwordid integer, senseid integer unique, sensenum smallint not null, lexid smallint not null, tagcount integer, sensekey varchar(100) unique, primary key (wordid, synsetid));
15
- INSERT INTO "senses" VALUES(138336,300491511,NULL,194444,4,0,0,'unique%5:00:00:unusual:00');
16
- INSERT INTO "senses" VALUES(138336,300505853,NULL,194441,1,0,10,'unique%5:00:00:incomparable:00');
17
- INSERT INTO "senses" VALUES(138336,301105042,NULL,194443,2,0,2,'unique%5:00:00:specific:00');
18
- INSERT INTO "senses" VALUES(138336,302215087,NULL,194442,3,0,0,'unique%5:00:00:single:05');
19
- INSERT INTO "senses" VALUES(138337,400175490,NULL,194445,1,0,4,'uniquely%4:02:00::');
20
- INSERT INTO "senses" VALUES(138338,104763650,NULL,194446,1,0,3,'uniqueness%1:07:00::');
21
- INSERT INTO "senses" VALUES(58843,103076708,NULL,81518,4,0,0,'good%1:06:00::');
22
- INSERT INTO "senses" VALUES(58843,104849241,NULL,81520,2,1,9,'good%1:07:01::');
23
- INSERT INTO "senses" VALUES(58843,105142180,NULL,81521,3,2,6,'good%1:07:02::');
24
- INSERT INTO "senses" VALUES(58843,105159725,NULL,81519,1,0,11,'good%1:07:00::');
25
- INSERT INTO "senses" VALUES(58843,300064787,NULL,81526,5,0,5,'good%5:00:00:advantageous:00');
26
- INSERT INTO "senses" VALUES(58843,300106020,NULL,81527,2,0,22,'good%5:00:00:ample:00');
27
- INSERT INTO "senses" VALUES(58843,300452883,NULL,81528,10,0,2,'good%5:00:00:close:02');
28
- INSERT INTO "senses" VALUES(58843,300523364,NULL,81529,9,0,2,'good%5:00:00:complete:00');
29
- INSERT INTO "senses" VALUES(58843,300775611,NULL,81530,21,0,0,'good%5:00:00:discriminating:00');
30
- INSERT INTO "senses" VALUES(58843,300832784,NULL,81538,14,0,1,'good%5:00:00:operative:00');
31
- INSERT INTO "senses" VALUES(58843,301048762,NULL,81531,13,0,1,'good%5:00:00:fortunate:00');
32
- INSERT INTO "senses" VALUES(58843,301068306,NULL,81532,20,0,0,'good%5:00:00:fresh:01');
33
- INSERT INTO "senses" VALUES(58843,301116026,NULL,81533,19,0,0,'good%5:00:00:genuine:00');
34
- INSERT INTO "senses" VALUES(58843,301123148,NULL,81522,1,1,190,'good%3:00:01::');
35
- INSERT INTO "senses" VALUES(58843,301129977,NULL,81523,3,2,12,'good%3:00:02::');
36
- INSERT INTO "senses" VALUES(58843,301166413,NULL,81534,18,0,0,'good%5:00:00:healthful:00');
37
- INSERT INTO "senses" VALUES(58843,301171213,NULL,81535,17,0,0,'good%5:00:00:healthy:00');
38
- INSERT INTO "senses" VALUES(58843,301333477,NULL,81536,16,0,0,'good%5:00:00:intellectual:00');
39
- INSERT INTO "senses" VALUES(58843,301586752,NULL,81537,6,0,4,'good%5:00:00:nice:00');
40
- INSERT INTO "senses" VALUES(58843,301661289,NULL,81539,12,0,1,'good%5:00:00:opportune:00');
41
- INSERT INTO "senses" VALUES(58843,301808329,NULL,81540,15,0,0,'good%5:00:00:pleasing:00');
42
- INSERT INTO "senses" VALUES(58843,301983162,NULL,81541,4,0,10,'good%5:00:00:reputable:00');
43
- INSERT INTO "senses" VALUES(58843,302036934,NULL,81542,7,0,3,'good%5:00:00:righteous:00');
44
- INSERT INTO "senses" VALUES(58843,302226162,NULL,81543,8,0,2,'good%5:00:00:skilled:00');
45
- INSERT INTO "senses" VALUES(58843,302273643,NULL,81544,11,1,1,'good%5:00:01:sound:00');
46
- INSERT INTO "senses" VALUES(58843,400011093,NULL,81524,1,0,9,'good%4:02:00::');
47
- INSERT INTO "senses" VALUES(58843,400057388,NULL,81525,2,1,4,'good%4:02:01::');
48
- INSERT INTO "senses" VALUES(27771,103076708,NULL,38135,1,0,4,'commodity%1:06:00::');
49
- INSERT INTO "senses" VALUES(134408,103076708,NULL,188782,1,0,0,'trade_good%1:06:00::');
50
- CREATE TABLE synsets (synsetid integer not null, pos char, lexdomainid smallint not null, definition longvarchar, primary key (synsetid));
51
- INSERT INTO "synsets" VALUES(103076708,'n',6,'articles of commerce');
52
- INSERT INTO "synsets" VALUES(104763650,'n',7,'the quality of being one of a kind');
53
- INSERT INTO "synsets" VALUES(104849241,'n',7,'moral excellence or admirableness');
54
- INSERT INTO "synsets" VALUES(105142180,'n',7,'that which is pleasing or valuable or useful');
55
- INSERT INTO "synsets" VALUES(105159725,'n',7,'benefit');
56
- INSERT INTO "synsets" VALUES(300064787,'s',0,'promoting or enhancing well-being');
57
- INSERT INTO "synsets" VALUES(300106020,'s',0,'having the normally expected amount');
58
- INSERT INTO "synsets" VALUES(300452883,'s',0,'with or in a close or intimate relationship');
59
- INSERT INTO "synsets" VALUES(300491511,'s',0,'highly unusual or rare but not the single instance');
60
- INSERT INTO "synsets" VALUES(300505853,'s',0,'radically distinctive and without equal');
61
- INSERT INTO "synsets" VALUES(300523364,'s',0,'thorough');
62
- INSERT INTO "synsets" VALUES(300775611,'s',0,'generally admired');
63
- INSERT INTO "synsets" VALUES(300832784,'s',0,'exerting force or influence');
64
- INSERT INTO "synsets" VALUES(301048762,'s',0,'resulting favorably');
65
- INSERT INTO "synsets" VALUES(301068306,'s',0,'not left to spoil');
66
- INSERT INTO "synsets" VALUES(301105042,'s',0,'(followed by `to'') applying exclusively to a given category or condition or locality');
67
- INSERT INTO "synsets" VALUES(301116026,'s',0,'not forged');
68
- INSERT INTO "synsets" VALUES(301123148,'a',0,'having desirable or positive qualities especially those suitable for a thing specified');
69
- INSERT INTO "synsets" VALUES(301129977,'a',0,'morally admirable');
70
- INSERT INTO "synsets" VALUES(301166413,'s',0,'tending to promote physical well-being; beneficial to health');
71
- INSERT INTO "synsets" VALUES(301171213,'s',0,'in excellent physical condition');
72
- INSERT INTO "synsets" VALUES(301333477,'s',0,'appealing to the mind');
73
- INSERT INTO "synsets" VALUES(301586752,'s',0,'agreeable or pleasing');
74
- INSERT INTO "synsets" VALUES(301661289,'s',0,'most suitable or right for a particular purpose');
75
- INSERT INTO "synsets" VALUES(301808329,'s',0,'capable of pleasing');
76
- INSERT INTO "synsets" VALUES(301983162,'s',0,'deserving of esteem and respect');
77
- INSERT INTO "synsets" VALUES(302036934,'s',0,'of moral excellence');
78
- INSERT INTO "synsets" VALUES(302215087,'s',0,'the single one of its kind');
79
- INSERT INTO "synsets" VALUES(302226162,'s',0,'having or showing knowledge and skill and aptitude');
80
- INSERT INTO "synsets" VALUES(302273643,'s',0,'financially sound');
81
- INSERT INTO "synsets" VALUES(400011093,'r',2,'(often used as a combining form) in a good or proper or satisfactory manner or to a high standard (`good'' is a nonstandard dialectal variant for `well'')');
82
- INSERT INTO "synsets" VALUES(400057388,'r',2,'completely and absolutely (`good'' is sometimes used informally for `thoroughly'')');
83
- INSERT INTO "synsets" VALUES(400175490,'r',2,'so as to be unique');
84
- CREATE TABLE vframemaps (synsetid integer not null, wordid integer not null, frameid smallint not null, primary key (synsetid, wordid, frameid));
85
- CREATE TABLE vframes (frameid smallint not null, frame varchar(50), primary key (frameid));
86
- CREATE TABLE vframesentencemaps (synsetid integer not null, wordid integer not null, sentenceid smallint not null, primary key (synsetid, wordid, sentenceid));
87
- CREATE TABLE vframesentences (sentenceid smallint not null, sentence longvarchar, primary key (sentenceid));
88
- CREATE TABLE words (wordid integer not null, lemma varchar(80) not null unique, primary key (wordid));
89
- INSERT INTO "words" VALUES(27771,'commodity');
90
- INSERT INTO "words" VALUES(58843,'good');
91
- INSERT INTO "words" VALUES(134408,'trade good');
92
- INSERT INTO "words" VALUES(138336,'unique');
93
- INSERT INTO "words" VALUES(138337,'uniquely');
94
- INSERT INTO "words" VALUES(138338,'uniqueness');
95
- COMMIT;
@@ -1,323 +0,0 @@
1
- /*
2
- Copyright (c) 2011, Rob Ellis, Chris Umbel
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining a copy
5
- of this software and associated documentation files (the "Software"), to deal
6
- in the Software without restriction, including without limitation the rights
7
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Software, and to permit persons to whom the Software is
9
- furnished to do so, subject to the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be included in
12
- all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
- THE SOFTWARE.
21
- */
22
-
23
- var TfIdf = require('../lib/natural/tfidf/tfidf');
24
- var tfidf;
25
-
26
- describe('tfidf', function() {
27
- describe('stateless operations', function() {
28
- it('should tf', function() {
29
- expect(TfIdf.tf('document', { document : 2, one : 1 })).toBe(2);
30
- expect(TfIdf.tf('document', { greetings : 1, program : 1 })).toBe(0);
31
- expect(TfIdf.tf('program', { greetings : 1, program : 1 })).toBe(1);
32
- });
33
- });
34
-
35
- describe('keys', function() {
36
- it('should store and recall keys', function() {
37
- tfidf = new TfIdf();
38
- tfidf.addDocument('document one', 'un');
39
- tfidf.addDocument('document Two', 'deux');
40
-
41
- tfidf.tfidfs('two', function(i, tfidf, key) {
42
- if(i === 0)
43
- expect(key).toBe('un');
44
- else
45
- expect(key).toBe('deux');
46
- });
47
- });
48
-
49
- it('should handle a deserialized object passed to the constructor', function () {
50
- tfidf = new TfIdf({
51
- documents: [
52
- {__key: 'un', document: 1, one: 1},
53
- {__key: 'deux', document: 1, two: 1}
54
- ]
55
- });
56
- tfidf.tfidfs('two', function(i, tfidf, key) {
57
- if(i === 0)
58
- expect(key).toBe('un');
59
- else
60
- expect(key).toBe('deux');
61
- });
62
- });
63
-
64
- it('should work when called without a callback', function () {
65
- tfidf = new TfIdf({
66
- documents: [
67
- {__key: 'un', document: 1, one: 1},
68
- {__key: 'deux', document: 1, two: 1}
69
- ]
70
- });
71
- tfidfs = tfidf.tfidfs('two');
72
- expect(tfidfs[1]).toBe(1 + Math.log( 2.0 / 2.0 ));
73
- });
74
-
75
- it('should work with the restoreCache flag set to true', function() {
76
- tfidf = new TfIdf();
77
- tfidf.addDocument('document one', 'un');
78
- expect(tfidf.idf("one")).toBe(1 + Math.log( 1.0 / 2.0 ));
79
- tfidf.addDocument('document Two', 'deux', true);
80
-
81
- tfidf.tfidfs('two', function(i, tfidf, key) {
82
- if(i === 0)
83
- expect(key).toBe('un');
84
- else
85
- expect(key).toBe('deux');
86
- });
87
- });
88
- });
89
-
90
- describe('stateful operations', function() {
91
- beforeEach(function() {
92
- tfidf = new TfIdf();
93
- tfidf.addDocument('document one');
94
- tfidf.addDocument('document Two');
95
- });
96
-
97
- it('should list important terms', function() {
98
- var terms = tfidf.listTerms(0);
99
- expect(terms[0].tfidf).toBeGreaterThan(terms[1].tfidf);
100
- });
101
- });
102
-
103
- describe("special cases", function(){
104
-
105
- // In response to
106
- it("should handle reserved function names correctly in documents", function(){
107
- var reservedWords = [
108
- 'toString',
109
- 'toLocaleString',
110
- 'valueOf',
111
- 'hasOwnProperty',
112
- 'isPrototypeOf',
113
- 'propertyIsEnumerable',
114
- 'constructor'
115
- ];
116
- tfidf = new TfIdf();
117
- tfidf.addDocument(reservedWords.join(" "));
118
-
119
- for(var i in reservedWords) {
120
- expect(tfidf.tfidf(reservedWords[i], 0)).toBe(1*(1+Math.log(1/2)));
121
- }
122
- });
123
-
124
- it('should handle an array passed to tfidf()', function() {
125
- tfidf = new TfIdf();
126
- var terms = ['this', 'document', 'is', 'about', 'poetry'];
127
- tfidf.addDocument(terms.join(" "));
128
- expect(tfidf.tfidf(terms, 0)).toBe( 2 * (1 + Math.log( 1.0 / 2.0 )) );
129
- });
130
- });
131
-
132
- describe("correct calculations", function(){
133
-
134
- it("should compute idf correctly", function(){
135
-
136
- tfidf = new TfIdf();
137
- tfidf.addDocument('this document is about node.');
138
- tfidf.addDocument('this document is about ruby.');
139
- tfidf.addDocument('this document is about ruby and node.');
140
- tfidf.addDocument('this document is about node. it has node examples');
141
-
142
- expect(tfidf.idf("node")).toBe(1 + Math.log( 4.0 / 4.0 ));
143
- });
144
-
145
- it("should compute idf correctly with non-string documents", function(){
146
-
147
- tfidf = new TfIdf();
148
- tfidf.addDocument('this document is about node.');
149
- tfidf.addDocument('this document is about ruby.');
150
- tfidf.addDocument('this document is about ruby and node.');
151
- tfidf.addDocument('this document is about node. it has node examples');
152
- tfidf.addDocument({text: 'this document is about python'});
153
- tfidf.addDocument(['this', 'document', 'is', 'about', 'node', 'and', 'JavaScript']);
154
-
155
- expect(tfidf.idf("node")).toBe(1 + Math.log( 6.0 / 5.0 ));
156
- });
157
-
158
- it("should compute tf correctly", function(){
159
- expect(TfIdf.tf("node", {this:1, document:1, is:1, about:1, node:1})).toBe(1);
160
- expect(TfIdf.tf("node", {this:1, document:1, is:1, about:1, ruby:1})).toBe(0);
161
- expect(TfIdf.tf("node", {this:1, document:1, is:1, about:1, ruby:1, and:1, node:1})).toBe(1);
162
- expect(TfIdf.tf("node", {this:1, document:1, is:1, about:1, node:2, it:1, has:1, examples:1})).toBe(2);
163
- });
164
-
165
- // This is a test of the use case outlined in the readme.
166
- it("should compute tf-idf correctly", function(){
167
-
168
- var correctCalculations = [
169
- 1 * (1 + Math.log( 4.0 / 4.0 )),
170
- 0,
171
- 2 * (1 + Math.log( 4.0 / 4.0 )),
172
- 1 * (1 + Math.log( 4.0 / 3.0 ))
173
- ];
174
-
175
- tfidf = new TfIdf();
176
- tfidf.addDocument('this document is about node.', {node: 0, ruby:1});
177
- tfidf.addDocument('this document is about ruby.', {node:1, ruby:3});
178
- tfidf.addDocument('this document is about ruby and node.', {node:0, ruby:3});
179
- tfidf.addDocument('this document is about node. it has node examples', {node:2, ruby:1});
180
-
181
- tfidf.tfidfs('node', function(i, measure, k) {
182
- expect(measure).toBe(correctCalculations[k.node]);
183
- });
184
-
185
- tfidf.tfidfs('ruby', function(i, measure, k) {
186
- expect(measure).toBe(correctCalculations[k.ruby]);
187
- });
188
-
189
- });
190
-
191
- it("should not return NaN if a term is not present in any documents", function() {
192
- tfidf = new TfIdf();
193
- tfidf.addDocument('this document is about node.');
194
-
195
- expect(tfidf.tfidf('ruby', 0)).toBe(0);
196
- });
197
-
198
- // This test assures that tf-idf is computed correctly before and after a document is added
199
- // Computes and tests a few tf-idfs, then adds a document and ensures that those terms tf-idf value
200
- // is updated accordingly.
201
- it("should update a terms tf-idf score after adding documents", function(){
202
-
203
- tfidf = new TfIdf();
204
-
205
- // Add 2 documents
206
- tfidf.addDocument('this document is about node.', 0);
207
- tfidf.addDocument('this document is about ruby.', 1);
208
-
209
- // check the tf-idf for 'node'
210
- expect( tfidf.tfidf("node", 0) ).toBe( 1 * ( 1 + Math.log( 2.0 / 2.0 ) ));
211
-
212
- // Add 2 more documents
213
- tfidf.addDocument('this document is about ruby and node.');
214
- tfidf.addDocument('this document is about node. it has node examples');
215
-
216
- // Ensure that the tf-idf in the same document has changed to reflect the new idf.
217
- expect( tfidf.tfidf("node", 0) ).toBe( 1 * ( 1 + Math.log( 4.0 / 4.0 ) ));
218
- });
219
-
220
- // Test tf-idf computation on files loaded using readFileSync
221
- it("should load documents from files", function(){
222
-
223
- tfidf = new TfIdf();
224
-
225
- tfidf.addFileSync("spec/test_data/tfidf_document1.txt", null, {node: 0, ruby:1});
226
- tfidf.addFileSync("spec/test_data/tfidf_document2.txt", null, {node:1, ruby:3});
227
- tfidf.addFileSync("spec/test_data/tfidf_document3.txt", null, {node:0, ruby:3});
228
- tfidf.addFileSync("spec/test_data/tfidf_document4.txt", null, {node:2, ruby:1});
229
-
230
- var correctCalculations = [
231
- 1 * (1 + Math.log( 4.0 / 4.0 )),
232
- 0,
233
- 2 * (1 + Math.log( 4.0 / 4.0 )),
234
- 1 * (1 + Math.log( 4.0 / 3.0 ))
235
- ];
236
-
237
- tfidf.tfidfs('node', function(i, measure, k) {
238
- expect(measure).toBe(correctCalculations[k.node]);
239
- });
240
-
241
- tfidf.tfidfs('ruby', function(i, measure, k) {
242
- expect(measure).toBe(correctCalculations[k.ruby]);
243
- });
244
-
245
- // addFileSync with restoreCache flag set to true.
246
- tfidf.addFileSync("spec/test_data/tfidf_document4.txt", null, {node:2, ruby:1}, true);
247
- });
248
-
249
- // Test idf caching when adding documents from addFileSync
250
- it("should update a terms tf-idf score after adding documents from addFileSync", function(){
251
- tfidf = new TfIdf();
252
-
253
- // Add 2 documents
254
- tfidf.addFileSync("spec/test_data/tfidf_document1.txt", null, 0);
255
- tfidf.addFileSync("spec/test_data/tfidf_document2.txt", null, 1);
256
-
257
- // check the tf-idf for 'node'
258
- expect( tfidf.tfidf("node", 0) ).toBe( 1 * (1 + Math.log( 2.0 / 2.0 ) ));
259
-
260
- // Add 2 more documents
261
- tfidf.addFileSync("spec/test_data/tfidf_document3.txt");
262
- tfidf.addFileSync("spec/test_data/tfidf_document4.txt");
263
-
264
- // Ensure that the tf-idf in the same document has changed to reflect the new idf.
265
- expect( tfidf.tfidf("node", 0) ).toBe( 1 * (1 + Math.log( 4.0 / 4.0 ) ));
266
- });
267
-
268
- // Test idf.setTokenizer
269
- it('should allow for specific types of Tokenizers', function(){
270
- tfidf = new TfIdf();
271
-
272
- tfidf.addDocument('this document isn\'t about node.', 0);
273
- tfidf.addDocument('that doc is about node.', 1);
274
- expect( tfidf.tfidf('n\'t', 0) ).toBe(0);
275
- expect( tfidf.tfidf('isn', 0) ).toBe( 1 * (1 + Math.log( 2 / 2 ) ));
276
-
277
- tfidf = new TfIdf();
278
-
279
- tfidf.addDocument('this document isn\'t about node.', 0);
280
- tfidf.addDocument('this document isn\'t about node.', 1);
281
-
282
- expect( tfidf.tfidf('isn', 0) ).toBe(1 * (1 + Math.log (2/3)));
283
-
284
- tfidf = new TfIdf();
285
- var TreebankWordTokenizer = require('../lib/natural/tokenizers/treebank_word_tokenizer');
286
- var tokenizer = new TreebankWordTokenizer();
287
-
288
- tfidf.addDocument('this document isn\'t about node.', 0);
289
- tfidf.setTokenizer(tokenizer);
290
- tfidf.addDocument('this doc isn\'t about node.', 1);
291
-
292
- expect( tfidf.tfidf('isn', 0) ).toBe( 1 * ( 1 + Math.log( 2 / 2 ) ));
293
- expect( tfidf.tfidf('n\'t', 1) ).toBe( 1 * ( 1 + Math.log( 2 / 2 ) ));
294
- expect( tfidf.tfidf('isn', 1) ).toBe(0);
295
- });
296
-
297
- it('should require a valid tokenizer when using setTokenizer', function(){
298
- tfidf = new TfIdf();
299
-
300
- expect( function() { tfidf.setTokenizer(1); } ).toThrow(new Error('Expected a valid Tokenizer'));
301
- expect( function() { tfidf.setTokenizer({}); } ).toThrow(new Error('Expected a valid Tokenizer'));
302
- });
303
-
304
- // Test encoding for addFileSync
305
- it('should use the specified encoding for addFileSync', function(){
306
-
307
- tfidf = new TfIdf();
308
-
309
- tfidf.addFileSync('spec/test_data/tfidf_document1.txt', 'base64');
310
- tfidf.addFileSync('spec/test_data/tfidf_document1.txt', 'utf8');
311
-
312
- expect( tfidf.tfidf('dghpcybkb2n1bwvudcbpcybhym91dcbub2rllg', 0) ).toBe( 1 * ( 1 + Math.log( 2.0 / 2.0 ) ));
313
- });
314
-
315
- // Test encoding check for addFileSync
316
- it('should require a valid encoding for addFileSync', function(){
317
-
318
- tfidf = new TfIdf();
319
-
320
- expect( function() { tfidf.addFileSync('spec/test_data/tfidf_document1.txt', 'foobar'); } ).toThrow(new Error('Invalid encoding: foobar'));
321
- });
322
- });
323
- });
@@ -1,58 +0,0 @@
1
- /*
2
- Copyright (c) 2012, Guillaume Marty
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 Tokenizer = require('../lib/natural/tokenizers/tokenizer_ja'),
24
- tokenizer = new Tokenizer();
25
-
26
- var text = '計算機科学における字句解析 (じくかいせき、英: Lexical Analysis) とは、ソースコードを構成する文字の並びを、トークン (token) の並びに変換することをいう。\n' +
27
- 'ここでいう「トークン」とは、意味を持つコードの最小単位のこと。字句解析を行うプログラムは、字句解析器 (lexical analyzer, 略称:lexer) と呼ばれる。\n' +
28
- '字句解析器はスキャナ (scanner) とトークナイザ (tokenizer) から構成される。\n';
29
- var result = ['計算', '機科', '学', 'に', 'おける', '字句', '解析',
30
- 'じくかい', 'せき', '英', 'Lexical', 'Analysis', 'と', 'は', 'ソースコード',
31
- 'を', '構成', 'する', '文字', 'の', '並び', 'を', 'トークン', 'token', 'の',
32
- '並び', 'に', '変換', 'する', 'こと', 'を', 'いう', 'ここ', 'でいう', 'トークン',
33
- 'と', 'は', '意味', 'を', '持つ', 'コード', 'の', '最小', '単位', 'の', 'こと',
34
- '字句', '解析', 'を', '行う', 'プログラム', 'は', '字句', '解析', '器', 'lexical',
35
- 'analyzer', '略称', 'lexer', 'と', '呼ば', 'れる', '字句', '解析', '器', 'は',
36
- 'スキャナ', 'scanner', 'と', 'トークナイザ', 'tokenizer', 'から', '構成', 'さ',
37
- 'れる'];
38
-
39
- describe('TokenizerJa', function() {
40
- it('should tokenize', function() {
41
- var tokens = tokenizer.tokenize(text);
42
- expect(tokens).toEqual(result);
43
-
44
- // This test is very hard to pass through, so we comment for now.
45
- //tokens = tokenizer.tokenize('すもももももももものうち。');
46
- //expect(tokens).toEqual(['すもも', 'も', 'もも', 'も', 'もも', 'の', 'うち', '。']);
47
- });
48
-
49
- it('should normalize input', function() {
50
- var converters = require('../lib/natural/normalizers/normalizer_ja').converters;
51
- var tokens = tokenizer.tokenize(
52
- converters.halfwidthToFullwidth.alphabet(
53
- converters.halfwidthToFullwidth.numbers(
54
- converters.fullwidthToHalfwidth.punctuation(
55
- converters.fullwidthToHalfwidth.katakana(text)))));
56
- expect(tokens).toEqual(result);
57
- });
58
- });
@@ -1,103 +0,0 @@
1
- /*
2
- Copyright (c) 2012, Guillaume Marty
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 transliterate_ja = require('../lib/natural/transliterators/ja');
24
-
25
- describe('transliterate_ja', function() {
26
- it('should transliterate hiragana', function() {
27
- expect(transliterate_ja('あいうえお かきくけこ')).toEqual('aiueo kakikukeko');
28
- expect(transliterate_ja('さしすせそ たちつてと')).toEqual('sashisuseso tachitsuteto');
29
- });
30
-
31
- it('should transliterate katakana', function() {
32
- expect(transliterate_ja('アイウエオ カキクケコ')).toEqual('aiueo kakikukeko');
33
- expect(transliterate_ja('サシスセソ タチツテト')).toEqual('sashisuseso tachitsuteto');
34
- });
35
-
36
- it('should transliterate small tsu differently depending on context', function() {
37
- expect(transliterate_ja('まっか ざっし たった はっぱ')).toEqual('makka zasshi tatta happa');
38
- expect(transliterate_ja('マッカ ザッシ タッタ ハッパ')).toEqual('makka zasshi tatta happa');
39
- });
40
-
41
- it('should transliterate final small tsu', function() {
42
- expect(transliterate_ja('ああっ')).toEqual('aat');
43
- expect(transliterate_ja('アアッ')).toEqual('aat');
44
- });
45
-
46
- it('should transliterate n differently depending on context', function() {
47
- expect(transliterate_ja('まんと ばんび ほんや')).toEqual("manto bambi hon'ya");
48
- expect(transliterate_ja('マント バンビ ホンヤ')).toEqual("manto bambi hon'ya");
49
- });
50
-
51
- it('should transliterate long vowels', function() {
52
- expect(transliterate_ja('ああ いい うう ええ おお おう')).toEqual('aa ii ū ee oo ō');
53
- expect(transliterate_ja('あー いー うー えー おー')).toEqual('ā ī ū ē ō');
54
- expect(transliterate_ja('アア イイ ウウ エエ オオ オウ')).toEqual('aa ii ū ee oo ō');
55
- expect(transliterate_ja('アー イー ウー エー オー')).toEqual('ā ī ū ē ō');
56
- });
57
-
58
- it('should leave non fullwidth kana unchanged', function() {
59
- expect(transliterate_ja('abc ABC 漢字 (.)')).toEqual('abc ABC 漢字 (.)');
60
- expect(transliterate_ja('アイウエオ カキクケコ')).toEqual('アイウエオ カキクケコ');
61
- });
62
-
63
- it('should transliterate misc. words correctly', function() {
64
- expect(transliterate_ja('アヴァンギャルド')).toEqual('avangyarudo'); // Avant-garde
65
- expect(transliterate_ja('アクサン・スィルコンフレックス')).toEqual('akusan sirukonfurekkusu'); // Accent circonflexe
66
- expect(transliterate_ja('アドレシッング')).toEqual('adoreshinngu'); // Addressing
67
- expect(transliterate_ja('イェス')).toEqual('yesu'); // Yes
68
- expect(transliterate_ja('インテリジェンス')).toEqual('interijensu'); // Intelligence
69
- expect(transliterate_ja('インテルメッツォ')).toEqual('interumettso'); // Intermezzo
70
- expect(transliterate_ja('グァテマラ')).toEqual('gwatemara'); // Guatemala
71
- expect(transliterate_ja('クァルテット')).toEqual('kwarutetto'); // Quartet
72
- expect(transliterate_ja('クィンテット')).toEqual('kwintetto'); // Quintet
73
- expect(transliterate_ja('クォーター')).toEqual('kwōtā'); // Quarter
74
- expect(transliterate_ja('サブウーファー')).toEqual('sabuūfā'); // Sub woofer
75
- expect(transliterate_ja('ソフトウェア')).toEqual('sofutowea'); // Software
76
- expect(transliterate_ja('ツィーター')).toEqual('tsītā'); // Tweeter
77
- expect(transliterate_ja('デューティー')).toEqual('dyūtī'); // Duty
78
- expect(transliterate_ja('ドキュメントィンドウ')).toEqual('dokyumentwindō'); // Document window
79
- expect(transliterate_ja('ヌーヴェルヴァーグ')).toEqual('nūveruvāgu'); // Nouvelle vague
80
- expect(transliterate_ja('ハイジャッンプ')).toEqual('haijanmpu'); // High jump
81
- expect(transliterate_ja('バッファ')).toEqual('baffa'); // Buffer
82
- expect(transliterate_ja('フーホェア')).toEqual('fūhwea'); // WhoWhere?
83
- expect(transliterate_ja('フェイズィング')).toEqual('feizingu'); // Phasing
84
- expect(transliterate_ja('フッロピー')).toEqual('furropī'); // Floppy (alternative transcription)
85
- expect(transliterate_ja('ブュー')).toEqual('byū'); // View
86
- expect(transliterate_ja('フューチャー')).toEqual('fyūchā'); // Future
87
- expect(transliterate_ja('フロッピィ')).toEqual('furoppī'); // Floppy
88
- expect(transliterate_ja('ボージョレー・ヌーヴォー')).toEqual('bōjorē nūvō'); // Beaujolais nouveau
89
- expect(transliterate_ja('ボスニア・ヘルツェゴビナ')).toEqual('bosunia herutsegobina'); // Bosnia and Herzegovina
90
- expect(transliterate_ja('マッハ')).toEqual('mahha'); // Mach
91
- expect(transliterate_ja('レヴュー')).toEqual('revyū'); // Review
92
- expect(transliterate_ja('レクリェーション')).toEqual('rekuryēshon'); // Recreation
93
- });
94
-
95
- it('should use fallback for small vowels on special case', function() {
96
- // These words use rare combination of small vowels.
97
- expect(transliterate_ja('アゲィンスト')).toEqual('ageinsuto'); // Against (alternative transcription)
98
- expect(transliterate_ja('エッセィ')).toEqual('essei'); // Essay (alternative transcription)
99
- expect(transliterate_ja('ゾゥアラジィ')).toEqual('zouarajī'); // Zoology (alternative transcription)
100
- expect(transliterate_ja('ゾゥァラジカル')).toEqual('zouarajikaru'); // Zoological (alternative transcription)
101
- expect(transliterate_ja('ボランテァ')).toEqual('borantea'); // Volunteer (alternative transcription)
102
- });
103
- });
@@ -1,36 +0,0 @@
1
- /*
2
- Copyright (c) 2011, Chris Umbel
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining a copy
5
- of this software and associated documentation files (the "Software"), to deal
6
- in the Software without restriction, including without limitation the rights
7
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Software, and to permit persons to whom the Software is
9
- furnished to do so, subject to the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be included in
12
- all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
- THE SOFTWARE.
21
- */
22
-
23
- var Tokenizer = require('../lib/natural/tokenizers/treebank_word_tokenizer'),
24
- tokenizer = new Tokenizer();
25
-
26
- describe('treebank', function() {
27
- it('should tokenize', function() {
28
- var tokens = tokenizer.tokenize("If we 'all' can't go. I'll stay home.");
29
- expect(tokens).toEqual(['If', 'we', "'all", "'", 'ca', "n't", 'go.',
30
- 'I', "'ll", 'stay', 'home', '.']);
31
-
32
- tokens = tokenizer.tokenize("If we 'all' can't go. I'll stay home. If we 'all' can't go. I'll stay home.");
33
- expect(tokens).toEqual(['If', 'we', "'all", "'", 'ca', "n't", 'go.',
34
- 'I', "'ll", 'stay', 'home.', 'If', 'we', "'all", "'", 'ca', "n't", 'go.', 'I', "'ll", 'stay', 'home', '.']);
35
- });
36
- });