natural 6.10.2 → 6.10.4

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 (44) hide show
  1. package/.gitconfig +3 -0
  2. package/.github/workflows/node.js.yml +1 -1
  3. package/.github/workflows/npm-publish.yml +4 -4
  4. package/lib/natural/brill_pos_tagger/index.d.ts +32 -26
  5. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tagger.js +33 -31
  6. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tester.js +33 -35
  7. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Trainer.js +326 -324
  8. package/lib/natural/brill_pos_tagger/lib/Corpus.js +140 -138
  9. package/lib/natural/brill_pos_tagger/lib/Lexicon.js +78 -76
  10. package/lib/natural/brill_pos_tagger/lib/Predicate.js +19 -17
  11. package/lib/natural/brill_pos_tagger/lib/RuleSet.js +58 -56
  12. package/lib/natural/brill_pos_tagger/lib/RuleTemplate.js +11 -9
  13. package/lib/natural/brill_pos_tagger/lib/Sentence.js +21 -20
  14. package/lib/natural/brill_pos_tagger/lib/TransformationRule.js +58 -58
  15. package/lib/natural/classifiers/bayes_classifier.js +24 -29
  16. package/lib/natural/classifiers/classifier.js +136 -121
  17. package/lib/natural/classifiers/logistic_regression_classifier.js +33 -37
  18. package/lib/natural/classifiers/maxent/Classifier.js +81 -79
  19. package/lib/natural/classifiers/maxent/Context.js +11 -9
  20. package/lib/natural/classifiers/maxent/Distribution.js +107 -105
  21. package/lib/natural/classifiers/maxent/Element.js +12 -10
  22. package/lib/natural/classifiers/maxent/Feature.js +56 -54
  23. package/lib/natural/classifiers/maxent/FeatureSet.js +40 -38
  24. package/lib/natural/classifiers/maxent/GISScaler.js +110 -109
  25. package/lib/natural/classifiers/maxent/POS/ME_Corpus.js +26 -31
  26. package/lib/natural/classifiers/maxent/POS/ME_Sentence.js +37 -42
  27. package/lib/natural/classifiers/maxent/POS/POS_Element.js +119 -124
  28. package/lib/natural/classifiers/maxent/Sample.js +96 -94
  29. package/lib/natural/classifiers/maxent/SimpleExample/SE_Element.js +14 -20
  30. package/lib/natural/inflectors/fr/count_inflector.js +1 -1
  31. package/lib/natural/inflectors/fr/noun_inflector.js +179 -196
  32. package/lib/natural/inflectors/ja/noun_inflector.js +67 -84
  33. package/lib/natural/inflectors/noun_inflector.js +124 -139
  34. package/lib/natural/inflectors/present_verb_inflector.js +34 -48
  35. package/lib/natural/inflectors/singular_plural_inflector.js +62 -61
  36. package/lib/natural/tfidf/tfidf.js +118 -114
  37. package/lib/natural/trie/trie.js +161 -159
  38. package/lib/natural/util/directed_edge.js +49 -0
  39. package/lib/natural/util/edge_weighted_digraph.js +54 -74
  40. package/lib/natural/util/index.js +1 -0
  41. package/lib/natural/util/longest_path_tree.js +54 -52
  42. package/lib/natural/util/shortest_path_tree.js +56 -54
  43. package/lib/natural/util/topological.js +17 -15
  44. package/package.json +1 -1
package/.gitconfig ADDED
@@ -0,0 +1,3 @@
1
+ [user]
2
+ email = hwl.ter.doest@gmail.com
3
+ name = Hugo W.L. ter Doest
@@ -22,7 +22,7 @@ jobs:
22
22
  steps:
23
23
  - uses: actions/checkout@v2
24
24
  - name: Use Node.js ${{ matrix.node-version }}
25
- uses: actions/setup-node@v1
25
+ uses: actions/setup-node@v4
26
26
  with:
27
27
  node-version: ${{ matrix.node-version }}
28
28
  - run: npm ci
@@ -1,6 +1,6 @@
1
1
  # This workflow will run tests using node and then publish a package to NPMjs when a release is created
2
2
  # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3
-
3
+ ---
4
4
  name: Node.js Package
5
5
 
6
6
  on:
@@ -12,7 +12,7 @@ jobs:
12
12
  runs-on: ubuntu-latest
