to-hakka 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +205 -2
- package/bin/to-hakka.js +175 -0
- package/data/huiyang.json +1 -0
- package/package.json +51 -4
- package/src/converter.js +548 -0
- package/src/index.js +64 -0
- package/src/ipa.js +163 -0
- package/types/index.d.ts +191 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HakkaCL Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,206 @@
|
|
|
1
|
-
#
|
|
1
|
+
# to-hakka
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Chinese text to **Standard Huiyang Hakka** romanization in
|
|
4
|
+
Hakka Pinyin System Extended (H++), with IPA output. Pure ESM, zero runtime dependencies, Unicode
|
|
5
|
+
code-point safe.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```shell
|
|
10
|
+
npm install to-hakka
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires Node.js 18 or newer. The package also runs in modern browsers and
|
|
14
|
+
bundlers that support ESM and JSON module imports.
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import {
|
|
20
|
+
getHakka,
|
|
21
|
+
getHakkaText,
|
|
22
|
+
getHakkaList,
|
|
23
|
+
getHakkaCandidates,
|
|
24
|
+
getIpa,
|
|
25
|
+
getIpaText,
|
|
26
|
+
customize,
|
|
27
|
+
} from "to-hakka";
|
|
28
|
+
|
|
29
|
+
getHakka("日頭月光");
|
|
30
|
+
// "ngid7 teu2 ngied8 gong1"
|
|
31
|
+
|
|
32
|
+
getHakkaText("日頭,食飯!");
|
|
33
|
+
// "日(ngid7)頭(teu2),食(sid8)飯(fan5)!"
|
|
34
|
+
|
|
35
|
+
getHakkaList("Hello 客");
|
|
36
|
+
// [["H", null], ["e", null], ["l", null], ["l", null], ["o", null],
|
|
37
|
+
// [" ", null], ["客", "hag7"]]
|
|
38
|
+
|
|
39
|
+
getHakkaCandidates("分數");
|
|
40
|
+
// [["分", ["fun5", "fun1", "bin1", "bun1"]], ["數", ["su5"]]]
|
|
41
|
+
|
|
42
|
+
getIpa("日頭月光");
|
|
43
|
+
// "ŋit7 tʰeu2 ŋiet8 koŋ1"
|
|
44
|
+
|
|
45
|
+
getIpa("日頭月光", "value");
|
|
46
|
+
// "ŋit¹ tʰeu¹¹ ŋiet⁵ koŋ⁴⁴"
|
|
47
|
+
|
|
48
|
+
getIpaText("唔好食", "chao");
|
|
49
|
+
// "唔[m̩˦˦]好[hau˧˩]食[sit˥]"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The default export contains the same functions:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
import ToHakkaAPI from "to-hakka";
|
|
56
|
+
|
|
57
|
+
ToHakkaAPI.getHakka("𠊎係客家人");
|
|
58
|
+
// "ngai2 he5 hag7 ga1 ngin2"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API
|
|
62
|
+
|
|
63
|
+
| Function | Returns | Description |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| `getHakka(text)` | `string` | Space-separated H++ syllables for recognized characters. |
|
|
66
|
+
| `getHakkaText(text)` | `string` | Input text annotated inline as `字(hpp)`; unrecognized characters are kept as-is. |
|
|
67
|
+
| `getHakkaList(text)` | `[string, string \| null][]` | Per-character `[char, primary pronunciation]` pairs. |
|
|
68
|
+
| `getHakkaCandidates(text)` | `[string, string[]][]` | Per-character `[char, candidate pronunciations]`; the first candidate is the primary reading. |
|
|
69
|
+
| `getIpa(text, toneStyle?)` | `string` | Space-separated IPA syllables. |
|
|
70
|
+
| `getIpaText(text, toneStyle?)` | `string` | Input text annotated inline as `字[ipa]`. |
|
|
71
|
+
| `getIpaList(text, toneStyle?)` | `[string, string \| null][]` | Per-character `[char, ipa]` pairs. |
|
|
72
|
+
| `customize(entries)` | `ToHakka` | New converter with custom word/character entries. |
|
|
73
|
+
| `hppToIpa(syllable, toneStyle?)` | `string` | Convert one H++ syllable to IPA. |
|
|
74
|
+
| `HUIYANG_TONE_VALUES` | `Readonly<Record<string, string>>` | Tone class to tone value mapping (`1 -> "44"`, etc.). |
|
|
75
|
+
| `VERSION` | `string` | Package version. |
|
|
76
|
+
| `ToHakka` | `class` | Converter class for custom tables and reusable instances. |
|
|
77
|
+
|
|
78
|
+
All result pairs and candidate arrays follow input code points, so astral
|
|
79
|
+
characters such as `𠊎` (U+2028E) count as one character.
|
|
80
|
+
|
|
81
|
+
### Tone styles
|
|
82
|
+
|
|
83
|
+
`toneStyle` accepts:
|
|
84
|
+
|
|
85
|
+
| Value | Example | Output |
|
|
86
|
+
| --- | --- | --- |
|
|
87
|
+
| `"class"` (default) | `hppToIpa("tien1")` | `tʰien1` |
|
|
88
|
+
| `"value"` | `hppToIpa("tien1", "value")` | `tʰien⁴⁴` |
|
|
89
|
+
| `"chao"` | `hppToIpa("tien1", "chao")` | `tʰien˦˦` |
|
|
90
|
+
|
|
91
|
+
An unsupported tone style throws a `RangeError`.
|
|
92
|
+
|
|
93
|
+
## Customization
|
|
94
|
+
|
|
95
|
+
`customize` returns a new immutable converter and leaves the default converter
|
|
96
|
+
untouched. Set a value to `null` (or `undefined`) to remove an entry. Removing
|
|
97
|
+
a word falls back to shorter words and individual characters; removing a
|
|
98
|
+
character entry simply leaves that character unannotated.
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
import { customize } from "to-hakka";
|
|
102
|
+
|
|
103
|
+
const custom = customize({
|
|
104
|
+
唔係: "m2 he5",
|
|
105
|
+
客家話: "hag7 ga1 fa5",
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
custom.getHakka("唔係客家話");
|
|
109
|
+
// "m2 he5 hag7 ga1 fa5"
|
|
110
|
+
|
|
111
|
+
customize({ 發夢: null }).getHakka("發夢");
|
|
112
|
+
// "fad7 mung5"
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Entries are plain objects or `Map` instances. Word values may be a single
|
|
116
|
+
pronunciation string or an array of pronunciations; the first is primary and
|
|
117
|
+
the rest are reported by `getHakkaCandidates`.
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
const variants = customize({
|
|
121
|
+
一刻: ["id7 kad7", "id7 ked7"],
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
variants.getHakkaCandidates("一刻");
|
|
125
|
+
// [["一", ["id7"]], ["刻", ["kad7", "ked7"]]]
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Converters can be chained without mutating their parents:
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
const base = customize({ 唔: "m2" });
|
|
132
|
+
const removed = base.customize({ 唔: null });
|
|
133
|
+
|
|
134
|
+
base.getHakka("唔"); // "m2"
|
|
135
|
+
removed.getHakka("唔"); // "" — the overridden character entry is removed
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Deleting an overridden entry removes it from that converter chain; it does not
|
|
139
|
+
restore the original built-in reading. Word deletions do fall back to shorter
|
|
140
|
+
words and individual characters, because those entries are still present.
|
|
141
|
+
|
|
142
|
+
For a fully custom lexicon, construct a converter directly:
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
import { ToHakka } from "to-hakka";
|
|
146
|
+
|
|
147
|
+
const converter = new ToHakka({
|
|
148
|
+
chars: { 客: ["kag7"], 人: ["ngin2"] },
|
|
149
|
+
words: { 客人: "kag7 ngin2" },
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
converter.getHakka("客人");
|
|
153
|
+
// "kag7 ngin2"
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## CLI
|
|
157
|
+
|
|
158
|
+
The package installs a `to-hakka` executable:
|
|
159
|
+
|
|
160
|
+
```shell
|
|
161
|
+
to-hakka "𠊎係客家人,今日食飯!" --inline
|
|
162
|
+
# 𠊎(ngai2)係(he5)客(hag7)家(ga1)人(ngin2),今(gim1)日(ngid7)食(sid8)飯(fan5)!
|
|
163
|
+
|
|
164
|
+
to-hakka "日頭月光" --format ipa-value
|
|
165
|
+
# ŋit¹ tʰeu¹¹ ŋiet⁵ koŋ⁴⁴
|
|
166
|
+
|
|
167
|
+
to-hakka "日頭月光" --json
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
`--json` emits an array of `{ "char": ..., "pron": ... }` objects with
|
|
171
|
+
2-space indentation.
|
|
172
|
+
|
|
173
|
+
| Option | Description |
|
|
174
|
+
| --- | --- |
|
|
175
|
+
| `--inline` | Annotate characters inline instead of printing a syllable list. |
|
|
176
|
+
| `--format hpp` | H++ output (default). |
|
|
177
|
+
| `--format ipa` | IPA with tone classes. |
|
|
178
|
+
| `--format ipa-value` | IPA with superscript tone values. |
|
|
179
|
+
| `--format ipa-chao` | IPA with Chao tone letters. |
|
|
180
|
+
| `--json` | Emit the per-character token list as JSON. |
|
|
181
|
+
| `--version` | Print `to-hakka 0.1.1`. |
|
|
182
|
+
| `--help` | Print usage information. |
|
|
183
|
+
|
|
184
|
+
## TypeScript
|
|
185
|
+
|
|
186
|
+
Type declarations are bundled and include `ToneStyle`, `HakkaPair`,
|
|
187
|
+
`HakkaCandidatePair`, `ToHakkaOptions`, and all function signatures.
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
import type { HakkaPair, ToneStyle } from "to-hakka";
|
|
191
|
+
|
|
192
|
+
const style: ToneStyle = "value";
|
|
193
|
+
const pairs: HakkaPair[] = [];
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## License
|
|
197
|
+
|
|
198
|
+
MIT
|
|
199
|
+
|
|
200
|
+
## Data attribution
|
|
201
|
+
|
|
202
|
+
Bundled character and word tables derive from
|
|
203
|
+
[HakkaCL/Hakka-Char-Tables](https://github.com/HakkaCL/Hakka-Char-Tables) and
|
|
204
|
+
[HakkaCL/Hakka-Word-Tables](https://github.com/HakkaCL/Hakka-Word-Tables)
|
|
205
|
+
(CC BY 4.0), and from
|
|
206
|
+
[hkilang/TTS](https://github.com/hkilang/TTS) (MIT).
|
package/bin/to-hakka.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Command-line interface for `to-hakka`.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* to-hakka "𠊎係客家人" --inline
|
|
8
|
+
* to-hakka "日頭月光" --format ipa-value
|
|
9
|
+
* to-hakka "日頭月光" --json
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import process from "node:process";
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
VERSION,
|
|
16
|
+
getHakka,
|
|
17
|
+
getHakkaList,
|
|
18
|
+
getHakkaText,
|
|
19
|
+
getIpa,
|
|
20
|
+
getIpaList,
|
|
21
|
+
getIpaText,
|
|
22
|
+
} from "../src/index.js";
|
|
23
|
+
|
|
24
|
+
const FORMATS = new Set(["hpp", "ipa", "ipa-value", "ipa-chao"]);
|
|
25
|
+
const IPA_STYLES = {
|
|
26
|
+
ipa: "class",
|
|
27
|
+
"ipa-value": "value",
|
|
28
|
+
"ipa-chao": "chao",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const HELP = `usage: to-hakka [-h] [--version] [--inline]
|
|
32
|
+
[--format {hpp,ipa,ipa-value,ipa-chao}] [--json]
|
|
33
|
+
text
|
|
34
|
+
|
|
35
|
+
Convert Chinese text to Standard Huiyang Hakka (H++ / IPA).
|
|
36
|
+
|
|
37
|
+
positional arguments:
|
|
38
|
+
text Input Chinese text (Traditional or Simplified)
|
|
39
|
+
|
|
40
|
+
options:
|
|
41
|
+
-h, --help show this help message and exit
|
|
42
|
+
--version show program's version number and exit
|
|
43
|
+
--inline annotate characters inline
|
|
44
|
+
--format {hpp,ipa,ipa-value,ipa-chao}
|
|
45
|
+
output phonetic format (default: hpp)
|
|
46
|
+
--json output JSON token list
|
|
47
|
+
`;
|
|
48
|
+
|
|
49
|
+
function fail(message) {
|
|
50
|
+
process.stderr.write(`to-hakka: error: ${message}\n`);
|
|
51
|
+
process.exitCode = 2;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function parseArguments(argv) {
|
|
55
|
+
let inline = false;
|
|
56
|
+
let json = false;
|
|
57
|
+
let format = "hpp";
|
|
58
|
+
let text = null;
|
|
59
|
+
let endOfOptions = false;
|
|
60
|
+
|
|
61
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
62
|
+
const argument = argv[index];
|
|
63
|
+
|
|
64
|
+
if (endOfOptions) {
|
|
65
|
+
if (text !== null) {
|
|
66
|
+
return { error: `unrecognized arguments: ${argument}` };
|
|
67
|
+
}
|
|
68
|
+
text = argument;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (argument === "--") {
|
|
73
|
+
endOfOptions = true;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (argument === "--version" || argument === "-V") {
|
|
78
|
+
return { version: true };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (argument === "--help" || argument === "-h") {
|
|
82
|
+
return { help: true };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (argument === "--inline") {
|
|
86
|
+
inline = true;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (argument === "--json") {
|
|
91
|
+
json = true;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (argument === "--format") {
|
|
96
|
+
index += 1;
|
|
97
|
+
if (index >= argv.length) {
|
|
98
|
+
return { error: "argument --format: expected one argument" };
|
|
99
|
+
}
|
|
100
|
+
format = argv[index];
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (argument.startsWith("--format=")) {
|
|
105
|
+
format = argument.slice("--format=".length);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (argument.startsWith("-") && argument !== "-") {
|
|
110
|
+
return { error: `unrecognized arguments: ${argument}` };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (text !== null) {
|
|
114
|
+
return { error: `unrecognized arguments: ${argument}` };
|
|
115
|
+
}
|
|
116
|
+
text = argument;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (!FORMATS.has(format)) {
|
|
120
|
+
return {
|
|
121
|
+
error: `argument --format: invalid choice: '${format}' (choose from 'hpp', 'hpp', 'ipa', 'ipa-value', 'ipa-chao')`,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (text === null) {
|
|
126
|
+
return { error: "the following arguments are required: text" };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { inline, json, format, text };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function main(argv) {
|
|
133
|
+
const parsed = parseArguments(argv);
|
|
134
|
+
|
|
135
|
+
if (parsed.help) {
|
|
136
|
+
process.stdout.write(HELP);
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (parsed.version) {
|
|
141
|
+
process.stdout.write(`to-hakka ${VERSION}\n`);
|
|
142
|
+
return 0;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (parsed.error) {
|
|
146
|
+
fail(parsed.error);
|
|
147
|
+
return 2;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const { inline, json, format, text } = parsed;
|
|
151
|
+
const ipaStyle = Object.hasOwn(IPA_STYLES, format) ? IPA_STYLES[format] : null;
|
|
152
|
+
|
|
153
|
+
if (json) {
|
|
154
|
+
const pairs =
|
|
155
|
+
ipaStyle === null
|
|
156
|
+
? getHakkaList(text)
|
|
157
|
+
: getIpaList(text, ipaStyle);
|
|
158
|
+
const tokens = pairs.map(([char, pron]) => ({ char, pron }));
|
|
159
|
+
process.stdout.write(`${JSON.stringify(tokens, null, 2)}\n`);
|
|
160
|
+
return 0;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let output;
|
|
164
|
+
if (ipaStyle === null) {
|
|
165
|
+
output = inline ? getHakkaText(text) : getHakka(text);
|
|
166
|
+
} else {
|
|
167
|
+
output = inline
|
|
168
|
+
? getIpaText(text, ipaStyle)
|
|
169
|
+
: getIpa(text, ipaStyle);
|
|
170
|
+
}
|
|
171
|
+
process.stdout.write(`${output}\n`);
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
process.exitCode = main(process.argv.slice(2));
|