-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
inverted-index.js
249 lines (198 loc) · 5.34 KB
/
inverted-index.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
/**
* Mnemonist Inverted Index
* =========================
*
* JavaScript implementation of an inverted index.
*/
var Iterator = require('obliterator/iterator'),
forEach = require('obliterator/foreach'),
helpers = require('./utils/merge.js');
function identity(x) {
return x;
}
/**
* InvertedIndex.
*
* @constructor
* @param {function} tokenizer - Tokenizer function.
*/
function InvertedIndex(descriptor) {
this.clear();
if (Array.isArray(descriptor)) {
this.documentTokenizer = descriptor[0];
this.queryTokenizer = descriptor[1];
}
else {
this.documentTokenizer = descriptor;
this.queryTokenizer = descriptor;
}
if (!this.documentTokenizer)
this.documentTokenizer = identity;
if (!this.queryTokenizer)
this.queryTokenizer = identity;
if (typeof this.documentTokenizer !== 'function')
throw new Error('mnemonist/InvertedIndex.constructor: document tokenizer is not a function.');
if (typeof this.queryTokenizer !== 'function')
throw new Error('mnemonist/InvertedIndex.constructor: query tokenizer is not a function.');
}
/**
* Method used to clear the InvertedIndex.
*
* @return {undefined}
*/
InvertedIndex.prototype.clear = function() {
// Properties
this.items = [];
this.mapping = new Map();
this.size = 0;
this.dimension = 0;
};
/**
* Method used to add a document to the index.
*
* @param {any} doc - Item to add.
* @return {InvertedIndex}
*/
InvertedIndex.prototype.add = function(doc) {
// Increasing size
this.size++;
// Storing document
var key = this.items.length;
this.items.push(doc);
// Tokenizing the document
var tokens = this.documentTokenizer(doc);
if (!Array.isArray(tokens))
throw new Error('mnemonist/InvertedIndex.add: tokenizer function should return an array of tokens.');
// Indexing
var done = new Set(),
token,
container;
for (var i = 0, l = tokens.length; i < l; i++) {
token = tokens[i];
if (done.has(token))
continue;
done.add(token);
container = this.mapping.get(token);
if (!container) {
container = [];
this.mapping.set(token, container);
}
container.push(key);
}
this.dimension = this.mapping.size;
return this;
};
/**
* Method used to query the index in a AND fashion.
*
* @param {any} query - Query
* @return {Set} - Intersection of documents matching the query.
*/
InvertedIndex.prototype.get = function(query) {
// Early termination
if (!this.size)
return [];
// First we need to tokenize the query
var tokens = this.queryTokenizer(query);
if (!Array.isArray(tokens))
throw new Error('mnemonist/InvertedIndex.query: tokenizer function should return an array of tokens.');
if (!tokens.length)
return [];
var results = this.mapping.get(tokens[0]),
c,
i,
l;
if (typeof results === 'undefined' || results.length === 0)
return [];
if (tokens.length > 1) {
for (i = 1, l = tokens.length; i < l; i++) {
c = this.mapping.get(tokens[i]);
if (typeof c === 'undefined' || c.length === 0)
return [];
results = helpers.intersectionUniqueArrays(results, c);
}
}
var docs = new Array(results.length);
for (i = 0, l = docs.length; i < l; i++)
docs[i] = this.items[results[i]];
return docs;
};
/**
* Method used to iterate over each of the documents.
*
* @param {function} callback - Function to call for each item.
* @param {object} scope - Optional scope.
* @return {undefined}
*/
InvertedIndex.prototype.forEach = function(callback, scope) {
scope = arguments.length > 1 ? scope : this;
for (var i = 0, l = this.documents.length; i < l; i++)
callback.call(scope, this.documents[i], i, this);
};
/**
* Method returning an iterator over the index's documents.
*
* @return {Iterator}
*/
InvertedIndex.prototype.documents = function() {
var documents = this.items,
l = documents.length,
i = 0;
return new Iterator(function() {
if (i >= l)
return {
done: true
};
var value = documents[i++];
return {
value: value,
done: false
};
});
};
/**
* Method returning an iterator over the index's tokens.
*
* @return {Iterator}
*/
InvertedIndex.prototype.tokens = function() {
return this.mapping.keys();
};
/**
* Attaching the #.values method to Symbol.iterator if possible.
*/
if (typeof Symbol !== 'undefined')
InvertedIndex.prototype[Symbol.iterator] = InvertedIndex.prototype.documents;
/**
* Convenience known methods.
*/
InvertedIndex.prototype.inspect = function() {
var array = this.items.slice();
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: InvertedIndex,
enumerable: false
});
return array;
};
if (typeof Symbol !== 'undefined')
InvertedIndex.prototype[Symbol.for('nodejs.util.inspect.custom')] = InvertedIndex.prototype.inspect;
/**
* Static @.from function taking an arbitrary iterable & converting it into
* a InvertedIndex.
*
* @param {Iterable} iterable - Target iterable.
* @param {function} tokenizer - Tokenizer function.
* @return {InvertedIndex}
*/
InvertedIndex.from = function(iterable, descriptor) {
var index = new InvertedIndex(descriptor);
forEach(iterable, function(doc) {
index.add(doc);
});
return index;
};
/**
* Exporting.
*/
module.exports = InvertedIndex;