-
Notifications
You must be signed in to change notification settings - Fork 0
/
skripto.js
executable file
·311 lines (294 loc) · 9.18 KB
/
skripto.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
require("dotenv").config();
const fs = require("fs");
const path = require("path");
const lojban = require("lojban");
const { GoogleSpreadsheet } = require("google-spreadsheet");
if (!process.env.GOOGLE_READONLY_API_KEY || !process.env.GOOGLE_CORPUS_DOC_ID) {
console.log("muplis update cancelled, no GOOGLE_READONLY_API_KEY or GOOGLE_CORPUS_DOC_ID specified");
process.exit();
}
const doc = new GoogleSpreadsheet(process.env.GOOGLE_CORPUS_DOC_ID);
doc.useApiKey(process.env.GOOGLE_READONLY_API_KEY);
(async () => {
await doc.loadInfo();
const sheet = doc.sheetsById[551499663];
let output = {
en2jb: [],
jb2en: [],
};
const limit = 10000;
for (let offset = 0; offset < 92000; offset += limit) {
try {
const rows = await sheet.getRows({
offset,
limit,
});
const { jb2en, en2jb } = processRows(rows);
output.jb2en = output.jb2en.concat(jb2en);
output.en2jb = output.en2jb.concat(en2jb);
} catch (error) {
console.log(error.response.data);
continue;
}
}
console.log(`writing to output...`);
createDexieCacheFile(output.jb2en);
output.jb2en = output.jb2en
.map((i) => `${i.source}\t${i.target}\t${i.tags.join(" ")}`)
.join("\n")
.replace(/[\r\n]{2,}/g, "\n");
output.en2jb = output.en2jb
.map((i) => `${i.source}\t${i.target}\t${i.tags}`)
.join("\n")
.replace(/[\r\n]{2,}/g, "\n");
fs.writeFileSync(path.join(__dirname, "./dist/jb2en.tsv"), output.jb2en);
fs.writeFileSync(path.join(__dirname, "./dist/en2jb.tsv"), output.en2jb);
})();
function createDexieCacheFile(arr) {
const a = arr.map((i) => {
let cache = `${i.source};${i.source.replace(/h/g, "'")};${i.source_opt};${(
i.source_opt || ""
).replace(/h/g, "'")};${i.target
.replace(/[\.,!?\/\\]/g, "")
.replace(/[h‘]/g, "'")};`;
const cache1 = cache
.toLowerCase()
.replace(/ /g, ";")
.split(";")
.map((i) => i.trim())
.filter((i) => i !== "");
let cache2 = cache
.toLowerCase()
.replace(
/[ \u2000-\u206F\u2E00-\u2E7F\\!"#$%&()*+,\-.\/:<=>?@\[\]^`{|}~:?。,《》「」『』-()]/g,
";"
)
.split(";")
.map((i) => i.trim())
.filter((i) => i !== "");
cache = cache1.concat(cache2);
cache = [...new Set(cache)];
const outRow = { w: i.source, bangu: "muplis", d: i.target, cache };
if (i.tags.length > 0) outRow.s = i.tags;
return outRow;
});
splitOutput(a);
}
function canonicalizeValsi(valsi) {
if (/^[aeiouy]/.test(valsi)) valsi = "." + valsi;
if (/y$/.test(valsi) && valsi.indexOf(".") !== 0) valsi = valsi + ".";
if (/[^aeiouy]$/.test(valsi)) valsi = "." + valsi + ".";
return valsi.replace(/\.\./g, ".");
}
function processRows(rows) {
let n = [];
for (const r of rows) {
if (r._rowNumber % 100 === 0) console.log(`processing row ${r._rowNumber}`);
let j;
const tags =
(r["Ilmen's tags"] || "") +
(r["gleki's tags"] || "") +
(r["uakci's optional new tags"] || "");
j = {
source: r["Tatoeba: English"] || "",
target:
r["gleki's alternative proposal"] ||
r["Ilmen's alternative proposal"] ||
r["uakci's revision"] ||
r["jelca proposal"] ||
r["Tatoeba: Lojban"] ||
"",
tags:
r["gleki's tags"] ||
r["Ilmen's tags"] ||
r["uakci's optional new tags"] ||
"",
};
if (
(tags.indexOf("B") >= 0 && j.target === r["Tatoeba: Lojban"]) ||
(r["Tatoeba: Lojban"] || "") === "" ||
(r["Tatoeba: English"] || "") === ""
)
continue;
j.target = lojban.preprocessing(j.target.toLowerCase());
try {
j.target_opt = lojban
.romoi_lahi_cmaxes(
lojban.zeizei(j.target.replace(/ĭ/g, "i").replace(/ŭ/g, "u")),
"T"
)
.kampu.filter((i) => i[0] !== "drata")
.map((i) => i[1])
.join(" ")
.replace(/-/g, "");
} catch (error) {
continue;
}
j.source = j.source
.replace(/ {2,}/g, " ")
.replace(/[\r\n]/g, "")
.replace(/’/g, "'")
.trim();
if (
j.source !== "" &&
j.target !== "" &&
j.target.search(/\bzoi\b/) === -1
) {
try {
const parsed = lojban.romoi_lahi_cmaxes(j.target);
if (parsed.tcini == "fliba") continue;
j.target = parsed.kampu
.filter((i) => i[0] !== "drata")
.map((i) => canonicalizeValsi(i[1]))
.join(" ")
.replace(/-/g, "");
if (!j.target.split(" ").includes("zei"))
j.target_opt = j.target_opt
.split(" ")
.filter((i) => i !== "zei")
.join(" ");
} catch (error) {
console.log(error);
}
n = duplicator({ n, j });
}
}
let en2jb = n.map((r) => {
const outRow = { source: r.source, target: r.target, tags: r.tags };
return outRow;
});
en2jb = [...new Set(en2jb.map((el) => JSON.stringify(el)))].map((el) =>
JSON.parse(el)
);
let jb2en = n.map((r) => {
// Or this is what la Ilmen uses: G (good), G− (a little good, not so good), G+ (very good), A (acceptable), B[−+] ([a little / very] bad), N (neologism, containing an undocumented Lojban word), E (experimental grammar), P (non-conventional punctuation), C - CLL style, X - xorlo. W - play on words and thus poorly translatable to/from Lojban
r.tags = r.tags
.replace(/ /g, "")
.split(/[A-Z][\+\-]?/)
.filter((i) => i !== "")
.map((i) => {
i = i
.replace(/^G\-$/, "👍")
.replace(/^G$/, "👍👍")
.replace(/^G\+$/, "👍👍👍")
.replace(/^A$/, "😐")
.replace(/^B-$/, "👎")
.replace(/^B$/, "👎👎")
.replace(/^B\+$/, "👎👎👎")
.replace(/^N$/, "👒")
.replace(/^E$/, "🧪")
.replace(/^P$/, "🎗")
.replace(/^C$/, "📕")
.replace(/^X$/, "xorlo")
.replace(/^W$/, "trokadilo");
return i;
});
const outRow = {
source: r.target,
source_opt: r.target_opt,
target: r.source,
tags: r.tags,
};
return outRow;
});
jb2en = [...new Set(jb2en.map((el) => JSON.stringify(el)))].map((el) =>
JSON.parse(el)
);
return { jb2en, en2jb };
}
function duplicator({ n, j }) {
j.target = j.target
.replace(/\bmeris\b/g, "maris")
.replace(/\btokion\b/g, "tokios")
.replace(/\ble\b/g, "lo")
.replace(/\blei\b/g, "loi");
n.push(j);
if (j.source.search(/\bTom\b/) >= 0) {
j2 = JSON.parse(JSON.stringify(j));
j2.source = j2.source.replace(/\bTom\b/g, "Alice");
j2.target = j2.target.replace(/\btom\b/g, "alis");
j2.target = j2.target.replace(/\btam\b/g, "alis");
n = n.concat(j2);
j2 = JSON.parse(JSON.stringify(j));
j2.source = j2.source.replace(/\bTom\b/g, "Mary");
j2.target = j2.target.replace(/\btom\b/g, "maris");
j2.target = j2.target.replace(/\btam\b/g, "maris");
n = n.concat(j2);
}
if (j.source.search(/\bapples?\b/) >= 0) {
j2 = JSON.parse(JSON.stringify(j));
j2.source = j2.source.replace(/\bapple\b/g, "pear");
j2.source = j2.source.replace(/\bapples\b/g, "pears");
j2.target = j2.target.replace(/\bplise\b/g, "perli");
n = n.concat(j2);
}
if (j.source.search(/\bOsaka\b/) >= 0) {
j2 = JSON.parse(JSON.stringify(j));
j2.source = j2.source.replace(/\bOsaka\b/g, "New York");
j2.target = j2.target.replace(/\bosakan\b/g, "nuiork");
n = n.concat(j2);
}
if (j.source.search(/\bTokio\b/) >= 0) {
j2 = JSON.parse(JSON.stringify(j));
j2.source = j2.source.replace(/\bTokio\b/g, "New York");
j2.target = j2.target.replace(/\btokios\b/g, "nuiork");
n = n.concat(j2);
}
return n;
}
function splitToChunks(array, parts) {
let result = [];
for (let i = parts; i > 0; i--) {
result.push(array.splice(0, Math.ceil(array.length / i)));
}
return result;
}
function splitOutput(arr) {
const tegerna = "muplis";
const hash = require("object-hash")(arr);
splitToChunks(arr, 5).forEach((chunk, index) => {
const outp = {
formatName: "dexie",
formatVersion: 1,
data: {
databaseName: "sorcu1",
databaseVersion: 2,
tables: [
{
name: "valsi",
schema: "++id, bangu, w, d, n, t, *s, g, *r, *cache, [r+bangu]",
rowCount: chunk.length,
},
],
data: [
{
tableName: "valsi",
inbound: true,
rows: chunk,
},
],
},
};
let dir = "/livla/build/sutysisku/data";
dir = fs.existsSync(dir) ? dir : "./dist";
let pathBinDump = path.join(dir, `parsed-${tegerna}-${index}.bin`);
fs.writeFileSync(
path.join(dir, `parsed-${tegerna}-${index}.json`),
JSON.stringify(outp)
);
const brotli = require("brotli-wasm");
fs.writeFileSync(
pathBinDump,
brotli.compress(Buffer.from(JSON.stringify(outp)))
);
});
const versio = "/livla/build/sutysisku/data/versio.json";
let jsonTimes = {};
try {
jsonTimes = JSON.parse(fs.readFileSync(versio, { encoding: "utf8" }));
} catch (error) {}
jsonTimes[tegerna] = hash;
try {
fs.writeFileSync(versio, JSON.stringify(jsonTimes));
} catch (error) {}
}