natural 6.10.2 → 6.10.3

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 (42) hide show
  1. package/.github/workflows/node.js.yml +1 -1
  2. package/.github/workflows/npm-publish.yml +4 -4
  3. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tagger.js +33 -31
  4. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Tester.js +33 -35
  5. package/lib/natural/brill_pos_tagger/lib/Brill_POS_Trainer.js +326 -324
  6. package/lib/natural/brill_pos_tagger/lib/Corpus.js +140 -138
  7. package/lib/natural/brill_pos_tagger/lib/Lexicon.js +78 -76
  8. package/lib/natural/brill_pos_tagger/lib/Predicate.js +19 -17
  9. package/lib/natural/brill_pos_tagger/lib/RuleSet.js +58 -56
  10. package/lib/natural/brill_pos_tagger/lib/RuleTemplate.js +11 -9
  11. package/lib/natural/brill_pos_tagger/lib/Sentence.js +21 -20
  12. package/lib/natural/brill_pos_tagger/lib/TransformationRule.js +58 -58
  13. package/lib/natural/classifiers/bayes_classifier.js +24 -29
  14. package/lib/natural/classifiers/classifier.js +136 -121
  15. package/lib/natural/classifiers/logistic_regression_classifier.js +33 -37
  16. package/lib/natural/classifiers/maxent/Classifier.js +81 -79
  17. package/lib/natural/classifiers/maxent/Context.js +11 -9
  18. package/lib/natural/classifiers/maxent/Distribution.js +107 -105
  19. package/lib/natural/classifiers/maxent/Element.js +12 -10
  20. package/lib/natural/classifiers/maxent/Feature.js +56 -54
  21. package/lib/natural/classifiers/maxent/FeatureSet.js +40 -38
  22. package/lib/natural/classifiers/maxent/GISScaler.js +110 -109
  23. package/lib/natural/classifiers/maxent/POS/ME_Corpus.js +26 -31
  24. package/lib/natural/classifiers/maxent/POS/ME_Sentence.js +37 -42
  25. package/lib/natural/classifiers/maxent/POS/POS_Element.js +119 -124
  26. package/lib/natural/classifiers/maxent/Sample.js +96 -94
  27. package/lib/natural/classifiers/maxent/SimpleExample/SE_Element.js +14 -20
  28. package/lib/natural/inflectors/fr/count_inflector.js +1 -1
  29. package/lib/natural/inflectors/fr/noun_inflector.js +179 -196
  30. package/lib/natural/inflectors/ja/noun_inflector.js +67 -84
  31. package/lib/natural/inflectors/noun_inflector.js +124 -139
  32. package/lib/natural/inflectors/present_verb_inflector.js +34 -48
  33. package/lib/natural/inflectors/singular_plural_inflector.js +62 -61
  34. package/lib/natural/tfidf/tfidf.js +118 -114
  35. package/lib/natural/trie/trie.js +161 -159
  36. package/lib/natural/util/directed_edge.js +49 -0
  37. package/lib/natural/util/edge_weighted_digraph.js +54 -74
  38. package/lib/natural/util/index.js +1 -0
  39. package/lib/natural/util/longest_path_tree.js +54 -52
  40. package/lib/natural/util/shortest_path_tree.js +56 -54
  41. package/lib/natural/util/topological.js +17 -15
  42. package/package.json +1 -1
@@ -0,0 +1,49 @@
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
+ const util = require('util')
24
+
25
+ class DirectedEdge {
26
+ constructor (start, end, weight) {
27
+ this.start = start
28
+ this.end = end
29
+ this.weight = weight
30
+ }
31
+
32
+ weight () {
33
+ return this.weight
34
+ }
35
+
36
+ from () {
37
+ return this.start
38
+ }
39
+
40
+ to () {
41
+ return this.end
42
+ }
43
+
44
+ toString () {
45
+ return util.format('%s -> %s, %s', this.start, this.end, this.weight)
46
+ }
47
+ }
48
+
49
+ module.exports = DirectedEdge
@@ -22,92 +22,72 @@ THE SOFTWARE.
22
22
 
23
23
  'use strict'
24
24
 
25
- const util = require('util')
26
25
  const Bag = require('./bag')
26
+ const DirectedEdge = require('./directed_edge')
27
27
 
