natural 7.0.2 → 7.0.5

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 (72) hide show
  1. package/.github/workflows/npm-publish.yml +2 -0
  2. package/.nycrc +9 -0
  3. package/lib/natural/analyzers/sentence_analyzer.js +115 -113
  4. package/lib/natural/spellcheck/spellcheck.js +63 -61
  5. package/lib/natural/util/bag.js +27 -25
  6. package/package.json +3 -4
  7. package/tsconfig.json +1 -1
  8. package/ts_spec/MaxEntClassifier_spec.ts +0 -130
  9. package/ts_spec/SentimentAnalyzer_spec.ts +0 -232
  10. package/ts_spec/WordPunctTokenizer_spec.ts +0 -57
  11. package/ts_spec/aggressive_tokenizer_de_spec.ts +0 -36
  12. package/ts_spec/aggressive_tokenizer_es_spec.ts +0 -39
  13. package/ts_spec/aggressive_tokenizer_fr_spec.ts +0 -111
  14. package/ts_spec/aggressive_tokenizer_hi_spec.ts +0 -70
  15. package/ts_spec/aggressive_tokenizer_nl_spec.ts +0 -39
  16. package/ts_spec/aggressive_tokenizer_pt_spec.ts +0 -48
  17. package/ts_spec/aggressive_tokenizer_spec.ts +0 -57
  18. package/ts_spec/aggressive_tokenizer_sv_spec.ts +0 -52
  19. package/ts_spec/aggressive_tokenizer_vi_spec.ts +0 -48
  20. package/ts_spec/bayes_classifier_spec.ts +0 -165
  21. package/ts_spec/brill_pos_tagger_spec.ts +0 -79
  22. package/ts_spec/brill_pos_trainer_spec.ts +0 -107
  23. package/ts_spec/carry_stemmer_fr_spec.ts +0 -97
  24. package/ts_spec/classifier_spec.ts +0 -104
  25. package/ts_spec/count_inflector_fr_spec.ts +0 -47
  26. package/ts_spec/count_inflector_spec.ts +0 -76
  27. package/ts_spec/damerau_levenshtein_spec.ts +0 -53
  28. package/ts_spec/dice_coefficient_spec.ts +0 -37
  29. package/ts_spec/double_metaphone_spec.ts +0 -739
  30. package/ts_spec/hamming_distance_spec.ts +0 -66
  31. package/ts_spec/jaro-winkler_spec.ts +0 -79
  32. package/ts_spec/lancaster_stemmer_spec.ts +0 -63
  33. package/ts_spec/levenshtein_spec.ts +0 -107
  34. package/ts_spec/logistic_regression_classifier_spec.ts +0 -93
  35. package/ts_spec/longest_path_tree_spec.ts +0 -111
  36. package/ts_spec/metaphone_spec.ts +0 -258
  37. package/ts_spec/ngram_spec.ts +0 -145
  38. package/ts_spec/ngram_zh_spec.ts +0 -124
  39. package/ts_spec/normalizer_ja_spec.ts +0 -165
  40. package/ts_spec/normalizer_no_spec.ts +0 -112
  41. package/ts_spec/normalizer_spec.ts +0 -141
  42. package/ts_spec/noun_inflector_fr_spec.ts +0 -472
  43. package/ts_spec/noun_inflector_ja_spec.ts +0 -116
  44. package/ts_spec/noun_inflector_spec.ts +0 -369
  45. package/ts_spec/orthography_tokenizer_spec.ts +0 -47
  46. package/ts_spec/porter_stemmer_de_spec.ts +0 -53
  47. package/ts_spec/porter_stemmer_es_spec.ts +0 -63
  48. package/ts_spec/porter_stemmer_fr_spec.ts +0 -103
  49. package/ts_spec/porter_stemmer_it_spec.ts +0 -56
  50. package/ts_spec/porter_stemmer_nl_spec.ts +0 -73
  51. package/ts_spec/porter_stemmer_no_spec.ts +0 -148
  52. package/ts_spec/porter_stemmer_pt_spec.ts +0 -50
  53. package/ts_spec/porter_stemmer_ru_spec.ts +0 -67
  54. package/ts_spec/porter_stemmer_spec.ts +0 -202
  55. package/ts_spec/porter_stemmer_sv_spec.ts +0 -41
  56. package/ts_spec/present_verb_inflector_spec.ts +0 -127
  57. package/ts_spec/remove_diacritics_spec.ts +0 -30
  58. package/ts_spec/sentence_analyzer_spec.ts +0 -293
  59. package/ts_spec/sentence_tokenizer_parser_spec.ts +0 -200
  60. package/ts_spec/sentence_tokenizer_spec.ts +0 -189
  61. package/ts_spec/shortest_path_tree_spec.ts +0 -141
  62. package/ts_spec/soundex_spec.ts +0 -162
  63. package/ts_spec/spellcheck_spec.ts +0 -112
  64. package/ts_spec/stemmer_id_spec.ts +0 -374
  65. package/ts_spec/stemmer_ja_spec.ts +0 -69
  66. package/ts_spec/stemmer_token_spec.ts +0 -163
  67. package/ts_spec/tfidf_spec.ts +0 -275
  68. package/ts_spec/tokenizer_case_spec.ts +0 -242
  69. package/ts_spec/tokenizer_ja_spec.ts +0 -63
  70. package/ts_spec/transliterator_ja_spec.ts +0 -105
  71. package/ts_spec/treebank_word_tokenizer_spec.ts +0 -38
  72. package/ts_spec/trie_spec.ts +0 -197
