-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfreedict_to_textractor.html
65 lines (61 loc) · 2.77 KB
/
freedict_to_textractor.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Freedict to Textractor</title>
</head>
<body>
This app converts dictionaries from Freedict (a.k.a. Dictd) format to Textractor's format.<br>
You can find Freedict format dictionaries <a href="https://freedict.org/downloads">here</a>.<br>
You can use other tools like <a href="https://github.com/ilius/pyglossary">pyglossary</a> to convert other
dictionary types such as Babylon to Freedict format.<br>
Extract whatever FreeDict dictionary you want with 7-zip. The contained .dict.dz file is also a archive 7-zip can
open: do so and extract the .dict file.<br>
Finally, select the .dict and .index files below to convert them to a Textractor dictionary.<br>
<input type="file" accept=".index" onchange="convert()">
<input type="file" accept=".dict" onchange="convert()">
<script>
function saveToFile(data = new Blob(0), fileName = "data.bin") {
Object.assign(document.createElement("a"),
{ download: fileName, href: window.URL.createObjectURL(data) }
).click();
};
function saveStringToFile(string, fileName = "data.txt") {
saveToFile(new Blob([string], { type: "text/plain" }), fileName);
}
function read(blob, as = "Text") {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => { resolve(reader.result); }
reader.onerror = () => { reject(reader.error); reader.abort(); }
reader["readAs" + as](blob);
});
}
function decrypt(string) {
if (!string) return NaN;
return string.split("").reverse().reduce((accumulator, value, i) => {
const base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(value);
return base64 === -1 ? NaN : accumulator + base64 * (64 ** i);
}, 0);
}
async function convert() {
const [indexFile, dictionaryFile] = [...document.querySelectorAll("input")].map(e => e.files[0]);
const [index, dictionary] = await Promise.all([read(indexFile), read(dictionaryFile, "ArrayBuffer")]);
const definitions = {};
for (const line of index.split("\n")) {
const pieces = line.split("\t"), offset = decrypt(pieces[1]), length = decrypt(pieces[2]);
if (pieces.length === 3 && offset && length) {
const definition = new TextDecoder().decode(dictionary.slice(offset, offset + length));
(definitions[definition] || (definitions[definition] = [])).push(pieces[0]);
}
}
saveStringToFile(Object.keys(definitions).map(
definition => definitions[definition].map(term => "|TERM|" + term).join("") +
"|DEFINITION|<p>" + definition.replace(/\n/g, "<br>") + "|END|"
).join("\n"), "SavedDictionary.txt");
}
</script>
</body>
</html>