natural 0.1.28 → 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 (117) hide show
  1. package/.editorconfig +7 -0
  2. package/.npmignore +4 -11
  3. package/README.md +190 -50
  4. package/bayes_classifier.json +1 -0
  5. package/lib/natural/analyzers/sentence_analyzer.js +8 -5
  6. package/lib/natural/brill_pos_tagger/data/Dutch/README.txt +2 -0
  7. package/lib/natural/brill_pos_tagger/data/Dutch/brill_CONTEXTRULES.jg +285 -0
  8. package/lib/natural/brill_pos_tagger/data/Dutch/brill_LEXICON.jg +11699 -0
  9. package/lib/natural/brill_pos_tagger/data/English/README.txt +8 -0
  10. package/lib/natural/brill_pos_tagger/data/English/lexicon_from_posjs.json +297827 -0
  11. package/lib/natural/brill_pos_tagger/data/English/tr_from_brill_paper.txt +10 -0
  12. package/lib/natural/brill_pos_tagger/data/English/tr_from_posjs.txt +18 -0
  13. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tagger.js +136 -0
  14. package/lib/natural/brill_pos_tagger/lib/Predicate.js +410 -0
  15. package/lib/natural/brill_pos_tagger/lib/TF_Parser.js +717 -0
  16. package/lib/natural/brill_pos_tagger/lib/TF_Parser.pegjs +55 -0
  17. package/lib/natural/brill_pos_tagger/lib/TransformationRule.js +49 -0
  18. package/lib/natural/classifiers/bayes_classifier.js +6 -5
  19. package/lib/natural/classifiers/classifier.js +3 -2
  20. package/lib/natural/classifiers/logistic_regression_classifier.js +9 -5
  21. package/lib/natural/distance/dice_coefficient.js +22 -12
  22. package/lib/natural/distance/jaro-winkler_distance.js +13 -8
  23. package/lib/natural/index.js +6 -0
  24. package/lib/natural/inflectors/present_verb_inflector.js +1 -0
  25. package/lib/natural/ngrams/ngrams.js +6 -0
  26. package/lib/natural/normalizers/normalizer_ja.js +26 -8
  27. package/lib/natural/phonetics/double_metaphone.js +9 -4
  28. package/lib/natural/phonetics/metaphone.js +4 -0
  29. package/lib/natural/phonetics/soundex.js +11 -7
  30. package/lib/natural/spellcheck/spellcheck.js +80 -0
  31. package/lib/natural/stemmers/porter_stemmer.js +90 -62
  32. package/lib/natural/stemmers/porter_stemmer_fr.js +8 -3
  33. package/lib/natural/stemmers/porter_stemmer_pt.js +302 -0
  34. package/lib/natural/stemmers/stemmer.js +31 -6
  35. package/lib/natural/stemmers/stemmer_pt.js +65 -0
  36. package/lib/natural/stemmers/token.js +163 -0
  37. package/lib/natural/tfidf/tfidf.js +49 -18
  38. package/lib/natural/tokenizers/regexp_tokenizer.js +3 -3
  39. package/{examples/inflection/noun.js → lib/natural/tokenizers/sentence_tokenizer.js} +14 -10
  40. package/lib/natural/tokenizers/tokenizer_case.js +58 -0
  41. package/lib/natural/util/bag.js +1 -0
  42. package/lib/natural/util/stopwords_pt.js +136 -0
  43. package/lib/natural/wordnet/wordnet.js +2 -2
  44. package/lib/natural/wordnet/wordnet_file.js +1 -1
  45. package/lr_classifier.json +1 -0
  46. package/package.json +7 -4
  47. package/package.json~ +66 -0
  48. package/benchmarks/index.js +0 -32
  49. package/benchmarks/metaphone.js +0 -27
  50. package/benchmarks/soundex.js +0 -27
  51. package/examples/classification/basic.js +0 -35
  52. package/examples/classification/recall.js +0 -28
  53. package/examples/classification/store.js +0 -37
  54. package/examples/inflection/count.js +0 -35
  55. package/examples/inflection/noun_attach.js +0 -33
  56. package/examples/phonetics/compare.js +0 -39
  57. package/examples/phonetics/sounds_like.js +0 -40
  58. package/examples/phonetics/tokenize_and_phoneticize.js +0 -54
  59. package/examples/stemming/stem_corpus.js +0 -29
  60. package/examples/stemming/stem_word.js +0 -26
  61. package/examples/tfidf/array_example.js +0 -34
  62. package/examples/tfidf/multiple_terms.js +0 -33
  63. package/examples/tfidf/tfidf_example.js +0 -44
  64. package/io_spec/test_data/tfidf/one +0 -1
  65. package/io_spec/test_data/tfidf/two +0 -1
  66. package/io_spec/tfidf_spec.js +0 -50
  67. package/io_spec/wordnet_spec.js +0 -81
  68. package/spec/aggressive_tokenizer_es_spec.js +0 -36
  69. package/spec/aggressive_tokenizer_fr_spec.js +0 -101
  70. package/spec/aggressive_tokenizer_nl_spec.js +0 -36
  71. package/spec/aggressive_tokenizer_pt_spec.js +0 -56
  72. package/spec/aggressive_tokenizer_spec.js +0 -56
  73. package/spec/bayes_classifier_spec.js +0 -173
  74. package/spec/classifier_spec.js +0 -91
  75. package/spec/count_inflector_fr_spec.js +0 -44
  76. package/spec/count_inflector_spec.js +0 -73
  77. package/spec/dice_coefficient_spec.js +0 -26
  78. package/spec/double_metaphone_spec.js +0 -743
  79. package/spec/jaro-winkler_spec.js +0 -53
  80. package/spec/lancaster_stemmer_spec.js +0 -73
  81. package/spec/levenshtein_spec.js +0 -66
  82. package/spec/logistic_regression_classifier_spec.js +0 -122
  83. package/spec/longest_path_tree_spec.js +0 -102
  84. package/spec/metaphone_spec.js +0 -247
  85. package/spec/ngram_spec.js +0 -130
  86. package/spec/ngram_zh_spec.js +0 -122
  87. package/spec/normalizer_ja_spec.js +0 -154
  88. package/spec/normalizer_no_spec.js +0 -111
  89. package/spec/normalizer_spec.js +0 -118
  90. package/spec/noun_inflector_fr_spec.js +0 -476
  91. package/spec/noun_inflector_ja_spec.js +0 -112
  92. package/spec/noun_inflector_spec.js +0 -270
  93. package/spec/porter_stemmer_fr_spec.js +0 -101
  94. package/spec/porter_stemmer_it_spec.js +0 -44
  95. package/spec/porter_stemmer_no_spec.js +0 -143
  96. package/spec/porter_stemmer_ru_spec.js +0 -63
  97. package/spec/porter_stemmer_spec.js +0 -137
  98. package/spec/present_verb_inflector_spec.js +0 -125
  99. package/spec/sentence_analyzer_spec.js +0 -29
  100. package/spec/shortest_path_tree_spec.js +0 -132
  101. package/spec/soundex_spec.js +0 -154
  102. package/spec/stemmer_ja_spec.js +0 -66
  103. package/spec/test_data/snowball_fr.txt +0 -20402
  104. package/spec/test_data/snowball_it.txt +0 -35482
  105. package/spec/test_data/snowball_no.txt +0 -20628
  106. package/spec/test_data/tfdif_testcorpus.txt +0 -7
  107. package/spec/test_data/tfidf_document1.txt +0 -1
  108. package/spec/test_data/tfidf_document2.txt +0 -1
  109. package/spec/test_data/tfidf_document3.txt +0 -1
  110. package/spec/test_data/tfidf_document4.txt +0 -1
  111. package/spec/test_data/tfidf_teststopwords.txt +0 -1
  112. package/spec/test_data/wordnet_mini.sql +0 -95
  113. package/spec/tfidf_spec.js +0 -198
  114. package/spec/tokenizer_ja_spec.js +0 -58
  115. package/spec/transliterator_ja_spec.js +0 -103
  116. package/spec/treebank_word_tokenizer_spec.js +0 -36
  117. package/spec/trie_spec.js +0 -206
