-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (47 loc) · 1.38 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
const path = require('path');
const lunr = require('lunr');
const esql = require('sql-extra');
var corpus = null;
var index = null;
function fixRow(r) {
if (r.sno==='1') r.monosaccharide = 1;
r.hydrolysis = r.hydrolysis || NaN;
r.monosaccharide = r.monosaccharide || NaN;
return r;
}
function createIndex() {
return lunr(function() {
this.ref('sno');
this.field('sno');
this.field('carbohydrate');
for (var r of corpus.values())
this.add(fixRow(r));
});
}
function load() {
if (corpus) return corpus;
corpus = require('./corpus');
index = createIndex();
return corpus;
}
function csv() {
return path.join(__dirname, 'index.csv');
}
function sql(tab='carbohydrates', opt={}) {
return esql.setupTable(tab, {sno: 'TEXT', carbohydrate: 'TEXT', hydrolysis: 'REAL', monosaccharide: 'REAL'},
load().values(), Object.assign({pk: 'sno', index: true, tsvector: {sno: 'A', carbohydrate: 'B'}}, opt));
}
function carbohydrates(txt) {
if (!corpus) load();
var a = [], txt = txt.replace(/\W/g, ' ');
var ms = index.search(txt), max = 0;
for (var m of ms)
max = Math.max(max, Object.keys(m.matchData.metadata).length);
for (var m of ms)
if (Object.keys(m.matchData.metadata).length===max) a.push(corpus.get(m.ref));
return a;
}
carbohydrates.load = load;
carbohydrates.csv = csv;
carbohydrates.sql = sql;
module.exports = carbohydrates;