-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (67 loc) · 1.99 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const Sql = require('sql-extra');
const lunr = require('lunr');
const path = require('path');
var corpus = new Map();
var ranges = 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('group');
this.field('group');
this.pipeline.remove(lunr.stopWordFilter);
for(var r of corpus.values())
this.add(r);
});
};
function setupRanges() {
for(var {group} of corpus.values()) {
if(!group.includes(' to ')) continue;
group.replace(/(\d+)\s+to\s+(\d+)/, (m, p1, p2) => {
ranges.set([parseInt(p1, 10), parseInt(p2, 10)], group);
});
}
}
function csv() {
return path.join(__dirname, 'index.csv');
};
function sql(tab='caloriecoefficient', opt={}) {
return Sql.setupTable(tab, {group: 'TEXT', cu: 'REAL'}, require('./corpus').values(),
Object.assign({pk: 'group', index: true, tsvector: {group: 'A'}}, opt));
};
function load() {
if(ready) return true;
loadCorpus(); setupIndex(); setupRanges();
return ready = true;
};
function findEntry(entry) {
if(!/\d+/.test(entry)) return null;
var n = parseInt(entry.substring(entry.search(/\d+/)), 10);
for(var [[bgn, end], group] of ranges)
if(bgn<=n && n<=end) return group;
return null;
}
function calorieCoefficient(txt) {
if(index==null) return [];
var z = [], txt = txt.replace(/\W|years?|old/gi, ' ');
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));
var code = findEntry(txt);
if(!code) return z;
var mat = corpus.get(code);
if(z.includes(mat)) z.splice(z.indexOf(mat), 1);
z.unshift(mat);
return z;
};
calorieCoefficient.csv = csv;
calorieCoefficient.sql = sql;
calorieCoefficient.load = load;
calorieCoefficient.corpus = corpus;
module.exports = calorieCoefficient;