@kensio/pinyinjs 1.3.0 → 1.4.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/CHANGELOG.md +105 -3
- package/NOTICE +9 -0
- package/README.md +53 -9
- package/data/core.entries +1 -13
- package/data/core.freq +0 -0
- package/data/core.keys +1 -13
- package/data/full.entries +141 -131
- package/data/full.freq +0 -0
- package/data/full.keys +167 -157
- package/data/manifest.json +24 -9
- package/data/script.map +26543 -0
- package/data/standard.entries +1 -22
- package/data/standard.freq +0 -0
- package/data/standard.keys +2 -23
- package/dist/cli/arguments.d.ts +27 -0
- package/dist/cli/arguments.d.ts.map +1 -1
- package/dist/cli/arguments.js +27 -0
- package/dist/cli/arguments.js.map +1 -1
- package/dist/cli/commands.d.ts +11 -0
- package/dist/cli/commands.d.ts.map +1 -1
- package/dist/cli/commands.js +54 -1
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/environment.d.ts.map +1 -1
- package/dist/cli/environment.js +2 -1
- package/dist/cli/environment.js.map +1 -1
- package/dist/cli/help.d.ts.map +1 -1
- package/dist/cli/help.js +4 -0
- package/dist/cli/help.js.map +1 -1
- package/dist/cli/run.d.ts +8 -0
- package/dist/cli/run.d.ts.map +1 -1
- package/dist/cli/run.js +11 -1
- package/dist/cli/run.js.map +1 -1
- package/dist/decode/script.d.ts +108 -0
- package/dist/decode/script.d.ts.map +1 -0
- package/dist/decode/script.js +195 -0
- package/dist/decode/script.js.map +1 -0
- package/dist/dictionary/dictionary.d.ts +13 -1
- package/dist/dictionary/dictionary.d.ts.map +1 -1
- package/dist/dictionary/dictionary.js +18 -4
- package/dist/dictionary/dictionary.js.map +1 -1
- package/dist/dictionary/merge.d.ts.map +1 -1
- package/dist/dictionary/merge.js +23 -3
- package/dist/dictionary/merge.js.map +1 -1
- package/dist/dictionary/script-tables.d.ts +17 -0
- package/dist/dictionary/script-tables.d.ts.map +1 -0
- package/dist/dictionary/script-tables.js +275 -0
- package/dist/dictionary/script-tables.js.map +1 -0
- package/dist/dictionary/source.d.ts +18 -0
- package/dist/dictionary/source.d.ts.map +1 -1
- package/dist/dictionary/source.js +20 -0
- package/dist/dictionary/source.js.map +1 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/script/conversion.d.ts +102 -0
- package/dist/script/conversion.d.ts.map +1 -0
- package/dist/script/conversion.js +194 -0
- package/dist/script/conversion.js.map +1 -0
- package/dist/script/glyphs.d.ts +95 -0
- package/dist/script/glyphs.d.ts.map +1 -0
- package/dist/script/glyphs.js +236 -0
- package/dist/script/glyphs.js.map +1 -0
- package/dist/sources/opencc.d.ts +32 -0
- package/dist/sources/opencc.d.ts.map +1 -0
- package/dist/sources/opencc.js +50 -0
- package/dist/sources/opencc.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { toCharacters } from "./characters.js";
|
|
2
|
+
/**
|
|
3
|
+
* The regional 繁體 orthographies this package writes.
|
|
4
|
+
*
|
|
5
|
+
* A third axis, independent of the script and locale axes in `script.ts`:
|
|
6
|
+
* Taiwan and Hong Kong write the same characters with the same readings, and
|
|
7
|
+
* disagree about the standard form of 58 of them. See SCRIPTS-AND-LOCALES.md.
|
|
8
|
+
*/
|
|
9
|
+
export const REGIONS = ["TW", "HK"];
|
|
10
|
+
/**
|
|
11
|
+
* The 繁體 orthography written when the caller does not name one.
|
|
12
|
+
*
|
|
13
|
+
* There is no region-free 繁體 to fall back on: converting character by
|
|
14
|
+
* character from OpenCC's baseline yields 爲, 衆, 峯, 羣, 裏, 麪, which are the
|
|
15
|
+
* Hong Kong forms. Declining to choose would mean choosing Hong Kong silently,
|
|
16
|
+
* so a region is always applied and always named.
|
|
17
|
+
*
|
|
18
|
+
* Taiwan wins it on population, and more to the point because it is the one
|
|
19
|
+
* polity where 繁體 orthography and Mandarin as the spoken standard coincide —
|
|
20
|
+
* which is the intersection a pinyin package serves.
|
|
21
|
+
*/
|
|
22
|
+
export const DEFAULT_REGION = "TW";
|
|
23
|
+
/**
|
|
24
|
+
* Read a table written as a flat run of `from`/`to` character pairs.
|
|
25
|
+
*
|
|
26
|
+
* Pairs rather than a `Record` so that the tables stay legible as data and cost
|
|
27
|
+
* one string each: these are code rather than an artifact to fetch, for the
|
|
28
|
+
* same reason the transcription tables are — 133 mappings do not justify a
|
|
29
|
+
* download, and they must be available before any dictionary has loaded.
|
|
30
|
+
*/
|
|
31
|
+
function readPairs(pairs) {
|
|
32
|
+
const characters = toCharacters(pairs);
|
|
33
|
+
const table = new Map();
|
|
34
|
+
for (let at = 0; at + 1 < characters.length; at += 2) {
|
|
35
|
+
/* c8 ignore next 2 -- both indices are in range while at + 1 < length */
|
|
36
|
+
table.set(characters[at] ?? "", characters[at + 1] ?? "");
|
|
37
|
+
}
|
|
38
|
+
return table;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Variant 繁體 forms, mapped to the canonical form this package stores.
|
|
42
|
+
*
|
|
43
|
+
* The canonical form is Taiwan's, because that is what the dictionary's 繁體
|
|
44
|
+
* keys already are: measured on CC-CEDICT's traditional column, 裡 outnumbers
|
|
45
|
+
* 裏 116 to 8, 群 outnumbers 羣 175 to 1 and 麵 outnumbers 麪 138 to 1. So
|
|
46
|
+
* normalisation means mapping toward `TW`, and only Hong Kong output needs a
|
|
47
|
+
* pass away from what is stored.
|
|
48
|
+
*
|
|
49
|
+
* This is what lets Hong Kong text find dictionary keys. 羣眾 and 麪包 are not
|
|
50
|
+
* keys and never will be, so they would otherwise fall back to converting
|
|
51
|
+
* character by character — the same loss 臺灣 had before `hantVariants`.
|
|
52
|
+
* Measured: of the 9,833 dictionary keys with a distinct Hong Kong spelling,
|
|
53
|
+
* 2,358 are already keys and normalisation reaches a further 5,439. The 2,036
|
|
54
|
+
* still out of reach are mostly 臺 written as 台, which the exclusions below
|
|
55
|
+
* deliberately decline to reverse.
|
|
56
|
+
*
|
|
57
|
+
* Nineteen mappings OpenCC makes are deliberately **excluded**, because the
|
|
58
|
+
* variant is itself current in the corpus the dictionary is built from and
|
|
59
|
+
* rewriting it would corrupt legitimate text. Two different reasons, and the
|
|
60
|
+
* second is the one that is easy to miss:
|
|
61
|
+
*
|
|
62
|
+
* 1. The variant is a live 繁體 spelling. 台 is the clearest — OpenCC maps it to
|
|
63
|
+
* 臺, but CC-CEDICT writes 台 164 times against 臺's 208, so it is a spelling
|
|
64
|
+
* someone chose rather than a Hong Kong-ism. Also 污, 濕, 睾, 祕, 泄.
|
|
65
|
+
* 2. **The variant is a current 简体 character.** Many Hong Kong standard forms
|
|
66
|
+
* are exactly the mainland simplified form, because the PRC simplification
|
|
67
|
+
* adopted the same 新字形 conventions Hong Kong did: 着, 温, 脱, 户, 税, 卧,
|
|
68
|
+
* 悦, 兑, 葱, 幺, 棱, 檐, 痹. Normalising those would rewrite 简体 text, and
|
|
69
|
+
* 走着 `zǒuzhe` is the case that caught it.
|
|
70
|
+
*
|
|
71
|
+
* Excluding them costs almost nothing, because a variant current in either
|
|
72
|
+
* script is a key in its own right and resolves without any help from here.
|
|
73
|
+
* Reversing a merge is ambiguous in exactly the way 简→繁 is, and where the
|
|
74
|
+
* evidence says the variant is current, the safe answer is to leave it alone.
|
|
75
|
+
*/
|
|
76
|
+
const CANONICAL_PAIRS = "僞偽衹只啓啟喫吃粧妝嫺嫻嬀媯峯峰捝挩敍敘枴柺棁梲" +
|
|
77
|
+
"樑梁枱檯涚涗潙溈潨潀爲為牀床癡痴皁皂竈灶糉粽糭粽" +
|
|
78
|
+
"綫線緼縕繮韁纔才羣群脣唇蒀蒕蔘參蔿蒍藴蘊衆眾衞衛" +
|
|
79
|
+
"裏裡覈核説說輼轀醖醞鉢缽鈎鉤鋭銳鍼針閲閱鮎鯰鰛鰮" +
|
|
80
|
+
"麪麵齶顎";
|
|
81
|
+
/**
|
|
82
|
+
* Canonical 繁體 forms, mapped to the form Hong Kong writes.
|
|
83
|
+
*
|
|
84
|
+
* The 58 characters the two standards genuinely disagree about, measured over
|
|
85
|
+
* the 86 that appear in either of OpenCC's variant tables. The other 28 are
|
|
86
|
+
* agreements rather than differences — 爲 → 為 and 衆 → 眾 are both moves *both*
|
|
87
|
+
* standards make, which is easy to get backwards by reading either table alone.
|
|
88
|
+
*
|
|
89
|
+
* Taiwan needs no table of its own: the canonical form is already Taiwan's, so
|
|
90
|
+
* `zh-Hant-TW` output is what the dictionary stores, unaltered.
|
|
91
|
+
*/
|
|
92
|
+
const HONG_KONG_PAIRS = "兌兑叄叁啟啓囪囱媼媪嫻嫺峰峯么幺悅悦慍愠戶户挩捝" +
|
|
93
|
+
"搵揾敓敚敘敍柺枴梲棁榲榅梁樑簷檐檯枱氳氲汙污洩泄" +
|
|
94
|
+
"涗涚溫温溼濕熅煴床牀痺痹著着睪睾稅税縕緼韁繮群羣" +
|
|
95
|
+
"脫脱膃腽臥卧臺台菸煙蒕蒀參蔘蔥葱蘊藴蛻蜕衛衞裡裏" +
|
|
96
|
+
"說説轀輼醞醖鉤鈎銳鋭閱閲鯰鮎鰮鰛麵麪顎齶";
|
|
97
|
+
/**
|
|
98
|
+
* Variant 繁體 forms mapped to the canonical form, as a table.
|
|
99
|
+
*
|
|
100
|
+
* Exported so the data pipeline can recompute it from OpenCC and fail the build
|
|
101
|
+
* if the two have drifted — these tables are code rather than an artifact, so
|
|
102
|
+
* nothing else would notice an upstream release that moved a character.
|
|
103
|
+
*/
|
|
104
|
+
export const CANONICAL_FORMS = readPairs(CANONICAL_PAIRS);
|
|
105
|
+
/**
|
|
106
|
+
* Canonical 繁體 forms mapped to Hong Kong's, as a table. Verified like
|
|
107
|
+
* {@link CANONICAL_FORMS}.
|
|
108
|
+
*/
|
|
109
|
+
export const HONG_KONG_FORMS = readPairs(HONG_KONG_PAIRS);
|
|
110
|
+
/**
|
|
111
|
+
* The one glyph mapping whose answer depends on how the character is read.
|
|
112
|
+
*
|
|
113
|
+
* Taiwan writes 著 for every sense; Hong Kong splits it, writing 着 for the
|
|
114
|
+
* aspect particle and its relatives and keeping 著 for `zhù`. CC-CEDICT records
|
|
115
|
+
* the same split in its 简体 column — `著 着 [zhe5]` against `著 著 [zhu4]` —
|
|
116
|
+
* which is independent confirmation that the divide is real and where it falls.
|
|
117
|
+
*
|
|
118
|
+
* OpenCC resolves this with a phrase table. We resolve it with the reading,
|
|
119
|
+
* which is the point of difference this feature exists for, and which
|
|
120
|
+
* generalises to words no phrase table lists.
|
|
121
|
+
*
|
|
122
|
+
* `zhuó` is deliberately absent, and that is not an oversight: CC-CEDICT writes
|
|
123
|
+
* it both ways — `著 着 [zhuo2]` for wearing or applying, `著 著 [zhuo2]` for
|
|
124
|
+
* 執著 — so the reading genuinely does not settle it. It falls through to the
|
|
125
|
+
* default below and is reported as uncertain rather than guessed at silently.
|
|
126
|
+
*/
|
|
127
|
+
const READING_SPLITS = new Map([["著", new Map([["zhu", "著"]])]]);
|
|
128
|
+
/**
|
|
129
|
+
* A reading key ignoring tone and 儿化, which never bear on a glyph choice.
|
|
130
|
+
*
|
|
131
|
+
* Tone is dropped because the split that needs it — 著 — divides `zhù` from
|
|
132
|
+
* `zhe` and `zháo`, and those differ in their vowels as well, so the initial
|
|
133
|
+
* and final are enough and are what sources agree on most.
|
|
134
|
+
*/
|
|
135
|
+
function readingKey(syllable) {
|
|
136
|
+
if (syllable === undefined) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
return `${syllable.initial}${syllable.final}`;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Whether a character's regional form depends on how it is read.
|
|
143
|
+
*
|
|
144
|
+
* Exposed so that a conversion can report the choice as uncertain when it had
|
|
145
|
+
* no reading to go on, rather than presenting a default as though it were
|
|
146
|
+
* settled.
|
|
147
|
+
*/
|
|
148
|
+
export function isReadingSensitive(character) {
|
|
149
|
+
return READING_SPLITS.has(character);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Normalise one 繁體 character to the canonical form the dictionary stores.
|
|
153
|
+
*
|
|
154
|
+
* Characters no table knows are returned unchanged, which is the right answer
|
|
155
|
+
* for nearly all of them: the two standards agree about everything except the
|
|
156
|
+
* 58 characters {@link HONG_KONG_FORMS} lists.
|
|
157
|
+
*/
|
|
158
|
+
export function toCanonicalGlyph(character) {
|
|
159
|
+
return CANONICAL_FORMS.get(character) ?? character;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Whether any character of a text has a canonical form other than itself.
|
|
163
|
+
*/
|
|
164
|
+
function hasVariantGlyph(text) {
|
|
165
|
+
for (const character of text) {
|
|
166
|
+
if (CANONICAL_FORMS.has(character)) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Normalise a whole 繁體 text to the canonical forms the dictionary stores.
|
|
174
|
+
*
|
|
175
|
+
* Safe to run over 简体 or mixed text, but only because the table is built to
|
|
176
|
+
* make it so: every variant that is also a current 简体 character is excluded,
|
|
177
|
+
* since many Hong Kong forms and mainland simplified forms are the same
|
|
178
|
+
* character. See {@link CANONICAL_FORMS}.
|
|
179
|
+
*
|
|
180
|
+
* Returns the original string when nothing needs normalising, which is nearly
|
|
181
|
+
* always. This sits on the dictionary lookup path, where it is called once per
|
|
182
|
+
* candidate word at every position of a run, so the common case has to cost a
|
|
183
|
+
* scan rather than an allocation.
|
|
184
|
+
*
|
|
185
|
+
* The mapping is one character to one character, which is what makes it safe
|
|
186
|
+
* here: normalising a prefix gives the same answer as taking the prefix of the
|
|
187
|
+
* normalised text, so a lattice scan that prunes on prefixes cannot disagree
|
|
188
|
+
* with the lookup that follows it.
|
|
189
|
+
*/
|
|
190
|
+
export function toCanonicalGlyphs(text) {
|
|
191
|
+
if (!hasVariantGlyph(text)) {
|
|
192
|
+
return text;
|
|
193
|
+
}
|
|
194
|
+
let normalised = "";
|
|
195
|
+
for (const character of toCharacters(text)) {
|
|
196
|
+
normalised += toCanonicalGlyph(character);
|
|
197
|
+
}
|
|
198
|
+
return normalised;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Write one canonical character in a region's orthography.
|
|
202
|
+
*
|
|
203
|
+
* The syllable is used only where the character is one the two standards split
|
|
204
|
+
* on a reading; everywhere else it is ignored, because a glyph choice never
|
|
205
|
+
* changes how a character is read.
|
|
206
|
+
*/
|
|
207
|
+
export function toRegionalGlyph(character, region, syllable) {
|
|
208
|
+
if (region === DEFAULT_REGION) {
|
|
209
|
+
return character;
|
|
210
|
+
}
|
|
211
|
+
const split = READING_SPLITS.get(character);
|
|
212
|
+
const kept = split?.get(readingKey(syllable) ?? "");
|
|
213
|
+
if (kept !== undefined) {
|
|
214
|
+
return kept;
|
|
215
|
+
}
|
|
216
|
+
return HONG_KONG_FORMS.get(character) ?? character;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Write a whole canonical text in a region's orthography.
|
|
220
|
+
*
|
|
221
|
+
* Readings are matched to characters by position, so a caller with a decoded
|
|
222
|
+
* reading can pass it and a caller without one can leave it out. A position
|
|
223
|
+
* with no syllable behind it falls back to the region's default form, which is
|
|
224
|
+
* what {@link isReadingSensitive} lets the caller flag.
|
|
225
|
+
*/
|
|
226
|
+
export function toRegionalGlyphs(text, region, readings = []) {
|
|
227
|
+
if (region === DEFAULT_REGION) {
|
|
228
|
+
return text;
|
|
229
|
+
}
|
|
230
|
+
let written = "";
|
|
231
|
+
for (const [at, character] of toCharacters(text).entries()) {
|
|
232
|
+
written += toRegionalGlyph(character, region, readings[at]);
|
|
233
|
+
}
|
|
234
|
+
return written;
|
|
235
|
+
}
|
|
236
|
+
//# sourceMappingURL=glyphs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"glyphs.js","sourceRoot":"","sources":["../../src/script/glyphs.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;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;IACnB,CAAC;IACD,MAAM,KAAK,GAAG,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC5C,MAAM,IAAI,GAAG,KAAK,EAAE,GAAG,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IACpD,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,eAAe,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,SAAS,CAAC;AACrD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAC9B,IAAY,EACZ,MAAc,EACd,QAAQ,GAAsC,EAAE;IAEhD,IAAI,MAAM,KAAK,cAAc,EAAE,CAAC;QAC9B,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,OAAO,GAAG,EAAE,CAAC;IACjB,KAAK,MAAM,CAAC,EAAE,EAAE,SAAS,CAAC,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC;QAC3D,OAAO,IAAI,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;IAC9D,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One OpenCC mapping table: each key with the forms it may be written as.
|
|
3
|
+
*
|
|
4
|
+
* The first value is OpenCC's default, and the rest are alternatives it keeps
|
|
5
|
+
* for the reverse tables it generates. Both matter here: the default is what a
|
|
6
|
+
* conversion takes when it has no other evidence, and the alternatives are what
|
|
7
|
+
* a normalisation has to recognise on input.
|
|
8
|
+
*/
|
|
9
|
+
export type OpenCcTable = ReadonlyMap<string, readonly string[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Read one of OpenCC's dictionary tables.
|
|
12
|
+
*
|
|
13
|
+
* The format is one mapping per line as `key<TAB>value(s)`, values separated by
|
|
14
|
+
* spaces with the first as the default, and `#` starting a comment line.
|
|
15
|
+
*
|
|
16
|
+
* OpenCC also writes directives into those comments — `# @reverse-prefer: 才`
|
|
17
|
+
* tells its own build which value to favour when it generates the reversed
|
|
18
|
+
* table. They are deliberately ignored: we do not use OpenCC's generated
|
|
19
|
+
* reverse tables, and reversing a table here is a decision for the code that
|
|
20
|
+
* knows what it is reversing for. See SCRIPTS-AND-LOCALES.md.
|
|
21
|
+
*
|
|
22
|
+
* Faithful to the file, per the convention in DATA-SOURCES.md: a key mapping to
|
|
23
|
+
* itself is kept rather than dropped as a no-op, because "this character is
|
|
24
|
+
* already standard" and "this character is unknown to the table" are different
|
|
25
|
+
* facts, and only the table can tell them apart.
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseOpenCcTable(text: string): OpenCcTable;
|
|
28
|
+
/**
|
|
29
|
+
* The form a table converts a character to, or undefined if it has no opinion.
|
|
30
|
+
*/
|
|
31
|
+
export declare function openCcDefault(table: OpenCcTable, character: string): string | undefined;
|
|
32
|
+
//# sourceMappingURL=opencc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"opencc.d.ts","sourceRoot":"","sources":["../../src/sources/opencc.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,GAAG,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC;AAYjE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAsB1D;AAED;;GAEG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,WAAW,EAClB,SAAS,EAAE,MAAM,GAChB,MAAM,GAAG,SAAS,CAEpB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Separator between an OpenCC key and its values.
|
|
3
|
+
*/
|
|
4
|
+
const COLUMN = "\t";
|
|
5
|
+
/**
|
|
6
|
+
* Separator between the values of one key.
|
|
7
|
+
*/
|
|
8
|
+
const VALUE = " ";
|
|
9
|
+
/**
|
|
10
|
+
* Read one of OpenCC's dictionary tables.
|
|
11
|
+
*
|
|
12
|
+
* The format is one mapping per line as `key<TAB>value(s)`, values separated by
|
|
13
|
+
* spaces with the first as the default, and `#` starting a comment line.
|
|
14
|
+
*
|
|
15
|
+
* OpenCC also writes directives into those comments — `# @reverse-prefer: 才`
|
|
16
|
+
* tells its own build which value to favour when it generates the reversed
|
|
17
|
+
* table. They are deliberately ignored: we do not use OpenCC's generated
|
|
18
|
+
* reverse tables, and reversing a table here is a decision for the code that
|
|
19
|
+
* knows what it is reversing for. See SCRIPTS-AND-LOCALES.md.
|
|
20
|
+
*
|
|
21
|
+
* Faithful to the file, per the convention in DATA-SOURCES.md: a key mapping to
|
|
22
|
+
* itself is kept rather than dropped as a no-op, because "this character is
|
|
23
|
+
* already standard" and "this character is unknown to the table" are different
|
|
24
|
+
* facts, and only the table can tell them apart.
|
|
25
|
+
*/
|
|
26
|
+
export function parseOpenCcTable(text) {
|
|
27
|
+
const table = new Map();
|
|
28
|
+
for (const line of text.split("\n")) {
|
|
29
|
+
if (line.startsWith("#")) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
const [key, values] = line.trim().split(COLUMN);
|
|
33
|
+
if (key === undefined || key === "" || values === undefined) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const forms = values.split(VALUE).filter((form) => form !== "");
|
|
37
|
+
if (forms.length === 0) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
table.set(key.normalize("NFC"), forms.map((form) => form.normalize("NFC")));
|
|
41
|
+
}
|
|
42
|
+
return table;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The form a table converts a character to, or undefined if it has no opinion.
|
|
46
|
+
*/
|
|
47
|
+
export function openCcDefault(table, character) {
|
|
48
|
+
return table.get(character)?.[0];
|
|
49
|
+
}
|
|
50
|
+
//# source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