-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (26 loc) · 895 Bytes
/
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
const path = require('path');
const esql = require('sql-extra');
var corpus = null;
function load() {
if (corpus) return corpus;
corpus = require('./corpus')
return corpus;
}
function csv() {
return path.join(__dirname, 'index.csv');
}
function sql(tab='frequencydistribution', opt={}) {
return esql.setupTable(tab, {districts: 'TEXT', states: 'INT', selected: 'INT', sampled: 'INT'},
require('./corpus').values(), Object.assign({pk: 'districts', index: true, tsvector: {districts: 'A'}}, opt));
}
function frequencyDistribution(dis) {
if (!corpus) load();
if (dis <= 5) return corpus.get(1);
if (dis <= 10) return corpus.get(6);
if (dis > 70) return corpus.get(71);
return corpus.get(Math.floor((dis-1)/10)*10+1);
}
frequencyDistribution.load = load;
frequencyDistribution.csv = csv;
frequencyDistribution.sql = sql;
module.exports = frequencyDistribution;