-
Notifications
You must be signed in to change notification settings - Fork 4
/
node.js
48 lines (45 loc) · 1.75 KB
/
node.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
const http = require('http');
const axios = require('axios');
const fs = require('fs');
const map = {};
const threads = 50;
function parseFile(filename) {
return new Promise((resolve) => {
fs.readFile(filename, async (err, data) => {
const lines = data.toString().split('\n');
for (let i = 0; i < lines.length; i += threads) {
const ary = [];
for (let j = 0; j < threads; ++j) {
const line = lines[i + j];
if (!line) {
continue;
}
const word = line.split(' ')[0].match( /[a-z]*/)[0];
if (word && !map[word]) {
ary.push(
axios.get(`http://apis.dict.cn/ajax/suggestion.php?callback=sugg1&q=${word}&dict=juhai&s=juhai<=`)
.then((res) => {
const data = JSON.parse(res.data.match(/{.*}/)[0]).s[0];
if (data) {
map[word] = `${word} | ${data.g} | ${data.e}`
console.log(word);
} else {
console.log(word, '没找到例句');
}
})
);
}
}
await Promise.all(ary);
}
resolve();
})
})
}
async function main() {
await parseFile("./CET4_edited.txt")
await parseFile("./CET6_edited.txt");
await parseFile("./TOEFL.txt");
fs.writeFileSync("./paragraph.txt", Object.values(map).join('\n'));
}
main();