-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfuse.js
342 lines (292 loc) · 13.1 KB
/
fuse.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* Fuse - Lightweight fuzzy-search
*
* Copyright (c) 2012 Kirollos Risk <[email protected]>.
* All Rights Reserved. Apache Software License 2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
/**
* Adapted from "Diff, Match and Patch", by Google
*
* http://code.google.com/p/google-diff-match-patch/
*
* Modified by: Kirollos Risk <[email protected]>
* -----------------------------------------------
* Details: the algorithm and structure was modified to allow the creation of
* <Searcher> instances with a <search> method inside which does the actual
* bitap search. The <pattern> (the string that is searched for) is only defined
* once per instance and thus it eliminates redundant re-creation when searching
* over a list of strings.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
function Searcher(pattern, options) {
options = options || {};
// Aproximately where in the text is the pattern expected to be found?
var MATCH_LOCATION = options.location || 0,
// Determines how close the match must be to the fuzzy location (specified above).
// An exact letter match which is 'distance' characters away from the fuzzy location
// would score as a complete mismatch. A distance of '0' requires the match be at
// the exact location specified, a threshold of '1000' would require a perfect match
// to be within 800 characters of the fuzzy location to be found using a 0.8 threshold.
MATCH_DISTANCE = options.distance || 100,
// At what point does the match algorithm give up. A threshold of '0.0' requires a perfect match
// (of both letters and location), a threshold of '1.0' would match anything.
MATCH_THRESHOLD = options.threshold || 0.6,
pattern = options.caseSensitive ? pattern : pattern.toLowerCase(),
patternLen = pattern.length;
if (patternLen > 32) {
throw new Error('Pattern length is too long');
}
var matchmask = 1 << (patternLen - 1);
/**
* Initialise the alphabet for the Bitap algorithm.
* @return {Object} Hash of character locations.
* @private
*/
var pattern_alphabet = (function () {
var mask = {},
i = 0;
for (i = 0; i < patternLen; i++) {
mask[pattern.charAt(i)] = 0;
}
for (i = 0; i < patternLen; i++) {
mask[pattern.charAt(i)] |= 1 << (pattern.length - i - 1);
}
return mask;
})();
/**
* Compute and return the score for a match with <e> errors and <x? location.
* @param {number} e Number of errors in match.
* @param {number} x Location of match.
* @return {number} Overall score for match (0.0 = good, 1.0 = bad).
* @private
*/
function match_bitapScore(e, x) {
var accuracy = e / patternLen,
proximity = Math.abs(MATCH_LOCATION - x);
if (!MATCH_DISTANCE) {
// Dodge divide by zero error.
return proximity ? 1.0 : accuracy;
}
return accuracy + (proximity / MATCH_DISTANCE);
}
/**
* Compute and return the result of the search
* @param {String} text The text to search in
* @return
* {Object} Literal containing:
* {Boolean} isMatch Whether the text is a match or not
* {Decimal} score Overal score for the match
* @public
*/
this.search = function (text) {
text = options.caseSensitive ? text : text.toLowerCase();
if (pattern === text) {
// Exact match
return {
isMatch: true,
score: 0
};
}
var i, j,
// Set starting location at beginning text and initialise the alphabet.
textLen = text.length,
// Highest score beyond which we give up.
scoreThreshold = MATCH_THRESHOLD,
// Is there a nearby exact match? (speedup)
bestLoc = text.indexOf(pattern, MATCH_LOCATION),
binMin, binMid,
binMax = patternLen + textLen,
lastRd, start, finish, rd, charMatch,
score = 1,
locations = [];
if (bestLoc != -1) {
scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
// What about in the other direction? (speedup)
bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
if (bestLoc != -1) {
scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
}
}
bestLoc = -1;
for (i = 0; i < patternLen; i++) {
// Scan for the best match; each iteration allows for one more error.
// Run a binary search to determine how far from 'MATCH_LOCATION' we can stray at this
// error level.
binMin = 0;
binMid = binMax;
while (binMin < binMid) {
if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
binMin = binMid;
} else {
binMax = binMid;
}
binMid = Math.floor((binMax - binMin) / 2 + binMin);
}
// Use the result from this iteration as the maximum for the next.
binMax = binMid;
start = Math.max(1, MATCH_LOCATION - binMid + 1);
finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
// Initialize the bit array
rd = Array(finish + 2);
rd[finish + 1] = (1 << i) - 1;
for (j = finish; j >= start; j--) {
// The alphabet <pattern_alphabet> is a sparse hash, so the following line generates warnings.
charMatch = pattern_alphabet[text.charAt(j - 1)];
if (i === 0) {
// First pass: exact match.
rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
} else {
// Subsequent passes: fuzzy match.
rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | (((lastRd[j + 1] | lastRd[j]) << 1) | 1) | lastRd[j + 1];
}
if (rd[j] & matchmask) {
score = match_bitapScore(i, j - 1);
// This match will almost certainly be better than any existing match.
// But check anyway.
if (score <= scoreThreshold) {
// Told you so.
scoreThreshold = score;
bestLoc = j - 1;
locations.push(bestLoc);
if (bestLoc > MATCH_LOCATION) {
// When passing loc, don't exceed our current distance from loc.
start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
} else {
// Already passed loc, downhill from here on in.
break;
}
}
}
}
// No hope for a (better) match at greater error levels.
if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
break;
}
lastRd = rd;
}
return {
isMatch: bestLoc >= 0,
score: score
};
}
}
/**
* @param {Array} list
* @param {Object} options
* @public
*/
function Fuse(list, options) {
options = options || {};
var keys = options.keys;
/**
* Searches for all the items whose keys (fuzzy) match the pattern.
* @param {String} pattern The pattern string to fuzzy search on.
* @return {Array} A list of all serch matches.
* @public
*/
this.search = function (pattern) {
//console.time('total');
var searcher = new Searcher(pattern, options),
i, j, item, text, dataLen = list.length,
bitapResult, rawResults = [], resultMap = {},
rawResultsLen, existingResult, results = [],
compute = null;
//console.time('search');
/**
* Calls <Searcher::search> for bitap analysis. Builds the raw result list.
* @param {String} text The pattern string to fuzzy search on.
* @param {String|Int} entity If the <data> is an Array, then entity will be an index,
* otherwise it's the item object.
* @param {Int} index
* @return {Object|Int}
* @private
*/
function analyzeText(text, entity, index) {
// Check if the text can be searched
if (text !== undefined && text !== null && typeof text === 'string') {
// Get the result
bitapResult = searcher.search(text);
// If a match is found, add the item to <rawResults>, including its score
if (bitapResult.isMatch) {
//console.log(bitapResult.score);
// Check if the item already exists in our results
existingResult = resultMap[index];
if (existingResult) {
// Use the lowest score
existingResult.score = Math.min(existingResult.score, bitapResult.score);
} else {
// Add it to the raw result list
resultMap[index] = {
item: entity,
score: bitapResult.score
};
rawResults.push(resultMap[index]);
}
}
}
}
// Check the first item in the list, if it's a string, then we assume
// that every item in the list is also a string, and thus it's a flattened array.
if (typeof list[0] === 'string') {
// Iterate over every item
for (i = 0; i < dataLen; i++) {
analyzeText(list[i], i, i);
}
} else {
// Otherwise, the first item is an Object (hopefully), and thus the searching
// is done on the values of the keys of each item.
// Iterate over every item
for (i = 0; i < dataLen; i++) {
item = list[i];
// Iterate over every key
for (j = 0; j < keys.length; j++) {
analyzeText(item[keys[j]], item, i);
}
}
}
//console.timeEnd('search');
// Sort the results, form lowest to highest score
//console.time('sort');
rawResults.sort(function (a, b) {
return a.score - b.score;
});
//console.timeEnd('sort');
// From the results, push into a new array only the item identifier (if specified)
// of the entire item. This is because we don't want to return the <rawResults>,
// since it contains other metadata;
//console.time('build');
rawResultsLen = rawResults.length;
for (i = 0; i < rawResultsLen; i++) {
results.push(options.id ? rawResults[i].item[options.id] : rawResults[i].item);
}
//console.timeEnd('build');
//console.timeEnd('total');
return results;
}
}
//Export to Common JS Loader
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
if (typeof module.setExports === 'function') {
module.setExports(Fuse);
} else {
module.exports = Fuse;
}
} else {
window.Fuse = Fuse;
}
})();