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.
Files changed (126) hide show
  1. package/README.md +118 -120
  2. package/README.zh-Hant.md +222 -0
  3. package/data/custom/CNTWPhrases.txt +6 -0
  4. package/dist/bundles/cn2t.d.ts +5 -4
  5. package/dist/bundles/cn2t.js +70 -13
  6. package/dist/bundles/full.d.ts +8 -3
  7. package/dist/bundles/full.js +42 -19
  8. package/dist/bundles/t2cn.d.ts +5 -4
  9. package/dist/bundles/t2cn.js +58 -11
  10. package/dist/converter.d.ts +11 -2
  11. package/dist/converter.js +60 -28
  12. package/dist/core.d.ts +120 -16
  13. package/dist/core.js +403 -51
  14. package/dist/dict/CNTWPhrases.d.ts +2 -3
  15. package/dist/dict/CNTWPhrases.js +2 -2
  16. package/dist/dict/HKPhrases.d.ts +2 -0
  17. package/dist/dict/HKPhrases.js +2 -0
  18. package/dist/dict/HKPhrasesRev.d.ts +2 -0
  19. package/dist/dict/HKPhrasesRev.js +2 -0
  20. package/dist/dict/HKVariants.d.ts +2 -3
  21. package/dist/dict/HKVariants.js +2 -2
  22. package/dist/dict/HKVariantsPhrases.d.ts +2 -0
  23. package/dist/dict/HKVariantsPhrases.js +2 -0
  24. package/dist/dict/HKVariantsRev.d.ts +2 -3
  25. package/dist/dict/HKVariantsRev.js +2 -2
  26. package/dist/dict/HKVariantsRevPhrases.d.ts +2 -3
  27. package/dist/dict/HKVariantsRevPhrases.js +2 -2
  28. package/dist/dict/JPShinjitaiCharacters.d.ts +2 -3
  29. package/dist/dict/JPShinjitaiCharacters.js +2 -2
  30. package/dist/dict/JPShinjitaiCharactersRev.d.ts +2 -3
  31. package/dist/dict/JPShinjitaiCharactersRev.js +2 -2
  32. package/dist/dict/JPShinjitaiPhrases.d.ts +2 -3
  33. package/dist/dict/JPShinjitaiPhrases.js +2 -2
  34. package/dist/dict/STCharacters.d.ts +2 -3
  35. package/dist/dict/STCharacters.js +2 -2
  36. package/dist/dict/STPhrases.d.ts +2 -3
  37. package/dist/dict/STPhrases.js +2 -2
  38. package/dist/dict/STPhrases_GeneratedFromRegionalPhrases.d.ts +2 -0
  39. package/dist/dict/STPhrases_GeneratedFromRegionalPhrases.js +2 -0
  40. package/dist/dict/TSCharacters.d.ts +2 -3
  41. package/dist/dict/TSCharacters.js +2 -2
  42. package/dist/dict/TSPhrases.d.ts +2 -3
  43. package/dist/dict/TSPhrases.js +2 -2
  44. package/dist/dict/TWPhrases.d.ts +2 -3
  45. package/dist/dict/TWPhrases.js +2 -2
  46. package/dist/dict/TWPhrasesRev.d.ts +2 -3
  47. package/dist/dict/TWPhrasesRev.js +2 -2
  48. package/dist/dict/TWVariants.d.ts +2 -3
  49. package/dist/dict/TWVariants.js +2 -2
  50. package/dist/dict/TWVariantsPhrases.d.ts +2 -0
  51. package/dist/dict/TWVariantsPhrases.js +2 -0
  52. package/dist/dict/TWVariantsRev.d.ts +2 -3
  53. package/dist/dict/TWVariantsRev.js +2 -2
  54. package/dist/dict/TWVariantsRevPhrases.d.ts +2 -3
  55. package/dist/dict/TWVariantsRevPhrases.js +2 -2
  56. package/dist/dict/index.d.ts +0 -1
  57. package/dist/dict/index.js +5 -1
  58. package/dist/html-converter.d.ts +0 -1
  59. package/dist/html-converter.js +65 -26
  60. package/dist/index.d.ts +2 -3
  61. package/dist/index.js +14 -5
  62. package/dist/presets.d.ts +92 -4
  63. package/dist/presets.js +113 -7
  64. package/dist/umd/cn2t.js +640 -102
  65. package/dist/umd/cn2t.js.map +1 -1
  66. package/dist/umd/cn2t.min.js +1 -1
  67. package/dist/umd/cn2t.min.js.map +1 -1
  68. package/dist/umd/full.js +657 -126
  69. package/dist/umd/full.js.map +1 -1
  70. package/dist/umd/full.min.js +1 -1
  71. package/dist/umd/full.min.js.map +1 -1
  72. package/dist/umd/t2cn.js +658 -107
  73. package/dist/umd/t2cn.js.map +1 -1
  74. package/dist/umd/t2cn.min.js +1 -1
  75. package/dist/umd/t2cn.min.js.map +1 -1
  76. package/package.json +2 -1
  77. package/dist/bundles/cn2t.d.ts.map +0 -1
  78. package/dist/bundles/cn2t.js.map +0 -1
  79. package/dist/bundles/full.d.ts.map +0 -1
  80. package/dist/bundles/full.js.map +0 -1
  81. package/dist/bundles/t2cn.d.ts.map +0 -1
  82. package/dist/bundles/t2cn.js.map +0 -1
  83. package/dist/converter.d.ts.map +0 -1
  84. package/dist/converter.js.map +0 -1
  85. package/dist/core.d.ts.map +0 -1
  86. package/dist/core.js.map +0 -1
  87. package/dist/dict/CNTWPhrases.d.ts.map +0 -1
  88. package/dist/dict/CNTWPhrases.js.map +0 -1
  89. package/dist/dict/HKVariants.d.ts.map +0 -1
  90. package/dist/dict/HKVariants.js.map +0 -1
  91. package/dist/dict/HKVariantsRev.d.ts.map +0 -1
  92. package/dist/dict/HKVariantsRev.js.map +0 -1
  93. package/dist/dict/HKVariantsRevPhrases.d.ts.map +0 -1
  94. package/dist/dict/HKVariantsRevPhrases.js.map +0 -1
  95. package/dist/dict/JPShinjitaiCharacters.d.ts.map +0 -1
  96. package/dist/dict/JPShinjitaiCharacters.js.map +0 -1
  97. package/dist/dict/JPShinjitaiCharactersRev.d.ts.map +0 -1
  98. package/dist/dict/JPShinjitaiCharactersRev.js.map +0 -1
  99. package/dist/dict/JPShinjitaiPhrases.d.ts.map +0 -1
  100. package/dist/dict/JPShinjitaiPhrases.js.map +0 -1
  101. package/dist/dict/STCharacters.d.ts.map +0 -1
  102. package/dist/dict/STCharacters.js.map +0 -1
  103. package/dist/dict/STPhrases.d.ts.map +0 -1
  104. package/dist/dict/STPhrases.js.map +0 -1
  105. package/dist/dict/TSCharacters.d.ts.map +0 -1
  106. package/dist/dict/TSCharacters.js.map +0 -1
  107. package/dist/dict/TSPhrases.d.ts.map +0 -1
  108. package/dist/dict/TSPhrases.js.map +0 -1
  109. package/dist/dict/TWPhrases.d.ts.map +0 -1
  110. package/dist/dict/TWPhrases.js.map +0 -1
  111. package/dist/dict/TWPhrasesRev.d.ts.map +0 -1
  112. package/dist/dict/TWPhrasesRev.js.map +0 -1
  113. package/dist/dict/TWVariants.d.ts.map +0 -1
  114. package/dist/dict/TWVariants.js.map +0 -1
  115. package/dist/dict/TWVariantsRev.d.ts.map +0 -1
  116. package/dist/dict/TWVariantsRev.js.map +0 -1
  117. package/dist/dict/TWVariantsRevPhrases.d.ts.map +0 -1
  118. package/dist/dict/TWVariantsRevPhrases.js.map +0 -1
  119. package/dist/dict/index.d.ts.map +0 -1
  120. package/dist/dict/index.js.map +0 -1
  121. package/dist/html-converter.d.ts.map +0 -1
  122. package/dist/html-converter.js.map +0 -1
  123. package/dist/index.d.ts.map +0 -1
  124. package/dist/index.js.map +0 -1
  125. package/dist/presets.d.ts.map +0 -1
  126. 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
- private map;
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
- * Wrap an inner converter with a protected dictionary layer.
73
- * Strings matching `protectedDict` are masked with PUA placeholders before
74
- * inner conversion runs, then restored afterward — guaranteeing the inner
75
- * dictionaries never see or modify them (hard override).
76
- *
77
- * Identical target values reuse the same placeholder char to economize the
78
- * 6400-slot PUA range; if more than 6400 distinct target values appear, a
79
- * RangeError is thrown.
80
- *
81
- * Safe to nest: each layer scans its input for existing PUA codepoints (from
82
- * outer layers) and allocates around them. PUA chars from outer layers pass
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 ProtectedConverter(protectedDict: DictLike, innerConvert: (input: string) => string): (input: string) => string;
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