-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
63 lines (56 loc) · 1.74 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
57
58
59
60
61
62
63
const Sql = require('sql-extra');
const parse = require('csv-parse');
const lunr = require('lunr');
const path = require('path');
const fs = require('fs');
const corpus = new Map();
var index = null;
var ready = null;
function csv() {
return path.join(__dirname, 'index.csv');
};
async function sql(tab='codes', opt={}) {
var cols = {name: 'TEXT', code: 'TEXT'};
var opt = Object.assign({pk: 'name', index: true, tsvector: {name: 'A', code: 'B'}}, opt);
var stream = fs.createReadStream(csv()).pipe(parse({columns: true, comment: '#'}));
var z = Sql.createTable(tab, cols, opt);
z = await Sql.insertInto.stream(tab, stream, opt, z);
z = Sql.setupTable.index(tab, cols, opt, z);
return z;
};
function loadCorpus() {
return new Promise((fres) => {
var stream = fs.createReadStream(csv()).pipe(parse({columns: true, comment: '#'}));
stream.on('data', (r) => corpus.set(r.name, r));
stream.on('end', fres);
});
};
function setupIndex() {
index = lunr(function() {
this.ref('key');
this.field('name');
this.field('code');
// this.pipeline.remove(lunr.stopWordFilter);
for(var r of corpus.values())
this.add({key: r.name, name: r.name.replace(/\W+/g, ' '), code: r.code});
});
};
function load() {
ready = ready||loadCorpus();
return ready.then(setupIndex);
};
function codes(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;
};
codes.csv = csv;
codes.sql = sql;
codes.load = load;
codes.corpus = corpus;
module.exports = codes;