Upload preprocess.js
Browse files- preprocess.js +37 -0
preprocess.js
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const fs = require('fs');
|
2 |
+
const readline = require('readline');
|
3 |
+
|
4 |
+
const answer = readline.createInterface({
|
5 |
+
input: fs.createReadStream('./train_answer.csv')
|
6 |
+
});
|
7 |
+
|
8 |
+
let answer_map = {};
|
9 |
+
|
10 |
+
answer.on('line', line => {
|
11 |
+
const spline = line.split(',');
|
12 |
+
const key = spline[0];
|
13 |
+
const choice = spline[1];
|
14 |
+
answer_map[key] = ~~choice;
|
15 |
+
});
|
16 |
+
|
17 |
+
|
18 |
+
const train = readline.createInterface({
|
19 |
+
input: fs.createReadStream('./train.json')
|
20 |
+
});
|
21 |
+
|
22 |
+
const padding = (n,m) => (Array(m).join(0) + n).slice(-m);
|
23 |
+
|
24 |
+
train.on('line', line => {
|
25 |
+
const obj = JSON.parse(line);
|
26 |
+
let str = obj.content;
|
27 |
+
|
28 |
+
for (let i of str) {
|
29 |
+
while(i.indexOf(`#idiom`) != -1) {
|
30 |
+
const id = ~~i.match(/#idiom\d+#/)[0].match(/\d+/)[0];
|
31 |
+
i = i.replace(`#idiom${padding(id, 6)}#`, obj.candidates[answer_map[`#idiom${padding(id, 6)}#`]]);
|
32 |
+
}
|
33 |
+
console.log(i);
|
34 |
+
|
35 |
+
}
|
36 |
+
|
37 |
+
});
|