@@ -1 +0,0 @@
1
- disk document one
@@ -1 +0,0 @@
1
- disk document two
@@ -1,50 +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 io', function() {
27
- it('should be able to be serialized', function() {
28
- tfidf = new TfIdf();
29
- tfidf.addDocument('document one', 'un');
30
- tfidf.addDocument('document Two', 'deux');
31
- var s = JSON.stringify(tfidf);
32
- expect(s).toBe('{"documents":[{"__key":"un","document":1,"one":1},{"__key":"deux","document":1,"two":1}]}');
33
- });
34
-
35
- it('should deserialize', function() {
36
- var s = '{"documents":[{"__key":"un","document":1,"one":1},{"__key":"deux","document":1,"two":1}]}';
37
- var tfidf = new TfIdf(JSON.parse(s));
38
- expect(tfidf.tfidf('one', 0)).toBe(0.9162907318741551);
39
- expect(tfidf.tfidf('two', 0)).toBe(0);
40
- });
41
-
42
- describe('files', function() {
43
- var tfidf = new TfIdf();
44
- tfidf.addFileSync('io_spec/test_data/tfidf/one');
45
- tfidf.addFileSync('io_spec/test_data/tfidf/two');
46
- expect(tfidf.tfidf('Document', 0)).toBe(0.8472978603872037);
47
- expect(tfidf.tfidf('one', 0)).toBe(0.9162907318741551);
48
- expect(tfidf.tfidf('two', 0)).toBe(0);
49
- });
50
- });
@@ -1,81 +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 WordNet = require('lib/natural/wordnet/wordnet');
24
- jasmine.asyncSpecWait.timeout = 30 * 1000;
25
-
26
- describe('wordnet', function() {
27
-
28
- it('should lookup synonyms', function() {
29
- var wordnet = new WordNet();
30
-
31
- wordnet.lookupSynonyms('entity', function(records) {
32
- expect(records.length).toBe(3);
33
- asyncSpecDone();
34
- });
35
-
36
- asyncSpecWait();
37
- });
38
-
39
- it('should lookup synonyms give a synset offset and a pos', function() {
40
- var wordnet = new WordNet();
41
-
42
- wordnet.getSynonyms(1740, 'n', function(records) {
43
- expect(records.length).toBe(3);
44
- expect(records[0].synsetOffset).toBe(4424418);
45
- expect(records[1].synsetOffset).toBe(2137);
46
- expect(records[2].synsetOffset).toBe(1930);
47
- asyncSpecDone();
48
- });
49
-
50
- asyncSpecWait();
51
- });
52
-
53
- it('should lookup synonyms via a provided synset object', function() {
54
- var wordnet = new WordNet();
55
-
56
- wordnet.lookup('entity', function(results) {
57
- wordnet.getSynonyms(results[0], function(records) {
58
- expect(records.length).toBe(3);
59
- expect(records[0].synsetOffset).toBe(4424418);
60
- expect(records[1].synsetOffset).toBe(2137);
61
- expect(records[2].synsetOffset).toBe(1930);
62
- asyncSpecDone();
63
- });
64
- });
65
-
66
- asyncSpecWait();
67
- });
68
-
69
- it('should add records but once', function() {
70
- var wordnet = new WordNet();
71
-
72
- wordnet.lookup('node', function(records) {
73
- expect(records.length).toBe(8);
74
- expect(records[0].lemma).toBe('node');
75
-
76
- asyncSpecDone();
77
- });
78
-
79
- asyncSpecWait();
80
- });
81
- });
@@ -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
- });