natural 6.11.1 → 7.0.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 (116) hide show
  1. package/.env +9 -7
  2. package/.eslintrc.json +13 -0
  3. package/.github/workflows/node.js.yml +57 -19
  4. package/lib/natural/analyzers/SenType.ts +30 -0
  5. package/lib/natural/analyzers/index.d.ts +6 -10
  6. package/lib/natural/brill_pos_tagger/index.d.ts +23 -14
  7. package/lib/natural/brill_pos_tagger/index.js +1 -1
  8. package/lib/natural/classifiers/bayes_classifier.js +6 -1
  9. package/lib/natural/classifiers/classifier.js +19 -32
  10. package/lib/natural/classifiers/classifier_train_parallel.js +7 -5
  11. package/lib/natural/classifiers/index.d.ts +36 -15
  12. package/lib/natural/classifiers/logistic_regression_classifier.js +7 -2
  13. package/lib/natural/classifiers/maxent/Classifier.js +5 -3
  14. package/lib/natural/distance/index.d.ts +11 -14
  15. package/lib/natural/inflectors/count_inflector.js +18 -21
  16. package/lib/natural/inflectors/fr/count_inflector.js +18 -19
  17. package/lib/natural/inflectors/index.d.ts +12 -3
  18. package/lib/natural/inflectors/index.js +3 -0
  19. package/lib/natural/ngrams/index.d.ts +5 -5
  20. package/lib/natural/normalizers/index.d.ts +24 -1
  21. package/lib/natural/normalizers/index.js +4 -1
  22. package/lib/natural/normalizers/normalizer_ja.js +109 -72
  23. package/lib/natural/phonetics/dm_soundex.js +52 -51
  24. package/lib/natural/phonetics/double_metaphone.js +397 -399
  25. package/lib/natural/phonetics/index.d.ts +42 -12
  26. package/lib/natural/phonetics/metaphone.js +134 -157
  27. package/lib/natural/phonetics/phonetic.js +5 -29
  28. package/lib/natural/phonetics/soundex.js +45 -54
  29. package/lib/natural/sentiment/index.d.ts +6 -6
  30. package/lib/natural/spellcheck/index.d.ts +2 -0
  31. package/lib/natural/stemmers/index.d.ts +43 -1
  32. package/lib/natural/stemmers/index.js +1 -0
  33. package/lib/natural/stemmers/token.js +131 -132
  34. package/lib/natural/tfidf/index.d.ts +7 -7
  35. package/lib/natural/tfidf/tfidf.js +7 -1
  36. package/lib/natural/tokenizers/index.d.ts +2 -2
  37. package/lib/natural/trie/index.d.ts +1 -1
  38. package/lib/natural/util/index.d.ts +8 -0
  39. package/lib/natural/util/index.js +2 -0
  40. package/lib/natural/util/storage/File.js +22 -0
  41. package/lib/natural/util/storage/Memcached.js +22 -0
  42. package/lib/natural/util/storage/MongoDB.js +31 -3
  43. package/lib/natural/util/storage/Postgres.js +49 -32
  44. package/lib/natural/util/storage/Redis.js +22 -0
  45. package/lib/natural/util/storage/StorageBackend.js +23 -0
  46. package/lib/natural/util/storage/index.d.ts +39 -0
  47. package/lib/natural/util/storage/index.js +24 -0
  48. package/package.json +22 -9
  49. package/ts_spec/MaxEntClassifier_spec.ts +130 -0
  50. package/ts_spec/SentimentAnalyzer_spec.ts +232 -0
  51. package/ts_spec/WordPunctTokenizer_spec.ts +57 -0
  52. package/ts_spec/aggressive_tokenizer_de_spec.ts +36 -0
  53. package/ts_spec/aggressive_tokenizer_es_spec.ts +39 -0
  54. package/ts_spec/aggressive_tokenizer_fr_spec.ts +111 -0
  55. package/ts_spec/aggressive_tokenizer_hi_spec.ts +70 -0
  56. package/ts_spec/aggressive_tokenizer_nl_spec.ts +39 -0
  57. package/ts_spec/aggressive_tokenizer_pt_spec.ts +48 -0
  58. package/ts_spec/aggressive_tokenizer_spec.ts +57 -0
  59. package/ts_spec/aggressive_tokenizer_sv_spec.ts +52 -0
  60. package/ts_spec/aggressive_tokenizer_vi_spec.ts +48 -0
  61. package/ts_spec/bayes_classifier_spec.ts +165 -0
  62. package/ts_spec/brill_pos_tagger_spec.ts +79 -0
  63. package/ts_spec/brill_pos_trainer_spec.ts +107 -0
  64. package/ts_spec/carry_stemmer_fr_spec.ts +97 -0
  65. package/ts_spec/classifier_spec.ts +104 -0
  66. package/ts_spec/count_inflector_fr_spec.ts +47 -0
  67. package/ts_spec/count_inflector_spec.ts +76 -0
  68. package/ts_spec/damerau_levenshtein_spec.ts +53 -0
  69. package/ts_spec/dice_coefficient_spec.ts +37 -0
  70. package/ts_spec/double_metaphone_spec.ts +739 -0
  71. package/ts_spec/hamming_distance_spec.ts +66 -0
  72. package/ts_spec/jaro-winkler_spec.ts +79 -0
  73. package/ts_spec/lancaster_stemmer_spec.ts +63 -0
  74. package/ts_spec/levenshtein_spec.ts +107 -0
  75. package/ts_spec/logistic_regression_classifier_spec.ts +93 -0
  76. package/ts_spec/longest_path_tree_spec.ts +111 -0
  77. package/ts_spec/metaphone_spec.ts +258 -0
  78. package/ts_spec/ngram_spec.ts +145 -0
  79. package/ts_spec/ngram_zh_spec.ts +124 -0
  80. package/ts_spec/normalizer_ja_spec.ts +165 -0
  81. package/ts_spec/normalizer_no_spec.ts +112 -0
  82. package/ts_spec/normalizer_spec.ts +141 -0
  83. package/ts_spec/noun_inflector_fr_spec.ts +472 -0
  84. package/ts_spec/noun_inflector_ja_spec.ts +116 -0
  85. package/ts_spec/noun_inflector_spec.ts +369 -0
  86. package/ts_spec/orthography_tokenizer_spec.ts +47 -0
  87. package/ts_spec/porter_stemmer_de_spec.ts +53 -0
  88. package/ts_spec/porter_stemmer_es_spec.ts +63 -0
  89. package/ts_spec/porter_stemmer_fr_spec.ts +103 -0
  90. package/ts_spec/porter_stemmer_it_spec.ts +56 -0
  91. package/ts_spec/porter_stemmer_nl_spec.ts +73 -0
  92. package/ts_spec/porter_stemmer_no_spec.ts +148 -0
  93. package/ts_spec/porter_stemmer_pt_spec.ts +50 -0
  94. package/ts_spec/porter_stemmer_ru_spec.ts +67 -0
  95. package/ts_spec/porter_stemmer_spec.ts +202 -0
  96. package/ts_spec/porter_stemmer_sv_spec.ts +41 -0
  97. package/ts_spec/present_verb_inflector_spec.ts +127 -0
  98. package/ts_spec/remove_diacritics_spec.ts +30 -0
  99. package/ts_spec/sentence_analyzer_spec.ts +293 -0
  100. package/ts_spec/sentence_tokenizer_parser_spec.ts +200 -0
  101. package/ts_spec/sentence_tokenizer_spec.ts +189 -0
  102. package/ts_spec/shortest_path_tree_spec.ts +141 -0
  103. package/ts_spec/soundex_spec.ts +162 -0
  104. package/ts_spec/spellcheck_spec.ts +112 -0
  105. package/ts_spec/stemmer_id_spec.ts +374 -0
  106. package/ts_spec/stemmer_ja_spec.ts +69 -0
  107. package/ts_spec/stemmer_token_spec.ts +163 -0
  108. package/ts_spec/tfidf_spec.ts +275 -0
  109. package/ts_spec/tokenizer_case_spec.ts +242 -0
  110. package/ts_spec/tokenizer_ja_spec.ts +63 -0
  111. package/ts_spec/transliterator_ja_spec.ts +105 -0
  112. package/ts_spec/treebank_word_tokenizer_spec.ts +38 -0
  113. package/ts_spec/trie_spec.ts +197 -0
  114. package/tsconfig.json +5 -3
  115. package/example.txt +0 -1
  116. package/f2f5d4bc-daa0-4392-8981-9614cd0c802e.json +0 -1
