This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
71 lines (61 loc) · 1.88 KB
/
index.ts
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
import fs from "fs";
import { read, utils, WorkBook } from "xlsx";
const WORD_COLUNM = "표제어";
const WORD_PARTS = "품사";
function checkKorean(str: string): boolean {
for (let i = 0; i < str.length; i++) {
if (str[i].charCodeAt(0) > 55215) {
return false;
}
}
return true;
}
function getFileNameByArgs(): string {
const myArgs = process.argv.slice(2);
return myArgs[0];
}
function readSheet(name: string): Promise<WorkBook> {
const buffers: Buffer[] = [];
const readStream = fs.createReadStream(name);
return new Promise((resolve, reject) => {
readStream.on("data", (c: Buffer) => {
buffers.push(c);
});
readStream.on("end", () => {
const buffer = Buffer.concat(buffers);
const wb = read(buffer, { type: "buffer" });
resolve(wb);
});
});
}
async function main() {
const wb = await readSheet(getFileNameByArgs());
const firstSheet = wb.SheetNames[0];
const sheet = wb.Sheets[firstSheet];
const sheetJson = utils.sheet_to_json(sheet, {
defval: "",
blankrows: true,
});
const filteredWord = sheetJson
.map((i) => {
const item = i as Record<string, string>;
if (item[WORD_COLUNM].length === 1) return undefined;
if (item[WORD_COLUNM].length >= 4) return undefined;
if (item[WORD_PARTS] !== "명사") return undefined;
if (item[WORD_COLUNM].includes("-")) return undefined;
if (!checkKorean(item[WORD_COLUNM])) return undefined;
return item;
})
.filter((i) => i !== undefined)
.map((i) => i && i[WORD_COLUNM]);
const uniqueWord = new Set(filteredWord);
const words = [...uniqueWord];
if (!fs.existsSync("./dist")) {
fs.mkdirSync("./dist");
}
const writeStream = fs.createWriteStream(`./dist/${Date.now()}.txt`);
const content = JSON.stringify(words).replace("]", "").replace("[", "");
writeStream.write(content);
writeStream.end();
}
main();