@vdegenne/kuromoji-core 0.0.0-stage → 0.1.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.
package/README.md CHANGED
@@ -1,3 +1,44 @@
1
- # Temporary Holding Version
1
+ # @vdegenne/kuromoji-core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A modified fork of [kuromoji.js](https://github.com/takuyaa/kuromoji.js).
4
+
5
+ ## Features
6
+
7
+ - Supports uncompressed `.dat` and compressed `.dat.gz` dictionary files.
8
+ - Provides download progress tracking for all 12 dictionary files through an optional `onProgress` callback.
9
+
10
+ ## Usage
11
+
12
+ This package does **not** include dictionary files.
13
+
14
+ It is primarily intended to be used through [`@vdegenne/kuromoji`](https://www.npmjs.com/package/@vdegenne/kuromoji), which provides the dictionaries and handles their integration.
15
+
16
+ For direct usage, you must provide your own dictionary files and specify their location:
17
+
18
+ ```js
19
+ var kuromoji = require("@vdegenne/kuromoji-core");
20
+
21
+ kuromoji.builder({
22
+ dicPath: "/path/to/dict/",
23
+ dictFileFormat: "dat",
24
+ onProgress: function (progress) {
25
+ console.log(progress);
26
+ }
27
+ }).build(function (err, tokenizer) {
28
+ if (err) {
29
+ throw err;
30
+ }
31
+
32
+ // Tokenizer is ready
33
+ });
34
+ ```
35
+
36
+ ## Options
37
+
38
+ | Option | Description | Default |
39
+ | --- | --- | --- |
40
+ | `dicPath` | Dictionary directory path | `"dict/"` |
41
+ | `dictFileFormat` | `"dat"` or `"gz"` | `"dat"` |
42
+ | `onProgress` | Optional download progress callback | `null` |
43
+
44
+ The progress object contains `loaded`, `total`, `lengthComputable`, `filesLoaded` and `filesTotal`.
package/package.json CHANGED
@@ -1,6 +1,75 @@
1
1
  {
2
- "name": "@vdegenne/kuromoji-core",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@vdegenne/kuromoji-core",
3
+ "description": "Core Kuromoji.js engine with support for .dat and .dat.gz dictionaries and loading progress tracking.",
4
+ "version": "0.1.3",
5
+ "browser": {
6
+ "./src/loader/NodeDictionaryLoader.js": "./src/loader/BrowserDictionaryLoader.js"
7
+ },
8
+ "bugs": {
9
+ "url": "https://github.com/vdegenne/kuromoji.js/issues"
10
+ },
11
+ "files": [
12
+ "src/"
13
+ ],
14
+ "types": "./src/kuromoji.d.ts",
15
+ "dependencies": {
16
+ "async": "^2.0.1",
17
+ "doublearray": "0.0.2"
18
+ },
19
+ "devDependencies": {
20
+ "@types/kuromoji": "^0.1.3",
21
+ "browserify": "^16.1.1",
22
+ "chai": "^4.1.2",
23
+ "coveralls": "^3.0.0",
24
+ "del": "^3.0.0",
25
+ "event-stream": "^3.3.4",
26
+ "gulp": "^3.8.10",
27
+ "gulp-bower": "0.0.14",
28
+ "gulp-bump": "^3.1.0",
29
+ "gulp-conventional-changelog": "^1.1.0",
30
+ "gulp-gh-pages-will": "^0.5.5",
31
+ "gulp-git": "^2.5.2",
32
+ "gulp-gzip": "^1.2.0",
33
+ "gulp-istanbul": "^1.0.0",
34
+ "gulp-jsdoc3": "^1.0.1",
35
+ "gulp-jshint": "^2.1.0",
36
+ "gulp-mocha": "^5.0.0",
37
+ "gulp-webserver": "^0.9.1",
38
+ "jshint": "^2.9.5",
39
+ "mecab-ipadic-seed": "0.0.2",
40
+ "minimist": "^1.2.0",
41
+ "mocha-lcov-reporter": "^1.2.0",
42
+ "run-sequence": "^2.2.1",
43
+ "vinyl-source-stream": "^2.0.0"
44
+ },
45
+ "homepage": "https://github.com/vdegenne/kuromoji.js",
46
+ "keywords": [
47
+ "japanese",
48
+ "morphological analyzer",
49
+ "nlp",
50
+ "pos",
51
+ "pos tagger",
52
+ "tokenizer"
53
+ ],
54
+ "license": "Apache-2.0",
55
+ "main": "./src/kuromoji.js",
56
+ "repository": {
57
+ "type": "git",
58
+ "url": "https://github.com/vdegenne/kuromoji.js.git"
59
+ },
60
+ "scripts": {
61
+ "build": "gulp build",
62
+ "build-demo": "gulp build-demo",
63
+ "build-dict": "gulp build-dict",
64
+ "clean": "gulp clean",
65
+ "coverage": "gulp coverage",
66
+ "deploy": "gulp deploy",
67
+ "jsdoc": "gulp jsdoc",
68
+ "lint": "gulp lint",
69
+ "release": "gulp release",
70
+ "test": "gulp test",
71
+ "version": "gulp version",
72
+ "watch": "gulp watch",
73
+ "webserver": "gulp webserver"
74
+ }
75
+ }
@@ -0,0 +1,129 @@
1
+ /*
2
+ * Copyright 2014 Takuya Asano
3
+ * Copyright 2010-2014 Atilika Inc. and contributors
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ "use strict";
19
+
20
+ var ViterbiBuilder = require("./viterbi/ViterbiBuilder");
21
+ var ViterbiSearcher = require("./viterbi/ViterbiSearcher");
22
+ var IpadicFormatter = require("./util/IpadicFormatter");
23
+
24
+ var PUNCTUATION = /、|。/;
25
+
26
+ /**
27
+ * Tokenizer
28
+ * @param {DynamicDictionaries} dic Dictionaries used by this tokenizer
29
+ * @constructor
30
+ */
31
+ function Tokenizer(dic) {
32
+ this.token_info_dictionary = dic.token_info_dictionary;
33
+ this.unknown_dictionary = dic.unknown_dictionary;
34
+ this.viterbi_builder = new ViterbiBuilder(dic);
35
+ this.viterbi_searcher = new ViterbiSearcher(dic.connection_costs);
36
+ this.formatter = new IpadicFormatter(); // TODO Other dictionaries
37
+ }
38
+
39
+ /**
40
+ * Split into sentence by punctuation
41
+ * @param {string} input Input text
42
+ * @returns {Array.<string>} Sentences end with punctuation
43
+ */
44
+ Tokenizer.splitByPunctuation = function (input) {
45
+ var sentences = [];
46
+ var tail = input;
47
+ while (true) {
48
+ if (tail === "") {
49
+ break;
50
+ }
51
+ var index = tail.search(PUNCTUATION);
52
+ if (index < 0) {
53
+ sentences.push(tail);
54
+ break;
55
+ }
56
+ sentences.push(tail.substring(0, index + 1));
57
+ tail = tail.substring(index + 1);
58
+ }
59
+ return sentences;
60
+ };
61
+
62
+ /**
63
+ * Tokenize text
64
+ * @param {string} text Input text to analyze
65
+ * @returns {Array} Tokens
66
+ */
67
+ Tokenizer.prototype.tokenize = function (text) {
68
+ var sentences = Tokenizer.splitByPunctuation(text);
69
+ var tokens = [];
70
+ for (var i = 0; i < sentences.length; i++) {
71
+ var sentence = sentences[i];
72
+ this.tokenizeForSentence(sentence, tokens);
73
+ }
74
+ return tokens;
75
+ };
76
+
77
+ Tokenizer.prototype.tokenizeForSentence = function (sentence, tokens) {
78
+ if (tokens == null) {
79
+ tokens = [];
80
+ }
81
+ var lattice = this.getLattice(sentence);
82
+ var best_path = this.viterbi_searcher.search(lattice);
83
+ var last_pos = 0;
84
+ if (tokens.length > 0) {
85
+ last_pos = tokens[tokens.length - 1].word_position;
86
+ }
87
+
88
+ for (var j = 0; j < best_path.length; j++) {
89
+ var node = best_path[j];
90
+
91
+ var token, features, features_line;
92
+ if (node.type === "KNOWN") {
93
+ features_line = this.token_info_dictionary.getFeatures(node.name);
94
+ if (features_line == null) {
95
+ features = [];
96
+ } else {
97
+ features = features_line.split(",");
98
+ }
99
+ token = this.formatter.formatEntry(node.name, last_pos + node.start_pos, node.type, features);
100
+ } else if (node.type === "UNKNOWN") {
101
+ // Unknown word
102
+ features_line = this.unknown_dictionary.getFeatures(node.name);
103
+ if (features_line == null) {
104
+ features = [];
105
+ } else {
106
+ features = features_line.split(",");
107
+ }
108
+ token = this.formatter.formatUnknownEntry(node.name, last_pos + node.start_pos, node.type, features, node.surface_form);
109
+ } else {
110
+ // TODO User dictionary
111
+ token = this.formatter.formatEntry(node.name, last_pos + node.start_pos, node.type, []);
112
+ }
113
+
114
+ tokens.push(token);
115
+ }
116
+
117
+ return tokens;
118
+ };
119
+
120
+ /**
121
+ * Build word lattice
122
+ * @param {string} text Input text to analyze
123
+ * @returns {ViterbiLattice} Word lattice
124
+ */
125
+ Tokenizer.prototype.getLattice = function (text) {
126
+ return this.viterbi_builder.build(text);
127
+ };
128
+
129
+ module.exports = Tokenizer;
@@ -0,0 +1,50 @@
1
+ /*
2
+ * Copyright 2014 Takuya Asano
3
+ * Copyright 2010-2014 Atilika Inc. and contributors
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with
7
+ * the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed
14
+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
15
+ * OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing
17
+ * permissions and limitations under the License.
18
+ */
19
+
20
+ 'use strict'
21
+
22
+ var Tokenizer = require('./Tokenizer')
23
+ var DictionaryLoader = require('./loader/NodeDictionaryLoader')
24
+
25
+ function TokenizerBuilder(option) {
26
+ option = option || {}
27
+
28
+ this.dic_path = option.dicPath == null ? 'dict/' : option.dicPath
29
+
30
+ this.dict_file_format = option.dictFileFormat || 'dat'
31
+ this.on_progress = option.onProgress || null
32
+ }
33
+
34
+ TokenizerBuilder.prototype.build = function (callback) {
35
+ var loader = new DictionaryLoader(this.dic_path, {
36
+ dictFileFormat: this.dict_file_format,
37
+ onProgress: this.on_progress,
38
+ })
39
+
40
+ loader.load(function (err, dic) {
41
+ if (err) {
42
+ callback(err, null)
43
+ return
44
+ }
45
+
46
+ callback(null, new Tokenizer(dic))
47
+ })
48
+ }
49
+
50
+ module.exports = TokenizerBuilder
@@ -0,0 +1,37 @@
1
+ /*
2
+ * Copyright 2014 Takuya Asano
3
+ * Copyright 2010-2014 Atilika Inc. and contributors
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ "use strict";
19
+
20
+ /**
21
+ * CharacterClass
22
+ * @param {number} class_id
23
+ * @param {string} class_name
24
+ * @param {boolean} is_always_invoke
25
+ * @param {boolean} is_grouping
26
+ * @param {number} max_length
27
+ * @constructor
28
+ */
29
+ function CharacterClass(class_id, class_name, is_always_invoke, is_grouping, max_length) {
30
+ this.class_id = class_id;
31
+ this.class_name = class_name;
32
+ this.is_always_invoke = is_always_invoke;
33
+ this.is_grouping = is_grouping;
34
+ this.max_length = max_length;
35
+ }
36
+
37
+ module.exports = CharacterClass;
@@ -0,0 +1,205 @@
1
+ /*
2
+ * Copyright 2014 Takuya Asano
3
+ * Copyright 2010-2014 Atilika Inc. and contributors
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ "use strict";
19
+
20
+ var InvokeDefinitionMap = require("./InvokeDefinitionMap");
21
+ var CharacterClass = require("./CharacterClass");
22
+ var SurrogateAwareString = require("../util/SurrogateAwareString");
23
+
24
+ var DEFAULT_CATEGORY = "DEFAULT";
25
+
26
+ /**
27
+ * CharacterDefinition represents char.def file and
28
+ * defines behavior of unknown word processing
29
+ * @constructor
30
+ */
31
+ function CharacterDefinition() {
32
+ this.character_category_map = new Uint8Array(65536); // for all UCS2 code points
33
+ this.compatible_category_map = new Uint32Array(65536); // for all UCS2 code points
34
+ this.invoke_definition_map = null;
35
+ }
36
+
37
+ /**
38
+ * Load CharacterDefinition
39
+ * @param {Uint8Array} cat_map_buffer
40
+ * @param {Uint32Array} compat_cat_map_buffer
41
+ * @param {InvokeDefinitionMap} invoke_def_buffer
42
+ * @returns {CharacterDefinition}
43
+ */
44
+ CharacterDefinition.load = function (cat_map_buffer, compat_cat_map_buffer, invoke_def_buffer) {
45
+ var char_def = new CharacterDefinition();
46
+ char_def.character_category_map = cat_map_buffer;
47
+ char_def.compatible_category_map = compat_cat_map_buffer;
48
+ char_def.invoke_definition_map = InvokeDefinitionMap.load(invoke_def_buffer);
49
+ return char_def;
50
+ };
51
+
52
+ CharacterDefinition.parseCharCategory = function (class_id, parsed_category_def) {
53
+ var category = parsed_category_def[1];
54
+ var invoke = parseInt(parsed_category_def[2]);
55
+ var grouping = parseInt(parsed_category_def[3]);
56
+ var max_length = parseInt(parsed_category_def[4]);
57
+ if (!isFinite(invoke) || (invoke !== 0 && invoke !== 1)) {
58
+ console.log("char.def parse error. INVOKE is 0 or 1 in:" + invoke);
59
+ return null;
60
+ }
61
+ if (!isFinite(grouping) || (grouping !== 0 && grouping !== 1)) {
62
+ console.log("char.def parse error. GROUP is 0 or 1 in:" + grouping);
63
+ return null;
64
+ }
65
+ if (!isFinite(max_length) || max_length < 0) {
66
+ console.log("char.def parse error. LENGTH is 1 to n:" + max_length);
67
+ return null;
68
+ }
69
+ var is_invoke = (invoke === 1);
70
+ var is_grouping = (grouping === 1);
71
+
72
+ return new CharacterClass(class_id, category, is_invoke, is_grouping, max_length);
73
+ };
74
+
75
+ CharacterDefinition.parseCategoryMapping = function (parsed_category_mapping) {
76
+ var start = parseInt(parsed_category_mapping[1]);
77
+ var default_category = parsed_category_mapping[2];
78
+ var compatible_category = (3 < parsed_category_mapping.length) ? parsed_category_mapping.slice(3) : [];
79
+ if (!isFinite(start) || start < 0 || start > 0xFFFF) {
80
+ console.log("char.def parse error. CODE is invalid:" + start);
81
+ }
82
+ return { start: start, default: default_category, compatible: compatible_category};
83
+ };
84
+
85
+ CharacterDefinition.parseRangeCategoryMapping = function (parsed_category_mapping) {
86
+ var start = parseInt(parsed_category_mapping[1]);
87
+ var end = parseInt(parsed_category_mapping[2]);
88
+ var default_category = parsed_category_mapping[3];
89
+ var compatible_category = (4 < parsed_category_mapping.length) ? parsed_category_mapping.slice(4) : [];
90
+ if (!isFinite(start) || start < 0 || start > 0xFFFF) {
91
+ console.log("char.def parse error. CODE is invalid:" + start);
92
+ }
93
+ if (!isFinite(end) || end < 0 || end > 0xFFFF) {
94
+ console.log("char.def parse error. CODE is invalid:" + end);
95
+ }
96
+ return { start: start, end: end, default: default_category, compatible: compatible_category};
97
+ };
98
+
99
+ /**
100
+ * Initializing method
101
+ * @param {Array} category_mapping Array of category mapping
102
+ */
103
+ CharacterDefinition.prototype.initCategoryMappings = function (category_mapping) {
104
+ // Initialize map by DEFAULT class
105
+ var code_point;
106
+ if (category_mapping != null) {
107
+ for (var i = 0; i < category_mapping.length; i++) {
108
+ var mapping = category_mapping[i];
109
+ var end = mapping.end || mapping.start;
110
+ for (code_point = mapping.start; code_point <= end; code_point++) {
111
+
112
+ // Default Category class ID
113
+ this.character_category_map[code_point] = this.invoke_definition_map.lookup(mapping.default);
114
+
115
+ for (var j = 0; j < mapping.compatible.length; j++) {
116
+ var bitset = this.compatible_category_map[code_point];
117
+ var compatible_category = mapping.compatible[j];
118
+ if (compatible_category == null) {
119
+ continue;
120
+ }
121
+ var class_id = this.invoke_definition_map.lookup(compatible_category); // Default Category
122
+ if (class_id == null) {
123
+ continue;
124
+ }
125
+ var class_id_bit = 1 << class_id;
126
+ bitset = bitset | class_id_bit; // Set a bit of class ID 例えば、class_idが3のとき、3ビット目に1を立てる
127
+ this.compatible_category_map[code_point] = bitset;
128
+ }
129
+ }
130
+ }
131
+ }
132
+ var default_id = this.invoke_definition_map.lookup(DEFAULT_CATEGORY);
133
+ if (default_id == null) {
134
+ return;
135
+ }
136
+ for (code_point = 0; code_point < this.character_category_map.length; code_point++) {
137
+ // 他に何のクラスも定義されていなかったときだけ DEFAULT
138
+ if (this.character_category_map[code_point] === 0) {
139
+ // DEFAULT class ID に対応するビットだけ1を立てる
140
+ this.character_category_map[code_point] = 1 << default_id;
141
+ }
142
+ }
143
+ };
144
+
145
+ /**
146
+ * Lookup compatible categories for a character (not included 1st category)
147
+ * @param {string} ch UCS2 character (just 1st character is effective)
148
+ * @returns {Array.<CharacterClass>} character classes
149
+ */
150
+ CharacterDefinition.prototype.lookupCompatibleCategory = function (ch) {
151
+ var classes = [];
152
+
153
+ /*
154
+ if (SurrogateAwareString.isSurrogatePair(ch)) {
155
+ // Surrogate pair character codes can not be defined by char.def
156
+ return classes;
157
+ }*/
158
+ var code = ch.charCodeAt(0);
159
+ var integer;
160
+ if (code < this.compatible_category_map.length) {
161
+ integer = this.compatible_category_map[code]; // Bitset
162
+ }
163
+
164
+ if (integer == null || integer === 0) {
165
+ return classes;
166
+ }
167
+
168
+ for (var bit = 0; bit < 32; bit++) { // Treat "bit" as a class ID
169
+ if (((integer << (31 - bit)) >>> 31) === 1) {
170
+ var character_class = this.invoke_definition_map.getCharacterClass(bit);
171
+ if (character_class == null) {
172
+ continue;
173
+ }
174
+ classes.push(character_class);
175
+ }
176
+ }
177
+ return classes;
178
+ };
179
+
180
+
181
+ /**
182
+ * Lookup category for a character
183
+ * @param {string} ch UCS2 character (just 1st character is effective)
184
+ * @returns {CharacterClass} character class
185
+ */
186
+ CharacterDefinition.prototype.lookup = function (ch) {
187
+
188
+ var class_id;
189
+
190
+ var code = ch.charCodeAt(0);
191
+ if (SurrogateAwareString.isSurrogatePair(ch)) {
192
+ // Surrogate pair character codes can not be defined by char.def, so set DEFAULT category
193
+ class_id = this.invoke_definition_map.lookup(DEFAULT_CATEGORY);
194
+ } else if (code < this.character_category_map.length) {
195
+ class_id = this.character_category_map[code]; // Read as integer value
196
+ }
197
+
198
+ if (class_id == null) {
199
+ class_id = this.invoke_definition_map.lookup(DEFAULT_CATEGORY);
200
+ }
201
+
202
+ return this.invoke_definition_map.getCharacterClass(class_id);
203
+ };
204
+
205
+ module.exports = CharacterDefinition;
@@ -0,0 +1,59 @@
1
+ /*
2
+ * Copyright 2014 Takuya Asano
3
+ * Copyright 2010-2014 Atilika Inc. and contributors
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ "use strict";
19
+
20
+ /**
21
+ * Connection costs matrix from cc.dat file.
22
+ * 2 dimension matrix [forward_id][backward_id] -> cost
23
+ * @constructor
24
+ * @param {number} forward_dimension
25
+ * @param {number} backward_dimension
26
+ */
27
+ function ConnectionCosts(forward_dimension, backward_dimension) {
28
+ this.forward_dimension = forward_dimension;
29
+ this.backward_dimension = backward_dimension;
30
+
31
+ // leading 2 integers for forward_dimension, backward_dimension, respectively
32
+ this.buffer = new Int16Array(forward_dimension * backward_dimension + 2);
33
+ this.buffer[0] = forward_dimension;
34
+ this.buffer[1] = backward_dimension;
35
+ }
36
+
37
+ ConnectionCosts.prototype.put = function (forward_id, backward_id, cost) {
38
+ var index = forward_id * this.backward_dimension + backward_id + 2;
39
+ if (this.buffer.length < index + 1) {
40
+ throw "ConnectionCosts buffer overflow";
41
+ }
42
+ this.buffer[index] = cost;
43
+ };
44
+
45
+ ConnectionCosts.prototype.get = function (forward_id, backward_id) {
46
+ var index = forward_id * this.backward_dimension + backward_id + 2;
47
+ if (this.buffer.length < index + 1) {
48
+ throw "ConnectionCosts buffer overflow";
49
+ }
50
+ return this.buffer[index];
51
+ };
52
+
53
+ ConnectionCosts.prototype.loadConnectionCosts = function (connection_costs_buffer) {
54
+ this.forward_dimension = connection_costs_buffer[0];
55
+ this.backward_dimension = connection_costs_buffer[1];
56
+ this.buffer = connection_costs_buffer;
57
+ };
58
+
59
+ module.exports = ConnectionCosts;
@@ -0,0 +1,82 @@
1
+ /*
2
+ * Copyright 2014 Takuya Asano
3
+ * Copyright 2010-2014 Atilika Inc. and contributors
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ "use strict";
19
+
20
+ var doublearray = require("doublearray");
21
+ var TokenInfoDictionary = require("./TokenInfoDictionary");
22
+ var ConnectionCosts = require("./ConnectionCosts");
23
+ var UnknownDictionary = require("./UnknownDictionary");
24
+
25
+ /**
26
+ * Dictionaries container for Tokenizer
27
+ * @param {DoubleArray} trie
28
+ * @param {TokenInfoDictionary} token_info_dictionary
29
+ * @param {ConnectionCosts} connection_costs
30
+ * @param {UnknownDictionary} unknown_dictionary
31
+ * @constructor
32
+ */
33
+ function DynamicDictionaries(trie, token_info_dictionary, connection_costs, unknown_dictionary) {
34
+ if (trie != null) {
35
+ this.trie = trie;
36
+ } else {
37
+ this.trie = doublearray.builder(0).build([
38
+ {k: "", v: 1}
39
+ ]);
40
+ }
41
+ if (token_info_dictionary != null) {
42
+ this.token_info_dictionary = token_info_dictionary;
43
+ } else {
44
+ this.token_info_dictionary = new TokenInfoDictionary();
45
+ }
46
+ if (connection_costs != null) {
47
+ this.connection_costs = connection_costs;
48
+ } else {
49
+ // backward_size * backward_size
50
+ this.connection_costs = new ConnectionCosts(0, 0);
51
+ }
52
+ if (unknown_dictionary != null) {
53
+ this.unknown_dictionary = unknown_dictionary;
54
+ } else {
55
+ this.unknown_dictionary = new UnknownDictionary();
56
+ }
57
+ }
58
+
59
+ // from base.dat & check.dat
60
+ DynamicDictionaries.prototype.loadTrie = function (base_buffer, check_buffer) {
61
+ this.trie = doublearray.load(base_buffer, check_buffer);
62
+ return this;
63
+ };
64
+
65
+ DynamicDictionaries.prototype.loadTokenInfoDictionaries = function (token_info_buffer, pos_buffer, target_map_buffer) {
66
+ this.token_info_dictionary.loadDictionary(token_info_buffer);
67
+ this.token_info_dictionary.loadPosVector(pos_buffer);
68
+ this.token_info_dictionary.loadTargetMap(target_map_buffer);
69
+ return this;
70
+ };
71
+
72
+ DynamicDictionaries.prototype.loadConnectionCosts = function (cc_buffer) {
73
+ this.connection_costs.loadConnectionCosts(cc_buffer);
74
+ return this;
75
+ };
76
+
77
+ DynamicDictionaries.prototype.loadUnknownDictionaries = function (unk_buffer, unk_pos_buffer, unk_map_buffer, cat_map_buffer, compat_cat_map_buffer, invoke_def_buffer) {
78
+ this.unknown_dictionary.loadUnknownDictionaries(unk_buffer, unk_pos_buffer, unk_map_buffer, cat_map_buffer, compat_cat_map_buffer, invoke_def_buffer);
79
+ return this;
80
+ };
81
+
82
+ module.exports = DynamicDictionaries;