28
- const DirectedEdge = function (start, end, weight) {
29
- this.start = start
30
- this.end = end
31
- this.weight = weight
32
- }
33
-
34
- DirectedEdge.prototype.weight = function () {
35
- return this.weight
36
- }
37
-
38
- DirectedEdge.prototype.from = function () {
39
- return this.start
40
- }
41
-
42
- DirectedEdge.prototype.to = function () {
43
- return this.end
44
- }
45
-
46
- DirectedEdge.prototype.toString = function () {
47
- return util.format('%s -> %s, %s', this.start, this.end, this.weight)
48
- }
28
+ class EdgeWeightedDigraph {
29
+ constructor () {
30
+ this.edgesNum = 0
31
+ this.adj = [] // adjacency list
32
+ }
49
33
 
50
- const EdgeWeightedDigraph = function () {
51
- this.edgesNum = 0
52
- this.adj = [] // adjacency list
53
- }
34
+ /**
35
+ * the number of vertexs saved.
36
+ */
37
+ v () {
38
+ return this.adj.length
39
+ }
54
40
 
55
- /**
56
- * the number of vertexs saved.
57
- */
58
- EdgeWeightedDigraph.prototype.v = function () {
59
- return this.adj.length
60
- }
41
+ /**
42
+ * the number of edges saved.
43
+ */
44
+ e () {
45
+ return this.edgesNum
46
+ }
61
47
 
62
- /**
63
- * the number of edges saved.
64
- */
65
- EdgeWeightedDigraph.prototype.e = function () {
66
- return this.edgesNum
67
- }
48
+ add (start, end, weight) {
49
+ const e = new DirectedEdge(start, end, weight)
50
+ this.addEdge(e)
51
+ }
68
52
 
69
- EdgeWeightedDigraph.prototype.add = function (start, end, weight) {
70
- const e = new DirectedEdge(start, end, weight)
71
- this.addEdge(e)
72
- }
53
+ addEdge (e) {
54
+ if (!this.adj[e.from()]) {
55
+ this.adj[e.from()] = new Bag()
56
+ }
57
+ this.adj[e.from()].add(e)
58
+ this.edgesNum++
59
+ }
73
60
 
74
- EdgeWeightedDigraph.prototype.addEdge = function (e) {
75
- if (!this.adj[e.from()]) {
76
- this.adj[e.from()] = new Bag()
61
+ /**
62
+ * use callback on all edges from v.
63
+ */
64
+ getAdj (v) {
65
+ if (!this.adj[v]) return []
66
+ return this.adj[v].unpack()
77
67
  }
78
- this.adj[e.from()].add(e)
79
- this.edgesNum++
80
- }
81
68
 
82
- /**
83
- * use callback on all edges from v.
84
- */
85
- EdgeWeightedDigraph.prototype.getAdj = function (v) {
86
- if (!this.adj[v]) return []
87
- return this.adj[v].unpack()
88
- }
69
+ /**
70
+ * use callback on all edges.
71
+ */
72
+ edges () {
73
+ const adj = this.adj
74
+ const list = new Bag()
75
+ for (const i in adj) {
76
+ adj[i].unpack().forEach(function (item) {
77
+ list.add(item)
78
+ })
79
+ }
80
+ return list.unpack()
81
+ }
89
82
 
90
- /**
91
- * use callback on all edges.
92
- */
93
- EdgeWeightedDigraph.prototype.edges = function () {
94
- const adj = this.adj
95
- const list = new Bag()
96
- for (const i in adj) {
97
- adj[i].unpack().forEach(function (item) {
98
- list.add(item)
83
+ toString () {
84
+ const result = []
85
+ const list = this.edges()
86
+ list.forEach(function (edge) {
87
+ result.push(edge.toString())
99
88
  })
89
+ return result.join('\n')
100
90
  }
101
- return list.unpack()
102
- }
103
-
104
- EdgeWeightedDigraph.prototype.toString = function () {
105
- const result = []
106
- const list = this.edges()
107
- list.forEach(function (edge) {
108
- result.push(edge.toString())
109
- })
110
- return result.join('\n')
111
91
  }
112
92
 
113
93
  module.exports = EdgeWeightedDigraph
@@ -25,4 +25,5 @@ THE SOFTWARE.
25
25
  exports.stopwords = require('./stopwords').words
26
26
  exports.ShortestPathTree = require('./shortest_path_tree')
27
27
  exports.LongestPathTree = require('./longest_path_tree')
28
+ exports.DirectedEdge = require('./directed_edge')
28
29
  exports.EdgeWeightedDigraph = require('./edge_weighted_digraph')
@@ -34,66 +34,68 @@ const Topological = require('./topological')
34
34
  * constant time and the pathTo() method takes time proportional to the
35
35
  * number of edges in the longest path returned.
36
36
  */
37
- const LongestPathTree = function (digraph, start) {
38
- const _this = this
39
- this.edgeTo = []
40
- this.distTo = []
41
- this.distTo[start] = 0.0
42
- this.start = start
43
- this.top = new Topological(digraph)
44
- this.top.order().forEach(function (vertex) {
45
- _this.relaxVertex(digraph, vertex, _this)
46
- })
47
- }
37
+ class LongestPathTree {
38
+ constructor (digraph, start) {
39
+ const _this = this
40
+ this.edgeTo = []
41
+ this.distTo = []
42
+ this.distTo[start] = 0.0
43
+ this.start = start
44
+ this.top = new Topological(digraph)
45
+ this.top.order().forEach(function (vertex) {
46
+ _this.relaxVertex(digraph, vertex, _this)
47
+ })
48
+ }
48
49
 
49
- LongestPathTree.prototype.relaxEdge = function (e) {
50
- const distTo = this.distTo
51
- const edgeTo = this.edgeTo
52
- const v = e.from(); const w = e.to()
53
- if (distTo[w] < distTo[v] + e.weight) {
54
- distTo[w] = distTo[v] + e.weight
55
- edgeTo[w] = e
50
+ relaxEdge (e) {
51
+ const distTo = this.distTo
52
+ const edgeTo = this.edgeTo
53
+ const v = e.from(); const w = e.to()
54
+ if (distTo[w] < distTo[v] + e.weight) {
55
+ distTo[w] = distTo[v] + e.weight
56
+ edgeTo[w] = e
57
+ }
56
58
  }
57
- }
58
59
 
59
- /**
60
- * relax a vertex v in the specified digraph g
61
- * @param {EdgeWeightedDigraph} the apecified digraph
62
- * @param {Vertex} v vertex to be relaxed
63
- */
64
- LongestPathTree.prototype.relaxVertex = function (digraph, vertex, tree) {
65
- const distTo = tree.distTo
66
- const edgeTo = tree.edgeTo
60
+ /**
61
+ * relax a vertex v in the specified digraph g
62
+ * @param {EdgeWeightedDigraph} the apecified digraph
63
+ * @param {Vertex} v vertex to be relaxed
64
+ */
65
+ relaxVertex (digraph, vertex, tree) {
66
+ const distTo = tree.distTo
67
+ const edgeTo = tree.edgeTo
67
68
 
68
- digraph.getAdj(vertex).forEach(function (edge) {
69
- const w = edge.to()
70
- distTo[w] = distTo[w] || 0.0
71
- distTo[vertex] = distTo[vertex] || 0.0
72
- if (distTo[w] < distTo[vertex] + edge.weight) {
73
- // in case of the result of 0.28+0.34 is 0.62000001
74
- distTo[w] = parseFloat((distTo[vertex] + edge.weight).toFixed(2))
75
- edgeTo[w] = edge
76
- }
77
- })
78
- }
69
+ digraph.getAdj(vertex).forEach(function (edge) {
70
+ const w = edge.to()
71
+ distTo[w] = distTo[w] || 0.0
72
+ distTo[vertex] = distTo[vertex] || 0.0
73
+ if (distTo[w] < distTo[vertex] + edge.weight) {
74
+ // in case of the result of 0.28+0.34 is 0.62000001
75
+ distTo[w] = parseFloat((distTo[vertex] + edge.weight).toFixed(2))
76
+ edgeTo[w] = edge
77
+ }
78
+ })
79
+ }
79
80
 
80
- LongestPathTree.prototype.getDistTo = function (v) {
81
- return this.distTo[v]
82
- }
81
+ getDistTo (v) {
82
+ return this.distTo[v]
83
+ }
83
84
 
84
- LongestPathTree.prototype.hasPathTo = function (v) {
85
- return !!this.distTo[v]
86
- }
85
+ hasPathTo (v) {
86
+ return !!this.distTo[v]
87
+ }
87
88
 
88
- LongestPathTree.prototype.pathTo = function (v) {
89
- if (!this.hasPathTo(v)) return []
90
- const path = []
91
- const edgeTo = this.edgeTo
92
- for (let e = edgeTo[v]; e; e = edgeTo[e.from()]) {
93
- path.push(e.to())
89
+ pathTo (v) {
90
+ if (!this.hasPathTo(v)) return []
91
+ const path = []
92
+ const edgeTo = this.edgeTo
93
+ for (let e = edgeTo[v]; e; e = edgeTo[e.from()]) {
94
+ path.push(e.to())
95
+ }
96
+ path.push(this.start)
97
+ return path.reverse()
94
98
  }
95
- path.push(this.start)
96
- return path.reverse()
97
99
  }
98
100
 
99
101
  module.exports = LongestPathTree
@@ -34,67 +34,69 @@ const Topological = require('./topological')
34
34
  * constant time and the pathTo() method takes time proportional to the
35
35
  * number of edges in the longest path returned.
36
36
  */
37
- const ShortestPathTree = function (digraph, start) {
38
- const _this = this
39
- this.edgeTo = []
40
- this.distTo = []
41
- this.distTo[start] = 0.0
42
- this.start = start
43
- this.top = new Topological(digraph)
44
- this.top.order().forEach(function (vertex) {
45
- _this.relaxVertex(digraph, vertex, _this)
46
- })
47
- }
48
-
49
- ShortestPathTree.prototype.relaxEdge = function (e) {
50
- const distTo = this.distTo
51
- const edgeTo = this.edgeTo
52
- const v = e.from(); const w = e.to()
53
- if (distTo[w] > distTo[v] + e.weight) {
54
- distTo[w] = distTo[v] + e.weight
55
- edgeTo[w] = e
37
+ class ShortestPathTree {
38
+ constructor (digraph, start) {
39
+ const _this = this
40
+ this.edgeTo = []
41
+ this.distTo = []
42
+ this.distTo[start] = 0.0
43
+ this.start = start
44
+ this.top = new Topological(digraph)
45
+ this.top.order().forEach(function (vertex) {
46
+ _this.relaxVertex(digraph, vertex, _this)
47
+ })
56
48
  }
57
- }
58
49
 
59
- /**
60
- * relax a vertex v in the specified digraph g
61
- * @param {EdgeWeightedDigraph} the apecified digraph
62
- * @param {Vertex} v vertex to be relaxed
63
- */
64
- ShortestPathTree.prototype.relaxVertex = function (digraph, vertex, tree) {
65
- const distTo = tree.distTo
66
- const edgeTo = tree.edgeTo
67
- digraph.getAdj(vertex).forEach(function (edge) {
68
- const w = edge.to()
69
- distTo[w] = /\d/.test(distTo[w]) ? distTo[w] : Number.MAX_VALUE
70
- distTo[vertex] = distTo[vertex] || 0
71
- if (distTo[w] > distTo[vertex] + edge.weight) {
72
- // in case of the result of 0.28+0.34 is 0.62000001
73
- distTo[w] = parseFloat((distTo[vertex] + edge.weight).toFixed(2))
74
- edgeTo[w] = edge
50
+ relaxEdge (e) {
51
+ const distTo = this.distTo
52
+ const edgeTo = this.edgeTo
53
+ const v = e.from(); const w = e.to()
54
+ if (distTo[w] > distTo[v] + e.weight) {
55
+ distTo[w] = distTo[v] + e.weight
56
+ edgeTo[w] = e
75
57
  }
76
- })
77
- }
58
+ }
78
59
 
79
- ShortestPathTree.prototype.getDistTo = function (v) {
80
- return this.distTo[v]
81
- }
60
+ /**
61
+ * relax a vertex v in the specified digraph g
62
+ * @param {EdgeWeightedDigraph} the apecified digraph
63
+ * @param {Vertex} v vertex to be relaxed
64
+ */
65
+ relaxVertex (digraph, vertex, tree) {
66
+ const distTo = tree.distTo
67
+ const edgeTo = tree.edgeTo
68
+ digraph.getAdj(vertex).forEach(function (edge) {
69
+ const w = edge.to()
70
+ distTo[w] = /\d/.test(distTo[w]) ? distTo[w] : Number.MAX_VALUE
71
+ distTo[vertex] = distTo[vertex] || 0
72
+ if (distTo[w] > distTo[vertex] + edge.weight) {
73
+ // in case of the result of 0.28+0.34 is 0.62000001
74
+ distTo[w] = parseFloat((distTo[vertex] + edge.weight).toFixed(2))
75
+ edgeTo[w] = edge
76
+ }
77
+ })
78
+ }
82
79
 
83
- ShortestPathTree.prototype.hasPathTo = function (v) {
84
- const dist = this.distTo[v]
85
- if (v === this.start) return false
86
- return /\d/.test(dist) ? dist !== Number.MAX_VALUE : false
87
- }
80
+ getDistTo (v) {
81
+ return this.distTo[v]
82
+ }
88
83
 
89
- ShortestPathTree.prototype.pathTo = function (v) {
90
- if (!this.hasPathTo(v) || v === this.start) return []
91
- const path = []
92
- const edgeTo = this.edgeTo
93
- for (let e = edgeTo[v]; e; e = edgeTo[e.from()]) {
94
- path.push(e.to())
84
+ hasPathTo (v) {
85
+ const dist = this.distTo[v]
86
+ if (v === this.start) return false
87
+ return /\d/.test(dist) ? dist !== Number.MAX_VALUE : false
88
+ }
89
+
90
+ pathTo (v) {
91
+ if (!this.hasPathTo(v) || v === this.start) return []
92
+ const path = []
93
+ const edgeTo = this.edgeTo
94
+ for (let e = edgeTo[v]; e; e = edgeTo[e.from()]) {
95
+ path.push(e.to())
96
+ }
97
+ path.push(this.start)
98
+ return path.reverse()
95
99
  }
96
- path.push(this.start)
97
- return path.reverse()
98
100
  }
99
101
 
100
102
  module.exports = ShortestPathTree
@@ -26,20 +26,22 @@ THE SOFTWARE.
26
26
  * a topo sort for a digraph
27
27
  * @param {Digraph}
28
28
  */
29
- const Topological = function (g) {
30
- this.isDag = true
31
- this.sorted = topoSort(uniqueVertexs(g.edges()), g.edges())
32
- }
29
+ class Topological {
30
+ constructor (g) {
31
+ this.isDag = true
32
+ this.sorted = topoSort(uniqueVertexs(g.edges()), g.edges())
33
+ }
33
34
 
34
- Topological.prototype.isDAG = function () {
35
- return this.isDag
36
- }
35
+ isDAG () {
36
+ return this.isDag
37
+ }
37
38
 
38
- /**
39
- * get ordered vertexs of digraph
40
- */
41
- Topological.prototype.order = function () {
42
- return this.sorted.slice()
39
+ /**
40
+ * get ordered vertexs of digraph
41
+ */
42
+ order () {
43
+ return this.sorted.slice()
44
+ }
43
45
  }
44
46
 
45
47
  /**
@@ -80,8 +82,8 @@ function topoSort (vertexs, edges) {
80
82
  })
81
83
 
82
84
  sorted[--cursor] = vertex
83
- };
84
- };
85
+ }
86
+ }
85
87
 
86
88
  function uniqueVertexs (edges) {
87
89
  const vertexs = []
@@ -93,6 +95,6 @@ function uniqueVertexs (edges) {
93
95
  if (vertexs.indexOf(to) < 0) vertexs.push(to)
94
96
  })
95
97
  return vertexs
96
- };
98
+ }
97
99
 
98
100
  module.exports = Topological
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "natural",
3
3
  "description": "General natural language (tokenizing, stemming (English, Russian, Spanish), part-of-speech tagging, sentiment analysis, classification, inflection, phonetics, tfidf, WordNet, jaro-winkler, Levenshtein distance, Dice's Coefficient) facilities for node.",
4
- "version": "6.10.2",
4
+ "version": "6.10.3",
5
5
  "homepage": "https://github.com/NaturalNode/natural",
6
6
  "repository": {
7
7
  "type": "git",