File: trie.js

package info (click to toggle)
tracker 3.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,096 kB
  • sloc: ansic: 57,908; javascript: 15,606; python: 6,272; cs: 242; perl: 106; sh: 98; xml: 29; makefile: 20
file content (306 lines) | stat: -rw-r--r-- 6,554 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Frozen trie interface for hotdoc's search extension.
 *
 * Copyright 2015,2016 Mathieu Duponchelle <mathieu.duponchelle@opencredd.com>
 * Copyright 2015,2016 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

LETTER_MASK = 0x7F;
FINAL_MASK = 1 << 7;
BFT_LAST_MASK = 1 << 8;

function TrieNode(trie, data) {
	this.edges = undefined;
	this.genitor = null;
	this.trie = trie;
	this.letter = String.fromCharCode((data & LETTER_MASK));

	this.is_final = false;
	if (data & FINAL_MASK) {
		this.is_final = true;
	}

	this.bft_last = false;
	if (data & BFT_LAST_MASK) {
		this.bft_last = true;
	}

	this.first_child_id = data >> 9;
}

TrieNode.prototype.get_edges = function() {
	if (this.edges != undefined) {
		return this.edges;
	}

	this.edges = {}

	var next_id = this.first_child_id;

	while (next_id != 0) {
		var edge = this.trie.get_node_by_index(next_id);
		edge.genitor = this;
		if (edge.bft_last) {
			next_id = 0;
		} else {
			next_id += 1;
		}
		this.edges[edge.letter] = edge;
	}

	return this.edges;
};

TrieNode.prototype.get_word = function() {
	var ancestor = this.genitor;
	var word_array = [this.letter];

	while (ancestor != null) {
		if (ancestor.genitor != null) {
			word_array.push(ancestor.letter);
		}
		ancestor = ancestor.genitor;
	}

	return (word_array.reverse().join(''));
};

function bytes_to_uint32be(data, index) {
	var result = 0;
	var i = 0;
	var bin_node;

	bin_node = data.slice(index * 4, index * 4 + 4);

	while (i < 4) {
		result = result << 8;
		var val = (bin_node[i].charCodeAt(0)) & 0xFF;
		result += val;
		i += 1;
	}

	return result;
}

function Trie(data, is_b64_encoded) {
	this.data = data;
	this.case_sensitive = true;
	if (is_b64_encoded) {
		this.data = atob(data);
	}
	this.root = this.get_node_by_index(0);
}

Trie.prototype.set_case_sensitive = function(case_sensitive) {
	this.case_sensitive = case_sensitive;
}

Trie.prototype.get_node_by_index = function(idx) {
	var uint32be = 	bytes_to_uint32be(this.data, idx);
	return new TrieNode(this, uint32be);
};

Trie.prototype.lookup_node = function (word, start_node) {
	var node;

	if (!this.case_sensitive) {
		word = word.toLowerCase();
	}

	if (start_node === undefined) {
		node = this.root;
	} else {
		node = start_node;
	}

	for (var i = 0; i < word.length; i++) {
		var letter = word[i];

		var edges = node.get_edges();
		if (letter in edges) {
			node = edges[letter];
		} else {
			return null;
		}
	}

	return node;
};

Trie.prototype.exists = function (word) {
	var node = this.lookup_node(word);

	return (node != null && node.is_final);
};

Trie.prototype.lookup_completions = function (start_node, max_completions) {
	var completions = [];

	var queue = [[start_node]];
	var node = null;

	while (queue.length) {
		var path = queue.pop();
		var vertex = path[path.length - 1];
		var cnodes = vertex.get_edges();
		for (var letter in cnodes) {
			node = cnodes[letter];

			if (node.is_final) {
				completions.push(node);
			}

			if (completions.length === max_completions) {
				queue = [];
				break;
			}

			var new_path = path.slice();
			new_path.push(node);
			queue.push(new_path);
		}
	}

	return completions;
};

Trie.prototype.submatches_for_node = function (node, word, submatches, max_submatches) {
	var match = this.lookup_node(word, node);

	if (!match) {
		return;
	}

	if (match.is_final) {
		submatches.push(match);
	}

	if (submatches.length === max_submatches) {
		return;
	}

	var completions = this.lookup_completions(match,
			max_submatches - submatches.length);

	submatches.push.apply(submatches, completions);
}

Trie.prototype.lookup_submatches = function (word, max_submatches) {
	var submatches = [];
	var queue = [[this.root]];
	var node = null;

	if (!this.case_sensitive) {
		word = word.toLowerCase();
	}

	this.submatches_for_node(this.root, word, submatches, max_submatches);

	if (submatches.length >= max_submatches) {
		return submatches;
	}

	while (queue.length) {
		var path = queue.pop();
		var vertex = path[path.length - 1];
		var cnodes = vertex.get_edges();
		for (var letter in cnodes) {
			node = cnodes[letter];

			this.submatches_for_node(node, word, submatches, max_submatches);

			if (submatches.length >= max_submatches) {
				queue = [];
				break;
			}

			var new_path = path.slice();
			new_path.push(node);
			queue.push(new_path);
		}
	}

	return submatches;
}

function my_range (arg_one, arg_two) {
	var res = new Array();
	var start = 0;
	var end = arg_one;

	if (arg_two != undefined) {
		start = arg_one;
		end = arg_two;
	}

	var i = start;

	while (i < end) {
		res.push(i);
		i += 1;
	}

	return res;
}

Trie.prototype.search_recursive = function (node, letter, word, previous_row, results,
		max_cost) {
	var columns = word.length + 1;
	var current_row = [previous_row[0] + 1];

	var column = 1;
	while (column < columns) {
		var insert_cost = current_row[column - 1] + 1;
		var delete_cost = previous_row[column] + 1;

		var replace_cost;
		if (word[column - 1] != letter) {
			replace_cost = previous_row[column - 1] + 1;
		} else {
			replace_cost = previous_row[column - 1];
		}

		current_row.push(Math.min(insert_cost, delete_cost, replace_cost));

		column += 1;
	}

	if (current_row[current_row.length - 1] <= max_cost && node.is_final) {
		results[node.get_word()] = current_row[current_row.length - 1];
	}

	if (Math.min.apply(null, current_row) <= max_cost) {
		var edges = node.get_edges();
		for (var letter in edges) {
			this.search_recursive(edges[letter], letter, word, current_row,
					results, max_cost);
		}
	}
};

Trie.prototype.search = function (word, max_cost) {
	var corrections = {};
	var current_row = my_range(word.length + 1);
	var edges = this.root.get_edges();

	for (var letter in edges) {
		this.search_recursive(edges[letter], letter, word, current_row,
				corrections, max_cost);
	}

	return corrections;
};