-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
187 lines (157 loc) · 5.13 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const fs = require('fs');
const path = require('path');
const lunr = require('lunr');
const csvx = require('csv-parse');
const esql = require('sql-extra');
const TEXTCOLS = new Set(['code', 'name', 'scie', 'lang', 'grup', 'tags']);
var corpus = new Map();
var index = null;
var ready = null;
function csv() {
return path.join(__dirname, 'index.csv');
}
function tsvector(tab, cols) {
var {code, name, scie, lang, grup, tags} = cols;
return `setweight(to_tsvector('english', "code"), '${code}')||`+
`setweight(to_tsvector('english', left("name", strpos("name", ','))), '${code}')||`+
`setweight(to_tsvector('english', "name"), '${name}')||`+
`setweight(to_tsvector('english', "scie"), '${scie}')||`+
`setweight(to_tsvector('english', ${tab}_lang_tags("lang")), '${lang}')||`+
`setweight(to_tsvector('english', "grup"), '${grup}')||`+
`setweight(to_tsvector('english', "tags"), '${tags}')`;
}
function createFunctionLangTags(tab) {
return `CREATE OR REPLACE FUNCTION "${tab}_lang_tags" (TEXT) RETURNS TEXT AS $$`+
` SELECT lower(regexp_replace(regexp_replace(regexp_replace($1, `+
` '\\[.*?\\]', '', 'g'), '\\w+\\.\\s([\\w\\'',\\/\\(\\)\\- ]+)[;\\.]?', '\\1', 'g'),`+
` '[,\\/\\(\\)\\- ]+', ' ', 'g')) $$`+
` LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT;\n`;
}
function createTable(tab, cols, opt={}, a='') {
var pre = ['code', 'name', 'scie', 'lang', 'grup', 'regn', 'tags'];
a += `CREATE TABLE IF NOT EXISTS "${tab}" (`;
for (var c of pre) {
var typ = c==='regn'? 'INT':'TEXT';
a += ` "${c}" ${typ} NOT NULL,`;
}
for (var c in cols) {
if (pre.includes(c)) continue;
a += ` "${c}" REAL NOT NULL,`;
}
if (opt.pk) a += ` PRIMARY KEY ("code"), `;
a = a.endsWith(', ')? a.substring(0, a.length-2) : a;
a += `);\n`;
return a;
}
function insertIntoBegin(tab, cols, a='') {
a += `INSERT INTO "${tab}" (`;
for (var c in cols)
a += `"${c}", `;
a = a.endsWith(', ')? a.substring(0, a.length-2) : a;
a += ') VALUES\n(';
return a;
}
function insertIntoMid(val, a='') {
for (var k in val)
a += `'${val[k]}', `;
a = a.endsWith(', ')? a.substring(0, a.length-2) : a;
a += `),\n(`;
return a;
}
function insertIntoEnd(a='') {
a = a.endsWith(',\n(')? a.substring(0, a.length-3) : a;
a += ';\n';
return a;
}
function sql(tab='compositions', opt={}) {
var i = -1, cols = null, a = '';
var opt = Object.assign({pk: 'code', index: true}, opt);
var tsv = tsvector(tab, {code: 'A', name: 'B', scie: 'B', lang: 'B', grup: 'C', tags: 'C'});
var stream = fs.createReadStream(csv()).pipe(csvx.parse({columns: true, comment: '#'}));
return new Promise((resolve, reject) => {
stream.on('error', reject);
stream.on('data', (r) => {
var x = fixColumns(r);
if (++i===0) { cols = x; a = createTable(tab, cols, opt, a); a = insertIntoBegin(tab, cols, a); }
a = insertIntoMid(x, a);
});
stream.on('end', () => {
a = insertIntoEnd(a);
a += createFunctionLangTags(tab);
a += esql.createView(`${tab}_tsvector`, `SELECT *, ${tsv} AS "tsvector" FROM "${tab}"`);
a += esql.createIndex(`${tab}_tsvector_idx`, tab, `(${tsv})`, {method: 'GIN'});
a = esql.setupTable.index(tab, cols, opt, a);
resolve(a);
});
});
}
// Fix column names of a row in the CSV file.
function fixColumns(row) {
var a = {};
for (var k in row) {
// Name of column is after the last semicolon.
var l = k.substring(k.lastIndexOf(';')+1).trim();
a[l] = row[k];
}
return a;
}
// Parse a row from the CSV file.
function parseRow(row) {
var a = {};
for (var k in row) {
var l = k.substring(k.lastIndexOf(';')+1).trim();
a[l] = TEXTCOLS.has(l)? row[k] : parseFloat(row[k]);
}
return a;
}
function loadCorpus() {
return new Promise((fres) => {
var s = fs.createReadStream(csv()).pipe(csvx.parse({columns: true, comment: '#'}));
s.on('data', (r) => { var x = parseRow(r); corpus.set(x.code, x); });
s.on('end', fres);
});
}
function createIndex() {
return lunr(function() {
this.ref('code');
this.field('code');
this.field('name');
this.field('scie');
this.field('lang');
this.field('grup');
this.field('tags');
for (var r of corpus.values()) {
var {code, name, scie, lang, grup, tags} = r;
name = name.replace(/^(\w+),/g, '$1 $1 $1 $1,');
lang = lang.replace(/\[.*?\]/g, '');
lang = lang.replace(/\w+\.\s([\w\',\/\(\)\- ]+)[;\.]?/g, '$1');
lang = lang.replace(/[,\/\(\)\- ]+/g, ' ');
this.add({code, name, scie, lang, grup, tags});
}
});
}
async function load() {
if (ready) await ready;
if (index) return corpus;
ready = loadCorpus();
await ready;
index = createIndex();
return corpus;
}
function matchRate(m) {
return Object.keys(m.matchData.metadata).length;
}
function compositions(txt) {
if (!index) { load(); return []; }
var a = [], txt = txt.replace(/\W/g, ' ');
var ms = index.search(txt), max = 0;
for (var m of ms)
max = Math.max(max, matchRate(m));
for (var m of ms)
if (matchRate(m)===max) a.push(corpus.get(m.ref));
return a;
}
compositions.load = load;
compositions.csv = csv;
compositions.sql = sql;
module.exports = compositions;