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,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 softwé and associated documentation files (the "Softwé"), to deal
6
- in the Softwé without restriction, including without limitation the rights
7
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Softwé, and to permit persons to whom the Softwé 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 Softwé.
13
-
14
- THE SOFTWé 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 SOFTWé OR THE USE OR OTHER DEALINGS IN
20
- THE SOFTWé.
21
- */
22
-
23
- var Tokenizer = require('../lib/natural/tokenizers/aggressive_tokenizer_es'),
24
- tokenizer = new Tokenizer();
25
-
26
- describe('aggressive_tokenizer_es', function() {
27
- it('should tokenize strings', function() {
28
- expect(tokenizer.tokenize('hola yo me llamo eduardo y esudié ingeniería')).toEqual(['hola', 'yo', 'me', 'llamo', 'eduardo', 'y', 'esudié', 'ingeniería']);
29
- });
30
-
31
- it('should tokenize strings via attached string method', function() {
32
- tokenizer.attach();
33
- expect('hola yo me llamo eduardo y esudié ingeniería'.tokenize()).toEqual(['hola', 'yo', 'me', 'llamo', 'eduardo', 'y', 'esudié', 'ingeniería']);
34
- });
35
-
36
- });
@@ -1,101 +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 softwé and associated documentation files (the "Softwé"), to deal
6
- in the Softwé without restriction, including without limitation the rights
7
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Softwé, and to permit persons to whom the Softwé 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 Softwé.
13
-
14
- THE SOFTWé 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 SOFTWé OR THE USE OR OTHER DEALINGS IN
20
- THE SOFTWé.
21
- */
22
-
23
- var Tokenizer = require('../lib/natural/tokenizers/aggressive_tokenizer_fr'),
24
- tokenizer = new Tokenizer();
25
-
26
- var text = "Affectueusement surnommé « Gabo » dans toute l'Amérique latine, le Colombien Gabriel Garcia Marquez, prix Nobel de littérature 1982, l'un des plus grands écrivains du XXe siècle, est mort À son domicile de Mexico jeudi 17 avril. Il était âgé de 87 ans. Son Œuvre a été traduite dans toutes les langues ou presque, et vendue à quelque 50 millions d'exemplaires.";
27
-
28
- var tokenized = [ 'Affectueusement',
29
- 'surnommé',
30
- 'Gabo',
31
- 'dans',
32
- 'toute',
33
- 'l',
34
- 'Amérique',
35
- 'latine',
36
- 'le',
37
- 'Colombien',
38
- 'Gabriel',
39
- 'Garcia',
40
- 'Marquez',
41
- 'prix',
42
- 'Nobel',
43
- 'de',
44
- 'littérature',
45
- '1982',
46
- 'l',
47
- 'un',
48
- 'des',
49
- 'plus',
50
- 'grands',
51
- 'écrivains',
52
- 'du',
53
- 'XXe',
54
- 'siècle',
55
- 'est',
56
- 'mort',
57
- 'À',
58
- 'son',
59
- 'domicile',
60
- 'de',
61
- 'Mexico',
62
- 'jeudi',
63
- '17',
64
- 'avril',
65
- 'Il',
66
- 'était',
67
- 'âgé',
68
- 'de',
69
- '87',
70
- 'ans',
71
- 'Son',
72
- 'Œuvre',
73
- 'a',
74
- 'été',
75
- 'traduite',
76
- 'dans',
77
- 'toutes',
78
- 'les',
79
- 'langues',
80
- 'ou',
81
- 'presque',
82
- 'et',
83
- 'vendue',
84
- 'à',
85
- 'quelque',
86
- '50',
87
- 'millions',
88
- 'd',
89
- 'exemplaires' ];
90
-
91
- describe('aggressive_tokenizer_fr', function() {
92
- it('should tokenize strings', function() {
93
- expect(tokenizer.tokenize(text)).toEqual(tokenized);
94
- });
95
-
96
- it('should tokenize strings via attached string method', function() {
97
- tokenizer.attach();
98
- expect(text.tokenize()).toEqual(tokenized);
99
- });
100
-
101
- });
@@ -1,36 +0,0 @@
1
- /*
2
- Copyright (c) 2011, Chris Umbel, Martijn de Boer
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining a copy
5
- of this softwé and associated documentation files (the "Softwé"), to deal
6
- in the Softwé without restriction, including without limitation the rights
7
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Softwé, and to permit persons to whom the Softwé 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 Softwé.
13
-
14
- THE SOFTWé 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 SOFTWé OR THE USE OR OTHER DEALINGS IN
20
- THE SOFTWé.
21
- */
22
-
23
- var Tokenizer = require('../lib/natural/tokenizers/aggressive_tokenizer_nl'),
24
- tokenizer = new Tokenizer();
25
-
26
- describe('aggressive_tokenizer_nl', function() {
27
- it('should tokenize strings', function() {
28
- expect(tokenizer.tokenize('\'s Morgens is het nog erg koud, vertelde de weerman over een van de radio\'s')).toEqual(['\'s','Morgens','is','het','nog','erg','koud','vertelde','de','weerman','over','een','van','de','radio\'s']);
29
- });
30
-
31
- it('should tokenize strings via attached string method', function() {
32
- tokenizer.attach();
33
- expect('\'s Morgens is het nog erg koud, vertelde de weerman over een van de radio\'s'.tokenize()).toEqual(['\'s','Morgens','is','het','nog','erg','koud','vertelde','de','weerman','over','een','van','de','radio\'s']);
34
- });
35
-
36
- });
@@ -1,56 +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 softwé and associated documentation files (the "Softwé"), to deal
6
- in the Softwé without restriction, including without limitation the rights
7
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Softwé, and to permit persons to whom the Softwé 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 Softwé.
13
-
14
- THE SOFTWé 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 SOFTWé OR THE USE OR OTHER DEALINGS IN
20
- THE SOFTWé.
21
- */
22
-
23
- var Tokenizer = require('../lib/natural/tokenizers/aggressive_tokenizer_pt'),
24
- tokenizer = new Tokenizer();
25
-
26
- describe('aggressive_tokenizer_pt', function() {
27
- it('should tokenize strings', function() {
28
- expect(tokenizer.tokenize('isso é coração')).toEqual(['isso', 'é', 'coração']);
29
- });
30
-
31
- it('should tokenize strings via attached string method', function() {
32
- tokenizer.attach();
33
- expect('isso é coração'.tokenize()).toEqual(['isso', 'é', 'coração']);
34
- });
35
-
36
- it('should tokenize strings via attached string method', function() {
37
- tokenizer.attach();
38
- expect('isso é coração'.tokenize()).toEqual(['isso', 'é', 'coração']);
39
- });
40
-
41
- it('should swollow punctuation', function() {
42
- expect(tokenizer.tokenize('isso é coração, no')).toEqual(['isso', 'é', 'coração', 'no']);
43
- });
44
-
45
- it('should swollow final punctuation', function() {
46
- expect(tokenizer.tokenize('isso é coração, no?')).toEqual(['isso', 'é', 'coração', 'no']);
47
- });
48
-
49
- it('should swollow initial punctuation', function() {
50
- expect(tokenizer.tokenize('.isso é coração, no')).toEqual(['isso', 'é', 'coração', 'no']);
51
- });
52
-
53
- it('should swollow duplicate punctuation', function() {
54
- expect(tokenizer.tokenize('eu vou... pause')).toEqual(['eu', 'vou', 'pause']);
55
- });
56
- });
@@ -1,56 +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/aggressive_tokenizer'),
24
- tokenizer = new Tokenizer();
25
-
26
- describe('aggressive_tokenizer', function() {
27
- it('should tokenize strings', function() {
28
- expect(tokenizer.tokenize('these are things')).toEqual(['these', 'are', 'things']);
29
- });
30
-
31
- it('should tokenize strings via attached string method', function() {
32
- tokenizer.attach();
33
- expect('these are things'.tokenize()).toEqual(['these', 'are', 'things']);
34
- });
35
-
36
- it('should tokenize strings via attached string method', function() {
37
- tokenizer.attach();
38
- expect('these are things'.tokenize()).toEqual(['these', 'are', 'things']);
39
- });
40
-
41
- it('should swollow punctuation', function() {
42
- expect(tokenizer.tokenize('these are things, no')).toEqual(['these', 'are', 'things', 'no']);
43
- });
44
-
45
- it('should swollow final punctuation', function() {
46
- expect(tokenizer.tokenize('these are things, no?')).toEqual(['these', 'are', 'things', 'no']);
47
- });
48
-
49
- it('should swollow initial punctuation', function() {
50
- expect(tokenizer.tokenize('.these are things, no')).toEqual(['these', 'are', 'things', 'no']);
51
- });
52
-
53
- it('should swollow duplicate punctuation', function() {
54
- expect(tokenizer.tokenize('i shal... pause')).toEqual(['i', 'shal', 'pause']);
55
- });
56
- });
@@ -1,173 +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 natural = require('../lib/natural');
24
-
25
- describe('bayes classifier', function() {
26
- describe('classifier', function() {
27
- it('should classify with arrays', function() {
28
- var classifier = new natural.BayesClassifier();
29
- classifier.addDocument(['fix', 'box'], 'computing');
30
- classifier.addDocument(['write', 'code'], 'computing');
31
- classifier.addDocument(['script', 'code'], 'computing');
32
- classifier.addDocument(['write', 'book'], 'literature');
33
- classifier.addDocument(['read', 'book'], 'literature');
34
- classifier.addDocument(['study', 'book'], 'literature');
35
-
36
- classifier.train();
37
-
38
- expect(classifier.classify(['bug', 'code'])).toBe('computing');
39
- expect(classifier.classify(['read', 'thing'])).toBe('literature');
40
- });
41
-
42
- it('should provide all classification scores', function() {
43
- var classifier = new natural.BayesClassifier();
44
- classifier.addDocument(['fix', 'box'], 'computing');
45
- classifier.addDocument(['write', 'code'], 'computing');
46
- classifier.addDocument(['script', 'code'], 'computing');
47
- classifier.addDocument(['write', 'book'], 'literature');
48
- classifier.addDocument(['read', 'book'], 'literature');
49
- classifier.addDocument(['study', 'book'], 'literature');
50
-
51
- classifier.train();
52
-
53
- expect(classifier.getClassifications('i write code')[0].label).toBe('computing');
54
- expect(classifier.getClassifications('i write code')[1].label).toBe('literature');
55
- });
56
-
57
- it('should classify with strings', function() {
58
- var classifier = new natural.BayesClassifier();
59
- classifier.addDocument('i fixed the box', 'computing');
60
- classifier.addDocument('i write code', 'computing');
61
- classifier.addDocument('nasty script code', 'computing');
62
- classifier.addDocument('write a book', 'literature');
63
- classifier.addDocument('read a book', 'literature');
64
- classifier.addDocument('study the books', 'literature');
65
-
66
- classifier.train();
67
-
68
- expect(classifier.classify('a bug in the code')).toBe('computing');
69
- expect(classifier.classify('read all the books')).toBe('literature');
70
- });
71
-
72
- it('should classify and re-classify after document-removal', function() {
73
- var classifier = new natural.BayesClassifier()
74
- , arr
75
- , item
76
- , classifications = {};
77
-
78
- // Add some good/bad docs and train
79
- classifier.addDocument('foo bar baz', 'good');
80
- classifier.addDocument('qux zooby', 'bad');
81
- classifier.addDocument('asdf qwer', 'bad');
82
- classifier.train();
83
-
84
- expect(classifier.classify('foo')).toBe('good');
85
- expect(classifier.classify('qux')).toBe('bad');
86
-
87
- // Remove one of the bad docs, retrain
88
- classifier.removeDocument('qux zooby', 'bad');
89
- classifier.retrain();
90
-
91
- // Simple `classify` will still return a single result, even if
92
- // ratio for each side is equal -- have to compare actual values in
93
- // the classifications, should be equal since qux is unclassified
94
- arr = classifier.getClassifications('qux');
95
- for (var i = 0, ii = arr.length; i < ii; i++) {
96
- item = arr[i];
97
- classifications[item.label] = item.value;
98
- }
99
- expect(classifications.good).toEqual(classifications.bad);
100
-
101
- // Re-classify as good, retrain
102
- classifier.addDocument('qux zooby', 'good');
103
- classifier.retrain();
104
-
105
- // Should now be good, original docs should be unaffected
106
- expect(classifier.classify('foo')).toBe('good');
107
- expect(classifier.classify('qux')).toBe('good');
108
- });
109
-
110
- it('should serialize and deserialize a working classifier', function() {
111
- var classifier = new natural.BayesClassifier();
112
- classifier.addDocument('i fixed the box', 'computing');
113
- classifier.addDocument('i write code', 'computing');
114
- classifier.addDocument('nasty script code', 'computing');
115
- classifier.addDocument('write a book', 'literature');
116
- classifier.addDocument('read a book', 'literature');
117
- classifier.addDocument('study the books', 'literature');
118
-
119
- var obj = JSON.stringify(classifier);
120
- var newClassifier = natural.BayesClassifier.restore(JSON.parse(obj));
121
-
122
- newClassifier.addDocument('kick a ball', 'sports');
123
- newClassifier.addDocument('hit some balls', 'sports');
124
- newClassifier.addDocument('kick and punch', 'sports');
125
-
126
- newClassifier.train();
127
-
128
- expect(newClassifier.classify('a bug in the code')).toBe('computing');
129
- expect(newClassifier.classify('read all the books')).toBe('literature');
130
- expect(newClassifier.classify('kick butt')).toBe('sports');
131
- });
132
-
133
- it('should save and load a working classifier', function() {
134
- var classifier = new natural.BayesClassifier();
135
- classifier.addDocument('i fixed the box', 'computing');
136
- classifier.addDocument('i write code', 'computing');
137
- classifier.addDocument('nasty script code', 'computing');
138
- classifier.addDocument('write a book', 'literature');
139
- classifier.addDocument('read a book', 'literature');
140
- classifier.addDocument('study the books', 'literature');
141
-
142
- classifier.train();
143
-
144
- classifier.save('bayes_classifier.json', function(err) {
145
- natural.BayesClassifier.load('bayes_classifier.json', null,
146
- function(err, newClassifier){
147
- newClassifier.addDocument('kick a ball', 'sports');
148
- newClassifier.addDocument('hit some balls', 'sports');
149
- newClassifier.addDocument('kick and punch', 'sports');
150
-
151
- newClassifier.train();
152
-
153
- expect(newClassifier.classify('a bug in the code')).toBe('computing');
154
- expect(newClassifier.classify('read all the books')).toBe('literature');
155
- expect(newClassifier.classify('kick butt')).toBe('sports');
156
- asyncSpecDone();
157
- });
158
- });
159
- });
160
-
161
-
162
- it('should accept an optional smoothing parameter for the Bayesian estimates', function() {
163
- var defaultClassifier = new natural.BayesClassifier();
164
- var PorterStemmer = require('../lib/natural/stemmers/porter_stemmer');
165
- var newClassifier1 = new natural.BayesClassifier(PorterStemmer);
166
- var newClassifier2 = new natural.BayesClassifier(PorterStemmer, 0.1);
167
-
168
- expect(defaultClassifier.classifier.smoothing).toBe(1.0);
169
- expect(newClassifier1.classifier.smoothing).toBe(1.0);
170
- expect(newClassifier2.classifier.smoothing).toBe(0.1);
171
- });
172
- });
173
- });
@@ -1,175 +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 natural = require('lib/natural');
24
- var baseClassifier = require('lib/natural/classifiers/classifier.js');
25
- var fs = require('fs');
26
-
27
- describe('classifier', function () {
28
-
29
- describe('addDocument', function () {
30
-
31
- it('should ignore an empty document', function () {
32
- var classifier = new natural.BayesClassifier();
33
- classifier.addDocument('', 'philosophy');
34
- expect(classifier.docs.length).toBe(0);
35
- });
36
- });
37
-
38
- describe('events emitters', function () {
39
-
40
- it('should be emitted when a document is classified', function () {
41
- var classifier = new natural.BayesClassifier();
42
- classifier.addDocument('i fixed the box', 'computing');
43
- classifier.addDocument('i write code', 'computing');
44
- classifier.addDocument('nasty script code', 'computing');
45
- classifier.addDocument('write a book', 'literature');
46
- classifier.addDocument('read a book', 'literature');
47
- classifier.addDocument('study the books', 'literature');
48
-
49
- classifier.train();
50
-
51
- var pushedEvents = [];
52
-
53
- function eventRegister(obj) {
54
- pushedEvents.push(obj);
55
- };
56
-
57
- function assertEventResults() {
58
- teardown();
59
- expect(pushedEvents[0].index).toBe(0);
60
- expect(pushedEvents[0].total).toBe(6);
61
- expect(pushedEvents.length).toBe(6);
62
- }
63
-
64
- function teardown() {
65
- classifier.events.removeListener('trainedWithDocument', eventRegister);
66
- classifier.events.removeListener('doneTraining', assertEventResults);
67
- }
68
-
69
- classifier.events.on('trainedWithDocument', eventRegister);
70
- classifier.events.on('doneTraining', assertEventResults);
71
- });
72
-
73
- it('should emit events only on an instance of Classifier', function () {
74
- var classifier = new natural.BayesClassifier();
75
- classifier.addDocument('i fixed the box', 'computing');
76
- classifier.addDocument('i write code', 'computing');
77
- classifier.addDocument('write a book', 'literature');
78
- classifier.addDocument('study the books', 'literature');
79
-
80
- var pushedEvents = [];
81
-
82
- function eventRegister(obj) {
83
- pushedEvents.push(obj);
84
- };
85
-
86
- function assertEventResults() {
87
- teardown();
88
- expect(pushedEvents.length).toBe(0);
89
- }
90
-
91
- function teardown() {
92
- classifier.events.removeListener('trainedWithDocument', eventRegister);
93
- classifier.events.removeListener('doneTraining', assertEventResults);
94
- }
95
-
96
- var classifier2 = new natural.BayesClassifier();
97
- classifier2.events.on('trainedWithDocument', eventRegister);
98
- classifier.events.on('doneTraining', assertEventResults);
99
- classifier.train();
100
- });
101
-
102
- });
103
-
104
- describe('removeDocument', function () {
105
-
106
- var classifier;
107
- beforeEach(function () {
108
- classifier = new natural.BayesClassifier();
109
- classifier.addDocument('i fixed the box', 'computing');
110
- classifier.addDocument('i write code', 'computing');
111
- classifier.addDocument('write a book', 'literature');
112
- classifier.addDocument('study the books', 'literature');
113
- });
114
-
115
- it('should do nothing if text is not a string', function () {
116
- expect(classifier.docs.length).toBe(4);
117
- classifier.removeDocument(['write a book'], 'literature');
118
- classifier.removeDocument(['study the books'], 'literature');
119
- expect(classifier.docs.length).toBe(4);
120
- });
121
-
122
- it('should do nothing if text is not a match', function () {
123
- expect(classifier.docs.length).toBe(4);
124
- classifier.removeDocument('something else', 'literature');
125
- classifier.removeDocument('another thing', 'literature');
126
- expect(classifier.docs.length).toBe(4);
127
- });
128
- });
129
-
130
- describe('save', function () {
131
-
132
- var tmpFilename = '/spec/test_data/deleteMe';
133
- var nonExistentFilename = '/nonExistentDir/deleteMe';
134
- var classifier;
135
-
136
- beforeEach(function () {
137
- classifier = new natural.BayesClassifier();
138
- classifier.addDocument('I went to see the doctor of', 'philosophy');
139
- });
140
-
141
- afterEach(function () {
142
- if (fs.existsSync(tmpFilename)) {
143
- fs.unlinkSync(tmpFilename);
144
- fs.unlinkSync(nonExistentFilename);
145
- }
146
- });
147
-
148
- it('does nothing if called without a callback', function () {
149
- classifier.save(tmpFilename);
150
- expect(fs.existsSync(tmpFilename)).toBe(false);
151
- });
152
-
153
- it('fails if writing to a file fails', function () {
154
- classifier.save(nonExistentFilename, function (err) {
155
- expect(err).toBe.ok;
156
- expect(fs.existsSync(tmpFilename)).toBe(false);
157
- });
158
- });
159
- });
160
-
161
- describe('load', function () {
162
-
163
- it('does nothing if called without a callback', function () {
164
- result = baseClassifier.load('/spec/test_data/tfidf_document1.txt');
165
- expect(result).not.toBe.ok;
166
- });
167
-
168
- it('does nothing if called without a callback', function () {
169
- result = baseClassifier.load('/nonexistentFilename', function (err, newClassifier){
170
- expect(err).toBe.ok;
171
- expect(newClassifier).not.toBe.ok;
172
- });
173
- });
174
- });
175
- });