@@ -1,66 +0,0 @@
1
- /*
2
- Copyright (c) 2018, Hugo ter Doest
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
- 'use strict'
24
-
25
- import { HammingDistance as hammingDistance } from 'lib/natural'
26
-
27
- // Convert the following to a Javascript object
28
- const stringsToCompare = [{
29
- string1: 'karolin',
30
- string2: 'kathrin',
31
- ignoreCase: false,
32
- expected: 3
33
- }, {
34
- string1: 'karolin',
35
- string2: 'kerstin',
36
- ignoreCase: false,
37
- expected: 3
38
- }, {
39
- string1: '1011101',
40
- string2: '1001001',
41
- ignoreCase: false,
42
- expected: 2
43
- }, {
44
- string1: '2173896',
45
- string2: '2233796',
46
- ignoreCase: false,
47
- expected: 3
48
- }, {
49
- string1: 'different',
50
- string2: 'length',
51
- ignoreCase: false,
52
- expected: -1
53
- }, {
54
- string1: 'ignorecase',
55
- string2: 'IgnoreCase',
56
- ignoreCase: true,
57
- expected: 0
58
- }]
59
-
60
- describe('The Hamming distance function compares strings of equal length', function () {
61
- stringsToCompare.forEach(function (record) {
62
- it('should calculate the difference between two strings correctly', function () {
63
- expect(hammingDistance(record.string1, record.string2, record.ignoreCase)).toEqual(record.expected)
64
- })
65
- })
66
- })
@@ -1,79 +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
- 'use strict'
24
-
25
- import { JaroWinklerDistance } from 'lib/natural'
26
-
27
- /*
28
- Do not change the prototype of native types
29
- Number.prototype.approxEql = function (val) {
30
- return Math.abs(this - val) < 1e-5
31
- }
32
- */
33
-
34
- function approxEql (val1: number, val2: number): boolean {
35
- return Math.abs(val1 - val2) < 1e-5
36
- }
37
-
38
- describe('jaro-winkler', function () {
39
- it('should evaluate string similarity', function () {
40
- expect(approxEql(JaroWinklerDistance('DIXON', 'DICKSONX', {}), 0.81333)).toBeTruthy()
41
- expect(approxEql(JaroWinklerDistance('DWAYNE', 'DUANE', {}), 0.84)).toBeTruthy()
42
- })
43
-
44
- it('should handle exact matches', function () {
45
- expect(JaroWinklerDistance('RICK', 'RICK', {})).toBe(1)
46
- expect(JaroWinklerDistance('abc', 'abc', {})).toBe(1)
47
- expect(JaroWinklerDistance('abcd', 'abcd', {})).toBe(1)
48
- expect(JaroWinklerDistance('seddon', 'seddon', {})).toBe(1)
49
- })
50
-
51
- it('should handle total mis-matches', function () {
52
- expect(JaroWinklerDistance('NOT', 'SAME', {})).toBe(0)
53
- })
54
-
55
- it('should handle partial mis-matches', function () {
56
- expect(approxEql(JaroWinklerDistance('aaa', 'abcd', {}), 0.575)).toBeTruthy()
57
- })
58
-
59
- it('should handle transpositions', function () {
60
- expect(approxEql(JaroWinklerDistance('MARTHA', 'MARHTA', {}), 0.96111)).toBeTruthy()
61
- })
62
-
63
- it('should handle transpositions regardless of string order', function () {
64
- expect(approxEql(JaroWinklerDistance('class', 'clams', {}), 0.90666)).toBeTruthy()
65
- expect(approxEql(JaroWinklerDistance('clams', 'class', {}), 0.90666)).toBeTruthy()
66
- })
67
-
68
- it('should ignore case when asked to', function () {
69
- expect(JaroWinklerDistance('aaa', 'aAa', { ignoreCase: true })).toEqual(JaroWinklerDistance('aaa', 'aaa', {}))
70
- expect(JaroWinklerDistance('aaa', 'aAa', {})).not.toEqual(JaroWinklerDistance('aaa', 'aaa', {}))
71
- expect(JaroWinklerDistance('dixon', 'DICKSONX', { ignoreCase: true })).toEqual(JaroWinklerDistance('DIXON', 'DICKSONX', {}))
72
- expect(JaroWinklerDistance('seddon', 'SEDDON', { ignoreCase: true })).toEqual(JaroWinklerDistance('SEDDON', 'SEDDON', {}))
73
- expect(approxEql(JaroWinklerDistance('MARTHA', 'MARHTA', { ignoreCase: true }), 0.96111)).toBeTruthy()
74
- expect(JaroWinklerDistance('abcd', 'ABCD', { ignoreCase: true })).toBe(1)
75
- })
76
- it('should not fail while passing no options', function () {
77
- expect(approxEql(JaroWinklerDistance('check', 'chakk'), 0.78666)).toBeTruthy()
78
- })
79
- })
@@ -1,63 +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
- 'use strict'
24
-
25
- import { LancasterStemmer as stemmer } from 'lib/natural'
26
-
27
- describe('lancaster_stemmer', function () {
28
- it('should stem', function () {
29
- expect(stemmer.stem('marks')).toBe('mark')
30
- expect(stemmer.stem('MARKs')).toBe('mark')
31
- })
32
-
33
- it('should stop running rules where appropriate', function () {
34
- expect(stemmer.stem('living')).toBe('liv')
35
- expect(stemmer.stem('thing')).toBe('thing')
36
- expect(stemmer.stem('ear')).toBe('ear')
37
- expect(stemmer.stem('string')).toBe('string')
38
- })
39
-
40
- it('should only pop the size specified by the rule', function () {
41
- expect(stemmer.stem('triplicate')).toBe('triply')
42
- expect(stemmer.stem('triPlicAte')).toBe('triply')
43
- })
44
-
45
- it('should stem and append and recurse', function () {
46
- expect(stemmer.stem('classified')).toBe('class')
47
- expect(stemmer.stem('ClaSsiFied')).toBe('class')
48
- })
49
-
50
- it('should apply intact rules only to intact string', function () {
51
- expect(stemmer.stem('maximum')).toBe('maxim')
52
- expect(stemmer.stem('presumably')).toBe('presum')
53
- expect(stemmer.stem('MAXIMUM')).toBe('maxim')
54
- expect(stemmer.stem('PRESUMABLY')).toBe('presum')
55
- })
56
-
57
- it('#171 and #174, exceed, anguish, affluxion, discept', function () {
58
- expect(stemmer.stem('exceed')).toBe('excess')
59
- expect(stemmer.stem('anguish')).toBe('anct')
60
- expect(stemmer.stem('affluxion')).toBe('affluct')
61
- expect(stemmer.stem('discept')).toBe('disceiv')
62
- })
63
- })
@@ -1,107 +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
- 'use strict'
24
-
25
- import {
26
- LevenshteinDistance as levenshteinDistance,
27
- LevenshteinDistanceSearch as levenshteinDistanceSearch
28
- } from 'lib/natural/distance/levenshtein_distance'
29
-
30
- describe('levenshtein_distance', function () {
31
- describe('options.search = true', function () {
32
- it('should find cheapest substring', function () {
33
- expect(levenshteinDistanceSearch('kitten', 'sitting'))
34
- .toEqual({ substring: 'sittin', distance: 2, offset: 0 })
35
- })
36
-
37
- it('should find 0 cost substring in target', function () {
38
- expect(levenshteinDistanceSearch('doctor', 'the doctor is in'))
39
- .toEqual({ substring: 'doctor', distance: 0, offset: 4 })
40
- })
41
-
42
- it('should find 1 cost substring in target', function () {
43
- expect(levenshteinDistanceSearch('doctor', 'the doktor is in'))
44
- .toEqual({ substring: 'doktor', distance: 1, offset: 4 })
45
- })
46
-
47
- it('should return empty substring when that is cleapest match', function () {
48
- expect(levenshteinDistanceSearch('doctor', '000000000000'))
49
- .toEqual({ substring: '', distance: 6, offset: 0 })
50
- })
51
-
52
- it('different insertion costs should work', function () {
53
- // delete 10 0's at cost 1 and insert the letters for doctor at cost -1
54
- expect(levenshteinDistanceSearch('0000000000', 'doctor', { insertion_cost: -1 }))
55
- .toEqual({ substring: 'doctor', distance: 4, offset: 0 })
56
- })
57
-
58
- it('different deletion costs should work', function () {
59
- // delete 10 0's at cost -10
60
- expect(levenshteinDistanceSearch('0000000000', 'doctor', { deletion_cost: -1 }))
61
- .toEqual({ substring: '', distance: -10, offset: 0 })
62
- })
63
- })
64
-
65
- describe('default / options.search = false', function () {
66
- it('should replace 2', function () {
67
- expect(levenshteinDistance('doctor', 'doktor')).toBe(1)
68
- })
69
-
70
- it('should allow altering replacement value', function () {
71
- expect(levenshteinDistance('doctor', 'doktor', { substitution_cost: 1 })).toBe(1)
72
- })
73
-
74
- it('should delete 1', function () {
75
- expect(levenshteinDistance('doctor', 'docto')).toBe(1)
76
- })
77
-
78
- it('should insert 1', function () {
79
- expect(levenshteinDistance('flat', 'flats')).toBe(1)
80
- })
81
-
82
- it('should combine operations', function () {
83
- expect(levenshteinDistance('flad', 'flaten')).toBe(3)
84
- expect(levenshteinDistance('flaten', 'flad')).toBe(3)
85
- })
86
-
87
- it('should consider perfect matches 0', function () {
88
- expect(levenshteinDistance('one', 'one')).toBe(0)
89
- })
90
-
91
- it('different deletion cost should work', function () {
92
- expect(levenshteinDistance('ones', 'one', { deletion_cost: 3 })).toBe(3)
93
- })
94
-
95
- it('different insertion cost should work', function () {
96
- expect(levenshteinDistance('one', 'ones', { deletion_cost: 3, insertion_cost: 5 })).toBe(5)
97
- })
98
-
99
- it('delete all characters with -ve cost', function () {
100
- expect(levenshteinDistance('delete', '', { deletion_cost: -1 })).toBe(-6)
101
- })
102
-
103
- it('insert all characters', function () {
104
- expect(levenshteinDistance('', 'insert')).toBe(6)
105
- })
106
- })
107
- })
@@ -1,93 +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
- 'use strict'
24
-
25
- import { LogisticRegressionClassifier } from 'lib/natural'
26
-
27
- describe('logistic regression', function () {
28
- it('should classify with individually trained documents', function () {
29
- const classifier = new LogisticRegressionClassifier()
30
-
31
- classifier.addDocument(['have', 'computer'], 'IT')
32
- classifier.addDocument(['have', 'phone'], 'IT')
33
- classifier.addDocument(['computer', 'suck'], 'IT')
34
- classifier.addDocument(['field', 'goal'], 'sports')
35
- classifier.addDocument(['score', 'goal'], 'sports')
36
- classifier.addDocument(['great', 'speed'], 'sports')
37
-
38
- classifier.train()
39
-
40
- expect(classifier.classify(['hate', 'computer'])).toBe('IT')
41
- expect(classifier.classify(['score', 'please'])).toBe('sports')
42
- })
43
-
44
- it('should provide all classification scores', function () {
45
- const classifier = new LogisticRegressionClassifier()
46
- classifier.addDocument(['fix', 'box'], 'computing')
47
- classifier.addDocument(['write', 'code'], 'computing')
48
- classifier.addDocument(['script', 'code'], 'computing')
49
- classifier.addDocument(['write', 'book'], 'literature')
50
- classifier.addDocument(['read', 'book'], 'literature')
51
- classifier.addDocument(['study', 'book'], 'literature')
52
-
53
- classifier.train()
54
-
55
- expect(classifier.getClassifications('i write code')[0].label).toBe('computing')
56
- expect(classifier.getClassifications('i write code')[1].label).toBe('literature')
57
- })
58
-
59
- function createClassifier (): LogisticRegressionClassifier {
60
- const classifier = new LogisticRegressionClassifier()
61
- classifier.addDocument('i fixed the box', 'computing')
62
- classifier.addDocument('i write code', 'computing')
63
- classifier.addDocument('nasty script code', 'computing')
64
- classifier.addDocument('write a book', 'literature')
65
- classifier.addDocument('read a book', 'literature')
66
- classifier.addDocument('study the books', 'literature')
67
- return classifier
68
- }
69
-
70
- it('should classify with arrays', function () {
71
- const classifier = createClassifier()
72
- classifier.train()
73
- expect(classifier.classify('a bug in the code')).toBe('computing')
74
- expect(classifier.classify('read all the books')).toBe('literature')
75
- })
76
-
77
- it('should serialize and deserialize a working classifier', function () {
78
- const classifier = createClassifier()
79
- const objString = JSON.stringify(classifier)
80
- const obj: Record<string, unknown> = JSON.parse(objString)
81
- const newClassifier = LogisticRegressionClassifier.restore(obj)
82
-
83
- newClassifier.addDocument('kick a ball', 'sports')
84
- newClassifier.addDocument('hit some balls', 'sports')
85
- newClassifier.addDocument('kick and punch', 'sports')
86
-
87
- newClassifier.train()
88
-
89
- expect(newClassifier.classify('a bug in the code')).toBe('computing')
90
- expect(newClassifier.classify('read all the books')).toBe('literature')
91
- expect(newClassifier.classify('kick butt')).toBe('sports')
92
- })
93
- })
@@ -1,111 +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
-
23
- 'use strict'
24
-
25
- import {
26
- LongestPathTree as LPT,
27
- Topological,
28
- EdgeWeightedDigraph
29
- } from 'lib/natural'
30
-
31
- describe('shortest path tree', function () {
32
- const digraph = new EdgeWeightedDigraph()
33
- digraph.add(5, 4, 0.35)
34
- digraph.add(4, 7, 0.37)
35
- digraph.add(5, 7, 0.28)
36
- digraph.add(5, 1, 0.32)
37
- digraph.add(4, 0, 0.38)
38
- digraph.add(0, 2, 0.26)
39
- digraph.add(3, 7, 0.39)
40
- digraph.add(1, 3, 0.29)
41
- digraph.add(7, 2, 0.34)
42
- digraph.add(6, 2, 0.40)
43
- digraph.add(3, 6, 0.52)
44
- digraph.add(6, 0, 0.58)
45
- digraph.add(6, 4, 0.93)
46
- describe('edge weighted digraph normal operations', function () {
47
- it('should get the numbers of vertexs', function () {
48
- expect(digraph.v()).toBe(8)
49
- })
50
- it('should get the numbers of edges', function () {
51
- expect(digraph.e()).toBe(13)
52
- })
53
- it('should print all item in digraph', function () {
54
- expect(digraph.toString()).toBe('0 -> 2, 0.26\n' +
55
- '1 -> 3, 0.29\n' +
56
- '3 -> 7, 0.39\n' +
57
- '3 -> 6, 0.52\n' +
58
- '4 -> 7, 0.37\n' +
59
- '4 -> 0, 0.38\n' +
60
- '5 -> 4, 0.35\n' +
61
- '5 -> 7, 0.28\n' +
62
- '5 -> 1, 0.32\n' +
63
- '6 -> 2, 0.4\n' +
64
- '6 -> 0, 0.58\n' +
65
- '6 -> 4, 0.93\n' +
66
- '7 -> 2, 0.34')
67
- })
68
- })
69
-
70
- describe('topo sort for digraph', function () {
71
- it('should sort all the vertexs', function () {
72
- const topoSort = new Topological(digraph)
73
- expect(topoSort.isDAG()).toBe(true)
74
- expect(topoSort.order()).toEqual([5, 1, 3, 6, 4, 7, 0, 2])
75
- })
76
- })
77
-
78
- describe('shortest path tree normal operations', function () {
79
- const lpt = new LPT(digraph, 5)
80
- it('should determine existence of paths', function () {
81
- expect(lpt.hasPathTo(0)).toBe(true)
82
- expect(lpt.hasPathTo(1)).toBe(true)
83
- expect(lpt.hasPathTo(2)).toBe(true)
84
- expect(lpt.hasPathTo(3)).toBe(true)
85
- expect(lpt.hasPathTo(4)).toBe(true)
86
- expect(lpt.hasPathTo(5)).toBe(false)
87
- expect(lpt.hasPathTo(6)).toBe(true)
88
- expect(lpt.hasPathTo(7)).toBe(true)
89
- })
90
- it('should determine paths', function () {
91
- expect(lpt.pathTo(0)).toEqual([5, 1, 3, 6, 4, 0])
92
- expect(lpt.pathTo(1)).toEqual([5, 1])
93
- expect(lpt.pathTo(2)).toEqual([5, 1, 3, 6, 4, 7, 2])
94
- expect(lpt.pathTo(3)).toEqual([5, 1, 3])
95
- expect(lpt.pathTo(4)).toEqual([5, 1, 3, 6, 4])
96
- expect(lpt.pathTo(5)).toEqual([])
97
- expect(lpt.pathTo(6)).toEqual([5, 1, 3, 6])
98
- expect(lpt.pathTo(7)).toEqual([5, 1, 3, 6, 4, 7])
99
- })
100
- it('should calculate distances', function () {
101
- expect(lpt.getDistTo(0)).toBe(2.44)
102
- expect(lpt.getDistTo(1)).toBe(0.32)
103
- expect(lpt.getDistTo(2)).toBe(2.77)
104
- expect(lpt.getDistTo(3)).toBe(0.61)
105
- expect(lpt.getDistTo(4)).toBe(2.06)
106
- expect(lpt.getDistTo(5)).toBe(0)
107
- expect(lpt.getDistTo(6)).toBe(1.13)
108
- expect(lpt.getDistTo(7)).toBe(2.43)
109
- })
110
- })
111
- })