@furifont/dictionary 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +2 -1
- package/data/dictionary.json +4 -10
- package/data/tatoeba.json +22978 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +106 -0
- package/dist/okurigana.d.mts +8 -0
- package/dist/okurigana.mjs +46252 -0
- package/package.json +7 -3
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Dictionary } from "@furifont/core";
|
|
2
|
+
//#region src/index.d.ts
|
|
3
|
+
/** Loads the compiled dictionary: JMdict and JMnedict place names, aligned by JmdictFurigana, with KANJIDIC2's on'yomi, JLPT levels and uses in Tatoeba's sentences. */
|
|
4
|
+
export declare function loadDictionary(): Promise<Dictionary>;
|
|
5
|
+
//#endregion
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
//#region src/overrides.ts
|
|
3
|
+
/**
|
|
4
|
+
* Readings that override the compiled data where it ranks them wrong, in inline notation. JMdict's
|
|
5
|
+
* frequency tags belong to a written form, so across entries they cannot tell
|
|
6
|
+
* two readings apart; and a form that drops its okurigana usually meets it in
|
|
7
|
+
* running text, where the font cannot see it. Users' pins override these.
|
|
8
|
+
*/
|
|
9
|
+
const OVERRIDE_READINGS = [
|
|
10
|
+
"出[で]来[き]",
|
|
11
|
+
"何[なん]人[にん]",
|
|
12
|
+
"何[なん]時[じ]",
|
|
13
|
+
"三[さん]十[じゅう]日[にち]",
|
|
14
|
+
"外[そと]付[づ]",
|
|
15
|
+
"支[し]払[はら]",
|
|
16
|
+
"字[じ]",
|
|
17
|
+
"明[みん]朝[ちょう]"
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* Kanji left bare when alone in a run, though one reading dominates the
|
|
21
|
+
* dictionary: the reading depends on the word around it, which a font cannot
|
|
22
|
+
* see past the run. 月 is つき in a poem and がつ in a date.
|
|
23
|
+
*/
|
|
24
|
+
const BARE_FORMS = [
|
|
25
|
+
"月",
|
|
26
|
+
"日",
|
|
27
|
+
"年"
|
|
28
|
+
];
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/index.ts
|
|
31
|
+
const DataSchema = v.object({
|
|
32
|
+
sources: v.record(v.string(), v.string()),
|
|
33
|
+
onyomi: v.record(v.string(), v.array(v.string())),
|
|
34
|
+
entries: v.array(v.tuple([
|
|
35
|
+
v.string(),
|
|
36
|
+
v.string(),
|
|
37
|
+
v.number(),
|
|
38
|
+
v.string(),
|
|
39
|
+
v.number(),
|
|
40
|
+
v.optional(v.literal(1))
|
|
41
|
+
]))
|
|
42
|
+
});
|
|
43
|
+
const PlacesSchema = v.array(v.tuple([v.string(), v.string()]));
|
|
44
|
+
const JlptSchema = v.array(v.tuple([
|
|
45
|
+
v.string(),
|
|
46
|
+
v.string(),
|
|
47
|
+
v.picklist([
|
|
48
|
+
1,
|
|
49
|
+
2,
|
|
50
|
+
3,
|
|
51
|
+
4,
|
|
52
|
+
5
|
|
53
|
+
])
|
|
54
|
+
]));
|
|
55
|
+
const TatoebaSchema = v.array(v.tuple([
|
|
56
|
+
v.string(),
|
|
57
|
+
v.string(),
|
|
58
|
+
v.pipe(v.number(), v.integer(), v.minValue(1))
|
|
59
|
+
]));
|
|
60
|
+
/**
|
|
61
|
+
* A data file as a JSON module, which Node loads and bundlers split into a
|
|
62
|
+
* lazy chunk. The path is a template so that TypeScript leaves the 11 MB file
|
|
63
|
+
* untyped rather than inferring it.
|
|
64
|
+
*/
|
|
65
|
+
async function readData(name, schema) {
|
|
66
|
+
const { default: data } = await import(`../data/${name}.json`, { with: { type: "json" } });
|
|
67
|
+
return v.parse(schema, data);
|
|
68
|
+
}
|
|
69
|
+
/** Loads the compiled dictionary: JMdict and JMnedict place names, aligned by JmdictFurigana, with KANJIDIC2's on'yomi, JLPT levels and uses in Tatoeba's sentences. */
|
|
70
|
+
async function loadDictionary() {
|
|
71
|
+
const [data, places, jlpt, tatoeba] = await Promise.all([
|
|
72
|
+
readData("dictionary", DataSchema),
|
|
73
|
+
readData("places", PlacesSchema),
|
|
74
|
+
readData("jlpt", JlptSchema),
|
|
75
|
+
readData("tatoeba", TatoebaSchema)
|
|
76
|
+
]);
|
|
77
|
+
const levels = new Map(jlpt.map(([form, furigana, level]) => [`${form}|${furigana}`, level]));
|
|
78
|
+
const uses = new Map(tatoeba.map(([form, furigana, n]) => [`${form}|${furigana}`, n]));
|
|
79
|
+
return {
|
|
80
|
+
sources: data.sources,
|
|
81
|
+
onyomi: data.onyomi,
|
|
82
|
+
defaults: OVERRIDE_READINGS,
|
|
83
|
+
bare: BARE_FORMS,
|
|
84
|
+
places: places.map(([form, furigana]) => ({
|
|
85
|
+
form,
|
|
86
|
+
furigana
|
|
87
|
+
})),
|
|
88
|
+
entries: data.entries.map(([form, furigana, nf, priority, order, conjugates]) => {
|
|
89
|
+
const level = levels.get(`${form}|${furigana}`);
|
|
90
|
+
const n = uses.get(`${form}|${furigana}`);
|
|
91
|
+
uses.delete(`${form}|${furigana}`);
|
|
92
|
+
return {
|
|
93
|
+
form,
|
|
94
|
+
furigana,
|
|
95
|
+
nf,
|
|
96
|
+
priority,
|
|
97
|
+
order,
|
|
98
|
+
...conjugates && { conjugatesReading: true },
|
|
99
|
+
...level && { jlpt: level },
|
|
100
|
+
...n && { tatoeba: n }
|
|
101
|
+
};
|
|
102
|
+
})
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
export { loadDictionary };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/okurigana.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Words of one kanji run and the kana after it, read as a pair: 来な is こ, 来ま
|
|
4
|
+
* is き, 一人も is ひとり. The longest key a text starts with decides; null marks
|
|
5
|
+
* a pair that reads two ways.
|
|
6
|
+
*/
|
|
7
|
+
export declare const OKURIGANA: ReadonlyMap<string, string | null>;
|
|
8
|
+
//#endregion
|