@@ -0,0 +1,79 @@
1
+ /*
2
+ Unit test for Brill's POS Tagger: test against the pos module
3
+ Copyright (C) 2019 Hugo W.L. ter Doest
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ 'use strict'
20
+
21
+ import { WordTokenizer, Lexicon, RuleSet, BrillPOSTagger } from 'lib/natural'
22
+ import type { Sentence, TagResults } from 'lib/natural'
23
+
24
+ import * as englishSentences from '../spec/test_data/NYT-20150205-picassos-granddaughter-plans-to-sell-art-worrying-the-market.json'
25
+
26
+ import jsonData from '../spec/test_data/NYT-20150205-picassos-granddaughter-plans_expected_tag_results.json'
27
+
28
+ import * as dutchSentences from '../spec/test_data/Volkskrant-20150205-Knot-geldpers-aanzetten-is-paardenmiddel-voor-half-procent-inflatie.json'
29
+
30
+ const englishTagResults: TagResults = jsonData as TagResults
31
+ const DEBUG = false
32
+
33
+ // Compares two tagged sentences. First one is in the old POSJS format, i.e.
34
+ // an array of two position arrays. The second one is a Sentence object
35
+ // that holds an array of objects for each position {token: "string", tag: "string"}
36
+ function compareTaggedSentences (sentenceInOldFormat: string[][], sentenceInNewFormat: Sentence): boolean {
37
+ let equal = true
38
+ sentenceInOldFormat.forEach(function (wordPlusTag: string[], index: number) {
39
+ equal = equal &&
40
+ (wordPlusTag[1] === sentenceInNewFormat.taggedWords[index].tag)
41
+ DEBUG && console.log(wordPlusTag[1] + ' ' + sentenceInNewFormat.taggedWords[index].tag)
42
+ })
43
+ return equal
44
+ }
45
+
46
+ describe('Brill\'s POS Tagger', function () {
47
+ let brillPOSTagger: BrillPOSTagger
48
+ let lexicon: Lexicon
49
+ let ruleSet: RuleSet
50
+ const tokenizer = new WordTokenizer()
51
+
52
+ it('should process an English newspaper article just like the dariusk/pos-js module', function () {
53
+ lexicon = new Lexicon('EN', 'NN')
54
+ expect(lexicon.nrEntries()).toBeGreaterThan(0)
55
+ ruleSet = new RuleSet('EN')
56
+ expect(ruleSet.nrRules()).toBeGreaterThan(0)
57
+ brillPOSTagger = new BrillPOSTagger(lexicon, ruleSet)
58
+
59
+ englishSentences.sentences.forEach(function (sentence: string, index: number) {
60
+ const tokenizedSentence = tokenizer.tokenize(sentence)
61
+ const taggedSentence = brillPOSTagger.tag(tokenizedSentence)
62
+ expect(compareTaggedSentences(englishTagResults.results[index], taggedSentence)).toBe(true)
63
+ })
64
+ })
65
+
66
+ it('should tag a Dutch news paper article', function () {
67
+ lexicon = new Lexicon('DU', 'N')
68
+ expect(lexicon.nrEntries()).toBeGreaterThan(0)
69
+ const ruleSet = new RuleSet('DU')
70
+ expect(ruleSet.nrRules()).toBeGreaterThan(0)
71
+ brillPOSTagger = new BrillPOSTagger(lexicon, ruleSet)
72
+
73
+ dutchSentences.sentences.forEach(function (sentence, index) {
74
+ const tokenizedSentence = tokenizer.tokenize(sentence)
75
+ const taggedSentence = brillPOSTagger.tag(tokenizedSentence)
76
+ expect(tokenizedSentence.length).toEqual(taggedSentence.taggedWords.length)
77
+ })
78
+ })
79
+ })
@@ -0,0 +1,107 @@
1
+ /*
2
+ Unit test for Brill's POS Trainer
3
+ Copyright (C) 2019 Hugo W.L. ter Doest
4
+
5
+ This program is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ 'use strict'
20
+
21
+ import {
22
+ Corpus,
23
+ ruleTemplates,
24
+ RuleTemplate,
25
+ Sentence,
26
+ BrillPOSTrainer,
27
+ BrillPOSTagger,
28
+ BrillPOSTester
29
+ } from 'lib/natural'
30
+
31
+ import type {
32
+ Lexicon,
33
+ RuleSet
34
+ } from 'lib/natural'
35
+
36
+ // Import a JSON file with the brown corpus
37
+ import brownCorpus from '../spec/test_data/browntag_nolines_excerpt.json'
38
+
39
+ const DEBUG = false
40
+ const JSON_FLAG = 2
41
+
42
+ function selectRuleTemplates (templateNames: string[]): RuleTemplate[] {
43
+ const templates: RuleTemplate[] = []
44
+ templateNames.forEach(function (name: string) {
45
+ if (ruleTemplates[name] !== undefined) {
46
+ const template = new RuleTemplate(name, ruleTemplates[name])
47
+ templates.push(template)
48
+ }
49
+ })
50
+ return templates
51
+ }
52
+
53
+ describe('Brill\'s POS Trainer', function () {
54
+ let corpus = null
55
+ let corpora: Corpus[]
56
+ const percentageTrain = 60
57
+ let trainLexicon: Lexicon
58
+ // Templates consider only tags, no words
59
+ const templateNames = [
60
+ 'NEXT-TAG',
61
+ 'PREV-TAG',
62
+ 'PREV-1-OR-2-OR-3-TAG',
63
+ 'PREV-1-OR-2-TAG',
64
+ 'NEXT1OR2TAG',
65
+ 'NEXT1OR2OR3TAG',
66
+ 'SURROUNDTAG',
67
+ 'PREV2TAG',
68
+ 'NEXT2TAG'
69
+ ]
70
+ let templates: RuleTemplate[]
71
+ let trainer = null
72
+ let ruleSet: RuleSet
73
+
74
+ it('should split the corpus in a training and testing corpus', function () {
75
+ corpus = new Corpus(brownCorpus, JSON_FLAG, Sentence)
76
+ DEBUG && console.log('Corpus: ' + JSON.stringify(corpus, null, 2))
77
+ corpora = corpus.splitInTrainAndTest(percentageTrain)
78
+ expect(corpora[0].nrSentences() + corpora[1].nrSentences()).toEqual(corpus.nrSentences())
79
+ })
80
+
81
+ it('should build a lexicon from the training corpus', function () {
82
+ trainLexicon = corpora[0].buildLexicon()
83
+ // Set default category to noun (NN)
84
+ // and default category for capitalised words to proper noun (NP)
85
+ trainLexicon.setDefaultCategories('NN', 'NP')
86
+ expect(trainLexicon.nrEntries()).not.toEqual(0)
87
+ })
88
+
89
+ it('should set up the rule templates', function () {
90
+ templates = selectRuleTemplates(templateNames)
91
+ expect(templates.length).toEqual(templateNames.length)
92
+ })
93
+
94
+ it('should train on the training corpus to derive transformation rules', function () {
95
+ trainer = new BrillPOSTrainer(1)
96
+ ruleSet = trainer.train(corpora[0], templates, trainLexicon)
97
+ expect(ruleSet.nrRules()).toBeGreaterThan(0)
98
+ expect(trainer.printRulesWithScores()).not.toEqual('')
99
+ })
100
+
101
+ it('should test the derived transformation rules on the test corpus', function () {
102
+ const tagger = new BrillPOSTagger(trainLexicon, ruleSet)
103
+ const tester = new BrillPOSTester()
104
+ const scores = tester.test(corpora[1], tagger)
105
+ expect(scores[1]).toBeGreaterThan(0)
106
+ })
107
+ })
@@ -0,0 +1,97 @@
1
+ /*
2
+ Copyright (c) 2020, Hugo W.L. 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 { CarryStemmerFr as stemmer } from 'lib/natural'
26
+
27
+ // const snowBallDict = require('spec/test_data/snowball_fr.json')
28
+
29
+ const tests = [
30
+ {
31
+ input: 'action',
32
+ output: 'ac'
33
+ },
34
+ {
35
+ input: 'acteur',
36
+ output: 'ac'
37
+ },
38
+ {
39
+ input: 'actrices',
40
+ output: 'ac'
41
+ },
42
+ {
43
+ input: 'Dleyton',
44
+ output: 'Dleyton'
45
+ },
46
+ {
47
+ input: 'volera',
48
+ output: 'vol'
49
+ }
50
+ ]
51
+
52
+ const testSentences = [
53
+ {
54
+ input: 'Le petit cheval de manège',
55
+ output: ['pet', 'cheval', 'manèg']
56
+ }
57
+ ]
58
+
59
+ describe('Carry stemmer', function () {
60
+ tests.forEach(test => {
61
+ it('stems ' + test.input + ' correctly', function () {
62
+ expect(stemmer.stem(test.input)).toBe(test.output)
63
+ })
64
+ })
65
+
66
+ testSentences.forEach(test => {
67
+ it('tokenizes and stems ' + test.input + ' correctly', function () {
68
+ expect(stemmer.tokenizeAndStem(test.input)).toEqual(test.output)
69
+ })
70
+ })
71
+
72
+ /*
73
+ it('should perform stemming on a lot of words', function() {
74
+ var ok = [];
75
+ var ko = [];
76
+
77
+ Object.keys(snowBallDict).forEach(word => {
78
+
79
+ var stemmed = stemmer.stem(word);
80
+ var expectedStem = snowBallDict[word];
81
+
82
+ if (stemmed === expectedStem) {
83
+ ok.push(word);
84
+ }
85
+ else {
86
+ ko.push({
87
+ word: word,
88
+ expected: expectedStem,
89
+ actual: stemmed
90
+ });
91
+ }
92
+ });
93
+
94
+ expect(ko.length).toBe(0);
95
+ });
96
+ */
97
+ })
@@ -0,0 +1,104 @@
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 { BayesClassifier } from 'lib/natural'
26
+
27
+ describe('classifier', function () {
28
+ describe('addDocument', function () {
29
+ it('should ignore an empty document', function () {
30
+ const classifier = new BayesClassifier()
31
+ classifier.addDocument('', 'philosophy')
32
+ expect(classifier.docs.length).toBe(0)
33
+ })
34
+
35
+ it('should increment features', function () {
36
+ const classifier = new BayesClassifier()
37
+ classifier.addDocument('foo', '')
38
+ classifier.addDocument('foo', '')
39
+ classifier.addDocument('bar', '')
40
+ classifier.addDocument('bar', '')
41
+ classifier.addDocument('bar', '')
42
+ classifier.addDocument('baz', '')
43
+ expect(classifier.docs.length).toBe(6)
44
+ expect(classifier.features.foo).toBe(2)
45
+ expect(classifier.features.bar).toBe(3)
46
+ expect(classifier.features.baz).toBe(1)
47
+ })
48
+ })
49
+
50
+ describe('Classifier', function () {
51
+ const pushedEvents: any[] = []
52
+ function eventRegister (obj: any): void {
53
+ pushedEvents.push(obj)
54
+ }
55
+
56
+ function assertEventResults (): void {
57
+ expect(pushedEvents[0].index).toBe(0)
58
+ expect(pushedEvents[0].total).toBe(6)
59
+ expect(pushedEvents.length).toBe(6)
60
+ }
61
+
62
+ it('should emit events when documents are added or training is finished', function () {
63
+ const classifier = new BayesClassifier()
64
+ classifier.on('trainedWithDocument', eventRegister)
65
+ classifier.on('doneTraining', assertEventResults)
66
+
67
+ classifier.addDocument('i fixed the box', 'computing')
68
+ classifier.addDocument('i write code', 'computing')
69
+ classifier.addDocument('nasty script code', 'computing')
70
+ classifier.addDocument('write a book', 'literature')
71
+ classifier.addDocument('read a book', 'literature')
72
+ classifier.addDocument('study the books', 'literature')
73
+
74
+ classifier.train()
75
+ classifier.removeListener('trainedWithDocument', eventRegister)
76
+ classifier.removeListener('doneTraining', assertEventResults)
77
+ })
78
+ })
79
+
80
+ describe('Classifier', function () {
81
+ let classifier: BayesClassifier
82
+ beforeEach(function () {
83
+ classifier = new BayesClassifier()
84
+ classifier.addDocument('i fixed the box', 'computing')
85
+ classifier.addDocument('i write code', 'computing')
86
+ classifier.addDocument('write a book', 'literature')
87
+ classifier.addDocument('study the books', 'literature')
88
+ })
89
+
90
+ it('should do nothing if text is not a string', function () {
91
+ expect(classifier.docs.length).toBe(4)
92
+ classifier.removeDocument(['write a book'], 'literature')
93
+ classifier.removeDocument(['study the books'], 'literature')
94
+ expect(classifier.docs.length).toBe(4)
95
+ })
96
+
97
+ it('should do nothing if text is not a match', function () {
98
+ expect(classifier.docs.length).toBe(4)
99
+ classifier.removeDocument('something else', 'literature')
100
+ classifier.removeDocument('another thing', 'literature')
101
+ expect(classifier.docs.length).toBe(4)
102
+ })
103
+ })
104
+ })
@@ -0,0 +1,47 @@
1
+ /*
2
+ Copyright (c) 2012, Guillaume Marty
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
23
+ 'use strict'
24
+
25
+ import { CountInflectorFr } from 'lib/natural'
26
+ const inflector = new CountInflectorFr()
27
+
28
+ describe('count_inflector', function () {
29
+ it('should handle 1er cases', function () {
30
+ expect(inflector.nth(1)).toBe('1er')
31
+ })
32
+
33
+ it('should handle the 2e cases', function () {
34
+ expect(inflector.nth(0)).toBe('0e')
35
+ expect(inflector.nth(2)).toBe('2e')
36
+ expect(inflector.nth(3)).toBe('3e')
37
+ expect(inflector.nth(5)).toBe('5e')
38
+ expect(inflector.nth(11)).toBe('11e')
39
+ expect(inflector.nth(100)).toBe('100e')
40
+ expect(inflector.nth(999)).toBe('999e')
41
+ })
42
+
43
+ it('should handle roman numerals', function () {
44
+ expect(inflector.nth('I')).toBe('Ier')
45
+ expect(inflector.nth('XX')).toBe('XXe')
46
+ })
47
+ })
@@ -0,0 +1,76 @@
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 { CountInflector } from 'lib/natural'
26
+ const inflector = new CountInflector()
27
+
28
+ describe('count_inflector', function () {
29
+ it('should handle 1st cases', function () {
30
+ expect(inflector.nth(1)).toBe('1st')
31
+ expect(inflector.nth(101)).toBe('101st')
32
+ expect(inflector.nth(11)).not.toBe('11st')
33
+ expect(inflector.nth(111)).not.toBe('111st')
34
+ })
35
+
36
+ it('should handle the 12th cases', function () {
37
+ expect(inflector.nth(12)).toBe('12th')
38
+ expect(inflector.nth(112)).toBe('112th')
39
+ expect(inflector.nth(1112)).toBe('1112th')
40
+ })
41
+
42
+ it('should handle the 11th cases', function () {
43
+ expect(inflector.nth(11)).toBe('11th')
44
+ expect(inflector.nth(111)).toBe('111th')
45
+ expect(inflector.nth(1111)).toBe('1111th')
46
+ })
47
+
48
+ it('should handle the 13th cases', function () {
49
+ expect(inflector.nth(13)).toBe('13th')
50
+ expect(inflector.nth(113)).toBe('113th')
51
+ expect(inflector.nth(1113)).toBe('1113th')
52
+ })
53
+
54
+ it('should handle the th cases', function () {
55
+ expect(inflector.nth(10)).toBe('10th')
56
+ expect(inflector.nth(4)).toBe('4th')
57
+ expect(inflector.nth(400)).toBe('400th')
58
+ expect(inflector.nth(404)).toBe('404th')
59
+ expect(inflector.nth(5)).toBe('5th')
60
+ expect(inflector.nth(5000)).toBe('5000th')
61
+ expect(inflector.nth(5005)).toBe('5005th')
62
+ expect(inflector.nth(9)).toBe('9th')
63
+ expect(inflector.nth(90009)).toBe('90009th')
64
+ expect(inflector.nth(90000)).toBe('90000th')
65
+ })
66
+
67
+ it('should handle 2nd cases', function () {
68
+ expect(inflector.nth(2)).toBe('2nd')
69
+ expect(inflector.nth(12)).not.toBe('12nd')
70
+ })
71
+
72
+ it('should handle 3rd cases', function () {
73
+ expect(inflector.nth(3)).toBe('3rd')
74
+ expect(inflector.nth(13)).not.toBe('13rd')
75
+ })
76
+ })
@@ -0,0 +1,53 @@
1
+ 'use strict'
2
+
3
+ import { DamerauLevenshteinDistance, DamerauLevenshteinDistanceSearch } from 'lib/natural'
4
+
5
+ describe('DamerauLevenshtein', function () {
6
+ describe('default', function () {
7
+ it('should be 0 when given equal strings', function () {
8
+ expect(DamerauLevenshteinDistance('test', 'test')).toBe(0)
9
+ })
10
+
11
+ it('should calculate 1 for adjacent transposition', function () {
12
+ expect(DamerauLevenshteinDistance('za', 'az')).toBe(1)
13
+ expect(DamerauLevenshteinDistance('Tomato', 'oTmato')).toBe(1)
14
+ })
15
+
16
+ it('should handle custom transposition_cost', function () {
17
+ expect(DamerauLevenshteinDistance('za', 'az', { transposition_cost: 0 })).toBe(0)
18
+ })
19
+
20
+ it('should calculate 2 when there are 2 transpositions', function () {
21
+ expect(DamerauLevenshteinDistance('tomato', 'otmaot')).toBe(2)
22
+ })
23
+
24
+ it('should calculate 2 for 1 transposition and 1 insertion', function () {
25
+ expect(DamerauLevenshteinDistance('CA', 'ABC')).toBe(2)
26
+ expect(DamerauLevenshteinDistance('a cat', 'a abct')).toBe(2)
27
+ })
28
+ })
29
+
30
+ describe('options.restricted = true', function () {
31
+ const restricted = { restricted: true }
32
+ it('should calculate 0 for equal strings', function () {
33
+ expect(DamerauLevenshteinDistance('identity', 'identity', restricted)).toBe(0)
34
+ })
35
+ it('should calculate 1 for an adjacent transposition', function () {
36
+ expect(DamerauLevenshteinDistance('za', 'az', restricted)).toBe(1)
37
+ })
38
+ it('should not count transposition more than 1 char away', function () {
39
+ expect(DamerauLevenshteinDistance('CA', 'ABC', restricted)).toBe(3)
40
+ })
41
+ })
42
+
43
+ it('should combine search with Damerau', function () {
44
+ const source = 'The RainCoat BookStore'
45
+ const target = 'All the best books are here at the Rain Coats Book Store'
46
+ const result = DamerauLevenshteinDistanceSearch(source, target)
47
+ expect(result).toEqual({
48
+ substring: 'the Rain Coats Book Store',
49
+ distance: 4,
50
+ offset: 31
51
+ })
52
+ })
53
+ })
@@ -0,0 +1,37 @@
1
+ 'use strict'
2
+
3
+ import { DiceCoefficient as dice } from 'lib/natural'
4
+ import text1 from '../spec/test_data/Wikipedia_EN_FrenchRevolution.json'
5
+ import text2 from '../spec/test_data/Wikipedia_EN_InfluenceOfTheFrenchRevolution.json'
6
+
7
+ describe('dice', function () {
8
+ it('should handle exact matches', function () {
9
+ expect(dice('john', 'john')).toBe(1)
10
+ })
11
+
12
+ it('should match single character words', function () {
13
+ expect(dice('a', 'a')).toBe(1)
14
+ expect(dice('a', 'b')).toBe(0)
15
+ })
16
+
17
+ it('should handle total mis-matches', function () {
18
+ expect(dice('john', 'matt')).toBe(0)
19
+ })
20
+
21
+ // Example from http://en.wikipedia.org/wiki/Dice's_coefficient
22
+ it('should handle a typical case', function () {
23
+ expect(dice('night', 'nacht')).toBe(0.25)
24
+ })
25
+
26
+ it('should sanitize case', function () {
27
+ expect(dice('night', 'NIGHT')).toBe(1)
28
+ })
29
+
30
+ it('should sanitize spacing', function () {
31
+ expect(dice('the space', 'the space')).toBe(1)
32
+ })
33
+
34
+ it('should compare complete texts', function () {
35
+ expect(dice(text1.text, text2.text)).toBe(0.7939374395356337)
36
+ })
37
+ })