js-opencc 1.4.1 → 1.4.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.
- package/README.md +111 -129
- package/README.zh-Hant.md +222 -0
- package/data/custom/CNTWPhrases.txt +6 -0
- package/dist/bundles/cn2t.d.ts +2 -3
- package/dist/bundles/cn2t.js +36 -11
- package/dist/bundles/full.d.ts +2 -3
- package/dist/bundles/full.js +22 -8
- package/dist/bundles/t2cn.d.ts +2 -3
- package/dist/bundles/t2cn.js +28 -9
- package/dist/converter.d.ts +6 -1
- package/dist/converter.js +43 -10
- package/dist/core.d.ts +107 -17
- package/dist/core.js +377 -51
- package/dist/dict/CNTWPhrases.d.ts +2 -3
- package/dist/dict/CNTWPhrases.js +2 -2
- package/dist/dict/HKPhrases.d.ts +2 -3
- package/dist/dict/HKPhrases.js +2 -2
- package/dist/dict/HKPhrasesRev.d.ts +2 -3
- package/dist/dict/HKPhrasesRev.js +2 -2
- package/dist/dict/HKVariants.d.ts +2 -3
- package/dist/dict/HKVariants.js +2 -2
- package/dist/dict/HKVariantsPhrases.d.ts +2 -3
- package/dist/dict/HKVariantsPhrases.js +2 -2
- package/dist/dict/HKVariantsRev.d.ts +2 -3
- package/dist/dict/HKVariantsRev.js +2 -2
- package/dist/dict/HKVariantsRevPhrases.d.ts +2 -3
- package/dist/dict/HKVariantsRevPhrases.js +2 -2
- package/dist/dict/JPShinjitaiCharacters.d.ts +2 -3
- package/dist/dict/JPShinjitaiCharacters.js +2 -2
- package/dist/dict/JPShinjitaiCharactersRev.d.ts +2 -3
- package/dist/dict/JPShinjitaiCharactersRev.js +2 -2
- package/dist/dict/JPShinjitaiPhrases.d.ts +2 -3
- package/dist/dict/JPShinjitaiPhrases.js +2 -2
- package/dist/dict/STCharacters.d.ts +2 -3
- package/dist/dict/STCharacters.js +2 -2
- package/dist/dict/STPhrases.d.ts +2 -3
- package/dist/dict/STPhrases.js +2 -2
- package/dist/dict/STPhrases_GeneratedFromRegionalPhrases.d.ts +2 -0
- package/dist/dict/STPhrases_GeneratedFromRegionalPhrases.js +2 -0
- package/dist/dict/TSCharacters.d.ts +2 -3
- package/dist/dict/TSCharacters.js +2 -2
- package/dist/dict/TSPhrases.d.ts +2 -3
- package/dist/dict/TSPhrases.js +2 -2
- package/dist/dict/TWPhrases.d.ts +2 -3
- package/dist/dict/TWPhrases.js +2 -2
- package/dist/dict/TWPhrasesRev.d.ts +2 -3
- package/dist/dict/TWPhrasesRev.js +2 -2
- package/dist/dict/TWVariants.d.ts +2 -3
- package/dist/dict/TWVariants.js +2 -2
- package/dist/dict/TWVariantsPhrases.d.ts +2 -3
- package/dist/dict/TWVariantsPhrases.js +2 -2
- package/dist/dict/TWVariantsRev.d.ts +2 -3
- package/dist/dict/TWVariantsRev.js +2 -2
- package/dist/dict/TWVariantsRevPhrases.d.ts +2 -3
- package/dist/dict/TWVariantsRevPhrases.js +2 -2
- package/dist/dict/index.d.ts +0 -1
- package/dist/dict/index.js +1 -1
- package/dist/html-converter.d.ts +0 -1
- package/dist/html-converter.js +65 -26
- package/dist/index.d.ts +2 -3
- package/dist/index.js +14 -5
- package/dist/presets.d.ts +44 -1
- package/dist/presets.js +58 -2
- package/dist/umd/cn2t.js +554 -102
- package/dist/umd/cn2t.js.map +1 -1
- package/dist/umd/cn2t.min.js +1 -1
- package/dist/umd/cn2t.min.js.map +1 -1
- package/dist/umd/full.js +560 -119
- package/dist/umd/full.js.map +1 -1
- package/dist/umd/full.min.js +1 -1
- package/dist/umd/full.min.js.map +1 -1
- package/dist/umd/t2cn.js +548 -103
- package/dist/umd/t2cn.js.map +1 -1
- package/dist/umd/t2cn.min.js +1 -1
- package/dist/umd/t2cn.min.js.map +1 -1
- package/package.json +2 -1
- package/dist/bundles/cn2t.d.ts.map +0 -1
- package/dist/bundles/cn2t.js.map +0 -1
- package/dist/bundles/full.d.ts.map +0 -1
- package/dist/bundles/full.js.map +0 -1
- package/dist/bundles/t2cn.d.ts.map +0 -1
- package/dist/bundles/t2cn.js.map +0 -1
- package/dist/converter.d.ts.map +0 -1
- package/dist/converter.js.map +0 -1
- package/dist/core.d.ts.map +0 -1
- package/dist/core.js.map +0 -1
- package/dist/dict/CNTWPhrases.d.ts.map +0 -1
- package/dist/dict/CNTWPhrases.js.map +0 -1
- package/dist/dict/HKPhrases.d.ts.map +0 -1
- package/dist/dict/HKPhrases.js.map +0 -1
- package/dist/dict/HKPhrasesRev.d.ts.map +0 -1
- package/dist/dict/HKPhrasesRev.js.map +0 -1
- package/dist/dict/HKVariants.d.ts.map +0 -1
- package/dist/dict/HKVariants.js.map +0 -1
- package/dist/dict/HKVariantsPhrases.d.ts.map +0 -1
- package/dist/dict/HKVariantsPhrases.js.map +0 -1
- package/dist/dict/HKVariantsRev.d.ts.map +0 -1
- package/dist/dict/HKVariantsRev.js.map +0 -1
- package/dist/dict/HKVariantsRevPhrases.d.ts.map +0 -1
- package/dist/dict/HKVariantsRevPhrases.js.map +0 -1
- package/dist/dict/JPShinjitaiCharacters.d.ts.map +0 -1
- package/dist/dict/JPShinjitaiCharacters.js.map +0 -1
- package/dist/dict/JPShinjitaiCharactersRev.d.ts.map +0 -1
- package/dist/dict/JPShinjitaiCharactersRev.js.map +0 -1
- package/dist/dict/JPShinjitaiPhrases.d.ts.map +0 -1
- package/dist/dict/JPShinjitaiPhrases.js.map +0 -1
- package/dist/dict/STCharacters.d.ts.map +0 -1
- package/dist/dict/STCharacters.js.map +0 -1
- package/dist/dict/STPhrases.d.ts.map +0 -1
- package/dist/dict/STPhrases.js.map +0 -1
- package/dist/dict/TSCharacters.d.ts.map +0 -1
- package/dist/dict/TSCharacters.js.map +0 -1
- package/dist/dict/TSPhrases.d.ts.map +0 -1
- package/dist/dict/TSPhrases.js.map +0 -1
- package/dist/dict/TWPhrases.d.ts.map +0 -1
- package/dist/dict/TWPhrases.js.map +0 -1
- package/dist/dict/TWPhrasesRev.d.ts.map +0 -1
- package/dist/dict/TWPhrasesRev.js.map +0 -1
- package/dist/dict/TWVariants.d.ts.map +0 -1
- package/dist/dict/TWVariants.js.map +0 -1
- package/dist/dict/TWVariantsPhrases.d.ts.map +0 -1
- package/dist/dict/TWVariantsPhrases.js.map +0 -1
- package/dist/dict/TWVariantsRev.d.ts.map +0 -1
- package/dist/dict/TWVariantsRev.js.map +0 -1
- package/dist/dict/TWVariantsRevPhrases.d.ts.map +0 -1
- package/dist/dict/TWVariantsRevPhrases.js.map +0 -1
- package/dist/dict/index.d.ts.map +0 -1
- package/dist/dict/index.js.map +0 -1
- package/dist/html-converter.d.ts.map +0 -1
- package/dist/html-converter.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/presets.d.ts.map +0 -1
- package/dist/presets.js.map +0 -1
package/dist/html-converter.js
CHANGED
|
@@ -12,6 +12,19 @@ const SKIP_TAGS = new Set(["SCRIPT", "STYLE", "TEXTAREA", "CODE", "PRE"]);
|
|
|
12
12
|
* inputs (text/password/search/…) hold user data and are left untouched.
|
|
13
13
|
*/
|
|
14
14
|
const CONVERTIBLE_INPUT_TYPES = new Set(["button", "submit", "reset"]);
|
|
15
|
+
/**
|
|
16
|
+
* Attributes whose value is **text the user reads**, so it must follow the
|
|
17
|
+
* page's conversion. Not to be confused with attributes that hold data.
|
|
18
|
+
*
|
|
19
|
+
* The distinction that matters here is display-vs-data, not element-vs-element:
|
|
20
|
+
* an editable input's `value` is the user's own text and is left alone, but its
|
|
21
|
+
* `placeholder` is UI chrome exactly like an `<img alt>` — leaving it behind
|
|
22
|
+
* means a page converted to Traditional still shows Simplified hint text in
|
|
23
|
+
* every empty field. `aria-label` and `title` are the same thing for screen
|
|
24
|
+
* readers and tooltips; skipping them silently degrades the accessible copy
|
|
25
|
+
* while the visible copy converts.
|
|
26
|
+
*/
|
|
27
|
+
const TEXT_ATTRIBUTES = ["placeholder", "title", "aria-label"];
|
|
15
28
|
/**
|
|
16
29
|
* Class name to ignore conversion
|
|
17
30
|
*/
|
|
@@ -23,22 +36,36 @@ const IGNORE_CLASS = "ignore-opencc";
|
|
|
23
36
|
*/
|
|
24
37
|
export function HTMLConverter(options) {
|
|
25
38
|
const { converter, rootNode, fromLangTag, toLangTag } = options;
|
|
26
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Original values, keyed by node and then by SLOT.
|
|
41
|
+
*
|
|
42
|
+
* One string per node is not enough: a single `<input>` can carry both a
|
|
43
|
+
* converted `value` and a converted `placeholder`, an `<img>` both `alt` and
|
|
44
|
+
* `title`. With a flat `WeakMap<Node, string>` the second conversion reuses
|
|
45
|
+
* the first one's stored original and `restore()` writes it into the wrong
|
|
46
|
+
* slot. Slot is `"#text"`, `"#value"`, or the attribute name.
|
|
47
|
+
*/
|
|
27
48
|
const originalValues = new WeakMap();
|
|
28
49
|
const changedLangNodes = new WeakSet();
|
|
50
|
+
const TEXT_SLOT = "#text";
|
|
51
|
+
const VALUE_SLOT = "#value";
|
|
29
52
|
/**
|
|
30
53
|
* Convert all text nodes in the DOM
|
|
31
54
|
*/
|
|
32
55
|
function convert() {
|
|
33
56
|
const fromLangLower = fromLangTag.toLowerCase();
|
|
34
|
-
// Record the pre-conversion value exactly once per node. Repeated
|
|
35
|
-
// calls then re-convert from the stored original instead of from
|
|
57
|
+
// Record the pre-conversion value exactly once per (node, slot). Repeated
|
|
58
|
+
// convert() calls then re-convert from the stored original instead of from
|
|
36
59
|
// already-converted text, and restore() always returns the true original.
|
|
37
|
-
function originalOf(node, current) {
|
|
38
|
-
|
|
39
|
-
|
|
60
|
+
function originalOf(node, slot, current) {
|
|
61
|
+
let slots = originalValues.get(node);
|
|
62
|
+
if (!slots) {
|
|
63
|
+
slots = new Map();
|
|
64
|
+
originalValues.set(node, slots);
|
|
40
65
|
}
|
|
41
|
-
|
|
66
|
+
if (!slots.has(slot))
|
|
67
|
+
slots.set(slot, current);
|
|
68
|
+
return slots.get(slot);
|
|
42
69
|
}
|
|
43
70
|
function processNode(node, langMatched) {
|
|
44
71
|
// Skip elements with ignore-opencc class
|
|
@@ -63,12 +90,23 @@ export function HTMLConverter(options) {
|
|
|
63
90
|
if (SKIP_TAGS.has(node.tagName)) {
|
|
64
91
|
return;
|
|
65
92
|
}
|
|
93
|
+
// Attributes that render as text, on any element. Runs before the
|
|
94
|
+
// INPUT early-return below so an editable field's `placeholder` still
|
|
95
|
+
// converts even though its `value` (user data) must not.
|
|
96
|
+
if (langMatched) {
|
|
97
|
+
for (const attr of TEXT_ATTRIBUTES) {
|
|
98
|
+
const current = node.getAttribute(attr);
|
|
99
|
+
if (current) {
|
|
100
|
+
node.setAttribute(attr, converter(originalOf(node, attr, current)));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
66
104
|
// INPUT is a void element: convert button-like labels, then stop —
|
|
67
105
|
// it has no child text nodes to recurse into.
|
|
68
106
|
if (node.tagName === "INPUT") {
|
|
69
107
|
const input = node;
|
|
70
108
|
if (langMatched && CONVERTIBLE_INPUT_TYPES.has(input.type)) {
|
|
71
|
-
input.value = converter(originalOf(input, input.value));
|
|
109
|
+
input.value = converter(originalOf(input, VALUE_SLOT, input.value));
|
|
72
110
|
}
|
|
73
111
|
return;
|
|
74
112
|
}
|
|
@@ -79,14 +117,14 @@ export function HTMLConverter(options) {
|
|
|
79
117
|
if (name === "description" || name === "keywords") {
|
|
80
118
|
const content = node.getAttribute("content");
|
|
81
119
|
if (content) {
|
|
82
|
-
node.setAttribute("content", converter(originalOf(node, content)));
|
|
120
|
+
node.setAttribute("content", converter(originalOf(node, "content", content)));
|
|
83
121
|
}
|
|
84
122
|
}
|
|
85
123
|
}
|
|
86
124
|
else if (node.tagName === "IMG") {
|
|
87
125
|
const alt = node.getAttribute("alt");
|
|
88
126
|
if (alt) {
|
|
89
|
-
node.setAttribute("alt", converter(originalOf(node, alt)));
|
|
127
|
+
node.setAttribute("alt", converter(originalOf(node, "alt", alt)));
|
|
90
128
|
}
|
|
91
129
|
}
|
|
92
130
|
}
|
|
@@ -96,7 +134,7 @@ export function HTMLConverter(options) {
|
|
|
96
134
|
if (child.nodeType === Node.TEXT_NODE && langMatched) {
|
|
97
135
|
const text = child.nodeValue;
|
|
98
136
|
if (text) {
|
|
99
|
-
child.nodeValue = converter(originalOf(child, text));
|
|
137
|
+
child.nodeValue = converter(originalOf(child, TEXT_SLOT, text));
|
|
100
138
|
}
|
|
101
139
|
}
|
|
102
140
|
else if (child.nodeType === Node.ELEMENT_NODE) {
|
|
@@ -118,21 +156,23 @@ export function HTMLConverter(options) {
|
|
|
118
156
|
if (node instanceof Element && changedLangNodes.has(node)) {
|
|
119
157
|
node.setAttribute("lang", fromLangTag);
|
|
120
158
|
}
|
|
121
|
-
// Restore
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (
|
|
129
|
-
node.
|
|
159
|
+
// Restore every slot we recorded. Keyed by slot rather than dispatched on
|
|
160
|
+
// tagName: one element can have several converted slots (an <input> with
|
|
161
|
+
// both `value` and `placeholder`), and a tagName switch can only put back
|
|
162
|
+
// one of them.
|
|
163
|
+
const slots = originalValues.get(node);
|
|
164
|
+
if (slots) {
|
|
165
|
+
for (const [slot, original] of slots) {
|
|
166
|
+
if (slot === TEXT_SLOT) {
|
|
167
|
+
node.nodeValue = original;
|
|
130
168
|
}
|
|
131
|
-
else if (node
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
169
|
+
else if (node instanceof Element) {
|
|
170
|
+
if (slot === VALUE_SLOT) {
|
|
171
|
+
node.value = original;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
node.setAttribute(slot, original);
|
|
175
|
+
}
|
|
136
176
|
}
|
|
137
177
|
}
|
|
138
178
|
}
|
|
@@ -145,4 +185,3 @@ export function HTMLConverter(options) {
|
|
|
145
185
|
}
|
|
146
186
|
return { convert, restore };
|
|
147
187
|
}
|
|
148
|
-
//# sourceMappingURL=html-converter.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* OpenCC JS - Main Entry Point
|
|
3
3
|
*/
|
|
4
|
-
export { Trie, ConverterFactory, CustomConverter, ProtectedConverter, parseOpenCCDict } from "./core.js";
|
|
4
|
+
export { Trie, ConverterFactory, ConverterFactoryWithSegmentation, CustomConverter, ProtectedConverter, normalizeCompatibilityIdeographs, parseOpenCCDict, reverseDictString, } from "./core.js";
|
|
5
5
|
export type { DictLike, DictGroup } from "./core.js";
|
|
6
6
|
export { ConverterBuilder, createConverter, getDictFiles } from "./converter.js";
|
|
7
7
|
export type { ConverterOptions, LocalePreset } from "./converter.js";
|
|
8
8
|
export { HTMLConverter } from "./html-converter.js";
|
|
9
9
|
export type { HTMLConverterOptions } from "./html-converter.js";
|
|
10
|
-
export { variants2standard, standard2variants, allDictFiles } from "./presets.js";
|
|
10
|
+
export { variants2standard, standard2variants, allDictFiles, segmentationDictsFor } from "./presets.js";
|
|
11
11
|
export type { LocaleCode } from "./presets.js";
|
|
12
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* OpenCC JS - Main Entry Point
|
|
3
3
|
*/
|
|
4
|
-
// Core exports
|
|
5
|
-
|
|
4
|
+
// Core exports.
|
|
5
|
+
//
|
|
6
|
+
// `ConverterFactoryWithSegmentation` and `normalizeCompatibilityIdeographs`
|
|
7
|
+
// belong here next to `ConverterFactory`, not only on the `./core` subpath:
|
|
8
|
+
// anyone assembling a custom chain from this entry would otherwise find the
|
|
9
|
+
// unsegmented factory as the only option and silently lose regional-phrase
|
|
10
|
+
// boundary handling (他优化了 → 他最佳化了). Exporting the plain factory but
|
|
11
|
+
// not the segmented one reads as "segmentation is internal", when it is in
|
|
12
|
+
// fact what every preset uses.
|
|
13
|
+
export { Trie, ConverterFactory, ConverterFactoryWithSegmentation, CustomConverter, ProtectedConverter, normalizeCompatibilityIdeographs, parseOpenCCDict, reverseDictString, } from "./core.js";
|
|
6
14
|
// Converter exports
|
|
7
15
|
export { ConverterBuilder, createConverter, getDictFiles } from "./converter.js";
|
|
8
16
|
// HTML Converter exports
|
|
9
17
|
export { HTMLConverter } from "./html-converter.js";
|
|
10
|
-
// Preset exports
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
// Preset exports. `segmentationDictsFor` ships too — a custom chain needs it
|
|
19
|
+
// to know WHICH dict to cut on, and getting that wrong (cutting an `s2*`
|
|
20
|
+
// chain on a traditional-keyed dict) fails silently.
|
|
21
|
+
export { variants2standard, standard2variants, allDictFiles, segmentationDictsFor } from "./presets.js";
|
package/dist/presets.d.ts
CHANGED
|
@@ -36,6 +36,50 @@ export declare const standard2variants: Record<string, string[]>;
|
|
|
36
36
|
* All dictionary file names
|
|
37
37
|
*/
|
|
38
38
|
export declare const allDictFiles: string[];
|
|
39
|
+
/**
|
|
40
|
+
* Which dictionary to cut the input on before running the conversion chain,
|
|
41
|
+
* or `[]` for "don't segment".
|
|
42
|
+
*
|
|
43
|
+
* Segmentation only earns its cost when the chain has a **second** step that
|
|
44
|
+
* could match across boundaries the first step set — see
|
|
45
|
+
* `ConverterFactoryWithSegmentation`. OpenCC therefore declares a
|
|
46
|
+
* `segmentation` on exactly eight configs (`s2tw`, `s2twp`, `s2hk`, `s2hkp`,
|
|
47
|
+
* `tw2s`, `tw2sp`, `hk2s`, `hk2sp`) and on none of the single-step ones
|
|
48
|
+
* (`s2t`, `t2tw`, `tw2t`, `t2s`, `t2hk`, `hk2t`, `jp2t`, `t2jp`). This
|
|
49
|
+
* function reproduces that set rather than inventing a broader rule:
|
|
50
|
+
*
|
|
51
|
+
* - **cn → tw/twp/hk/hkp** cuts on `STPhrases`.
|
|
52
|
+
* - **tw/twp/hk/hkp → cn** cuts on `TSPhrases`.
|
|
53
|
+
* - everything else, including anything touching `t` or `jp`, does not cut.
|
|
54
|
+
*
|
|
55
|
+
* The direction of the dictionary is the part worth remembering: it is the
|
|
56
|
+
* **raw input** being cut, so the keys must be in the input's script.
|
|
57
|
+
* `s2*` cuts on the simplified-keyed `STPhrases`; `*2s` cuts on the
|
|
58
|
+
* traditional-keyed `TSPhrases` — which lives in the chain's *second* step,
|
|
59
|
+
* not its first. Picking "the first step's phrase dict" looks tidier and is
|
|
60
|
+
* wrong for `tw2s`.
|
|
61
|
+
*
|
|
62
|
+
* `jp` is excluded deliberately: Shinjitai conversion has no phrase table
|
|
63
|
+
* keyed in the source script, and OpenCC declares no segmentation for it.
|
|
64
|
+
*
|
|
65
|
+
* ## The second segmentation dict: `STPhrases_GeneratedFromRegionalPhrases`
|
|
66
|
+
*
|
|
67
|
+
* OpenCC's `s2*` configs segment on **two** dictionaries — `STPhrases` plus a
|
|
68
|
+
* generated one that maps each regional term's simplified projection to its
|
|
69
|
+
* plain traditional form (`出租车 出租車`), keeping the term whole through
|
|
70
|
+
* segmentation so the regional table can replace it as a unit. Without it
|
|
71
|
+
* `出租车司机` under-applies to `出租車司機` where OpenCC gives `計程車司機`.
|
|
72
|
+
*
|
|
73
|
+
* We once thought its filter was unrecoverable (a naive derivation yielded 276
|
|
74
|
+
* extra entries) and documented it as a known gap. The actual rule was found
|
|
75
|
+
* by reading OpenCC's `generate_st_phrases_from_regional_phrases.py`: project
|
|
76
|
+
* the keys of HKPhrases + TWPhrases through t2s and **drop projections shorter
|
|
77
|
+
* than 3 code points** — short keys would split longer Simplified words before
|
|
78
|
+
* STPhrases could match them. That one filter reproduces all 508 upstream
|
|
79
|
+
* entries exactly. The sync script now generates the dict; see
|
|
80
|
+
* `generateRegionalStPhrases` comments there for the full recipe.
|
|
81
|
+
*/
|
|
82
|
+
export declare function segmentationDictsFor(from: LocaleCode, to: LocaleCode): string[];
|
|
39
83
|
/**
|
|
40
84
|
* How the custom CNTWPhrases dict should be applied to a conversion:
|
|
41
85
|
* `"forward"` as shipped (cn keys → tw values), `"reverse"` swapped, or `null`
|
|
@@ -69,4 +113,3 @@ export type PhraseDictDirection = "forward" | "reverse" | null;
|
|
|
69
113
|
* score `null` (e.g. `cn → hk`) still get no dict.
|
|
70
114
|
*/
|
|
71
115
|
export declare function phraseDictDirection(from: LocaleCode, to: LocaleCode, loadCustomPhrases?: boolean): PhraseDictDirection;
|
|
72
|
-
//# sourceMappingURL=presets.d.ts.map
|
package/dist/presets.js
CHANGED
|
@@ -12,7 +12,12 @@
|
|
|
12
12
|
* `tw2sp` / `hk2sp` in OpenCC's data/config.
|
|
13
13
|
*/
|
|
14
14
|
export const variants2standard = {
|
|
15
|
-
|
|
15
|
+
// The generated dict sits between: upstream's group is
|
|
16
|
+
// [STPhrases ∪ Generated] short-circuit STCharacters, so in last-write-wins
|
|
17
|
+
// order STPhrases stays highest. Its value is the point — 出租车 must become
|
|
18
|
+
// 出租車 as a pinned unit (char-wise conversion could pick wrong variants for
|
|
19
|
+
// ambiguous characters), and segmentation needs it as a boundary entry.
|
|
20
|
+
cn: ["STCharacters", "STPhrases_GeneratedFromRegionalPhrases", "STPhrases"],
|
|
16
21
|
hk: ["HKVariantsRev", "HKVariantsRevPhrases"],
|
|
17
22
|
hkp: ["HKVariantsRev", "HKVariantsRevPhrases", "HKPhrasesRev"],
|
|
18
23
|
tw: ["TWVariantsRev", "TWVariantsRevPhrases"],
|
|
@@ -39,6 +44,58 @@ export const standard2variants = {
|
|
|
39
44
|
* All dictionary file names
|
|
40
45
|
*/
|
|
41
46
|
export const allDictFiles = [...new Set([...Object.values(variants2standard).flat(), ...Object.values(standard2variants).flat()])];
|
|
47
|
+
/** Traditional variants that carry their own regional vocabulary tables. */
|
|
48
|
+
const REGIONAL_VARIANTS = new Set(["tw", "twp", "hk", "hkp"]);
|
|
49
|
+
/**
|
|
50
|
+
* Which dictionary to cut the input on before running the conversion chain,
|
|
51
|
+
* or `[]` for "don't segment".
|
|
52
|
+
*
|
|
53
|
+
* Segmentation only earns its cost when the chain has a **second** step that
|
|
54
|
+
* could match across boundaries the first step set — see
|
|
55
|
+
* `ConverterFactoryWithSegmentation`. OpenCC therefore declares a
|
|
56
|
+
* `segmentation` on exactly eight configs (`s2tw`, `s2twp`, `s2hk`, `s2hkp`,
|
|
57
|
+
* `tw2s`, `tw2sp`, `hk2s`, `hk2sp`) and on none of the single-step ones
|
|
58
|
+
* (`s2t`, `t2tw`, `tw2t`, `t2s`, `t2hk`, `hk2t`, `jp2t`, `t2jp`). This
|
|
59
|
+
* function reproduces that set rather than inventing a broader rule:
|
|
60
|
+
*
|
|
61
|
+
* - **cn → tw/twp/hk/hkp** cuts on `STPhrases`.
|
|
62
|
+
* - **tw/twp/hk/hkp → cn** cuts on `TSPhrases`.
|
|
63
|
+
* - everything else, including anything touching `t` or `jp`, does not cut.
|
|
64
|
+
*
|
|
65
|
+
* The direction of the dictionary is the part worth remembering: it is the
|
|
66
|
+
* **raw input** being cut, so the keys must be in the input's script.
|
|
67
|
+
* `s2*` cuts on the simplified-keyed `STPhrases`; `*2s` cuts on the
|
|
68
|
+
* traditional-keyed `TSPhrases` — which lives in the chain's *second* step,
|
|
69
|
+
* not its first. Picking "the first step's phrase dict" looks tidier and is
|
|
70
|
+
* wrong for `tw2s`.
|
|
71
|
+
*
|
|
72
|
+
* `jp` is excluded deliberately: Shinjitai conversion has no phrase table
|
|
73
|
+
* keyed in the source script, and OpenCC declares no segmentation for it.
|
|
74
|
+
*
|
|
75
|
+
* ## The second segmentation dict: `STPhrases_GeneratedFromRegionalPhrases`
|
|
76
|
+
*
|
|
77
|
+
* OpenCC's `s2*` configs segment on **two** dictionaries — `STPhrases` plus a
|
|
78
|
+
* generated one that maps each regional term's simplified projection to its
|
|
79
|
+
* plain traditional form (`出租车 出租車`), keeping the term whole through
|
|
80
|
+
* segmentation so the regional table can replace it as a unit. Without it
|
|
81
|
+
* `出租车司机` under-applies to `出租車司機` where OpenCC gives `計程車司機`.
|
|
82
|
+
*
|
|
83
|
+
* We once thought its filter was unrecoverable (a naive derivation yielded 276
|
|
84
|
+
* extra entries) and documented it as a known gap. The actual rule was found
|
|
85
|
+
* by reading OpenCC's `generate_st_phrases_from_regional_phrases.py`: project
|
|
86
|
+
* the keys of HKPhrases + TWPhrases through t2s and **drop projections shorter
|
|
87
|
+
* than 3 code points** — short keys would split longer Simplified words before
|
|
88
|
+
* STPhrases could match them. That one filter reproduces all 508 upstream
|
|
89
|
+
* entries exactly. The sync script now generates the dict; see
|
|
90
|
+
* `generateRegionalStPhrases` comments there for the full recipe.
|
|
91
|
+
*/
|
|
92
|
+
export function segmentationDictsFor(from, to) {
|
|
93
|
+
if (from === "cn" && REGIONAL_VARIANTS.has(to))
|
|
94
|
+
return ["STPhrases", "STPhrases_GeneratedFromRegionalPhrases"];
|
|
95
|
+
if (to === "cn" && REGIONAL_VARIANTS.has(from))
|
|
96
|
+
return ["TSPhrases"];
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
42
99
|
/** Locales that carry Taiwan vocabulary. `t`/`hk`/`jp` are traditional but not Taiwanese. */
|
|
43
100
|
const isTwVocab = (locale) => locale === "tw" || locale === "twp";
|
|
44
101
|
/**
|
|
@@ -76,4 +133,3 @@ export function phraseDictDirection(from, to, loadCustomPhrases) {
|
|
|
76
133
|
return "reverse";
|
|
77
134
|
return null;
|
|
78
135
|
}
|
|
79
|
-
//# sourceMappingURL=presets.js.map
|