-
Notifications
You must be signed in to change notification settings - Fork 1
/
Autocomplete.js
51 lines (40 loc) · 989 Bytes
/
Autocomplete.js
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
var fs = require("fs");
var BurstTrie = require("burst-trie");
var OptionsFromBSTNode = require("./OptionsFromBSTNode.js");
var Autocomplete = module.exports = function (dictPath)
{
this.trie = BurstTrie.createTrie();
this.options = new OptionsFromBSTNode(this.trie);
this.trieWriter = BurstTrie.createTrieWriter();
this.load(dictPath);
};
Autocomplete.prototype.trieToString = function ()
{
return this.trieWriter.writeTrie(this.trie);
};
Autocomplete.prototype.load = function(path)
{
var data = fs.readFileSync(path, "utf8");
var words = data.split(" ");
words.sort(randOrd);
for (var i=0; i < words.length; i++)
{
this.add(words[i]);
}
};
Autocomplete.prototype.add = function(term)
{
this.trie.add(term);
};
Autocomplete.prototype.unload = function()
{
this.trie = BurstTrie.createTrie();
};
Autocomplete.prototype.getOptionsFor = function (term)
{
return this.options.getOptionsFor(term);
};
function randOrd()
{
return (Math.round(Math.random())-0.5);
};