-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_dictionary.js
247 lines (213 loc) · 7.75 KB
/
generate_dictionary.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
const fs = require("fs");
const path = require("path");
const cheerio = require("cheerio");
function normalizeText(text = "") {
text = text.normalize("NFC");
return text
.replace(/\u00A0/g, " ")
.replace(/’/g, "'")
.replace(/\s+/g, " ")
.trim();
}
function generateKindleHeader() {
return `<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns:math="http://exslt.org/math"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:tl="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf"
xmlns:saxon="http://saxon.sf.net/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cx="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:mbp="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf"
xmlns:mmc="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf"
xmlns:idx="https://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<mbp:frameset>`;
}
function generateKindleFooter() {
return `
</mbp:frameset>
</body>
</html>`;
}
function generateDictionaryEntry(id, headword, terms, body) {
const additionalForms = terms.slice(1);
const inflSection =
additionalForms.length > 0
? `<idx:infl>
${additionalForms.map((term) => `<idx:iform value="${term}" exact="true"></idx:iform>`).join(" ")}
</idx:infl>`
: "";
return {
id,
headword,
content: `
<idx:entry name="catalan" scriptable="yes">
<idx:short>
<a id="${id}"></a>
<idx:orth value="${terms[0]}" exact="true">${headword}${inflSection}</idx:orth>
${body}
</idx:short>
</idx:entry>`,
};
}
function buildKindleDictionary() {
const outputDir = path.join(__dirname, "kindle");
const directory = path.join(__dirname, "data");
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
// First, collect the conjugations.
const verbForms = {};
const directory_c = "conjugacions";
const files_c = fs
.readdirSync(path.join(__dirname, directory_c))
.filter((file) => file.endsWith(".html"));
files_c.forEach((file) => {
const filePath = path.join(directory_c, file);
const htmlContent = fs.readFileSync(filePath, "utf-8");
const id = 100000 + path.basename(file, ".html");
const $ = cheerio.load(htmlContent);
$('[style="display: none;"]').remove();
// Get an array of unique text of infinitive forms
const infinitius = $('span.dm2-linia.dm2-temps:contains("Infinitiu")')
.map((_, elem) => $(elem).next(".dm2-linia").text()) // Get text of the next sibling
.get() // Convert Cheerio object to array
.flatMap((text) => text.split(/\s+o\s+/)) // Split on " o " and flatten the result
.filter((value, index, self) => self.indexOf(value) === index); // Remove duplicates
const formes = $("span.dm2-linia:not(.dm2-temps)")
.map((_, elem) => $(elem).text())
.get()
.flatMap((text) => text.split(/\s+o\s+/))
.filter((verb) => verb !== " ")
.filter((verb) => verb !== " ")
.filter((verb) => verb !== "-")
.filter((verb) => !infinitius.includes(verb))
.filter((value, index, self) => self.indexOf(value) === index);
if (infinitius && formes) {
infinitius.forEach((infinitive) => {
verbForms[infinitive] = formes;
});
}
});
const files = fs
.readdirSync(directory)
.filter((file) => file.endsWith(".html"));
const entries = [];
files.forEach((file) => {
const filePath = path.join(directory, file);
const htmlContent = fs.readFileSync(filePath, "utf-8");
const id = path.basename(file, ".html");
const $ = cheerio.load(htmlContent);
if ($(".dm2-areatem").length === 0) {
// This happens with entries that do not contain useful information.
// Hopefully the word will get handled in another entry.
return;
}
$('[style="display: none;"]').remove();
// Remove unnecessary extra information
$(".dm2-infcom").remove();
$(".dm2-areatem").remove();
$(".dm2-rmt-cnt").remove();
$("[onclick]").each(function () {
$(this).removeAttr("onclick");
});
const mainTerm = normalizeText($(".dm2-article").html());
let terms_array = [];
$(".dm2-article > b, .rel b").each((_, element) => {
$(element).find("sup").remove();
terms_array.push(normalizeText($(element).text()));
});
$(".dm2-articleflex i").each((_, element) => {
$(element).find("sup").remove();
let form = normalizeText($(element).text());
// Split by " o " or ",", then trim whitespace and filter out empty entries.
const parts = form
.split(/,\s*| o /)
.map((part) => part.trim())
.filter(Boolean);
terms_array.push(...parts);
});
const isVerb = mainTerm.includes("<i>v.</i>");
if (isVerb) {
if (!verbForms[terms_array[0]]) {
console.log("Error amb la conjugació del verb " + terms_array[0]);
} else {
terms_array.push(...verbForms[terms_array[0]]);
}
}
// Ensure terms_array only contains unique values
terms_array = [...new Set(terms_array)];
$(".k-i-kpi-status-open").html("●"); // ●
$(".k-i-kpi-status-deny").html("◆"); // ◆
$(".k-i-stop").html("■"); // ■
const body = normalizeText($("#cont-article").html());
entries.push(generateDictionaryEntry(id, mainTerm, terms_array, body));
});
// Sort entries.
entries.sort((a, b) => a.headword.localeCompare(b.headword, "ca"));
let dictionaryContent = "";
let fileIndex = 1;
let entryCount = 0;
const maxEntriesPerFile = 1000;
const fileNames = [];
entries.forEach((entry, idx) => {
dictionaryContent += entry.content;
entryCount++;
if (entryCount === maxEntriesPerFile || idx === entries.length - 1) {
const fileName = `deiec${String(fileIndex).padStart(3, "0")}.xhtml`;
fileNames.push(fileName);
const outputPath = path.join(outputDir, fileName);
const fileContent =
generateKindleHeader() + dictionaryContent + generateKindleFooter();
fs.writeFileSync(outputPath, fileContent, "utf-8");
console.log(`File ${outputPath} written with ${entryCount} entries`);
// Reset for next file
dictionaryContent = "";
entryCount = 0;
fileIndex++;
}
});
generateOpfFile(outputDir, fileNames);
console.log(`Dictionary split into ${fileIndex - 1} files successfully.`);
}
function generateOpfFile(outputDir, fileNames) {
const opfContent = `<?xml version="1.0"?>
<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId">
<metadata>
<dc:title>Diccionari Essencial de la Llengua Catalana</dc:title>
<dc:creator opf:role="aut">Institut d'Estudis Catalans</dc:creator>
<dc:language>ca</dc:language>
<x-metadata>
<DictionaryInLanguage>ca</DictionaryInLanguage>
<DictionaryOutLanguage>ca</DictionaryOutLanguage>
<DefaultLookupIndex>catalan</DefaultLookupIndex>
</x-metadata>
</metadata>
<manifest>
${fileNames
.map(
(fileName, index) =>
`<item id="deiec${String(index + 1).padStart(3, "0")}" href="${fileName}" media-type="application/xhtml+xml" />`,
)
.join("\n ")}
</manifest>
<spine>
${fileNames
.map(
(_, index) =>
`<itemref idref="deiec${String(index + 1).padStart(3, "0")}"/>`,
)
.join("\n ")}
</spine>
</package>`;
const opfPath = path.join(outputDir, "deiec.opf");
fs.writeFileSync(opfPath, opfContent, "utf-8");
console.log(`OPF file written at ${opfPath}`);
}
buildKindleDictionary();