-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
99 lines (89 loc) · 3.5 KB
/
index.mjs
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
import FS from "fs";
import path from 'path';
import {fileURLToPath} from 'url';
import { Dictionary, DictionaryIndex, TermEntry } from "yomichan-dict-builder";
async function init() {
console.log("generating Yomitan dictionary file. This may take a few seconds.");
const __filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(__filename);
// the following wadokudict file needs to be downloaded externally, see readme
// (EDICT-Version from here: https://www.wadoku.de/wiki/display/WAD/Downloads+und+Links -> extract text file)
FS.readFile(dirname + "/wadokudict", "utf8" , (err, data) => {
if (err) {
console.error(err);
console.log("wadokudict not found, please download it, see Readme! Exiting.");
return;
}
processFile(data);
});
}
async function processFile(fileString) {
const dictionary = new Dictionary({
fileName: 'wadokuYomitan.zip',
});
const index = new DictionaryIndex()
.setTitle('Wadokutan')
.setRevision('0.1')
.setAuthor('sschmidTU')
.setDescription('Wadoku Japanese-German Dictionary (EDICT version) converted to Yomitan with yomichan-dict-builder')
.setAttribution("MIT License")
.setUrl('https://github.com/sschmidTU/wadoku-to-yomitan-converter')
// ...additional index details
.build();
await dictionary.setIndex(index);
const rows = fileString.split("\n");
for (let i=0; i < rows.length - 1; i++) {
const row = rows[i];
let readingPartIndex = row.indexOf("[") + 1;
// let definitionPartIndex = row.indexOf(" ", readingPartIndex + 1);
let definitionPartIndex = row.indexOf("]") + 1;
const wordPart = row.substring(0, readingPartIndex - 1).trim();
if (wordPart === "") {
continue;
}
const readingPart = row.substring(readingPartIndex, definitionPartIndex - 1).trim();
const definitionPart = row.substring(definitionPartIndex + 1).trim();
const definitions = definitionPart.split("/");
const entry = new TermEntry(wordPart)
.setReading(readingPart);
let addedDefinition = false;
for (const definition of definitions) {
if (definition.trim() === "") {
continue;
}
// more complex way to add definition, with styling:
// /** @type {import('../dist/types/yomitan/termbank').StructuredContent} */
// const sc = {
// tag: 'span',
// content: 'string',
// data: {
// 'dict-data': definition
// },
// lang: 'de',
// style: {
// fontSize: '20px',
// fontWeight: 'normal',
// textDecorationLine: 'overline',
// },
// }
// const detailedDefinition = {
// type: 'structured-content',
// content: sc,
// };
// entry.addDetailedDefinition(detailedDefinition);
// simply add definition as string:
entry.addDetailedDefinition(definition);
addedDefinition = true;
}
if (!addedDefinition) {
continue;
}
const termInformation = entry.build();
await dictionary.addTerm(termInformation);
}
// export
const stats = await dictionary.export('./');
console.log('Done exporting!');
console.table(stats);
}
init();