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,53 +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 jaroWinklerDistance = require('../lib/natural/distance/jaro-winkler_distance')
24
-
25
- Number.prototype.approxEql = function(val) {
26
- return Math.abs(this - val) < 1e-5;
27
- }
28
-
29
- describe('jaro-winkler', function () {
30
- it('should evaluate string similarity', function () {
31
- expect(jaroWinklerDistance('DIXON', 'DICKSONX').approxEql(0.81333)).toBeTruthy();
32
- expect(jaroWinklerDistance('DWAYNE', 'DUANE').approxEql(0.84)).toBeTruthy();
33
- });
34
-
35
- it('should handle exact matches', function () {
36
- expect(jaroWinklerDistance('RICK', 'RICK')).toBe(1);
37
- expect(jaroWinklerDistance('abc', 'abc')).toBe(1);
38
- expect(jaroWinklerDistance('abcd', 'abcd')).toBe(1);
39
- expect(jaroWinklerDistance('seddon', 'seddon')).toBe(1);
40
- });
41
-
42
- it('should handle total mis-matches', function () {
43
- expect(jaroWinklerDistance('NOT', 'SAME')).toBe(0);
44
- });
45
-
46
- it('should handle partial mis-matches', function () {
47
- expect(jaroWinklerDistance('aaa', 'abcd').approxEql(0.575)).toBeTruthy();
48
- });
49
-
50
- it('should handle transpositions', function () {
51
- expect(jaroWinklerDistance('MARTHA', 'MARHTA').approxEql(0.96111)).toBeTruthy();
52
- });
53
- });
@@ -1,73 +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
- describe('lancaster_stemmer', function() {
24
- var stemmer = require('../lib/natural/stemmers/lancaster_stemmer');
25
-
26
- it('should stem', function() {
27
- stemmer.attach();
28
-
29
- expect('marks'.stem()).toBe('mark');
30
- expect('MARKs'.stem()).toBe('mark');
31
- });
32
-
33
- it('should stop running rules where appropriate', function() {
34
- stemmer.attach();
35
-
36
- expect('living'.stem()).toBe('liv');
37
- expect('thing'.stem()).toBe('thing');
38
- expect('ear'.stem()).toBe('ear');
39
- expect('string'.stem()).toBe('string');
40
- });
41
-
42
- it('should only pop the size specified by the rule', function() {
43
- stemmer.attach();
44
-
45
- expect('triplicate'.stem()).toBe('triply');
46
- expect('triPlicAte'.stem()).toBe('triply');
47
- });
48
-
49
- it('should stem and append and recurse', function() {
50
- stemmer.attach();
51
-
52
- expect('classified'.stem()).toBe('class');
53
- expect('ClaSsiFied'.stem()).toBe('class');
54
- });
55
-
56
- it('should apply intact rules only to intact string', function() {
57
- stemmer.attach();
58
-
59
- expect('maximum'.stem()).toBe('maxim');
60
- expect('presumably'.stem()).toBe('presum');
61
- expect('MAXIMUM'.stem()).toBe('maxim');
62
- expect('PRESUMABLY'.stem()).toBe('presum');
63
- });
64
-
65
- it('#171 and #174, exceed, anguish, affluxion, discept', function() {
66
- stemmer.attach();
67
-
68
- expect('exceed'.stem()).toBe('excess');
69
- expect('anguish'.stem()).toBe('anct');
70
- expect('affluxion'.stem()).toBe('affluct');
71
- expect('discept'.stem()).toBe('disceiv');
72
- });
73
- });
@@ -1,66 +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 levenshteinDistance = require('../lib/natural/distance/levenshtein_distance')
24
-
25
- describe('levenshtein_distance', function() {
26
- it('should replace 2', function() {
27
- expect(levenshteinDistance('doctor', 'doktor')).toBe(1);
28
- });
29
-
30
- it('should allow altering replacement value', function() {
31
- expect(levenshteinDistance('doctor', 'doktor', {substitution_cost: 1})).toBe(1);
32
- });
33
-
34
- it('should delete 1', function() {
35
- expect(levenshteinDistance('doctor', 'docto')).toBe(1);
36
- });
37
-
38
- it('should insert 1', function() {
39
- expect(levenshteinDistance('flat', 'flats')).toBe(1);
40
- });
41
-
42
- it('should combine operations', function() {
43
- expect(levenshteinDistance('flad', 'flaten')).toBe(3);
44
- expect(levenshteinDistance('flaten', 'flad')).toBe(3);
45
- });
46
-
47
- it('should consider perfect matches 0', function() {
48
- expect(levenshteinDistance('one', 'one')).toBe(0);
49
- });
50
-
51
- it('different deletion cost should work', function() {
52
- expect(levenshteinDistance('ones', 'one', {deletion_cost: 3})).toBe(3);
53
- });
54
-
55
- it('different insertion cost should work', function() {
56
- expect(levenshteinDistance('one', 'ones', {deletion_cost: 3, insertion_cost: 5})).toBe(5);
57
- });
58
-
59
- it('delete all characters with -ve cost', function() {
60
- expect(levenshteinDistance('delete', '', {deletion_cost: -1})).toBe(-6);
61
- });
62
-
63
- it('insert all characters', function() {
64
- expect(levenshteinDistance('', 'insert')).toBe(6);
65
- });
66
- });
@@ -1,122 +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 = new require('../lib/natural'),
24
- LogisticRegressionClassifier = natural.LogisticRegressionClassifier;
25
-
26
- describe('logistic regression', function() {
27
- it('should classify with individually trained documents', function() {
28
- var classifier = new LogisticRegressionClassifier();
29
-
30
- classifier.addDocument(['have', 'computer'], 'IT');
31
- classifier.addDocument(['have', 'phone'], 'IT');
32
- classifier.addDocument(['computer', 'suck'], 'IT');
33
- classifier.addDocument(['field', 'goal'], 'sports');
34
- classifier.addDocument(['score', 'goal'], 'sports');
35
- classifier.addDocument(['great', 'speed'], 'sports');
36
-
37
- classifier.train();
38
-
39
- expect(classifier.classify(['hate', 'computer'])).toBe('IT');
40
- expect(classifier.classify(['score', 'please'])).toBe('sports');
41
- });
42
-
43
- it('should provide all classification scores', function() {
44
- var classifier = new natural.LogisticRegressionClassifier();
45
- classifier.addDocument(['fix', 'box'], 'computing');
46
- classifier.addDocument(['write', 'code'], 'computing');
47
- classifier.addDocument(['script', 'code'], 'computing');
48
- classifier.addDocument(['write', 'book'], 'literature');
49
- classifier.addDocument(['read', 'book'], 'literature');
50
- classifier.addDocument(['study', 'book'], 'literature');
51
-
52
- classifier.train();
53
-
54
- expect(classifier.getClassifications('i write code')[0].label).toBe('computing');
55
- expect(classifier.getClassifications('i write code')[1].label).toBe('literature');
56
- });
57
-
58
- it('should classify with arrays', function() {
59
- var classifier = new natural.LogisticRegressionClassifier();
60
- classifier.addDocument('i fixed the box', 'computing');
61
- classifier.addDocument('i write code', 'computing');
62
- classifier.addDocument('nasty script code', 'computing');
63
- classifier.addDocument('write a book', 'literature');
64
- classifier.addDocument('read a book', 'literature');
65
- classifier.addDocument('study the books', 'literature');
66
-
67
- classifier.train();
68
-
69
- expect(classifier.classify('a bug in the code')).toBe('computing');
70
- expect(classifier.classify('read all the books')).toBe('literature');
71
- });
72
-
73
- it('should serialize and deserialize a working classifier', function() {
74
- var classifier = new natural.LogisticRegressionClassifier();
75
- classifier.addDocument('i fixed the box', 'computing');
76
- classifier.addDocument('i write code', 'computing');
77
- classifier.addDocument('nasty script code', 'computing');
78
- classifier.addDocument('write a book', 'literature');
79
- classifier.addDocument('read a book', 'literature');
80
- classifier.addDocument('study the books', 'literature');
81
-
82
- var obj = JSON.stringify(classifier);
83
- var newClassifier = natural.LogisticRegressionClassifier.restore(JSON.parse(obj));
84
-
85
- newClassifier.addDocument('kick a ball', 'sports');
86
- newClassifier.addDocument('hit some balls', 'sports');
87
- newClassifier.addDocument('kick and punch', 'sports');
88
-
89
- newClassifier.train();
90
-
91
- expect(newClassifier.classify('a bug in the code')).toBe('computing');
92
- expect(newClassifier.classify('read all the books')).toBe('literature');
93
- expect(newClassifier.classify('kick butt')).toBe('sports');
94
- });
95
-
96
- it('should save and load a working classifier', function() {
97
- var classifier = new natural.LogisticRegressionClassifier();
98
- classifier.addDocument('i fixed the box', 'computing');
99
- classifier.addDocument('i write code', 'computing');
100
- classifier.addDocument('nasty script code', 'computing');
101
- classifier.addDocument('write a book', 'literature');
102
- classifier.addDocument('read a book', 'literature');
103
- classifier.addDocument('study the books', 'literature');
104
- classifier.train();
105
-
106
- classifier.save('lr_classifier.json', function(err) {
107
- natural.LogisticRegressionClassifier.load('lr_classifier.json', null,
108
- function(err, newClassifier){
109
- newClassifier.addDocument('hit some balls', 'sports');
110
- newClassifier.addDocument('kick a ball', 'sports');
111
- newClassifier.addDocument('kick and punch things', 'sports');
112
- newClassifier.train();
113
-
114
- expect(newClassifier.classify('a bug in the code')).toBe('computing');
115
- expect(newClassifier.classify('read all the books')).toBe('literature');
116
- expect(newClassifier.classify('kick butt')).toBe('sports');
117
-
118
- asyncSpecDone();
119
- });
120
- });
121
- });
122
- });
@@ -1,102 +0,0 @@
1
- /*
2
- Copyright (c) 2014, Lee Wenzhu
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
- 'use strict';
23
-
24
- var LPT = require('../lib/natural/util/longest_path_tree'),
25
- Topological = require('../lib/natural/util/topological'),
26
- EdgeWeightedDigraph = require('../lib/natural/util/edge_weighted_digraph');
27
-
28
- describe("shortest path tree", function() {
29
- var digraph = new EdgeWeightedDigraph();
30
- digraph.add(5,4,0.35);
31
- digraph.add(4,7,0.37);
32
- digraph.add(5,7,0.28);
33
- digraph.add(5,1,0.32);
34
- digraph.add(4,0,0.38);
35
- digraph.add(0,2,0.26);
36
- digraph.add(3,7,0.39);
37
- digraph.add(1,3,0.29);
38
- digraph.add(7,2,0.34);
39
- digraph.add(6,2,0.40);
40
- digraph.add(3,6,0.52);
41
- digraph.add(6,0,0.58);
42
- digraph.add(6,4,0.93);
43
- describe("edge weighted digraph normal operations", function() {
44
- it("should get the numbers of vertexs", function() {
45
- expect(digraph.v()).toBe(8);
46
- });
47
- it("should get the numbers of edges", function() {
48
- expect(digraph.e()).toBe(13);
49
- });
50
- it("should print all item in digraph", function() {
51
- expect(digraph.toString()).toBe('0 -> 2, 0.26\n' +
52
- '1 -> 3, 0.29\n' +
53
- '3 -> 7, 0.39\n' +
54
- '3 -> 6, 0.52\n' +
55
- '4 -> 7, 0.37\n' +
56
- '4 -> 0, 0.38\n' +
57
- '5 -> 4, 0.35\n' +
58
- '5 -> 7, 0.28\n' +
59
- '5 -> 1, 0.32\n' +
60
- '6 -> 2, 0.4\n' +
61
- '6 -> 0, 0.58\n' +
62
- '6 -> 4, 0.93\n' +
63
- '7 -> 2, 0.34');
64
- });
65
- });
66
-
67
- describe("topo sort for digraph", function() {
68
- it("should sort all the vertexs", function() {
69
- var topoSort = new Topological(digraph);
70
- expect(topoSort.isDAG()).toBe(true);
71
- expect(topoSort.order()).toEqual([ 5, 1, 3, 6, 4, 7, 0, 2 ]);
72
- });
73
- });
74
-
75
- describe("shortest path tree normal operations", function() {
76
- var lpt = new LPT(digraph, 5);
77
- expect(lpt.hasPathTo(0)).toBe(true);
78
- expect(lpt.hasPathTo(1)).toBe(true);
79
- expect(lpt.hasPathTo(2)).toBe(true);
80
- expect(lpt.hasPathTo(3)).toBe(true);
81
- expect(lpt.hasPathTo(4)).toBe(true);
82
- expect(lpt.hasPathTo(5)).toBe(false);
83
- expect(lpt.hasPathTo(6)).toBe(true);
84
- expect(lpt.hasPathTo(7)).toBe(true);
85
- expect(lpt.pathTo(0)).toEqual([ 5, 1, 3, 6, 4, 0 ]);
86
- expect(lpt.pathTo(1)).toEqual([ 5, 1 ]);
87
- expect(lpt.pathTo(2)).toEqual([ 5, 1, 3, 6, 4, 7, 2 ]);
88
- expect(lpt.pathTo(3)).toEqual([ 5, 1, 3 ]);
89
- expect(lpt.pathTo(4)).toEqual([ 5, 1, 3, 6, 4 ]);
90
- expect(lpt.pathTo(5)).toEqual([ ]);
91
- expect(lpt.pathTo(6)).toEqual([ 5, 1, 3, 6 ]);
92
- expect(lpt.pathTo(7)).toEqual([ 5, 1, 3, 6, 4, 7 ]);
93
- expect(lpt.getDistTo(0)).toBe(2.44);
94
- expect(lpt.getDistTo(1)).toBe(0.32);
95
- expect(lpt.getDistTo(2)).toBe(2.77);
96
- expect(lpt.getDistTo(3)).toBe(0.61);
97
- expect(lpt.getDistTo(4)).toBe(2.06);
98
- expect(lpt.getDistTo(5)).toBe(0);
99
- expect(lpt.getDistTo(6)).toBe(1.13);
100
- expect(lpt.getDistTo(7)).toBe(2.43);
101
- });
102
- });
@@ -1,252 +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 metaphone = require('../lib/natural/phonetics/metaphone');
24
-
25
- describe('metaphone', function() {
26
- it('should drop duplicate adjacent letters, except C', function() {
27
- expect(metaphone.dedup('dropping')).toBe('droping');
28
- });
29
-
30
- it('should not drop duplicat C', function() {
31
- expect(metaphone.dedup('accelerate')).toBe('accelerate');
32
- });
33
-
34
- it('should drop some initial letters', function() {
35
- expect(metaphone.dropInitialLetters('knuth')).toBe('nuth');
36
- expect(metaphone.dropInitialLetters('gnat')).toBe('nat');
37
- expect(metaphone.dropInitialLetters('aegis')).toBe('egis');
38
- expect(metaphone.dropInitialLetters('pneumatic')).toBe('neumatic');
39
- expect(metaphone.dropInitialLetters('wrack')).toBe('rack');
40
- });
41
-
42
- it('should not drop other initial letters', function() {
43
- expect(metaphone.dropInitialLetters('garbage')).toBe('garbage');
44
- });
45
-
46
- it('should b if if words end with mb', function() {
47
- expect(metaphone.dropBafterMAtEnd('dumb')).toBe('dum');
48
- });
49
-
50
- it('should not drop b after m if not at end of word', function() {
51
- expect(metaphone.dropBafterMAtEnd('dumbo')).toBe('dumbo');
52
- });
53
-
54
- it('should replace CH to X', function() {
55
- expect(metaphone.cTransform('change')).toBe('xhange');
56
- });
57
-
58
- it('should not replace CH to X if part of SCH', function() {
59
- expect(metaphone.cTransform('discharger')).toBe('diskharger');
60
- });
61
-
62
- it('should replace CIA to X', function() {
63
- expect(metaphone.cTransform('aesthetician')).toBe('aesthetixian');
64
- });
65
-
66
- it('C should become S if followed by I, E, or Y', function() {
67
- expect(metaphone.cTransform('cieling')).toBe('sieling');
68
- });
69
-
70
- it('should transform other C\'s to K', function() {
71
- expect(metaphone.cTransform('cuss')).toBe('kuss');
72
- });
73
-
74
- it('should transform D to J if followed by GE, GY, GI', function() {
75
- expect(metaphone.dTransform('abridge')).toBe('abrijge');
76
- });
77
-
78
- it('should transform D to T if not followed by GE, GY, GI', function() {
79
- expect(metaphone.dTransform('bid')).toBe('bit');
80
- });
81
-
82
- it('should drop G before H if not at the end or before vowell', function() {
83
- expect(metaphone.dropG('alight')).toBe('aliht');
84
- expect(metaphone.dropG('fright')).toBe('friht');
85
- });
86
-
87
- it('should drop G if followed by N or NED at the end', function() {
88
- expect(metaphone.dropG('aligned')).toBe('alined');
89
- expect(metaphone.dropG('align')).toBe('alin');
90
- });
91
-
92
-
93
- it('should transform G to J if followed by I, E or Y and not preceeded by G', function() {
94
- expect(metaphone.transformG('age')).toBe('aje');
95
- expect(metaphone.transformG('gin')).toBe('jin');
96
- });
97
-
98
- it('should transform G to K', function() {
99
- expect(metaphone.transformG('august')).toBe('aukust');
100
- });
101
-
102
- it('should reduce GG to G before turning to K', function() {
103
- expect(metaphone.transformG('aggrade')).toBe('akrade');
104
- });
105
-
106
- it('should drop H if after vowell and not before vowell', function() {
107
- expect(metaphone.dropH('alriht')).toBe('alrit');
108
- });
109
-
110
- it('should not drop H if after vowell', function() {
111
- expect(metaphone.dropH('that')).toBe('that');
112
- });
113
-
114
- it('should not drop H if not before vowell', function() {
115
- expect(metaphone.dropH('chump')).toBe('chump');
116
- });
117
-
118
- it('should transform CK to K', function() {
119
- expect(metaphone.transformCK('check')).toBe('chek');
120
- });
121
-
122
- it('should transform PH to F', function() {
123
- expect(metaphone.transformPH('phone')).toBe('fone');
124
- });
125
-
126
- it('should transform Q to K', function() {
127
- expect(metaphone.transformQ('quack')).toBe('kuack');
128
- });
129
-
130
- it('should transform S to X if followed by H, IO, or IA', function() {
131
- expect(metaphone.transformS('shack')).toBe('xhack');
132
- expect(metaphone.transformS('sialagogues')).toBe('xialagogues');
133
- expect(metaphone.transformS('asia')).toBe('axia');
134
- });
135
-
136
- it('should not transform S to X if not followed by H, IO, or IA', function() {
137
- expect(metaphone.transformS('substance')).toBe('substance');
138
- });
139
-
140
- it('should transform T to X if followed by IA or IO', function() {
141
- expect(metaphone.transformT('dementia')).toBe('demenxia');
142
- expect(metaphone.transformT('abbreviation')).toBe('abbreviaxion');
143
- });
144
-
145
- it('should transform TH to 0', function() {
146
- expect(metaphone.transformT('that')).toBe('0at');
147
- });
148
-
149
- it('should drop T if followed by CH', function() {
150
- expect(metaphone.dropT('backstitch')).toBe('backstich');
151
- });
152
-
153
- it('should transform V to F', function() {
154
- expect(metaphone.transformV('vestige')).toBe('festige');
155
- });
156
-
157
- it('should transform WH to W if at beginning', function() {
158
- expect(metaphone.transformWH('whisper')).toBe('wisper');
159
- });
160
-
161
- it('should drop W if not followed by vowell', function() {
162
- expect(metaphone.dropW('bowl')).toBe('bol');
163
- expect(metaphone.dropW('warsaw')).toBe('warsa');
164
- });
165
-
166
- it('should transform X to S if at beginning', function() {
167
- expect(metaphone.transformX('xenophile')).toBe('senophile');
168
- });
169
-
170
- it('should transform X to KS if not at beginning', function() {
171
- expect(metaphone.transformX('admixed')).toBe('admiksed');
172
- });
173
-
174
- it('should drop Y of not followed by a vowell', function() {
175
- expect(metaphone.dropY('analyzer')).toBe('analzer');
176
- expect(metaphone.dropY('specify')).toBe('specif');
177
- });
178
-
179
- it('should not drop Y of followed by a vowell', function() {
180
- expect(metaphone.dropY('allying')).toBe('allying');
181
- });
182
-
183
- it('should transform Z to S', function() {
184
- expect(metaphone.transformZ('blaze')).toBe('blase');
185
- });
186
-
187
- it('should drop all vowels except initial', function() {
188
- expect(metaphone.dropVowels('ablaze')).toBe('ablz');
189
- expect(metaphone.dropVowels('adamantium')).toBe('admntm');
190
- });
191
-
192
- it('should do all', function() {
193
- expect(metaphone.process('ablaze')).toBe('ABLS');
194
- expect(metaphone.process('transition')).toBe('TRNSXN');
195
- expect(metaphone.process('astronomical')).toBe('ASTRNMKL');
196
- expect(metaphone.process('buzzard')).toBe('BSRT');
197
- expect(metaphone.process('wonderer')).toBe('WNTRR');
198
- expect(metaphone.process('district')).toBe('TSTRKT');
199
- expect(metaphone.process('hockey')).toBe('HK');
200
- expect(metaphone.process('capital')).toBe('KPTL');
201
- expect(metaphone.process('penguin')).toBe('PNKN');
202
- expect(metaphone.process('garbonzo')).toBe('KRBNS');
203
- expect(metaphone.process('lightning')).toBe('LTNNK');
204
- expect(metaphone.process('light')).toBe('LT');
205
- });
206
-
207
- it('should compare strings', function() {
208
- expect(metaphone.compare('phonetics', 'fonetix')).toBeTruthy();
209
- expect(metaphone.compare('phonetics', 'garbonzo')).toBeFalsy();
210
- expect(metaphone.compare('PHONETICS', 'fonetix')).toBeTruthy();
211
- expect(metaphone.compare('PHONETICS', 'garbonzo')).toBeFalsy();
212
- });
213
-
214
- it('#173', function() {
215
- //expect(metaphone.process('caesar')).toBe('KSR');
216
- //expect(metaphone.process('chemical')).toBe('KMKL');
217
- });
218
-
219
- it('should compare strings with string patch', function() {
220
- metaphone.attach();
221
- expect('phonetics'.soundsLike('fonetix')).toBeTruthy();
222
- expect('phonetics'.soundsLike('garbonzo')).toBeFalsy();
223
- expect('PHONETICS'.soundsLike('fonetix')).toBeTruthy();
224
- expect('PHONETICS'.soundsLike('garbonzo')).toBeFalsy();
225
- });
226
-
227
- it('should return string phonetics with string patch', function() {
228
- metaphone.attach();
229
- expect('phonetics'.phonetics()).toBe('FNTKS');
230
- expect('PHONETICS'.phonetics()).toBe('FNTKS');
231
- });
232
-
233
- it('should transform PH to F #169', function() {
234
- expect('pharaoh'.phonetics()).toBe('FR');
235
- expect('tough'.phonetics()).toBe('TF');
236
- });
237
-
238
- it('should tokenize and return string phonetics with string patch', function() {
239
- metaphone.attach();
240
- expect('phonetics'.tokenizeAndPhoneticize()).toEqual(['FNTKS']);
241
- expect('phonetics garbonzo'.tokenizeAndPhoneticize()).toEqual(['FNTKS', 'KRBNS']);
242
- expect('phonetics garbonzo lightning'.tokenizeAndPhoneticize()).toEqual(['FNTKS', 'KRBNS', 'LTNNK']);
243
- });
244
-
245
- it('should truncate to length specified if code exceeds', function() {
246
- expect(metaphone.process('phonetics', 4)).toBe('FNTK');
247
- });
248
-
249
- it('should not truncate to length specified if code does not exceed', function() {
250
- expect(metaphone.process('phonetics', 8)).toBe('FNTKS');
251
- });
252
- });