forked from jedp/redis-completer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completer.js
208 lines (173 loc) · 5.84 KB
/
completer.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
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
var r = require('redis').createClient();
var _ = require('underscore');
var fs = require('fs');
// prefixes for redis sets
// use applicationPrefix() to set the initial prefix
var _appPrefix = '';
var ZKEY_COMPL = 'compl';
var ZKEY_DOCS_PREFIX = 'docs:';
exports.applicationPrefix = applicationPrefix = function(prefix) {
// update key prefixes with user-specified application prefix
_appPrefix = prefix;
ZKEY_COMPL = prefix + ':' + 'compl';
ZKEY_DOCS_PREFIX = prefix + ':' + 'docs:';
};
exports.deleteAll = deleteAll = function(cb) {
// clear all data
r.zremrangebyrank(ZKEY_COMPL, 0, -1, cb);
}
exports.counter = 0;
exports.addCompletions = addCompletions = function (phrase, id, score, cb) {
// Add completions for originalText to the completions trie.
// Store the original text, prefixed by the optional 'key'
if (typeof score === 'function') {
cb = score;
score = null;
}
var text = phrase.trim().toLowerCase();
if (! text) {
return null, null;
}
if (id !== null) {
phraseToStore = id + ':' + phrase;
} else {
phraseToStore = phrase;
}
_.each(text.split(/\s+/), function(word) {
for (var end_index=1; end_index <= word.length; end_index++) {
var prefix = word.slice(0, end_index);
exports.counter++;
r.zadd(ZKEY_COMPL, 0, prefix, cb);
}
exports.counter++;
r.zadd(ZKEY_COMPL, 0, word+'*', cb);
exports.counter++;
r.zadd(ZKEY_DOCS_PREFIX + word, score||0, phraseToStore, cb);
});
}
exports.addFromFile = addFromFile = function(filename) {
// reads the whole file at once
// no error-checking. just cross your fingers.
fs.readFile(filename, function(err, buf) {
if (err) {
return console.log("ERROR: reading " + filename + ": " + err), null;
}
_.each(buf.toString().split(/\n/), function(s) { addCompletions(s)});
});
}
exports.getWordCompletions = getWordCompletions = function(word, count, callback) {
// get up to count completions for the given word
// if prefix ends with '*', get the next exact completion
var rangelen = 50;
var prefix = word.toLowerCase().trim();
var getExact = word[word.length-1] === '*'
var results = []
if (! prefix) {
return callback(null, results);
}
r.zrank(ZKEY_COMPL, prefix, function(err, start) {
if (! start) {
return callback(null, results);
}
r.zrange(ZKEY_COMPL, start, start + rangelen - 1, function(err, entries) {
while (results.length <= count) {
if (! entries || entries.length === 0) {
break;
}
for (var i=0; i<entries.length; i++) {
var entry = entries[i];
var minlen = Math.min(entry.length, prefix.length);
if (entry.slice(0, minlen) !== prefix.slice(0, minlen)) {
return callback(null, results);
}
if (entry[entry.length-1] === '*' && results.length <= count) {
results.push(entry.slice(0, -1));
if (getExact) {
return callback(null, results);
}
}
}
}
return callback(null, results);
});
});
}
exports.getPhraseCompletions = getPhraseCompletions = function(phrase, count, callback) {
// when getting phrase completions, we should find a fuzzy match for the last
// word, but treat the words before it as what the user intends. So for
// instance, if we get "more pie", treat that as "more* pie"
phrase = phrase.toLowerCase().trim();
// tag all words but last as 'exact' matches
phrase = phrase.replace(/(\w)\s+/g, "$1\* ");
var prefixes = phrase.split(/\s+/);
var resultSet = {};
var iter = 0;
_.each(prefixes, function(prefix) {
getWordCompletions(prefix, count, function(err, results) {
if (err) {
callback(err, []);
} else {
_.each(results, function(result) {
// intending this to be a set, so we only
// add each result once
resultSet[result] = result;
});
}
iter++;
if (iter === prefixes.length) {
// map set back to list
var resultList = _.map(resultSet, function(val, key) { return key; });
callback(null, resultList);
}
});
});
}
exports.search = search = function(phrase, count, callback) {
// @callback with up to @count matches for @phrase
var count = count || 10;
var callback = callback || function() {};
getPhraseCompletions(phrase, 10, function(err, completions) {
if (err) {
callback(err, null);
} else {
var keys = _.map(completions, function(key) {
return ZKEY_DOCS_PREFIX+key
});
if (keys.length) {
var results = {};
var iter = 0;
// accumulate docs and the scores for each key
_.each(keys, function(key) {
r.zrevrangebyscore(key, 'inf', 0, 'withscores', function (err, docs) {
// returns a list of [doc, score, doc, score ...]
iter ++;
if (err) {
return callback(err, {});
} else {
while (docs.length > 0) {
var doc = docs.shift();
var score = parseFloat(docs.shift());
var prevScore = typeof results[doc] !== 'undefined' && results[doc] || 0;
results[doc] = score + prevScore;
}
// credit for more overall matches
results[doc] += 10 * keys.length;
}
if (iter == keys.length) {
// it's annoying to deal with dictionaries in js
// turn it into a sorted list for the client's convenience
var ret = [];
for (var key in results) {
ret.push(key);
}
ret.sort(function(a,b) { return results[b] - results[a] });
return callback(null, ret);
}
});
});
} else {
callback(null, []);
}
}
});
}