-
Notifications
You must be signed in to change notification settings - Fork 61
/
wade.js
179 lines (144 loc) · 5.43 KB
/
wade.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
/**
* Wade v0.2.0
* Copyright 2017 Kabir Shah
* Released under the MIT License
*/
(function(root, factory) {
/* ======= Global Wade ======= */
(typeof module === "object" && module.exports) ? module.exports = factory() : root.Wade = factory();
}(this, function() {
var stopWords = ['about', 'after', 'all', 'also', 'am', 'an', 'and', 'another', 'any', 'are', 'as', 'at', 'be', 'because', 'been', 'before', 'being', 'between', 'both', 'but', 'by', 'came', 'can', 'come', 'could', 'did', 'do', 'each', 'for', 'from', 'get', 'got', 'has', 'had', 'he', 'have', 'her', 'here', 'him', 'himself', 'his', 'how', 'if', 'in', 'into', 'is', 'it', 'like', 'make', 'many', 'me', 'might', 'more', 'most', 'much', 'must', 'my', 'never', 'now', 'of', 'on', 'only', 'or', 'other', 'our', 'out', 'over', 'said', 'same', 'see', 'should', 'since', 'some', 'still', 'such', 'take', 'than', 'that', 'the', 'their', 'them', 'then', 'there', 'these', 'they', 'this', 'those', 'through', 'to', 'too', 'under', 'up', 'very', 'was', 'way', 'we', 'well', 'were', 'what', 'where', 'which', 'while', 'who', 'with', 'would', 'you', 'your', 'a', 'i'];
var punctuationRE = /\.|\,|\!/g;
var getRoot = function(pattern, index) {
var node = index;
for(var i = 0; i < pattern.length; i++) {
var char = pattern[i];
node = node[char];
if(node === undefined) {
break;
}
}
return node;
}
var updateResults = function(id, results, resultsLocations, scoreIncrement) {
var location = null;
for(var i = 0; i < id.length; i++) {
var documentID = id[i];
location = resultsLocations[documentID];
if(location === undefined) {
resultsLocations[documentID] = results.length;
results.push({
index: documentID,
score: scoreIncrement
})
} else {
results[location].score += scoreIncrement;
}
}
}
var contains = function(pattern, index, results, resultsLocations, scoreIncrement) {
var node = getRoot(pattern, index);
if(node !== undefined && node.id !== undefined) {
updateResults(node.id, results, resultsLocations, scoreIncrement);
}
}
var containsPrefix = function(pattern, index, results, resultsLocations, scoreIncrement) {
var node = getRoot(pattern, index);
if(node !== undefined) {
var stack = [node];
var current = null;
var currentIndex = 0;
while(stack.length !== 0) {
current = stack[currentIndex];
if(current.id !== undefined) {
updateResults(current.id, results, resultsLocations, scoreIncrement);
}
stack.pop();
currentIndex--;
for(var child in current) {
stack.push(current[child]);
currentIndex++;
}
}
}
}
var lowercase = function(str) {
return str.toLowerCase();
}
var removePunctuation = function(str) {
return str.replace(punctuationRE, "");
}
var removeStopWords = function(str) {
var words = str.split(" ");
for(var i = 0; i < words.length; i++) {
if(stopWords.indexOf(words[i]) !== -1) {
words.splice(i, 1);
}
}
return words.join(" ");
}
var Wade = function(data) {
var search = function(item) {
var index = search.index;
var keywords = Wade.process(item).split(" ");
var keywordsLength = keywords.length;
var fullwordsLength = keywordsLength - 1;
var scoreIncrement = 1 / keywordsLength;
var results = [];
var resultsLocations = {};
for(var i = 0; i < fullwordsLength; i++) {
contains(keywords[i], index, results, resultsLocations, scoreIncrement);
}
containsPrefix(keywords[fullwordsLength], index, results, resultsLocations, scoreIncrement);
return results;
}
for(var i = 0; i < data.length; i++) {
data[i] = Wade.process(data[i]);
}
search.index = Wade.index(data);
search.data = data;
return search;
}
Wade.pipeline = [lowercase, removePunctuation, removeStopWords];
Wade.process = function(item) {
var pipeline = Wade.pipeline;
for(var j = 0; j < pipeline.length; j++) {
item = pipeline[j](item);
}
return item;
}
Wade.index = function(data) {
var index = {};
for(var i = 0; i < data.length; i++) {
var str = data[i].split(" ");
for(var j = 0; j < str.length; j++) {
var item = str[j];
var itemLength = item.length - 1;
var node = index;
for(var n = 0; n < itemLength; n++) {
var char = item[n];
var newNode = node[char];
newNode = newNode === undefined ? {} : newNode;
node[char] = newNode;
node = newNode;
}
var lastChar = item[itemLength];
if(node[lastChar] === undefined) {
node[lastChar] = {
id: [i]
}
} else {
node = node[lastChar];
if(node.id === undefined) {
node.id = [i];
} else {
node.id.push(i);
}
}
}
}
return index;
}
Wade.version = "0.2.0";
return Wade;
}));