13
13
  steps:
14
14
  - uses: actions/checkout@v2
15
- - uses: actions/setup-node@v1
15
+ - uses: actions/setup-node@v4
16
16
  with:
17
17
  node-version: 12
18
18
  - run: npm ci
@@ -23,12 +23,12 @@ jobs:
23
23
  runs-on: ubuntu-latest
24
24
  steps:
25
25
  - uses: actions/checkout@v2
26
- - uses: actions/setup-node@v1
26
+ - uses: actions/setup-node@v4
27
27
  with:
28
28
  node-version: 14
29
29
  registry-url: https://registry.npmjs.org/
30
30
  - run: npm ci
31
- - run: git config user.name $GITHUB_ACTOR
31
+ - run: git config user.name "$GITHUB_ACTOR"
32
32
  - run: npm publish
33
33
  env:
34
34
  NODE_AUTH_TOKEN: ${{secrets.NPM_SECRET}}
@@ -23,6 +23,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
23
  THE SOFTWARE.
24
24
  */
25
25
 
26
+ import { Feature } from '../classifiers'
27
+
26
28
  declare interface RuleTemplatesItem {
27
29
  function: (sentence: Sentence, i: number, parameter1?: string, parameter2?: string) => boolean
28
30
  window: number[]
@@ -93,8 +95,12 @@ export class Lexicon {
93
95
  setDefaultCategories (category: string, categoryCapitalised: string): void
94
96
  }
95
97
 
96
- export class Corpus {
98
+ declare class Corpus {
97
99
  constructor (data: string | Corpus, typeOfCorpus: number, SentenceClass: typeof Sentence)
100
+ private readonly wordCount: number
101
+ private readonly sentences: Sentence[]
102
+ private readonly tagFrequencies: { [key: string]: string[] | undefined }
103
+ private readonly posTags: { [key: string]: string[] | undefined }
98
104
  parseBrownCorpus (data: string, SentenceClass: typeof Sentence): void
99
105
  getTags (): string[]
100
106
  splitInTrainAndTest (percentageTrain: number): [Corpus, Corpus]
@@ -103,8 +109,8 @@ export class Corpus {
103
109
  tag (lexicon: Lexicon): void
104
110
  nrSentences (): number
105
111
  nrWords (): number
106
- // Incomplete methods
107
- // generateFeatures
112
+ private generateFeatures (): Feature[]
113
+ // Incomplete method:
108
114
  // prettyPrint
109
115
  }
110
116
 
@@ -122,8 +128,8 @@ export class Sentence {
122
128
 
123
129
  export class BrillPOSTagger {
124
130
  constructor (lexicon: Lexicon, ruleSet: RuleSet)
125
- lexicon: Lexicon
126
- ruleSet: RuleSet
131
+ private readonly lexicon: Lexicon
132
+ private readonly ruleSet: RuleSet
127
133
  tag (sentence: string[]): Sentence
128
134
  tagWithLexicon (sentence: string[]): Sentence
129
135
  applyRules (sentence: Sentence): Sentence
@@ -131,32 +137,32 @@ export class BrillPOSTagger {
131
137
 
132
138
  export class BrillPOSTester {
133
139
  constructor (lexicon: Lexicon, ruleSet: RuleSet)
134
- lexicon: Lexicon
135
- ruleSet: RuleSet
140
+ private readonly lexicon: Lexicon
141
+ private readonly ruleSet: RuleSet
136
142
  test (corpus: Corpus, tagger: BrillPOSTagger): [number, number]
137
143
  }
138
144
 
139
145
  export class BrillPOSTrainer {
140
146
  constructor (ruleScoreThreshold: number)
141
- ruleScoreThreshold: number
142
- corpus: Corpus
143
- templates: RuleTemplates
144
- positiveRules: RuleSet
145
- mapRuleToSites: { [key: string]: { [key: number ]: { [key: number ]: boolean | undefined } | undefined } | undefined }
146
- mapSiteToRules: { [key: number]: { [key: number ]: { [key: string ]: TransformationRule | undefined } | undefined } | undefined }
147
- selectHighRule (): TransformationRule
148
- mapRuleToSite (rule: TransformationRule, i: number, j: number): void
149
- mapSiteToRule (i: number, j: number, rule: TransformationRule): void
150
- associateSiteWithRule (i: number, j: number, rule: TransformationRule): void
151
- siteIsAssociatedWithRule (i: number, j: number, rule: TransformationRule): boolean
152
- getSites (rule: TransformationRule): Array<[number, number]>
153
- getRules (i: number, j: number): TransformationRule[]
154
- disconnectSiteFromRule (i: number, j: number, rule: TransformationRule): void
155
- scoreRule (rule: TransformationRule, i: number, j: number): void
156
- generatePositiveRules (i: number, j: number): RuleSet
157
- scanForPositiveRules (): void
158
- scanForSites (): void
159
- neighbourhood (i: number, j: number): Array<[number, number]>
147
+ private readonly ruleScoreThreshold: number
148
+ private readonly corpus: Corpus
149
+ private readonly templates: RuleTemplates
150
+ private readonly positiveRules: RuleSet
151
+ private readonly mapRuleToSites: { [key: string]: { [key: number ]: { [key: number ]: boolean | undefined } | undefined } | undefined }
152
+ private readonly mapSiteToRules: { [key: number]: { [key: number ]: { [key: string ]: TransformationRule | undefined } | undefined } | undefined }
153
+ private selectHighRule (): TransformationRule
154
+ private mapRuleToSite (rule: TransformationRule, i: number, j: number): void
155
+ private mapSiteToRule (i: number, j: number, rule: TransformationRule): void
156
+ private associateSiteWithRule (i: number, j: number, rule: TransformationRule): void
157
+ private siteIsAssociatedWithRule (i: number, j: number, rule: TransformationRule): boolean
158
+ private getSites (rule: TransformationRule): Array<[number, number]>
159
+ private getRules (i: number, j: number): TransformationRule[]
160
+ private disconnectSiteFromRule (i: number, j: number, rule: TransformationRule): void
161
+ private scoreRule (rule: TransformationRule, i: number, j: number): void
162
+ private generatePositiveRules (i: number, j: number): RuleSet
163
+ private scanForPositiveRules (): void
164
+ private scanForSites (): void
165
+ private neighbourhood (i: number, j: number): Array<[number, number]>
160
166
  train (corpus: Corpus, templates: RuleTemplates, lexicon: Lexicon): RuleSet
161
167
  printRulesWithScores (): string
162
168
  }
@@ -22,42 +22,44 @@ const Sentence = require('./Sentence')
22
22
 
23
23
  const DEBUG = false
24
24
 
25
- function BrillPOSTagger (lexicon, ruleSet) {
26
- this.lexicon = lexicon
27
- this.ruleSet = ruleSet
28
- }
29
-
30
- // Tags a sentence, sentence is an array of words
31
- // Returns an array of tagged words; a tagged words is an array consisting of
32
- // the word itself followed by its lexical category
33
- BrillPOSTagger.prototype.tag = function (sentence) {
34
- const taggedSentence = this.tagWithLexicon(sentence)
35
- DEBUG && console.log(taggedSentence)
36
- return this.applyRules(taggedSentence)
37
- }
25
+ class BrillPOSTagger {
26
+ constructor (lexicon, ruleSet) {
27
+ this.lexicon = lexicon
28
+ this.ruleSet = ruleSet
29
+ }
38
30
 
39
- BrillPOSTagger.prototype.tagWithLexicon = function (sentence) {
40
- const taggedSentence = new Sentence()
31
+ // Tags a sentence, sentence is an array of words
32
+ // Returns an array of tagged words; a tagged words is an array consisting of
33
+ // the word itself followed by its lexical category
34
+ tag (sentence) {
35
+ const taggedSentence = this.tagWithLexicon(sentence)
36
+ DEBUG && console.log(taggedSentence)
37
+ return this.applyRules(taggedSentence)
38
+ }
41
39
 
42
- const that = this
43
- sentence.forEach(function (word, index) {
44
- const categories = that.lexicon.tagWord(word)
45
- taggedSentence.addTaggedWord(word, categories[0])
46
- })
47
- return (taggedSentence)
48
- }
40
+ tagWithLexicon (sentence) {
41
+ const taggedSentence = new Sentence()
49
42
 
50
- // Applies the transformation rules to an initially tagged sentence.
51
- // taggedSentence is an array of tagged words.
52
- // A tagged word is an array consisting of the word itself followed by its lexical category.
53
- // Returns an array of tagged words as well
54
- BrillPOSTagger.prototype.applyRules = function (sentence) {
55
- for (let i = 0, size = sentence.taggedWords.length; i < size; i++) {
56
- this.ruleSet.getRules().forEach(function (rule) {
57
- rule.apply(sentence, i)
43
+ const that = this
44
+ sentence.forEach(function (word, index) {
45
+ const categories = that.lexicon.tagWord(word)
46
+ taggedSentence.addTaggedWord(word, categories[0])
58
47
  })
48
+ return (taggedSentence)
49
+ }
50
+
51
+ // Applies the transformation rules to an initially tagged sentence.
52
+ // taggedSentence is an array of tagged words.
53
+ // A tagged word is an array consisting of the word itself followed by its lexical category.
54
+ // Returns an array of tagged words as well
55
+ applyRules (sentence) {
56
+ for (let i = 0, size = sentence.taggedWords.length; i < size; i++) {
57
+ this.ruleSet.getRules().forEach(function (rule) {
58
+ rule.apply(sentence, i)
59
+ })
60
+ }
61
+ return sentence
59
62
  }
60
- return sentence
61
63
  }
62
64
 
63
65
  module.exports = BrillPOSTagger
@@ -18,43 +18,41 @@
18
18
 
19
19
  'use strict'
20
20
 
21
- function BrillPOSTester () {
22
-
23
- }
24
-
25
- BrillPOSTester.prototype.test = function (corpus, tagger) {
26
- let totalWords = 0
27
- let correctTagsLexicon = 0
28
- let correctTagsAfterRules = 0
29
-
30
- // Tag the corpus using the tagger
31
- corpus.sentences.forEach(function (sentence) {
32
- const s = sentence.taggedWords.map(function (token) {
33
- return token.token
34
- })
35
-
36
- // Use the lexicon to tag the sentence
37
- const taggedSentence = tagger.tagWithLexicon(s)
38
- // Count the right tags
39
- sentence.taggedWords.forEach(function (token, i) {
40
- totalWords++
41
- if (token.tag === taggedSentence.taggedWords[i].tag) {
42
- correctTagsLexicon++
43
- }
44
- })
45
-
46
- // Use the rule set to tag the sentence
47
- const taggedSentenceAfterRules = tagger.applyRules(taggedSentence)
48
- // Count the right tags
49
- sentence.taggedWords.forEach(function (token, i) {
50
- if (token.tag === taggedSentenceAfterRules.taggedWords[i].tag) {
51
- correctTagsAfterRules++
52
- }
21
+ class BrillPOSTester {
22
+ test (corpus, tagger) {
23
+ let totalWords = 0
24
+ let correctTagsLexicon = 0
25
+ let correctTagsAfterRules = 0
26
+
27
+ // Tag the corpus using the tagger
28
+ corpus.sentences.forEach(function (sentence) {
29
+ const s = sentence.taggedWords.map(function (token) {
30
+ return token.token
31
+ })
32
+
33
+ // Use the lexicon to tag the sentence
34
+ const taggedSentence = tagger.tagWithLexicon(s)
35
+ // Count the right tags
36
+ sentence.taggedWords.forEach(function (token, i) {
37
+ totalWords++
38
+ if (token.tag === taggedSentence.taggedWords[i].tag) {
39
+ correctTagsLexicon++
40
+ }
41
+ })
42
+
43
+ // Use the rule set to tag the sentence
44
+ const taggedSentenceAfterRules = tagger.applyRules(taggedSentence)
45
+ // Count the right tags
46
+ sentence.taggedWords.forEach(function (token, i) {
47
+ if (token.tag === taggedSentenceAfterRules.taggedWords[i].tag) {
48
+ correctTagsAfterRules++
49
+ }
50
+ })
53
51
  })
54
- })
55
52
 
56
- // Return percentage right
57
- return [100 * correctTagsLexicon / totalWords, 100 * correctTagsAfterRules / totalWords]
53
+ // Return percentage right
54
+ return [100 * correctTagsLexicon / totalWords, 100 * correctTagsAfterRules / totalWords]
55
+ }
58
56
  }
59
57
 
60
58
  module.exports = BrillPOSTester