@furifont/dictionary 0.1.1 → 0.1.2

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.
@@ -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 and JLPT levels. */
4
+ export declare function loadDictionary(): Promise<Dictionary>;
5
+ //#endregion
package/dist/index.mjs ADDED
@@ -0,0 +1,94 @@
1
+ import * as v from "valibot";
2
+ //#region src/defaults.ts
3
+ /**
4
+ * Default readings the compiled data ranks 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 DEFAULT_READINGS = [
10
+ "出[で]来[き]",
11
+ "何[なん]人[にん]",
12
+ "何[なん]時[じ]",
13
+ "三[さん]十[じゅう]日[にち]",
14
+ "外[そと]付[づ]",
15
+ "支[し]払[はら]"
16
+ ];
17
+ /**
18
+ * Kanji left bare when alone in a run, though one reading dominates the
19
+ * dictionary: the reading depends on the word around it, which a font cannot
20
+ * see past the run. 月 is つき in a poem and がつ in a date.
21
+ */
22
+ const BARE_FORMS = [
23
+ "月",
24
+ "日",
25
+ "年"
26
+ ];
27
+ //#endregion
28
+ //#region src/index.ts
29
+ const DataSchema = v.object({
30
+ sources: v.record(v.string(), v.string()),
31
+ onyomi: v.record(v.string(), v.array(v.string())),
32
+ entries: v.array(v.tuple([
33
+ v.string(),
34
+ v.string(),
35
+ v.number(),
36
+ v.string(),
37
+ v.number(),
38
+ v.optional(v.literal(1))
39
+ ]))
40
+ });
41
+ const PlacesSchema = v.array(v.tuple([v.string(), v.string()]));
42
+ const JlptSchema = v.array(v.tuple([
43
+ v.string(),
44
+ v.string(),
45
+ v.picklist([
46
+ 1,
47
+ 2,
48
+ 3,
49
+ 4,
50
+ 5
51
+ ])
52
+ ]));
53
+ /**
54
+ * A data file as a JSON module, which Node loads and bundlers split into a
55
+ * lazy chunk. The path is a template so that TypeScript leaves the 11 MB file
56
+ * untyped rather than inferring it.
57
+ */
58
+ async function readData(name, schema) {
59
+ const { default: data } = await import(`../data/${name}.json`, { with: { type: "json" } });
60
+ return v.parse(schema, data);
61
+ }
62
+ /** Loads the compiled dictionary: JMdict and JMnedict place names, aligned by JmdictFurigana, with KANJIDIC2's on'yomi and JLPT levels. */
63
+ async function loadDictionary() {
64
+ const [data, places, jlpt] = await Promise.all([
65
+ readData("dictionary", DataSchema),
66
+ readData("places", PlacesSchema),
67
+ readData("jlpt", JlptSchema)
68
+ ]);
69
+ const levels = new Map(jlpt.map(([form, furigana, level]) => [`${form}|${furigana}`, level]));
70
+ return {
71
+ sources: data.sources,
72
+ onyomi: data.onyomi,
73
+ defaults: DEFAULT_READINGS,
74
+ bare: BARE_FORMS,
75
+ places: places.map(([form, furigana]) => ({
76
+ form,
77
+ furigana
78
+ })),
79
+ entries: data.entries.map(([form, furigana, nf, priority, order, conjugates]) => {
80
+ const level = levels.get(`${form}|${furigana}`);
81
+ return {
82
+ form,
83
+ furigana,
84
+ nf,
85
+ priority,
86
+ order,
87
+ ...conjugates && { conjugatesReading: true },
88
+ ...level && { jlpt: level }
89
+ };
90
+ })
91
+ };
92
+ }
93
+ //#endregion
94
+ 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