| 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 |
9x
9x
9x
9x
9x
9x
9x
9x
76x
76x
76x
76x
76x
76x
76x
76x
76x
76x
76x
76x
76x
16x
444x
444x
417x
363x
363x
336x
444x
514x
406x
406x
406x
406x
406x
406x
406x
1988x
1840x
406x
1840x
1840x
1840x
1840x
1840x
1840x
1840x
736x
736x
736x
736x
406x
1434x
1434x
1434x
1434x
1434x
1840x
406x
406x
108x
56x
68x
444x
444x
1635x
1635x
1635x
1635x
444x
2034x
2034x
2034x
2034x
444x
68x
68x
2789x
68x
79x
552x
552x
108x
243x
243x
108x
288x
288x
70x
67x
3x
70x
70x
70x
82x
82x
82x
54x
54x
48x
48x
138x
90x
90x
6x
8x
6x
16x
8x
8x
28x
28x
28x
70x
42x
42x
42x
28x
68x
40x
40x
70x
70x
3x
70x
86x
2x
2x
2x
2x
86x
86x
8x
9x
9x
| "use strict";
const Index = require('./Index');
const extractObjectValues = require('../misc/extractObjectValues');
const decoder = require('unidecode');
const Lexer = require('pos').Lexer;
const Tagger = require('pos')
.Tagger;
const lemmer = require('lemmatizer').lemmatizer;
const stemmer = require('lancaster-stemmer');
const INDEX_TYPE = 'text';
class TextIndex extends Index
{
constructor(config = {}, type = undefined)
{
super(config, type || INDEX_TYPE);
this.filters = ['query', 'queryexact'];
this.decoder = decoder;
this.lex = new Lexer();
this.lex = this.lex.lex.bind(this.lex)
this.tag = new Tagger();
this.tag = this.tag.tag.bind(this.tag);
this.lemm = lemmer;
this.stem = stemmer;
this.totalWordsTally = config.totalWordsTally || 0;
this.maximumDocumentsByTerm = config.maximumDocumentsByTerm || 0;
this.sorts = [this.name];
this.allowPartialMatch = this.allowPartialMatch || false;
}
async state()
{
return Object.assign(await super.state(), {
totalWordsTally: this.totalWordsTally,
maximumDocumentsByTerm: this.maximumDocumentsByTerm,
allowPartialMatch: this.allowPartialMatch
});
}
getDocumentValues(document)
{
let values = [];
extractObjectValues(document, this.fields, (newValue, field, scale) =>
{
if (typeof newValue === 'string')
{
newValue = newValue.replace(/\s+/g, ' ')
.trim();
if (newValue)
{
values.push([newValue, scale]);
}
}
});
return values.length && values || null;
}
analyseValue(valuesList)
{
if (valuesList)
{
let count = 0;
let words = {};
let bagOfWords = {};
let stemmedFull = {};
let maximum = 0;
for (let [original, scale] of valuesList)
{
let tokens = this.tag(this.lex(decoder(original)
.toLowerCase())).filter(t => t[1].match(/[A-Z]/));
tokens.forEach(token => token.push(this.stem(token[0])))
for (let value of tokens)
{
const token = value[2];
words[token] = words[token] || 0;
words[token] += scale;
count += scale;
maximum = Math.max(words[token], maximum)
const lemm = this.lemm(value[0]);
if (lemm !== value[0])
{
const ltoken = this.stem(lemm);
bagOfWords[ltoken] = bagOfWords[ltoken] || 0;
bagOfWords[ltoken] += .001;
count += .001;
}
}
for (let tokenIndex = 1; tokenIndex < tokens.length; tokenIndex++)
{
const t1 = tokens[tokenIndex - 1], t2 = tokens[tokenIndex];
const token = t1[2] + t2[2];
bagOfWords[token] = bagOfWords[token] || 0;
bagOfWords[token] += 1;
count += 1;
}
stemmedFull[tokens.map(token => token[2])
.join(' ')] = 1;
}
stemmedFull = Object.keys(stemmedFull)
.join(' ');
return {
stemmedFull,
words,
bagOfWords,
count,
maximum
};
}
else
{
return {
stemmedFull: '',
words: {},
bagOfWords: {},
count: 0
};
}
}
createIndex()
{
return {};
}
async addToIndex(index, documentIndices, documentsValues)
{
/**
* O(documentIndices)
*/
documentIndices.forEach((documentIndex, valuesOffset) =>
{
const analysed = documentsValues[valuesOffset];
for (const [term, tally] of Object.entries(analysed.words))
{
index[term] = index[term] || {
total: 0
};
index[term][documentIndex] = index[term][documentIndex] || 0;
index[term][documentIndex] += tally
index[term].total += tally;
}
for (const [term, tally] of Object.entries(analysed.bagOfWords))
{
index[term] = index[term] || {
total: 0
};
index[term][documentIndex] = index[term][documentIndex] || 0;
index[term][documentIndex] += tally
index[term].total += tally;
}
this.totalWordsTally += analysed.count;
});
let maximumDocumentsByTerm = 0;
for (const docs of Object.values(index))
{
maximumDocumentsByTerm = Math.max(maximumDocumentsByTerm, Object.keys(docs)
.length - 1);
}
this.maximumDocumentsByTerm = maximumDocumentsByTerm;
}
/**
* O(documentIndices)
*/
async removeFromIndex(index, documentIndices, documentsValues)
{
documentIndices.forEach((documentIndex, valuesOffset) =>
{
let value = documentsValues[valuesOffset];
if (value && value.words)
{
for (const [term, tally] of Object.entries(value.words))
{
index[term][documentIndex] -= tally
index[term].total -= tally;
}
for (const [term, tally] of Object.entries(value.bagOfWords))
{
index[term][documentIndex] -= tally
index[term].total -= tally;
}
}
});
}
filterBasedOnIndex(index, filter, results, score)
{
switch (filter.filter)
{
case 'query':
return this.filterQueryImpl(index, filter, results, score, false);
case 'queryexact':
return this.filterQueryImpl(index, filter, results, score, true);
}
return results;
}
filterQueryImpl(index, filter, results, score, exact)
{
let final = false;
const analysed = this.analyseValue([
[filter.values.join(' '), 1]
]);
Object.entries(analysed.words)
.map((keywordTally, keywordIndex, all) =>
{
const [keyword, tally] = keywordTally;
let resultFragment = index[keyword];
if (resultFragment && resultFragment.total > 0)
{
results.addKeyword(filter.values[keywordIndex], resultFragment.total);
if (!final)
{
final = {};
for (const [result, resultTally] of Object.entries(resultFragment))
{
if (result !== 'total')
{
const value = this.values[result];
final[result] = score(resultTally,
value.count,
value.maximum,
resultFragment.total,
this.totalWordsTally,
this.values.length,
all.length,
this.maximumDocumentsByTerm) * tally;
}
}
}
else
{
for (let result in final)
{
Iif (!(result in resultFragment))
{
final[result] = undefined;
}
}
for (const [result, resultTally] of Object.entries(resultFragment))
{
if (result in final)
{
const value = this.values[result];
final[result] *= score(resultTally,
value.count,
value.maximum,
resultFragment.total,
this.totalWordsTally,
this.values.length,
all.length,
this.maximumDocumentsByTerm) * tally;
}
}
}
}
else
{
results.addKeyword(filter.values[keywordIndex], 0);
Eif (!this.allowPartialMatch)
{
final = {};
}
}
});
Object.entries(analysed.bagOfWords)
.map((keywordTally, keywordIndex, all) =>
{
const [keyword, tally] = keywordTally;
let resultFragment = index[keyword];
if (resultFragment && resultFragment.total > 0)
{
for (const [result, resultTally] of Object.entries(resultFragment))
{
if (result !== 'total')
{
const value = this.values[result];
final[result] += score(resultTally,
value.count,
value.maximum,
resultFragment.total,
this.totalWordsTally,
this.values.length,
all.length,
this.maximumDocumentsByTerm) * tally;
}
}
}
});
Eif (final)
{
if (exact)
{
exact = new RegExp(analysed.values, 'ig')
}
for (let [result, resultTally] of Object.entries(final))
{
if (exact)
{
try
{
const match = this.values[result].stemmedFull.match(exact);
Eif (match && match.length)
{
resultTally *= match.length
}
}
catch (e)
{
console.log(e.stack)
}
}
Eif (resultTally > 0)
{
results.addHit(result, resultTally);
}
}
}
}
getSortValue(index)
{
return this.values[index].stemmedFull
}
}
module.exports = TextIndex;
require('./register')
.add(TextIndex, INDEX_TYPE);
|