natural 0.1.27 → 0.2.1
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.
- package/.editorconfig +7 -0
- package/LICENSE.txt +0 -0
- package/Makefile +0 -0
- package/README.md +237 -24
- package/benchmarks/index.js +1 -1
- package/benchmarks/metaphone.js +0 -0
- package/benchmarks/soundex.js +0 -0
- package/examples/classification/basic.js +0 -0
- package/examples/classification/recall.js +0 -0
- package/examples/classification/store.js +0 -0
- package/examples/inflection/count.js +0 -0
- package/examples/inflection/noun.js +0 -0
- package/examples/inflection/noun_attach.js +0 -0
- package/examples/phonetics/compare.js +0 -0
- package/examples/phonetics/sounds_like.js +0 -0
- package/examples/phonetics/tokenize_and_phoneticize.js +0 -0
- package/examples/stemming/stem_corpus.js +0 -0
- package/examples/stemming/stem_word.js +0 -0
- package/examples/tfidf/array_example.js +0 -0
- package/examples/tfidf/multiple_terms.js +0 -0
- package/examples/tfidf/tfidf_example.js +0 -0
- package/index.js +0 -0
- package/io_spec/test_data/tfidf/one +0 -0
- package/io_spec/test_data/tfidf/two +0 -0
- package/io_spec/tfidf_spec.js +0 -0
- package/io_spec/wordnet_spec.js +0 -0
- package/lib/natural/analyzers/sentence_analyzer.js +16 -13
- package/lib/natural/classifiers/bayes_classifier.js +6 -2
- package/lib/natural/classifiers/classifier.js +0 -0
- package/lib/natural/classifiers/logistic_regression_classifier.js +0 -0
- package/lib/natural/distance/dice_coefficient.js +0 -0
- package/lib/natural/distance/jaro-winkler_distance.js +13 -8
- package/lib/natural/distance/levenshtein_distance.js +0 -0
- package/lib/natural/index.js +8 -0
- package/lib/natural/inflectors/count_inflector.js +0 -0
- package/lib/natural/inflectors/form_set.js +0 -0
- package/lib/natural/inflectors/fr/count_inflector.js +0 -0
- package/lib/natural/inflectors/fr/noun_inflector.js +0 -0
- package/lib/natural/inflectors/ja/noun_inflector.js +0 -0
- package/lib/natural/inflectors/noun_inflector.js +0 -0
- package/lib/natural/inflectors/present_verb_inflector.js +1 -0
- package/lib/natural/inflectors/singular_plural_inflector.js +1 -2
- package/lib/natural/ngrams/ngrams.js +45 -8
- package/lib/natural/ngrams/ngrams_zh.js +83 -0
- package/lib/natural/normalizers/normalizer.js +30 -38
- package/lib/natural/normalizers/normalizer_ja.js +26 -8
- package/lib/natural/normalizers/normalizer_no.js +59 -0
- package/lib/natural/phonetics/double_metaphone.js +9 -4
- package/lib/natural/phonetics/metaphone.js +6 -1
- package/lib/natural/phonetics/phonetic.js +0 -0
- package/lib/natural/phonetics/soundex.js +11 -7
- package/lib/natural/spellcheck/spellcheck.js +80 -0
- package/lib/natural/stemmers/lancaster_rules.js +4 -4
- package/lib/natural/stemmers/lancaster_stemmer.js +0 -0
- package/lib/natural/stemmers/porter_stemmer.js +90 -63
- package/lib/natural/stemmers/porter_stemmer_es.js +22 -23
- package/lib/natural/stemmers/porter_stemmer_fr.js +377 -0
- package/lib/natural/stemmers/porter_stemmer_no.js +155 -0
- package/lib/natural/stemmers/porter_stemmer_pt.js +302 -0
- package/lib/natural/stemmers/stemmer.js +0 -0
- package/lib/natural/stemmers/stemmer_fr.js +58 -0
- package/lib/natural/stemmers/stemmer_no.js +61 -0
- package/lib/natural/stemmers/stemmer_pt.js +65 -0
- package/lib/natural/stemmers/token.js +163 -0
- package/lib/natural/tfidf/tfidf.js +50 -19
- package/lib/natural/tokenizers/aggressive_tokenizer.js +0 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_es.js +0 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_fr.js +36 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_nl.js +0 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_no.js +39 -0
- package/lib/natural/tokenizers/aggressive_tokenizer_pt.js +0 -0
- package/lib/natural/tokenizers/regexp_tokenizer.js +0 -0
- package/lib/natural/tokenizers/tokenizer.js +2 -2
- package/lib/natural/tokenizers/treebank_word_tokenizer.js +0 -0
- package/lib/natural/trie/trie.js +47 -0
- package/lib/natural/util/bag.js +50 -0
- package/lib/natural/util/edge_weighted_digraph.js +112 -0
- package/lib/natural/util/longest_path_tree.js +99 -0
- package/lib/natural/util/shortest_path_tree.js +100 -0
- package/lib/natural/util/stopwords.js +0 -0
- package/lib/natural/util/stopwords_fr.js +194 -0
- package/lib/natural/util/stopwords_nl.js +43 -0
- package/lib/natural/util/stopwords_no.js +42 -0
- package/lib/natural/util/stopwords_pt.js +136 -0
- package/lib/natural/util/topological.js +98 -0
- package/lib/natural/wordnet/data_file.js +0 -0
- package/lib/natural/wordnet/index_file.js +0 -0
- package/lib/natural/wordnet/wordnet.js +0 -0
- package/lib/natural/wordnet/wordnet_file.js +0 -0
- package/package.json +2 -2
- package/spec/aggressive_tokenizer_fr_spec.js +101 -0
- package/spec/aggressive_tokenizer_pt_spec.js +0 -0
- package/spec/aggressive_tokenizer_spec.js +0 -0
- package/spec/bayes_classifier_spec.js +12 -0
- package/spec/classifier_spec.js +84 -0
- package/spec/count_inflector_fr_spec.js +0 -0
- package/spec/count_inflector_spec.js +0 -0
- package/spec/dice_coefficient_spec.js +0 -0
- package/spec/double_metaphone_spec.js +10 -1
- package/spec/jaro-winkler_spec.js +0 -0
- package/spec/lancaster_stemmer_spec.js +10 -1
- package/spec/levenshtein_spec.js +0 -0
- package/spec/logistic_regression_classifier_spec.js +0 -0
- package/spec/longest_path_tree_spec.js +102 -0
- package/spec/metaphone_spec.js +10 -0
- package/spec/ngram_spec.js +61 -0
- package/spec/ngram_zh_spec.js +122 -0
- package/spec/normalizer_ja_spec.js +9 -3
- package/spec/normalizer_no_spec.js +111 -0
- package/spec/normalizer_spec.js +26 -5
- package/spec/noun_inflector_fr_spec.js +0 -0
- package/spec/noun_inflector_ja_spec.js +0 -0
- package/spec/noun_inflector_spec.js +0 -0
- package/spec/porter_stemmer_fr_spec.js +101 -0
- package/spec/porter_stemmer_no_spec.js +143 -0
- package/spec/porter_stemmer_pt_spec.js +50 -0
- package/spec/porter_stemmer_spec.js +49 -0
- package/spec/present_verb_inflector_spec.js +3 -0
- package/spec/remove_diacritics_spec.js +31 -0
- package/spec/sentence_analyzer_spec.js +248 -0
- package/spec/shortest_path_tree_spec.js +132 -0
- package/spec/soundex_spec.js +13 -9
- package/spec/spellcheck_spec.js +92 -0
- package/spec/stemmer_token_spec.js +161 -0
- package/spec/test_data/snowball_fr.txt +20402 -0
- package/spec/test_data/snowball_no.txt +20628 -0
- package/spec/test_data/snowball_pt.txt +32016 -0
- package/spec/test_data/tfdif_testcorpus.txt +0 -0
- package/spec/test_data/tfidf_teststopwords.txt +0 -0
- package/spec/test_data/wordnet_mini.sql +0 -0
- package/spec/tfidf_spec.js +153 -28
- package/spec/tokenizer_ja_spec.js +0 -0
- package/spec/treebank_word_tokenizer_spec.js +0 -0
- package/spec/trie_spec.js +27 -3
|
@@ -28,13 +28,13 @@ var _ = require("underscore")._,
|
|
|
28
28
|
|
|
29
29
|
function buildDocument(text, key) {
|
|
30
30
|
var stopOut;
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
if(typeof text === 'string') {
|
|
33
33
|
text = tokenizer.tokenize(text.toLowerCase());
|
|
34
34
|
stopOut = true;
|
|
35
35
|
} else if(!_.isArray(text)) {
|
|
36
|
-
return text;
|
|
37
36
|
stopOut = false;
|
|
37
|
+
return text;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
return text.reduce(function(document, term) {
|
|
@@ -63,21 +63,42 @@ function TfIdf(deserialized) {
|
|
|
63
63
|
this._idfCache = {};
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
// backwards compatibility for < node 0.10
|
|
67
|
+
function isEncoding(encoding) {
|
|
68
|
+
if (typeof Buffer.isEncoding !== 'undefined')
|
|
69
|
+
return Buffer.isEncoding(encoding);
|
|
70
|
+
switch ((encoding + '').toLowerCase()) {
|
|
71
|
+
case 'hex':
|
|
72
|
+
case 'utf8':
|
|
73
|
+
case 'utf-8':
|
|
74
|
+
case 'ascii':
|
|
75
|
+
case 'binary':
|
|
76
|
+
case 'base64':
|
|
77
|
+
case 'ucs2':
|
|
78
|
+
case 'ucs-2':
|
|
79
|
+
case 'utf16le':
|
|
80
|
+
case 'utf-16le':
|
|
81
|
+
case 'raw':
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
66
87
|
module.exports = TfIdf;
|
|
67
88
|
TfIdf.tf = tf;
|
|
68
89
|
|
|
69
90
|
TfIdf.prototype.idf = function(term, force) {
|
|
70
91
|
|
|
71
|
-
// Lookup the term in the New term-IDF caching,
|
|
92
|
+
// Lookup the term in the New term-IDF caching,
|
|
72
93
|
// this will cut search times down exponentially on large document sets.
|
|
73
|
-
if(this._idfCache[term] &&
|
|
94
|
+
if(this._idfCache[term] && this._idfCache.hasOwnProperty(term) && force !== true)
|
|
74
95
|
return this._idfCache[term];
|
|
75
96
|
|
|
76
97
|
var docsWithTerm = this.documents.reduce(function(count, document) {
|
|
77
98
|
return count + (documentHasTerm(term, document) ? 1 : 0);
|
|
78
99
|
}, 0);
|
|
79
100
|
|
|
80
|
-
var idf = Math.log((this.documents.length) / (docsWithTerm));
|
|
101
|
+
var idf = 1 + Math.log((this.documents.length) / ( 1 + docsWithTerm ));
|
|
81
102
|
|
|
82
103
|
// Add the idf to the term cache and return it
|
|
83
104
|
this._idfCache[term] = idf;
|
|
@@ -92,13 +113,12 @@ TfIdf.prototype.addDocument = function(document, key, restoreCache) {
|
|
|
92
113
|
// make sure the cache is invalidated when new documents arrive
|
|
93
114
|
if(restoreCache === true) {
|
|
94
115
|
for(var term in this._idfCache) {
|
|
95
|
-
// invoking idf with the force option set will
|
|
116
|
+
// invoking idf with the force option set will
|
|
96
117
|
// force a recomputation of the idf, and it will
|
|
97
118
|
// automatically refresh the cache value.
|
|
98
119
|
this.idf(term, true);
|
|
99
120
|
}
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
121
|
+
} else {
|
|
102
122
|
this._idfCache = {};
|
|
103
123
|
}
|
|
104
124
|
};
|
|
@@ -107,15 +127,17 @@ TfIdf.prototype.addDocument = function(document, key, restoreCache) {
|
|
|
107
127
|
// Otherwise, the cache will just be wiped clean
|
|
108
128
|
TfIdf.prototype.addFileSync = function(path, encoding, key, restoreCache) {
|
|
109
129
|
if(!encoding)
|
|
110
|
-
encoding = '
|
|
111
|
-
|
|
112
|
-
|
|
130
|
+
encoding = 'utf8';
|
|
131
|
+
if(!isEncoding(encoding))
|
|
132
|
+
throw new Error('Invalid encoding: ' + encoding);
|
|
133
|
+
|
|
134
|
+
var document = fs.readFileSync(path, encoding);
|
|
113
135
|
this.documents.push(buildDocument(document, key));
|
|
114
136
|
|
|
115
137
|
// make sure the cache is invalidated when new documents arrive
|
|
116
138
|
if(restoreCache === true) {
|
|
117
139
|
for(var term in this._idfCache) {
|
|
118
|
-
// invoking idf with the force option set will
|
|
140
|
+
// invoking idf with the force option set will
|
|
119
141
|
// force a recomputation of the idf, and it will
|
|
120
142
|
// automatically refresh the cache value.
|
|
121
143
|
this.idf(term, true);
|
|
@@ -128,12 +150,14 @@ TfIdf.prototype.addFileSync = function(path, encoding, key, restoreCache) {
|
|
|
128
150
|
|
|
129
151
|
TfIdf.prototype.tfidf = function(terms, d) {
|
|
130
152
|
var _this = this;
|
|
131
|
-
|
|
153
|
+
|
|
132
154
|
if(!_.isArray(terms))
|
|
133
155
|
terms = tokenizer.tokenize(terms.toString().toLowerCase());
|
|
134
156
|
|
|
135
157
|
return terms.reduce(function(value, term) {
|
|
136
|
-
|
|
158
|
+
var idf = _this.idf(term);
|
|
159
|
+
idf = idf === Infinity ? 0 : idf;
|
|
160
|
+
return value + (tf(term, _this.documents[d]) * idf);
|
|
137
161
|
}, 0.0);
|
|
138
162
|
};
|
|
139
163
|
|
|
@@ -142,21 +166,28 @@ TfIdf.prototype.listTerms = function(d) {
|
|
|
142
166
|
|
|
143
167
|
for(var term in this.documents[d]) {
|
|
144
168
|
if(term != '__key')
|
|
145
|
-
|
|
169
|
+
terms.push({term: term, tfidf: this.tfidf(term, d)});
|
|
146
170
|
}
|
|
147
171
|
|
|
148
|
-
return terms.sort(function(x, y) { return y.tfidf - x.tfidf });
|
|
149
|
-
}
|
|
172
|
+
return terms.sort(function(x, y) { return y.tfidf - x.tfidf; });
|
|
173
|
+
};
|
|
150
174
|
|
|
151
175
|
TfIdf.prototype.tfidfs = function(terms, callback) {
|
|
152
176
|
var tfidfs = new Array(this.documents.length);
|
|
153
|
-
|
|
177
|
+
|
|
154
178
|
for(var i = 0; i < this.documents.length; i++) {
|
|
155
179
|
tfidfs[i] = this.tfidf(terms, i);
|
|
156
|
-
|
|
180
|
+
|
|
157
181
|
if(callback)
|
|
158
182
|
callback(i, tfidfs[i], this.documents[i].__key);
|
|
159
183
|
}
|
|
160
184
|
|
|
161
185
|
return tfidfs;
|
|
162
186
|
};
|
|
187
|
+
|
|
188
|
+
// Define a tokenizer other than the default "WordTokenizer"
|
|
189
|
+
TfIdf.prototype.setTokenizer = function(t) {
|
|
190
|
+
if(!_.isFunction(t.tokenize))
|
|
191
|
+
throw new Error('Expected a valid Tokenizer');
|
|
192
|
+
tokenizer = t;
|
|
193
|
+
};
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,36 @@
|
|
|
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 Tokenizer = require('./tokenizer'),
|
|
24
|
+
util = require('util');
|
|
25
|
+
|
|
26
|
+
var AggressiveTokenizer = function() {
|
|
27
|
+
Tokenizer.call(this);
|
|
28
|
+
};
|
|
29
|
+
util.inherits(AggressiveTokenizer, Tokenizer);
|
|
30
|
+
|
|
31
|
+
module.exports = AggressiveTokenizer;
|
|
32
|
+
|
|
33
|
+
AggressiveTokenizer.prototype.tokenize = function(text) {
|
|
34
|
+
// break a string up into an array of tokens by anything non-word
|
|
35
|
+
return this.trim(text.split(/[^a-z0-9äâàéèëêïîöôùüûœç]+/i));
|
|
36
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2014, Kristoffer Brabrand
|
|
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 Tokenizer = require('./tokenizer'),
|
|
24
|
+
normalizer = require('../normalizers/normalizer_no'),
|
|
25
|
+
util = require('util');
|
|
26
|
+
|
|
27
|
+
var AggressiveTokenizer = function() {
|
|
28
|
+
Tokenizer.call(this);
|
|
29
|
+
};
|
|
30
|
+
util.inherits(AggressiveTokenizer, Tokenizer);
|
|
31
|
+
|
|
32
|
+
module.exports = AggressiveTokenizer;
|
|
33
|
+
|
|
34
|
+
AggressiveTokenizer.prototype.tokenize = function(text) {
|
|
35
|
+
text = normalizer.remove_diacritics(text);
|
|
36
|
+
|
|
37
|
+
// break a string up into an array of tokens by anything non-word
|
|
38
|
+
return this.trim(text.split(/[^A-Za-z0-9_æøåÆØÅäÄöÖüÜ]+/));
|
|
39
|
+
};
|
|
File without changes
|
|
File without changes
|
|
@@ -28,10 +28,10 @@ var Tokenizer = function() {
|
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
Tokenizer.prototype.trim = function(array) {
|
|
31
|
-
|
|
31
|
+
while (array[array.length - 1] == '')
|
|
32
32
|
array.pop();
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
while (array[0] == '')
|
|
35
35
|
array.shift();
|
|
36
36
|
|
|
37
37
|
return array;
|
|
File without changes
|
package/lib/natural/trie/trie.js
CHANGED
|
@@ -71,6 +71,53 @@ Trie.prototype.addStrings = function(list) {
|
|
|
71
71
|
}
|
|
72
72
|
};
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* A function to search the TRIE and return an array of
|
|
76
|
+
* words which have same prefix <prefix>
|
|
77
|
+
* for example if we had the following words in our database:
|
|
78
|
+
* a, ab, bc, cd, abc, abd
|
|
79
|
+
* and we search the string: a
|
|
80
|
+
* we will get :
|
|
81
|
+
* [a, ab, abc, abd]
|
|
82
|
+
**/
|
|
83
|
+
Trie.prototype.keysWithPrefix = function(prefix) {
|
|
84
|
+
if(this.caseSensitive === false) {
|
|
85
|
+
prefix = prefix.toLowerCase();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function isEmpty (object) {
|
|
89
|
+
for (var key in object) if (object.hasOwnProperty(key)) return false;
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function get (node, word) {
|
|
94
|
+
if(!node) return null;
|
|
95
|
+
if(word.length == 0) return node;
|
|
96
|
+
return get(node.dictionary[word[0]], word.substring(1));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function recurse ( node, stringAgg, resultsAgg) {
|
|
100
|
+
if (!node) return;
|
|
101
|
+
|
|
102
|
+
// Check if this is a word
|
|
103
|
+
if (node.$) {
|
|
104
|
+
resultsAgg.push(stringAgg);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (isEmpty(node.dictionary)) {
|
|
108
|
+
return ;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
for (var c in node.dictionary) {
|
|
112
|
+
recurse (node.dictionary[c],stringAgg + c, resultsAgg);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
var results = [];
|
|
117
|
+
recurse (get(this, prefix), prefix, results);
|
|
118
|
+
return results;
|
|
119
|
+
};
|
|
120
|
+
|
|
74
121
|
/**
|
|
75
122
|
* A function to search the given string and return true if it lands
|
|
76
123
|
* on on a word. Essentially testing if the word exists in the database.
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
function Bag() {
|
|
25
|
+
this.dictionary = [];
|
|
26
|
+
this.nElement = 0;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
Bag.prototype.add = function(element) {
|
|
30
|
+
this.dictionary.push(element);
|
|
31
|
+
return this;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
Bag.prototype.isEmpty = function() {
|
|
35
|
+
return this.nElement > 0;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
Bag.prototype.contains = function(item) {
|
|
39
|
+
return this.dictionary.indexOf(item) >= 0;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* unpack the bag , and get all items
|
|
44
|
+
*/
|
|
45
|
+
Bag.prototype.unpack = function() {
|
|
46
|
+
// return a copy is better than original
|
|
47
|
+
return this.dictionary.slice();
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
module.exports = Bag;
|
|
@@ -0,0 +1,112 @@
|
|
|
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 util = require('util'),
|
|
25
|
+
Bag = require('./bag');
|
|
26
|
+
|
|
27
|
+
var DirectedEdge = function(start, end, weight) {
|
|
28
|
+
this.start = start;
|
|
29
|
+
this.end = end;
|
|
30
|
+
this.weight = weight;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
DirectedEdge.prototype.weight = function() {
|
|
34
|
+
return this.weight;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
DirectedEdge.prototype.from = function() {
|
|
38
|
+
return this.start;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
DirectedEdge.prototype.to = function() {
|
|
42
|
+
return this.end;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
DirectedEdge.prototype.toString = function() {
|
|
46
|
+
return util.format('%s -> %s, %s', this.start, this.end, this.weight);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
var EdgeWeightedDigraph = function() {
|
|
50
|
+
this.edgesNum = 0;
|
|
51
|
+
this.adj = []; // adjacency list
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* the number of vertexs saved.
|
|
56
|
+
*/
|
|
57
|
+
EdgeWeightedDigraph.prototype.v = function() {
|
|
58
|
+
return this.adj.length;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* the number of edges saved.
|
|
63
|
+
*/
|
|
64
|
+
EdgeWeightedDigraph.prototype.e = function() {
|
|
65
|
+
return this.edgesNum;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
EdgeWeightedDigraph.prototype.add = function(start, end, weight) {
|
|
69
|
+
var e = new DirectedEdge(start, end, weight);
|
|
70
|
+
this.addEdge(e);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
EdgeWeightedDigraph.prototype.addEdge = function(e) {
|
|
74
|
+
if(!this.adj[e.from()]) {
|
|
75
|
+
this.adj[e.from()] = new Bag();
|
|
76
|
+
}
|
|
77
|
+
this.adj[e.from()].add(e);
|
|
78
|
+
this.edgesNum++;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* use callback on all edges from v.
|
|
83
|
+
*/
|
|
84
|
+
EdgeWeightedDigraph.prototype.getAdj = function(v) {
|
|
85
|
+
if(!this.adj[v]) return [];
|
|
86
|
+
return this.adj[v].unpack();
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* use callback on all edges.
|
|
91
|
+
*/
|
|
92
|
+
EdgeWeightedDigraph.prototype.edges = function() {
|
|
93
|
+
var adj = this.adj;
|
|
94
|
+
var list = new Bag();
|
|
95
|
+
for(var i in adj) {
|
|
96
|
+
adj[i].unpack().forEach(function(item) {
|
|
97
|
+
list.add(item);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return list.unpack();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
EdgeWeightedDigraph.prototype.toString = function() {
|
|
104
|
+
var result = [];
|
|
105
|
+
var list = this.edges();
|
|
106
|
+
list.forEach(function(edge) {
|
|
107
|
+
result.push(edge.toString());
|
|
108
|
+
});
|
|
109
|
+
return result.join('\n');
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
module.exports = EdgeWeightedDigraph;
|
|
@@ -0,0 +1,99 @@
|
|
|
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 EdgeWeightedDigraph = require('./edge_weighted_digraph'),
|
|
25
|
+
Topological = require('./topological');
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The LongestPathTree represents a data type for solving the
|
|
29
|
+
* single-source longest paths problem in edge-weighted directed
|
|
30
|
+
* acyclic graphs (DAGs). The edge weights can be positive, negative, or zero.
|
|
31
|
+
* This implementation uses a topological-sort based algorithm.
|
|
32
|
+
* the distTo() and hasPathTo() methods take
|
|
33
|
+
* constant time and the pathTo() method takes time proportional to the
|
|
34
|
+
* number of edges in the longest path returned.
|
|
35
|
+
*/
|
|
36
|
+
var LongestPathTree = function(digraph, start) {
|
|
37
|
+
var _this = this;
|
|
38
|
+
this.edgeTo = [];
|
|
39
|
+
this.distTo = [];
|
|
40
|
+
this.distTo[start] = 0.0;
|
|
41
|
+
this.start = start;
|
|
42
|
+
this.top = new Topological(digraph);
|
|
43
|
+
this.top.order().forEach(function(vertex){
|
|
44
|
+
_this.relaxVertex(digraph, vertex, _this);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
LongestPathTree.prototype.relaxEdge = function(e) {
|
|
49
|
+
var distTo = this.distTo,
|
|
50
|
+
edgeTo = this.edgeTo;
|
|
51
|
+
var v = e.from(), w = e.to();
|
|
52
|
+
if (distTo[w] < distTo[v] + e.weight) {
|
|
53
|
+
distTo[w] = distTo[v] + e.weight;
|
|
54
|
+
edgeTo[w] = e;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* relax a vertex v in the specified digraph g
|
|
60
|
+
* @param {EdgeWeightedDigraph} the apecified digraph
|
|
61
|
+
* @param {Vertex} v vertex to be relaxed
|
|
62
|
+
*/
|
|
63
|
+
LongestPathTree.prototype.relaxVertex = function(digraph, vertex, tree) {
|
|
64
|
+
var distTo = tree.distTo;
|
|
65
|
+
var edgeTo = tree.edgeTo;
|
|
66
|
+
|
|
67
|
+
digraph.getAdj(vertex).forEach(function(edge){
|
|
68
|
+
var w = edge.to();
|
|
69
|
+
distTo[w] = distTo[w] || 0.0;
|
|
70
|
+
distTo[vertex] = distTo[vertex] || 0.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;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
LongestPathTree.prototype.getDistTo = function(v) {
|
|
81
|
+
return this.distTo[v];
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
LongestPathTree.prototype.hasPathTo = function(v) {
|
|
85
|
+
return !!this.distTo[v];
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
LongestPathTree.prototype.pathTo = function(v) {
|
|
89
|
+
if (!this.hasPathTo(v)) return [];
|
|
90
|
+
var path = [];
|
|
91
|
+
var edgeTo = this.edgeTo;
|
|
92
|
+
for (var e = edgeTo[v]; !!e; e = edgeTo[e.from()]) {
|
|
93
|
+
path.push(e.to());
|
|
94
|
+
}
|
|
95
|
+
path.push(this.start);
|
|
96
|
+
return path.reverse();
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
module.exports = LongestPathTree;
|
|
@@ -0,0 +1,100 @@
|
|
|
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 EdgeWeightedDigraph = require('./edge_weighted_digraph'),
|
|
25
|
+
Topological = require('./topological');
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The ShortestPathTree represents a data type for solving the
|
|
29
|
+
* single-source shortest paths problem in edge-weighted directed
|
|
30
|
+
* acyclic graphs (DAGs). The edge weights can be positive, negative, or zero.
|
|
31
|
+
* This implementation uses a topological-sort based algorithm.
|
|
32
|
+
* the distTo() and hasPathTo() methods take
|
|
33
|
+
* constant time and the pathTo() method takes time proportional to the
|
|
34
|
+
* number of edges in the longest path returned.
|
|
35
|
+
*/
|
|
36
|
+
var ShortestPathTree = function(digraph, start) {
|
|
37
|
+
var _this = this;
|
|
38
|
+
this.edgeTo = [];
|
|
39
|
+
this.distTo = [];
|
|
40
|
+
this.distTo[start] = 0.0;
|
|
41
|
+
this.start = start;
|
|
42
|
+
this.top = new Topological(digraph);
|
|
43
|
+
this.top.order().forEach(function(vertex){
|
|
44
|
+
_this.relaxVertex(digraph, vertex, _this);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
ShortestPathTree.prototype.relaxEdge = function(e) {
|
|
49
|
+
var distTo = this.distTo,
|
|
50
|
+
edgeTo = this.edgeTo;
|
|
51
|
+
var v = e.from(), w = e.to();
|
|
52
|
+
if (distTo[w] > distTo[v] + e.weight) {
|
|
53
|
+
distTo[w] = distTo[v] + e.weight;
|
|
54
|
+
edgeTo[w] = e;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* relax a vertex v in the specified digraph g
|
|
60
|
+
* @param {EdgeWeightedDigraph} the apecified digraph
|
|
61
|
+
* @param {Vertex} v vertex to be relaxed
|
|
62
|
+
*/
|
|
63
|
+
ShortestPathTree.prototype.relaxVertex = function(digraph, vertex, tree) {
|
|
64
|
+
var distTo = tree.distTo;
|
|
65
|
+
var edgeTo = tree.edgeTo;
|
|
66
|
+
digraph.getAdj(vertex).forEach(function(edge){
|
|
67
|
+
var w = edge.to();
|
|
68
|
+
distTo[w] = /\d/.test(distTo[w]) ? distTo[w] : Number.MAX_VALUE;
|
|
69
|
+
distTo[vertex] = distTo[vertex] || 0;
|
|
70
|
+
if (distTo[w] > distTo[vertex] + edge.weight) {
|
|
71
|
+
// in case of the result of 0.28+0.34 is 0.62000001
|
|
72
|
+
distTo[w] = parseFloat((distTo[vertex] + edge.weight).toFixed(2));
|
|
73
|
+
edgeTo[w] = edge;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
ShortestPathTree.prototype.getDistTo = function(v) {
|
|
80
|
+
return this.distTo[v];
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
ShortestPathTree.prototype.hasPathTo = function(v) {
|
|
84
|
+
var dist = this.distTo[v];
|
|
85
|
+
if(v == this.start) return false;
|
|
86
|
+
return /\d/.test(dist) ? dist != Number.MAX_VALUE : false;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
ShortestPathTree.prototype.pathTo = function(v) {
|
|
90
|
+
if (!this.hasPathTo(v) || v == this.start) return [];
|
|
91
|
+
var path = [];
|
|
92
|
+
var edgeTo = this.edgeTo;
|
|
93
|
+
for (var e = edgeTo[v]; !!e; e = edgeTo[e.from()]) {
|
|
94
|
+
path.push(e.to());
|
|
95
|
+
}
|
|
96
|
+
path.push(this.start);
|
|
97
|
+
return path.reverse();
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
module.exports = ShortestPathTree;
|
|
File without changes
|