-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (48 loc) · 1.43 KB
/
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
const Sql = require('sql-extra');
const lunr = require('lunr');
const path = require('path');
var corpus = new Map();
var index = null;
var ready = false;
function loadCorpus() {
for(var [k, v] of require('./corpus'))
corpus.set(k, v);
};
function setupIndex() {
index = lunr(function() {
this.ref('code');
this.field('code', {boost: 3});
this.field('name', {boost: 2});
this.field('tags');
this.pipeline.remove(lunr.stopWordFilter);
for(var {code, name, tags} of corpus.values())
this.add({code, name: name.replace(/\W/g, ' '), tags});
});
};
function csv() {
return path.join(__dirname, 'index.csv');
};
function sql(tab='columns', opt={}) {
return Sql.setupTable(tab, {code: 'TEXT', name: 'TEXT', tags: 'TEXT'}, require('./corpus').values(),
Object.assign({pk: 'code', index: true, tsvector: {code: 'A', name: 'B', tags: 'C'}}, opt));
};
function load() {
if(ready) return true;
loadCorpus(); setupIndex();
return ready = true;
};
function columns(txt) {
if(index==null) return [];
var z = [], txt = txt.replace(/\W/g, ' ');
var mats = index.search(txt), max = 0;
for(var mat of mats)
max = Math.max(max, Object.keys(mat.matchData.metadata).length);
for(var mat of mats)
if(Object.keys(mat.matchData.metadata).length===max) z.push(corpus.get(mat.ref));
return z;
};
columns.csv = csv;
columns.sql = sql;
columns.load = load;
columns.corpus = corpus;
module.exports = columns;