-
Notifications
You must be signed in to change notification settings - Fork 3
/
fuzzle.js
178 lines (150 loc) · 4.13 KB
/
fuzzle.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
export default function find(options, search, fields=[], return_all=false, coverage_multiplier=0.02975){
search = trim(search.toLowerCase());
var parts = [];
for (var size = 1; size < search.length+1; size++) {
for (var i = 0; i < search.length; i++) {
var part = search.substring(i, i+size);
if (!parts.includes(part)){
parts.push(part);
}
}
}
parts.sort(compareLength);
var words = search.split(" ");
var max_coverage = 1 - search.length * coverage_multiplier;
var results = [];
for (var i in options){
var option = options[i];
if (typeof option == "string"){
option = {key: option};
}
var key = trim(option.key.toLowerCase());
var tags = "tags" in option ? option["tags"] : [];
var best = "";
for (var j in parts){
if (best != ""){
break;
}
if (key.includes(parts[j])){
best = parts[j];
} else {
for (var k in fields){
var field = fields[k];
var fieldValue = field in option ? option[field].toLowerCase() : "";
if (fieldValue.includes(parts[j])){
best = parts[j];
}
}
}
}
var coverage = best.length / search.length;
var match = key == search ? 1 : 0;
var word_matches = [];
var tag_match = false;
var tag_occurence = false;
var starts_with = key.startsWith(search) ? 2 : 0;
var starts_with_word = 0;
var starts_with_key = search.startsWith(key) ? 1 : 0;
var key_in_search = search.includes(key) ? 1 : 0;
var search_in_key = key.includes(search) ? 2 : 0;
for (var j in words){
var word = words[j];
for (var k in key.split(" ")){
var word1 = key.split(" ")[k];
if (word == word1 || word1.startsWith(word) || word1.endsWith(word) || word.startsWith(word1) || word.endsWith(word1)){
if (key.startsWith(word1)){
starts_with_word = 2;
}
if (word_matches.indexOf(word1) == -1) { // word1 not present in word_matches
word_matches.push(word1);
}
}
}
for (var k in tags){
var tag = tags[k].toLowerCase();
if (search == tag) {
tag_match = true;
} else if (tag.includes(word)) {
tag_occurence = true;
}
}
}
var possible_accuracy = 1 + 2 + 1 + 1 + 2 + 2 + words.length;
var accuracy = (match + starts_with + starts_with_word + starts_with_key + key_in_search + search_in_key + word_matches.length) / possible_accuracy;
if (coverage < max_coverage && !tag_match && !tag_occurence){
continue;
}
var cat = 7;
if (match == 1){
cat = 0;
} else if (search_in_key == 2){
cat = 1;
} else if (starts_with == 2){
cat = 2;
} else if (tag_match){
cat = 3;
} else if (starts_with_word == 2){
cat = 4;
} else if (key_in_search == 1){
cat = 5;
} else if (starts_with_key == 1){
cat = 6;
} else if (tag_occurence) {
cat = 7;
}
option["coverage"] = coverage;
option["accuracy"] = accuracy;
option["cat"] = cat;
option["match"] = match == 1;
if (!return_all && match == 1){
return [option];
}
results.push(option);
}
results.sort(compareAccuracy);
results.sort(compareCat);
return results;
}
/*
var query = "frontpage";
var options = [
{
"key": "Reddit",
"tags": ["memes", "jokes", "humor"],
"description": "The Frontpage of the Internet."
},
{
"key": "Google",
"tags": ["Alphabet", "Sundar Pichai"],
"description": "Search Engine used by the entire World."
}
]
console.log(find(options, query, ["description"]));
*/
function trim(s){
return ( s || '' ).replace( /^\s+|\s+$/g, '' );
}
function compareLength(a,b){
if (a.length > b.length){
return -1;
} else if (a.length < b.length){
return 1;
}
return 0;
}
function compareCat(a,b){
if (a["cat"] > b["cat"]){
return 1;
} else if (a["cat"] < b["cat"]){
return -1;
}
return 0;
}
function compareAccuracy(a,b){
if (a["accuracy"] > b["accuracy"]){
return -1;
} else if (a["accuracy"] < b["accuracy"]){
return 1;
}
return 0;
}