-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
236 lines (221 loc) · 6.49 KB
/
build.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { TextLineStream } from "jsr:@std/streams/text-line-stream";
function getLineStream(file) {
return file.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
}
function getWordLevel(word, mGSL, lemmatizationDict) {
if (word in lemmatizationDict) {
const newWord = lemmatizationDict[word];
if (newWord in mGSL) {
return mGSL[newWord];
} else if (newWord.length == 1) {
return 0;
} else {
console.log("x mGSL: " + word + " -> " + newWord);
}
} else if (word in mGSL) {
return mGSL[word];
}
switch (word) {
case "christmas":
case "oh":
case "jim":
case "sam":
case "japanese":
console.log("fixed: " + word);
return 0;
default:
// console.log('error: ' + word);
}
}
function getSentenceLevel(words, mGSL, lemmatizationDict) {
let levels = words.map((word) => {
if (word.match(/[0-9A-Z]/)) { // 可能な限り大文字/小文字を考慮してレベルを推定する
let level = getWordLevel(word, mGSL, lemmatizationDict);
if (!level) {
level = getWordLevel(word.toLowerCase(), mGSL, lemmatizationDict);
}
return level;
} else {
return getWordLevel(word, mGSL, lemmatizationDict);
}
});
// console.log(levels);
levels = levels.filter((x) => x != undefined);
return Math.max(...levels);
}
function getWords(sentence) {
sentence = sentence.trim()
.replace(/n't/g, " not") // don't, isn't --> do not, is not
.replace(/'/g, " '") // he's, I'm --> he 's, I'm
.replace(/[,.'"!?〜]/g, "")
.replace(/-/g, " ") // left-hand --> left hand
.replace(/[A-Z][a-z]*\. /g, "") // Mr., Ms., Mt., etc.
.trim();
return sentence.split(/\s+/).filter((word) => word && word.length > 1);
}
function includeBadWordsJa(ja) { // TODO: slow & noisy
const errorWords = [
"リフレッシュ", // リフレ
"パチンコ", // チンコ
"クスコ", // クスコ
];
return inappropriateWordsJa.some((badWord) => {
if (ja.includes(badWord)) {
if (!errorWords.some((errorWord) => ja.includes(errorWord))) {
console.log("bad-ja: " + badWord);
return true;
}
}
});
}
function includeBadWordsEn(words) {
return words.some((word) => {
if (word in badWords) {
console.log("bad-en: " + word);
return true;
} else if (word in profanityWords) {
console.log("bad-en: " + word);
return true;
}
});
}
async function readLineWithIndex(filePath, callback) {
let i = 0;
const file = await Deno.open(filePath);
for await (const word of getLineStream(file)) {
callback(word, i);
i += 1;
}
}
const badWords = {};
let file = await Deno.open(
"mGSL/vendor/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/en",
);
for await (const word of getLineStream(file)) {
badWords[word] = true;
}
const profanityWords = {};
file = await Deno.open("mGSL/vendor/Google-profanity-words/data/en.txt");
for await (const word of getLineStream(file)) {
profanityWords[word] = true;
}
const inappropriateWordsJa = [];
file = await Deno.open("inappropriate-words-ja/Sexual.txt");
for await (const word of getLineStream(file)) {
if (!["イク", "催眠"].includes(word)) {
inappropriateWordsJa.push(word);
}
}
inappropriateWordsJa.push("性病");
const lemmatizationDict = { an: "a" };
file = await Deno.open("mGSL/vendor/agid-2016.01.19/infl.txt");
for await (const line of getLineStream(file)) {
const [toStr, fromStr] = line.split(": ");
if (!toStr.endsWith("?")) {
const [to, _toPos] = toStr.split(" ");
const froms = [];
fromStr.split(" | ").forEach((forms) => {
forms.split(", ").forEach((entry) => {
if (!entry.match(/[~<!?]/)) {
const word = entry.split(" ")[0];
froms.push(word);
}
});
});
froms.forEach((from) => {
lemmatizationDict[from] = to;
});
}
}
delete lemmatizationDict["danger"];
const range = [50, 100, 150, 200, 400, 600, 800, 1200, 1600, 2200, 3000, 5000];
const mGSL = {};
await readLineWithIndex("mGSL/dist/mGSL.lst", (line, i) => {
const [en, _ja] = line.split("\t", 2);
let level = range.findIndex((r) => r > i);
if (level == -1) level = range.length;
mGSL[en] = level;
});
file = await Deno.open("mGSL/filter-ngsl.lst");
for await (const word of getLineStream(file)) {
mGSL[word] = 0;
}
const problemList = [...Array(range.length + 1)].map(() => []);
file = await Deno.open("jec.tsv");
for await (const line of getLineStream(file)) {
const [_id, ja, en, _zh] = line.split("\t");
if (!includeBadWordsJa(ja)) {
const words = getWords(en);
if (!includeBadWordsEn(words)) {
const level = getSentenceLevel(words, mGSL, lemmatizationDict);
const problem = en + "\t" + ja;
if (isFinite(level)) {
problemList[level].push(problem);
// console.log(line);
// console.log(words);
// console.log(level);
} else {
// console.log(line);
// console.log(words);
// console.log(level);
}
} else {
console.log("bad-en: " + en);
console.log(line);
}
} else {
console.log("bad-ja: " + ja);
console.log(line);
}
}
file = await Deno.open("tanaka-corpus-plus/tanaka.txt");
for await (const line of getLineStream(file)) {
if (!line.startsWith("A:")) {
continue;
}
const [jaTmp, enTmp] = line.split("\t");
const ja = jaTmp.slice(3);
const en = enTmp.split("#")[0];
if (!includeBadWordsJa(ja)) {
const words = getWords(en);
if (!includeBadWordsEn(words)) {
const level = getSentenceLevel(words, mGSL, lemmatizationDict);
const problem = en + "\t" + ja;
if (isFinite(level)) {
problemList[level].push(problem);
// console.log(line);
// console.log(words);
// console.log(level);
} else {
// console.log(line);
// console.log(words);
// console.log(level);
}
} else {
console.log("bad-en: " + en);
console.log(line);
}
} else {
console.log("bad-ja: " + ja);
console.log(line);
}
}
problemList.forEach((problems, i) => {
// Deno.writeFileSync('dist/' + (i+1) + '.tsv', problems.join('\n'));
problems.sort((a, b) => {
if (a.length < b.length) return -1;
if (a.length > b.length) return 1;
return 0;
});
const pos = Math.ceil(problems.length / 2);
Deno.writeTextFileSync(
"dist/easy/" + (i + 3) + ".tsv",
problems.slice(0, pos).join("\n"),
);
Deno.writeTextFileSync(
"dist/hard/" + (i + 3) + ".tsv",
problems.slice(pos).join("\n"),
);
});