js-opencc 1.4.0 → 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 +118 -120
- package/README.zh-Hant.md +222 -0
- package/data/custom/CNTWPhrases.txt +6 -0
- package/dist/bundles/cn2t.d.ts +5 -4
- package/dist/bundles/cn2t.js +70 -13
- package/dist/bundles/full.d.ts +8 -3
- package/dist/bundles/full.js +42 -19
- package/dist/bundles/t2cn.d.ts +5 -4
- package/dist/bundles/t2cn.js +58 -11
- package/dist/converter.d.ts +11 -2
- package/dist/converter.js +60 -28
- package/dist/core.d.ts +120 -16
- package/dist/core.js +403 -51
- package/dist/dict/CNTWPhrases.d.ts +2 -3
- package/dist/dict/CNTWPhrases.js +2 -2
- package/dist/dict/HKPhrases.d.ts +2 -0
- package/dist/dict/HKPhrases.js +2 -0
- package/dist/dict/HKPhrasesRev.d.ts +2 -0
- package/dist/dict/HKPhrasesRev.js +2 -0
- package/dist/dict/HKVariants.d.ts +2 -3
- package/dist/dict/HKVariants.js +2 -2
- package/dist/dict/HKVariantsPhrases.d.ts +2 -0
- package/dist/dict/HKVariantsPhrases.js +2 -0
- 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 -0
- package/dist/dict/TWVariantsPhrases.js +2 -0
- 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 +5 -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 +92 -4
- package/dist/presets.js +113 -7
- package/dist/umd/cn2t.js +640 -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 +657 -126
- 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 +658 -107
- 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/HKVariants.d.ts.map +0 -1
- package/dist/dict/HKVariants.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/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/core.d.ts
CHANGED
|
@@ -14,9 +14,18 @@ export type DictGroup = DictLike[];
|
|
|
14
14
|
* Trie tree for efficient string matching and conversion
|
|
15
15
|
*/
|
|
16
16
|
export declare class Trie {
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Children, allocated on first insert.
|
|
19
|
+
*
|
|
20
|
+
* Half the nodes in a real dictionary trie are leaves (measured on
|
|
21
|
+
* STPhrases: 42 950 of 86 388, 49.7%), and an eagerly-constructed `new Map()`
|
|
22
|
+
* on every one of them is pure overhead — an empty Map is not free. Leaving
|
|
23
|
+
* this `undefined` until a child is actually added drops a Map allocation
|
|
24
|
+
* for every leaf. Read sites must therefore go through `?.get(...)`, which
|
|
25
|
+
* returns `undefined` for a leaf exactly as an empty Map's `get` would.
|
|
26
|
+
*/
|
|
27
|
+
private map?;
|
|
18
28
|
private value?;
|
|
19
|
-
constructor();
|
|
20
29
|
/**
|
|
21
30
|
* Add a word to the trie
|
|
22
31
|
* @param key - The string to match
|
|
@@ -42,11 +51,23 @@ export declare class Trie {
|
|
|
42
51
|
* not relative to `start`. Callers should check `end > start`.
|
|
43
52
|
* @param input - The string to search
|
|
44
53
|
* @param start - The index to start matching from
|
|
54
|
+
*
|
|
55
|
+
* 热路径请用 {@link matchLongestInto}(免分配变体,语义一致)——两边改其一必同步另一个。
|
|
45
56
|
*/
|
|
46
57
|
findLongestMatch(input: string, start: number): {
|
|
47
58
|
end: number;
|
|
48
59
|
value: string | undefined;
|
|
49
60
|
};
|
|
61
|
+
/**
|
|
62
|
+
* findLongestMatch 的免分配变体:结果写进调用方复用的 `out`,不造对象。
|
|
63
|
+
* 给每个位置都要探一次的热调用方用(maskWithTrie 一个位置一次——对象版
|
|
64
|
+
* 在 4 万字语料上就是 4 万次 {end,value} 分配)。语义与 findLongestMatch
|
|
65
|
+
* 完全一致,改其一必同步另一个。
|
|
66
|
+
*/
|
|
67
|
+
matchLongestInto(input: string, start: number, out: {
|
|
68
|
+
end: number;
|
|
69
|
+
value: string | undefined;
|
|
70
|
+
}): void;
|
|
50
71
|
/**
|
|
51
72
|
* Convert a string using the trie
|
|
52
73
|
* Uses longest match algorithm for optimal conversion
|
|
@@ -54,7 +75,48 @@ export declare class Trie {
|
|
|
54
75
|
* @returns The converted string
|
|
55
76
|
*/
|
|
56
77
|
convert(input: string): string;
|
|
78
|
+
/**
|
|
79
|
+
* Split input into segments on this trie's entries, longest match first.
|
|
80
|
+
*
|
|
81
|
+
* Every matched entry becomes its own segment, and the runs between them are
|
|
82
|
+
* emitted as segments too, so `segments.join("")` reconstructs the input
|
|
83
|
+
* exactly. Nothing is converted here — only cut.
|
|
84
|
+
*
|
|
85
|
+
* Exists for {@link ConverterFactoryWithSegmentation}; see there for why the
|
|
86
|
+
* cut changes results.
|
|
87
|
+
*
|
|
88
|
+
* @param input - The string to split
|
|
89
|
+
* @returns Segments whose concatenation equals `input`
|
|
90
|
+
*/
|
|
91
|
+
segment(input: string): string[];
|
|
57
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Fold CJK Compatibility Ideographs to their unified equivalents.
|
|
95
|
+
*
|
|
96
|
+
* OpenCC does this with a 1002-entry dictionary
|
|
97
|
+
* (`CJK_Compatibility_Ideographs`) loaded as a normalization step by every
|
|
98
|
+
* config. **We ship no such table.** Those mappings are canonical singleton
|
|
99
|
+
* decompositions in the Unicode standard, which is precisely what
|
|
100
|
+
* `String.prototype.normalize` performs — checked entry by entry against
|
|
101
|
+
* opencc-js's copy: all 1002 agree, and NFC folds nothing the table misses.
|
|
102
|
+
* So ~8.7 KB of data becomes a call into the engine's own ICU, which has the
|
|
103
|
+
* side benefit of tracking new Unicode versions without a resync.
|
|
104
|
+
*
|
|
105
|
+
* Two deliberate details:
|
|
106
|
+
*
|
|
107
|
+
* 1. **NFC, not NFKC.** NFKC would also fold full-width forms (`,`→`,`,
|
|
108
|
+
* `A`→`A`) and enclosed characters (`㈱`→`(株)`). In Chinese prose that is
|
|
109
|
+
* destructive: full-width punctuation is correct typography, not a
|
|
110
|
+
* compatibility artifact.
|
|
111
|
+
* 2. **Only the two compatibility blocks are touched**, not the whole string.
|
|
112
|
+
* Whole-string NFC would also compose combining sequences the caller never
|
|
113
|
+
* asked us to touch. The pre-test means ordinary text — which has no
|
|
114
|
+
* compatibility ideographs at all — is returned as-is with no allocation.
|
|
115
|
+
*
|
|
116
|
+
* @param input - The string to normalize
|
|
117
|
+
* @returns The string with compatibility ideographs folded
|
|
118
|
+
*/
|
|
119
|
+
export declare function normalizeCompatibilityIdeographs(input: string): string;
|
|
58
120
|
/**
|
|
59
121
|
* Create a converter from multiple dictionary groups
|
|
60
122
|
* Each dictionary group is applied in sequence
|
|
@@ -62,27 +124,70 @@ export declare class Trie {
|
|
|
62
124
|
* @returns Converter function
|
|
63
125
|
*/
|
|
64
126
|
export declare function ConverterFactory(...dictGroups: DictGroup[]): (input: string) => string;
|
|
127
|
+
/**
|
|
128
|
+
* Like {@link ConverterFactory}, but cuts the input into segments first and
|
|
129
|
+
* runs every conversion step **inside each segment** instead of across the
|
|
130
|
+
* whole string.
|
|
131
|
+
*
|
|
132
|
+
* ## What this is for
|
|
133
|
+
*
|
|
134
|
+
* With a two-step chain (`cn → t`, then `t → tw`), the second step sees text
|
|
135
|
+
* the first step produced. Without segmentation it can match across a word
|
|
136
|
+
* boundary the first step established, and the regional vocabulary tables
|
|
137
|
+
* (`TWPhrases`, `HKPhrases`) then fire in places OpenCC does not fire them.
|
|
138
|
+
* Measured against OpenCC's own output on 1707 phrases in context, dropping
|
|
139
|
+
* segmentation changes 21 of them — always by over-applying:
|
|
140
|
+
*
|
|
141
|
+
* ```text
|
|
142
|
+
* 他优化了 OpenCC 他優化了 no segmentation 他最佳化了
|
|
143
|
+
* 他函数了 OpenCC 他函數了 no segmentation 他函式了
|
|
144
|
+
* 他宽带了 OpenCC 他寬帶了 no segmentation 他寬頻了
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* Note the bare phrase still converts (`优化` → `最佳化`) either way. It is
|
|
148
|
+
* only *in context* that the boundary matters, which is what makes the bug
|
|
149
|
+
* easy to miss with word-list tests.
|
|
150
|
+
*
|
|
151
|
+
* ## Which dictionary to cut on
|
|
152
|
+
*
|
|
153
|
+
* The segmentation dictionary must be keyed in the **input's** script, since
|
|
154
|
+
* it is the raw input being cut: OpenCC uses `STPhrases` for `s2*` and
|
|
155
|
+
* `TSPhrases` for `*2s`. {@link segmentationDictsFor} encodes that choice.
|
|
156
|
+
*
|
|
157
|
+
* ## Cost
|
|
158
|
+
*
|
|
159
|
+
* One extra trie walk plus per-segment string handling — roughly 2× the
|
|
160
|
+
* conversion time (0.73 ms → 1.45 ms on a 15 000-character chapter, which cut
|
|
161
|
+
* into ~5200 segments). Only chains with two or more steps need it, and
|
|
162
|
+
* {@link segmentationDictsFor} returns nothing for the rest, so single-step
|
|
163
|
+
* conversions keep the cheap path.
|
|
164
|
+
*
|
|
165
|
+
* @param segmentation - Dictionary group to cut on, or `null` for no cutting
|
|
166
|
+
* @param dictGroups - Conversion steps, applied in order within each segment
|
|
167
|
+
* @returns Converter function
|
|
168
|
+
*/
|
|
169
|
+
export declare function ConverterFactoryWithSegmentation(segmentation: DictGroup | null, ...dictGroups: DictGroup[]): (input: string) => string;
|
|
65
170
|
/**
|
|
66
171
|
* Create a custom converter with user-defined dictionary
|
|
67
172
|
* @param dict - Custom dictionary entries
|
|
68
173
|
* @returns Converter function
|
|
69
174
|
*/
|
|
70
175
|
export declare function CustomConverter(dict: DictLike): (input: string) => string;
|
|
176
|
+
export declare function ProtectedConverter(protectedDict: DictLike, innerConvert: (input: string) => string): (input: string) => string;
|
|
71
177
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* through unchanged.
|
|
178
|
+
* Reverse a packed dict string ("k1 v1|k2 v2") into `[value, key][]` entries.
|
|
179
|
+
* The value is everything after the FIRST space (values may contain spaces,
|
|
180
|
+
* e.g. 二维码 → "QR Code") — same rule as `Trie.loadDict`'s string form.
|
|
181
|
+
*
|
|
182
|
+
* Collision policy, identical to `reverseEntries` in scripts/sync-opencc.ts:
|
|
183
|
+
* when several keys share one value (U盘/优盘 → 隨身碟) the FIRST key wins,
|
|
184
|
+
* because dicts list the preferred term first and the trie is last-wins — so
|
|
185
|
+
* without this the trailing synonym takes over (隨身碟 → 优盘). An identity pair
|
|
186
|
+
* outranks that, leaving the term untouched rather than guessing a synonym.
|
|
187
|
+
*
|
|
188
|
+
* Entries with no separator are skipped, as `Trie.loadDict` does.
|
|
84
189
|
*/
|
|
85
|
-
export declare function
|
|
190
|
+
export declare function reverseDictString(data: string): string[][];
|
|
86
191
|
/**
|
|
87
192
|
* Parse OpenCC-format dictionary text into `[key, value][]` — the shape
|
|
88
193
|
* accepted by `protectedDict`, `CustomConverter`, and `ConverterFactory`.
|
|
@@ -107,4 +212,3 @@ export declare function ProtectedConverter(protectedDict: DictLike, innerConvert
|
|
|
107
212
|
* @returns Array of `[key, value]` pairs ready to pass to `protectedDict`
|
|
108
213
|
*/
|
|
109
214
|
export declare function parseOpenCCDict(text: string): string[][];
|
|
110
|
-
//# sourceMappingURL=core.d.ts.map
|