-
Notifications
You must be signed in to change notification settings - Fork 20
/
fuzzy.dart
279 lines (229 loc) · 7.51 KB
/
fuzzy.dart
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
library fuzzy;
import 'dart:math';
import 'bitap/bitap.dart';
import 'data/fuzzy_options.dart';
import 'data/result.dart';
export 'data/fuzzy_options.dart';
/// Fuzzy search in Dart.
///
/// Import this library as follows:
///
/// ```
/// import 'package:fuzzy/fuzzy.dart';
/// ```
class Fuzzy<T> {
/// Instantiates it given a list of strings to look into, and options
Fuzzy(
this.list, {
FuzzyOptions<T> options,
}) : options = options ?? FuzzyOptions<T>();
/// The original list of string
final List<T> list;
/// Fuzz search Options
final FuzzyOptions<T> options;
/// Search for a given [pattern] on the [list], optionally [limit]ing the result length
List<Result<T>> search(String pattern, [int limit = -1]) {
final searchers = _prepareSearchers(pattern);
final resultsAndWeights =
_search(searchers.tokenSearchers, searchers.fullSearcher);
_computeScore(resultsAndWeights.weights, resultsAndWeights.results);
if (options.shouldSort) {
_sort(resultsAndWeights.results);
}
if (limit > 0) {
return resultsAndWeights.results.sublist(0, limit);
}
return resultsAndWeights.results;
}
Searchers _prepareSearchers(String pattern) {
final tokenSearchers = <Bitap>[];
if (options.tokenize) {
// Tokenize on the separator
final tokens = pattern.split(options.tokenSeparator);
for (var i = 0, len = tokens.length; i < len; i += 1) {
tokenSearchers.add(Bitap(tokens[i], options: options));
}
}
final fullSearcher = Bitap(pattern, options: options);
return Searchers(
tokenSearchers: tokenSearchers,
fullSearcher: fullSearcher,
);
}
ResultsAndWeights<T> _search(List<Bitap> tokenSearchers, Bitap fullSearcher) {
final results = <Result<T>>[];
final resultMap = <int, Result<T>>{};
// 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 (list[0] is String) {
// Iterate over every item
for (var i = 0, len = list.length; i < len; i += 1) {
_analyze(
key: '',
value: list[i].toString(),
record: list[i],
index: i,
tokenSearchers: tokenSearchers,
fullSearcher: fullSearcher,
results: results,
resultMap: resultMap,
);
}
return ResultsAndWeights(results: results, weights: {});
}
// Otherwise, the first item is an Object (hopefully), and thus the searching
// is done on the values of the keys of each item.
final weights = <String, double>{};
for (var i = 0, len = list.length; i < len; i += 1) {
final item = list[i];
// Iterate over every key
for (var j = 0; j < options.keys.length; j += 1) {
final key = options.keys[j].name;
final value = options.keys[j].getter(item);
final weight = 1.0 - options.keys[j].weight ?? 0.0;
weights.update(key, (_) => weight, ifAbsent: () => weight);
_analyze(
key: key,
value: value,
record: list[i],
index: i,
tokenSearchers: tokenSearchers,
fullSearcher: fullSearcher,
results: results,
resultMap: resultMap,
);
}
}
return ResultsAndWeights(results: results, weights: weights);
}
List<Result<T>> _analyze({
String key = '',
int arrayIndex = -1,
String value,
T record,
int index,
List<Bitap> tokenSearchers = const [],
Bitap fullSearcher,
List<Result<T>> results,
Map<int, Result<T>> resultMap,
}) {
results ??= <Result<T>>[];
resultMap ??= <int, Result<T>>{};
// Check if the texvaluet can be searched
if (value == null) {
return [];
}
var exists = false;
var averageScore = -1;
var numTextMatches = 0;
final mainSearchResult = fullSearcher.search(value.toString());
_log('Full text: "${value}", score: ${mainSearchResult.score}');
if (options.tokenize) {
final words = value.toString().split(options.tokenSeparator);
final scores = <double>[];
for (var i = 0; i < tokenSearchers.length; i += 1) {
final tokenSearcher = tokenSearchers[i];
_log('\nPattern: "${tokenSearcher.pattern}"');
var hasMatchInText = false;
for (var j = 0; j < words.length; j += 1) {
final word = words[j];
final tokenSearchResult = tokenSearcher.search(word);
if (tokenSearchResult.isMatch) {
exists = true;
hasMatchInText = true;
scores.add(tokenSearchResult.score);
} else {
if (options.matchAllTokens) {
scores.add(1);
}
}
_log('Token: "${word}", score: ${tokenSearchResult.score}');
}
if (hasMatchInText) {
numTextMatches += 1;
}
}
final averageScore =
scores.fold(0, (memo, score) => memo + score) / scores.length;
_log('Token score average: $averageScore');
}
var finalScore = mainSearchResult.score;
if (averageScore > -1) {
finalScore = (finalScore + averageScore) / 2;
}
_log('Score average (final): $finalScore');
final checkTextMatches = (options.tokenize && options.matchAllTokens)
? numTextMatches >= tokenSearchers.length
: true;
_log('\nCheck Matches: ${checkTextMatches}');
// If a match is found, add the item to <rawResults>, including its score
if ((exists || mainSearchResult.isMatch) && checkTextMatches) {
// Check if the item already exists in our results
final existingResult = resultMap[index];
if (existingResult != null) {
// Use the lowest score
// existingResult.score, bitapResult.score
existingResult.matches.add(ResultDetails<T>(
key: key,
arrayIndex: arrayIndex,
value: value,
score: finalScore,
matchedIndices: mainSearchResult.matchedIndices,
));
} else {
// Add it to the raw result list
final res = Result(
item: record,
matches: [
ResultDetails<T>(
key: key,
arrayIndex: arrayIndex,
value: value,
score: finalScore,
matchedIndices: mainSearchResult.matchedIndices,
),
],
);
resultMap.update(
index,
(_) => res,
ifAbsent: () => res,
);
results.add(res);
}
}
return results;
}
void _computeScore(Map<String, double> weights, List<Result<T>> results) {
_log('\n\nComputing score:\n');
for (var i = 0, len = results.length; i < len; i += 1) {
final matches = results[i].matches;
final scoreLen = matches.length;
var currScore = 1.0;
var bestScore = 1.0;
for (var j = 0; j < scoreLen; j += 1) {
final weight = weights[matches[j].key] ?? 1.0;
final score = weight == 1.0
? matches[j].score
: (matches[j].score == 0.0 ? 0.001 : matches[j].score);
final nScore = score * weight;
if (weight != 1) {
bestScore = min(bestScore, nScore);
} else {
matches[j].nScore = nScore;
currScore *= nScore;
}
}
results[i].score = bestScore == 1.0 ? currScore : bestScore;
}
}
void _sort(List<Result<T>> results) {
_log('\n\nSorting....');
results.sort(options.sortFn);
}
void _log(String log) {
if (options.verbose) {
print(log);
}
}
}