-
Notifications
You must be signed in to change notification settings - Fork 9
/
make-glossary.js
184 lines (158 loc) · 5.35 KB
/
make-glossary.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
const LineByLineReader = require('line-by-line');
const { consoleOverwrite, clearConsoleLine, logProgress, findPartOfSpeech, loadJsonArray, writeInBatches, currentDate } = require('./util/util');
const { readdirSync, unlinkSync, writeFileSync } = require('fs');
const { isUint16Array } = require('util/types');
const {
source_iso: sourceIso,
target_iso: targetIso,
kaikki_file: kaikkiFile,
temp_folder: writeFolder,
} = process.env;
function processTranslations(translations, glosses, senses, sense){
if (!translations) return;
for (const translation of translations) {
const translationIso = translation.code || translation.lang_code;
if (translationIso !== targetIso) continue;
const translated = translation.word || translation.note;
if(!translated) continue;
if(!translation.sense){
if(!translation.sense_id){
if(sense){
translation.sense = sense?.glosses?.[0];
}
} else {
translation.sense = senses?.[translation.sense_id - 1]?.glosses?.[0]
}
}
if(!translation.sense){
translation.sense = "_none";
}
const senseTranslations = glosses.get(translation.sense);
if(!senseTranslations){
glosses.set(translation.sense, [translated]);
} else {
senseTranslations.push(translated);
}
}
}
const partsOfSpeech = loadJsonArray(`data/language/target-language-tags/en/parts_of_speech.json`);
const skippedPartsOfSpeech = {};
const url = 'https://github.com/yomidevs/kaikki-to-yomitan';
const latestReleaseUrl = `${url}/releases/latest/download/`;
const title = `kty-${sourceIso}-${targetIso}-gloss`;
const indexJson = {
title: title,
format: 3,
revision: currentDate,
sequenced: true,
author: 'Kaikki-to-Yomitan contributors',
url,
description: 'Dictionaries for various language pairs generated from Wiktionary data, via Kaikki and Kaikki-to-Yomitan.',
attribution: 'https://kaikki.org/',
sourceLanguage: sourceIso,
targetLanguage: targetIso,
isUpdatable: true,
indexUrl: `${latestReleaseUrl}${title}-index.json`,
downloadUrl: `${latestReleaseUrl}${title}.zip`,
};
const ymtLemmas = [];
let lineCount = 0;
consoleOverwrite(`make-glossary.js started...`);
const lr = new LineByLineReader(kaikkiFile);
lr.on('line', (line) => {
if (line) {
lineCount += 1;
logProgress("Processing lines", lineCount);
handleLine(line);
}
});
function handleLine(line) {
const parsedLine = JSON.parse(line);
const { pos, senses, translations } = parsedLine;
const word = getCanonicalForm(parsedLine);
const reading = getReading(word, parsedLine);
if(!(word && pos && senses)) return;
const glosses = new Map();
processTranslations(translations, glosses, senses, null);
for (const sense of senses) {
const { translations } = sense;
processTranslations(translations, glosses, senses, sense);
}
if (glosses.length === 0) return;
const processedPoS = findPartOfSpeech(pos, partsOfSpeech, skippedPartsOfSpeech);
const definitions = [];
for (const [sense, translations] of glosses.entries()) {
if(sense === "_none") {
definitions.push(...translations)
continue;
}
definitions.push({
type: "structured-content",
content: {
tag: 'div',
content: [
{
tag: 'span',
content: sense
},
{
tag: 'ul',
content: translations.map(translation => ({
tag: 'li',
content: translation
}))
}
]
}
})
}
if(definitions.length === 0) return;
ymtLemmas.push([
word,
reading,
processedPoS,
processedPoS,
0, // frequency
definitions, // glosses
0, // sequence
'', // term_tags
]);
}
function getCanonicalForm({word, forms}) {
if(!forms) return word;
const canonicalForm = forms.find(form =>
form.tags &&
form.tags.includes('canonical')
);
if (canonicalForm) {
word = canonicalForm.form;
}
return word;
}
function getReading(word, line){
switch(sourceIso){
case 'fa':
return getPersianReading(word, line);
default:
return word;
}
}
function getPersianReading(word, line){
const {forms} = line;
if(!forms) return word;
const romanization = forms.find(({form, tags}) => tags && tags.includes('romanization') && tags.length === 1 && form);
return romanization ? romanization.form : word;
}
lr.on('end', () => {
clearConsoleLine();
process.stdout.write(`Processed ${lineCount} lines...\n`);
if(ymtLemmas.length === 0){
console.log("No translations found. Exiting...");
process.exit(0);
}
for (const file of readdirSync(`${writeFolder}/dict`)) {
unlinkSync(`${writeFolder}/dict/${file}`);
}
writeFileSync(`${writeFolder}/dict/index.json`, JSON.stringify(indexJson, null, 2));
return writeInBatches(writeFolder, ymtLemmas, `dict/term_bank_`, 25000);
});