taiwan-phonetics 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +23 -0
- data/LICENSE +19 -0
- data/NOTICES.md +47 -0
- data/README.md +297 -0
- data/lib/taiwan-phonetics.rb +3 -0
- data/lib/taiwan_phonetics/bopomofo.rb +132 -0
- data/lib/taiwan_phonetics/error.rb +34 -0
- data/lib/taiwan_phonetics/inventory.rb +105 -0
- data/lib/taiwan_phonetics/pinyin.rb +44 -0
- data/lib/taiwan_phonetics/sandhi.rb +182 -0
- data/lib/taiwan_phonetics/scheme.rb +279 -0
- data/lib/taiwan_phonetics/spellings.rb +150 -0
- data/lib/taiwan_phonetics/syllable.rb +40 -0
- data/lib/taiwan_phonetics/systems.rb +144 -0
- data/lib/taiwan_phonetics/text.rb +64 -0
- data/lib/taiwan_phonetics/version.rb +5 -0
- data/lib/taiwan_phonetics.rb +69 -0
- data/sig/taiwan_phonetics.rbs +58 -0
- metadata +108 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d82aaf0fb1b4151baab047680c3c7abc037d176cab9fad007d8fcd62ac1fcf10
|
|
4
|
+
data.tar.gz: e1f24fc7751184848cc28d74fddf6d1dea6b861e7c2a700151b86f9b4d347b6f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d12ff0930c1aad356c03f1515b2a77a857830e0ea374e5bcfe16400b52a9362ce6f813735671eebc6deca58e079564db719ba39f13b6c2234399a03476f92561
|
|
7
|
+
data.tar.gz: 2acaf45b8466b75a1d5cfe5876bbf6fd6e779f9e2c98e1b36a29117187df012be22747b4f39a527afffcf0892e4dd7a2a9ebe236d8ccbeff4b73e0e98998e0ab
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 — 2026-12-07
|
|
4
|
+
|
|
5
|
+
Initial release.
|
|
6
|
+
|
|
7
|
+
- Conversion between zhuyin, Hanyu Pinyin, Tongyong Pinyin, Wade–Giles and MPS II, in both
|
|
8
|
+
directions, with tone marks, tone numbers or no tones (`TaiwanPhonetics.convert`,
|
|
9
|
+
`.to_zhuyin`, `.to_pinyin`); system names accepted as symbols or in Chinese
|
|
10
|
+
- Reading of the forms people type: zhuyin without spaces, look-alike tone marks, 一 for ㄧ, ㄭ;
|
|
11
|
+
pinyin with digits, superscripts, breves, misplaced marks, v and u: for ü, passport spellings,
|
|
12
|
+
uncontracted finals, full-width and script letters, any apostrophe; punctuation and spacing kept
|
|
13
|
+
- Syllables with an unknown tone, for pinyin written without tones
|
|
14
|
+
- Validation against the 418 syllables of the CNS 11643 voice index (`.valid?`, `.syllable`,
|
|
15
|
+
`.syllables`, `TaiwanPhonetics::Syllable`)
|
|
16
|
+
- Tone sandhi of 一, 不 and third tones over caller-grouped words and phrases, with underlying and
|
|
17
|
+
surface tones (`.sandhi`)
|
|
18
|
+
- Shared data for other implementations: `data/syllables.json` and conversion and sandhi cases in
|
|
19
|
+
`data/cases/`, written by `rake data`
|
|
20
|
+
- RBS signatures under `sig/`
|
|
21
|
+
- The npm package of the same name in `js/`: the same API in JavaScript (`convert`, `toZhuyin`,
|
|
22
|
+
`toPinyin`, `syllables`, `syllable`, `isValid`, `sandhi`), ESM with TypeScript types, tested
|
|
23
|
+
against the same cases
|
data/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Den Patin 丁丹旭 <hi@dpat.in>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
|
6
|
+
software and associated documentation files (the "Software"), to deal in the Software
|
|
7
|
+
without restriction, including without limitation the rights to use, copy, modify, merge,
|
|
8
|
+
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
|
9
|
+
to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all copies or
|
|
12
|
+
substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
15
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
|
16
|
+
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
|
17
|
+
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
18
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
19
|
+
DEALINGS IN THE SOFTWARE.
|
data/NOTICES.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Notices
|
|
2
|
+
|
|
3
|
+
## CNS 11643 voice index
|
|
4
|
+
|
|
5
|
+
`spec/fixtures/cns11643_voice.txt` is the voice index 全字庫音檔說明文件.txt from the open data
|
|
6
|
+
file Voice.zip, version 20250113, published at <https://data.cns11643.gov.tw/opendata/Voice.zip>.
|
|
7
|
+
The toneless syllable inventory in `lib/taiwan_phonetics/inventory.rb`, `data/syllables.json` and
|
|
8
|
+
`js/tables.js` is the set of syllables this index lists.
|
|
9
|
+
|
|
10
|
+
Attribution: 數位發展部,CNS11643中文標準交換碼全字庫網站,<https://www.cns11643.gov.tw>
|
|
11
|
+
|
|
12
|
+
License: 政府資料開放授權條款-第1版 (Open Government Data License, version 1.0),
|
|
13
|
+
<https://data.gov.tw/license>
|
|
14
|
+
|
|
15
|
+
The only change is the conversion of CRLF line endings to LF and the addition of a final newline.
|
|
16
|
+
|
|
17
|
+
| File | SHA-256 |
|
|
18
|
+
| ------------------------------------ | ------------------------------------------------------------------ |
|
|
19
|
+
| Voice.zip, as downloaded | `6e6793d532e800a75df423cf337adeb2bafe0ee4fee12813c00260102e0a9efa` |
|
|
20
|
+
| 全字庫音檔說明文件.txt, as extracted | `04878e82c0e7368d90f3778f2340b8b9987efb70751b2b03368772ee67ad0e7b` |
|
|
21
|
+
| `spec/fixtures/cns11643_voice.txt` | `0da9c616b9cad1fbb6124e020cf2bb79725c67b53a99b33d6df6182d0bc22740` |
|
|
22
|
+
|
|
23
|
+
## Passport romanization table
|
|
24
|
+
|
|
25
|
+
`spec/fixtures/boca_romanization.tsv` holds the rows of 護照外文姓名拼音對照表 (zhuyin, Wade–Giles,
|
|
26
|
+
MPS II, Tongyong Pinyin, Hanyu Pinyin), retrieved on 2026-12-06 from
|
|
27
|
+
<https://www.boca.gov.tw/cp-2-4226-c0eff-1.html>. The table cells are copied unchanged into
|
|
28
|
+
tab-separated lines; the repeated header rows of the page are dropped.
|
|
29
|
+
|
|
30
|
+
Attribution: 外交部領事事務局全球資訊網,護照外文姓名拼音對照表,<https://www.boca.gov.tw>
|
|
31
|
+
|
|
32
|
+
License: the site's 政府網站資料開放宣告 (<https://www.boca.gov.tw/np-341-1.html>) releases its
|
|
33
|
+
content under 政府資料開放授權條款-第1版 (Open Government Data License, version 1.0).
|
|
34
|
+
|
|
35
|
+
| File | SHA-256 |
|
|
36
|
+
| ------------------------------------- | ------------------------------------------------------------------ |
|
|
37
|
+
| The page, as downloaded | `52c18b8df30234a1c59b3a479ca7c93ff54a34469e51923ea1e5a8ff4149a92f` |
|
|
38
|
+
| `spec/fixtures/boca_romanization.tsv` | `e6abf551d12d3cc14c35450827324b880c099eabca1ebd50bebc993de7262c93` |
|
|
39
|
+
|
|
40
|
+
## Ministry of Education transliteration service
|
|
41
|
+
|
|
42
|
+
The Tongyong Pinyin, MPS II and Wade–Giles spellings and tone notations were also checked, outside
|
|
43
|
+
the test suite, against the Ministry of Education's 中文譯音轉換系統
|
|
44
|
+
(<https://crptransfer.moe.gov.tw/>, queried 2026-12-06) and its 漢語拼音與通用拼音對照表
|
|
45
|
+
(<https://crptransfer.moe.gov.tw/files/970918duizhao.pdf>, SHA-256
|
|
46
|
+
`c758913715229bf51634a874ab670e237cf54a81fe419d43497ddf26320c0c0a`). The site is marked
|
|
47
|
+
「版權所有」, so no part of it is distributed here.
|
data/README.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# taiwan-phonetics · 臺灣華語注音與拼音
|
|
2
|
+
|
|
3
|
+
Mandarin as written and spoken in Taiwan: zhuyin, Hanyu Pinyin, Tongyong Pinyin, Wade–Giles,
|
|
4
|
+
MPS II, and tone sandhi. Taiwan only.
|
|
5
|
+
|
|
6
|
+
臺灣華語的注音跟拼音:注音、漢語拼音、通用拼音、威妥瑪拼音、注音二式,還有變調。只做臺灣。
|
|
7
|
+
|
|
8
|
+
**English** · [中文](#中文)
|
|
9
|
+
|
|
10
|
+
## English
|
|
11
|
+
|
|
12
|
+
Taiwan writes readings in zhuyin. Learners, passports, signs and software often need Hanyu Pinyin
|
|
13
|
+
or one of the romanizations Taiwan used before it. And the tones people say are not always the
|
|
14
|
+
tones the dictionary gives: 一, 不 and third tones change before other syllables. This gem
|
|
15
|
+
converts readings between the five systems, reads the forms people actually type, and gives every
|
|
16
|
+
syllable both its dictionary tone and its spoken tone. Pure Ruby, no dependencies.
|
|
17
|
+
|
|
18
|
+
### Install
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
gem "taiwan-phonetics"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npm install taiwan-phonetics
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The gem needs Ruby 3.4 or later. The npm package, in [`js/`](js), needs Node 24 or later or a
|
|
29
|
+
current browser. Both editions share the same tables and test cases.
|
|
30
|
+
|
|
31
|
+
### Use
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
require "taiwan-phonetics"
|
|
35
|
+
|
|
36
|
+
TaiwanPhonetics.to_pinyin("ㄊㄞˊ ㄅㄟˇ ㄐㄧㄝˊ ㄩㄣˋ") # => "tái běi jié yùn"
|
|
37
|
+
TaiwanPhonetics.to_zhuyin("zhēnzhū nǎichá") # => "ㄓㄣ ㄓㄨ ㄋㄞˇ ㄔㄚˊ"
|
|
38
|
+
TaiwanPhonetics.to_zhuyin("Nǐ hǎo, Táiwān!") # => "ㄋㄧˇ ㄏㄠˇ, ㄊㄞˊ ㄨㄢ!"
|
|
39
|
+
TaiwanPhonetics.to_pinyin("ㄌㄜˋㄙㄜˋ,ㄒㄧㄥ ㄑㄧˊ ㄙㄢ。") # => "lè sè,xīng qí sān。"
|
|
40
|
+
|
|
41
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :tongyong) # => "yè shìh"
|
|
42
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :wade_giles) # => "yeh4 shih4"
|
|
43
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :mps2) # => "yè shr̀"
|
|
44
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :pinyin, tones: :numbers) # => "ye4 shi4"
|
|
45
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :pinyin, tones: :none) # => "ye shi"
|
|
46
|
+
TaiwanPhonetics.convert("t'ai2-pei3", from: :wade_giles, to: :zhuyin) # => "ㄊㄞˊ-ㄅㄟˇ"
|
|
47
|
+
TaiwanPhonetics.convert("ts'ai4 ying1 wen2", from: :wade_giles, to: :pinyin) # => "cài yīng wén"
|
|
48
|
+
TaiwanPhonetics.convert("ㄅㄧㄢˋ ㄌㄧˋ ㄕㄤ ㄉㄧㄢˋ", to: "漢語拼音") # => "biàn lì shāng diàn"
|
|
49
|
+
|
|
50
|
+
TaiwanPhonetics.valid?("ㄐㄩㄝˊ") # => true
|
|
51
|
+
TaiwanPhonetics.valid?("ㄐㄚ") # => false
|
|
52
|
+
TaiwanPhonetics.syllable("ㄓㄨㄤˋ")
|
|
53
|
+
# => #<data TaiwanPhonetics::Syllable initial="ㄓ", medial="ㄨ", final="ㄤ", tone=4, erhua=false>
|
|
54
|
+
TaiwanPhonetics.syllable("ㄓㄨㄤˋ").spell(:wade_giles) # => "chuang4"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`syllable` and `syllables` return frozen `TaiwanPhonetics::Syllable` values: `tone` is 1 to 4, 5
|
|
58
|
+
for the neutral tone, `nil` for none. `to_zhuyin`, `to_pinyin(numbered:)` and
|
|
59
|
+
`spell(system, tones:)` spell a syllable, `with(**changes)` changes it, and `to_s` is its zhuyin.
|
|
60
|
+
|
|
61
|
+
All of these give `"ㄋㄧˇ ㄏㄠˇ"`:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
["nǐ hǎo", "Nǐhǎo", "ni3 hao3", "ni3hao3", "NI3 HAO3", "ni³ hao³", "nĭ hăo", "ni3hao3",
|
|
65
|
+
"ㄋㄧˇㄏㄠˇ", "ㄋ一ˇㄏㄠˇ"].map { |text| TaiwanPhonetics.to_zhuyin(text) }.uniq
|
|
66
|
+
# => ["ㄋㄧˇ ㄏㄠˇ"]
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Punctuation, numbers and spacing pass through unchanged. Zhuyin may be written without spaces;
|
|
70
|
+
the longest syllable is read first, as in pinyin written without apostrophes. In pinyin with tone
|
|
71
|
+
marks, an unmarked syllable is neutral (`xièxie`); pinyin with no tones at all stays toneless
|
|
72
|
+
(`ni hao` → `ㄋㄧ ㄏㄠ`). `unmarked: :notation` or `unmarked: :toneless` forces one reading or the
|
|
73
|
+
other.
|
|
74
|
+
|
|
75
|
+
### Errors
|
|
76
|
+
|
|
77
|
+
Input that cannot be read raises `TaiwanPhonetics::Error`. The message says what is wrong; `kind`
|
|
78
|
+
is one of these symbols, listed in `TaiwanPhonetics::Error::KINDS`:
|
|
79
|
+
|
|
80
|
+
| `kind` | Raised when |
|
|
81
|
+
| --------------------------- | ---------------------------------------------------------------------------------------------------- |
|
|
82
|
+
| `:not_a_string` | the text, or a word or reading given to `sandhi`, is not a String |
|
|
83
|
+
| `:invalid_encoding` | the text is not valid Unicode |
|
|
84
|
+
| `:han_character` | the text contains a Han character |
|
|
85
|
+
| `:unknown_system` | `to:` or `from:` names no supported system |
|
|
86
|
+
| `:invalid_option` | `tones:`, `unmarked:`, or the `tone` or `erhua` of a `Syllable`, has a value it cannot take |
|
|
87
|
+
| `:foreign_bopomofo` | a Bopomofo letter for another language, such as ㄪ or ㆠ |
|
|
88
|
+
| `:tone_digits_after_zhuyin` | digits right after zhuyin (`ㄋㄧ3`) |
|
|
89
|
+
| `:mixed_tone_notation` | tone marks and tone digits in one text |
|
|
90
|
+
| `:missing_tone_digit` | numbered pinyin with a syllable that has no digit (`ni3 hao`) |
|
|
91
|
+
| `:ambiguous` | a spelling that stands for two syllables in the named system (Wade–Giles `lo`) |
|
|
92
|
+
| `:no_spelling` | the target system has no spelling for the syllable or for its erhua |
|
|
93
|
+
| `:not_in_inventory` | not one of the 418 syllables, or a tone or erhua the syllable cannot take |
|
|
94
|
+
| `:wrong_script` | the text is not in the system named by `from:` |
|
|
95
|
+
| `:toneless_sandhi` | a reading given to `sandhi` has a syllable without a tone |
|
|
96
|
+
| `:sandhi_shape` | `sandhi` input is not phrases of `[text, reading]` pairs, or characters and syllables do not pair up |
|
|
97
|
+
| `:malformed` | anything else that cannot be read |
|
|
98
|
+
|
|
99
|
+
### Systems
|
|
100
|
+
|
|
101
|
+
| System | `to:` / `from:` | 中文 | Where you see it in Taiwan |
|
|
102
|
+
| --------------- | --------------- | ---------- | ------------------------------------------------------------------------------ |
|
|
103
|
+
| Zhuyin | `:zhuyin` | 注音 | Schools, dictionaries, the keyboard |
|
|
104
|
+
| Hanyu Pinyin | `:pinyin` | 漢語拼音 | Official since 2009: road and MRT signs, one of the four passport name systems |
|
|
105
|
+
| Tongyong Pinyin | `:tongyong` | 通用拼音 | Official 2002–2008; signs in Kaohsiung and the south |
|
|
106
|
+
| Wade–Giles | `:wade_giles` | 威妥瑪拼音 | Many personal names in passports, older place names |
|
|
107
|
+
| MPS II | `:mps2` | 注音二式 | Official 1986–2002; some names |
|
|
108
|
+
|
|
109
|
+
`tones:` is `:marks` (the default; Wade–Giles has no marks and defaults to `:numbers`), `:numbers`
|
|
110
|
+
or `:none`. `from:` may be left out for zhuyin and Hanyu Pinyin; the other systems must be named,
|
|
111
|
+
because their spellings overlap. A system may also be named in Chinese, as in the table, or as
|
|
112
|
+
拼音, 威妥瑪 or 國語注音符號第二式. Everyday spellings such as Taipei and Taichung are Wade–Giles
|
|
113
|
+
without apostrophes and tones, so they do not convert back: `Taipei` reads as ㄉㄞ ㄅㄟ.
|
|
114
|
+
|
|
115
|
+
### Tone sandhi
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
TaiwanPhonetics.sandhi([[["我", "ㄨㄛˇ"], ["很好", "ㄏㄣˇ ㄏㄠˇ"]]]).map { |t| t.surface.to_zhuyin }
|
|
119
|
+
# => ["ㄨㄛˇ", "ㄏㄣˊ", "ㄏㄠˇ"]
|
|
120
|
+
TaiwanPhonetics.sandhi([[["一起", "yī qǐ"], ["去", "qù"]], [["不要", "bù yào"]]]).map { |t| t.surface.to_pinyin }
|
|
121
|
+
# => ["yì", "qǐ", "qù", "bú", "yào"]
|
|
122
|
+
TaiwanPhonetics.sandhi([[["不要", "ㄅㄨˋ ㄧㄠˋ"]]]).map(&:changed?) # => [true, false]
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The input is a list of phrases, each a list of words, each word a pair of characters and reading.
|
|
126
|
+
Each token carries the `text`, the `underlying` syllable, the `surface` syllable and `changed?`,
|
|
127
|
+
true when the two tones differ. No rule crosses a phrase boundary.
|
|
128
|
+
|
|
129
|
+
1. 一 is tone 2 before a fourth tone and tone 4 before the others. It keeps tone 1 at the end of a
|
|
130
|
+
phrase or of a longer word (唯一), after 第, next to a numeral, and in dates (一月一日).
|
|
131
|
+
2. 不 is tone 2 before a fourth tone and tone 4 otherwise.
|
|
132
|
+
3. Before a neutral syllable, 一 and 不 keep a second or fourth tone written in the reading and
|
|
133
|
+
otherwise take tone 4; 個 counts as the fourth tone it has underneath (一個 yí ge). A 一 or 不
|
|
134
|
+
written neutral stays neutral.
|
|
135
|
+
4. A third tone becomes second before a third tone: first inside each word, then across words from
|
|
136
|
+
right to left.
|
|
137
|
+
|
|
138
|
+
Grouping decides rule 4: 我|很好 gives 3-2-3, as in careful speech; the whole phrase as one word,
|
|
139
|
+
我很好, gives 2-2-3, as in fast speech.
|
|
140
|
+
|
|
141
|
+
### What it does not do
|
|
142
|
+
|
|
143
|
+
- Taiwan only: no conventions of China, Singapore or elsewhere, no simplified characters.
|
|
144
|
+
- It does not read Han characters or choose between readings: the caller supplies the reading.
|
|
145
|
+
- It does not split text into words; the caller supplies the grouping for sandhi.
|
|
146
|
+
- It accepts only the 418 syllables of the CNS 11643 inventory; ㄎㄟ and ㄊㄟ, for example, are
|
|
147
|
+
refused. Tongyong, Wade–Giles and MPS II have no spelling for ㄍㄨㄜ, bare ㄑ or erhua;
|
|
148
|
+
Wade–Giles and MPS II also have none for ㄝ.
|
|
149
|
+
|
|
150
|
+
Developed and maintained in Taipei, Taiwan by Den Patin 丁丹旭 (<hi@dpat.in>), independent researcher, linguist and software engineer.
|
|
151
|
+
|
|
152
|
+
### License
|
|
153
|
+
|
|
154
|
+
MIT — see [LICENSE](LICENSE). The syllable inventory comes from 數位發展部,CNS11643中文標準交換碼
|
|
155
|
+
全字庫網站,<https://www.cns11643.gov.tw>; the tests also check the passport table of the Bureau of
|
|
156
|
+
Consular Affairs. See [NOTICES.md](NOTICES.md).
|
|
157
|
+
|
|
158
|
+
## 中文
|
|
159
|
+
|
|
160
|
+
在臺灣,字音是用注音標的。不過學華語的外國人、護照、路牌跟很多程式,要的常常是漢語拼音,或是臺灣以前用過
|
|
161
|
+
的其他拼音。而且字典上的聲調跟實際說出來的不一定一樣:「一」、「不」跟三聲碰到後面的字會變調。這個 gem
|
|
162
|
+
可以在這五種系統之間互轉,看得懂大家實際打出來的各種寫法,每個音節也會同時給出字典上的聲調跟實際念的聲調。
|
|
163
|
+
純 Ruby,沒有相依套件。
|
|
164
|
+
|
|
165
|
+
### 安裝
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
gem "taiwan-phonetics"
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
```sh
|
|
172
|
+
npm install taiwan-phonetics
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
gem 需要 Ruby 3.4 以上。npm 套件在 [`js/`](js),需要 Node 24 以上,或是現行的瀏覽器。兩個版本用的是
|
|
176
|
+
同一份表格,也跑同一套測試案例。
|
|
177
|
+
|
|
178
|
+
### 用法
|
|
179
|
+
|
|
180
|
+
```ruby
|
|
181
|
+
require "taiwan-phonetics"
|
|
182
|
+
|
|
183
|
+
TaiwanPhonetics.to_pinyin("ㄊㄞˊ ㄅㄟˇ ㄐㄧㄝˊ ㄩㄣˋ") # => "tái běi jié yùn"
|
|
184
|
+
TaiwanPhonetics.to_zhuyin("zhēnzhū nǎichá") # => "ㄓㄣ ㄓㄨ ㄋㄞˇ ㄔㄚˊ"
|
|
185
|
+
TaiwanPhonetics.to_zhuyin("Nǐ hǎo, Táiwān!") # => "ㄋㄧˇ ㄏㄠˇ, ㄊㄞˊ ㄨㄢ!"
|
|
186
|
+
TaiwanPhonetics.to_pinyin("ㄌㄜˋㄙㄜˋ,ㄒㄧㄥ ㄑㄧˊ ㄙㄢ。") # => "lè sè,xīng qí sān。"
|
|
187
|
+
|
|
188
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :tongyong) # => "yè shìh"
|
|
189
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :wade_giles) # => "yeh4 shih4"
|
|
190
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :mps2) # => "yè shr̀"
|
|
191
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :pinyin, tones: :numbers) # => "ye4 shi4"
|
|
192
|
+
TaiwanPhonetics.convert("ㄧㄝˋ ㄕˋ", to: :pinyin, tones: :none) # => "ye shi"
|
|
193
|
+
TaiwanPhonetics.convert("t'ai2-pei3", from: :wade_giles, to: :zhuyin) # => "ㄊㄞˊ-ㄅㄟˇ"
|
|
194
|
+
TaiwanPhonetics.convert("ts'ai4 ying1 wen2", from: :wade_giles, to: :pinyin) # => "cài yīng wén"
|
|
195
|
+
TaiwanPhonetics.convert("ㄅㄧㄢˋ ㄌㄧˋ ㄕㄤ ㄉㄧㄢˋ", to: "漢語拼音") # => "biàn lì shāng diàn"
|
|
196
|
+
|
|
197
|
+
TaiwanPhonetics.valid?("ㄐㄩㄝˊ") # => true
|
|
198
|
+
TaiwanPhonetics.valid?("ㄐㄚ") # => false
|
|
199
|
+
TaiwanPhonetics.syllable("ㄓㄨㄤˋ")
|
|
200
|
+
# => #<data TaiwanPhonetics::Syllable initial="ㄓ", medial="ㄨ", final="ㄤ", tone=4, erhua=false>
|
|
201
|
+
TaiwanPhonetics.syllable("ㄓㄨㄤˋ").spell(:wade_giles) # => "chuang4"
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
`syllable` 跟 `syllables` 回傳的是不能修改的 `TaiwanPhonetics::Syllable`:`tone` 是 1 到 4,輕聲是
|
|
205
|
+
5,沒有聲調是 `nil`。`to_zhuyin`、`to_pinyin(numbered:)` 跟 `spell(system, tones:)` 會把音節拼出來,
|
|
206
|
+
`with(**changes)` 可以換掉其中幾個欄位,`to_s` 就是注音。
|
|
207
|
+
|
|
208
|
+
下面這些寫法都會得到 `"ㄋㄧˇ ㄏㄠˇ"`:
|
|
209
|
+
|
|
210
|
+
```ruby
|
|
211
|
+
["nǐ hǎo", "Nǐhǎo", "ni3 hao3", "ni3hao3", "NI3 HAO3", "ni³ hao³", "nĭ hăo", "ni3hao3",
|
|
212
|
+
"ㄋㄧˇㄏㄠˇ", "ㄋ一ˇㄏㄠˇ"].map { |text| TaiwanPhonetics.to_zhuyin(text) }.uniq
|
|
213
|
+
# => ["ㄋㄧˇ ㄏㄠˇ"]
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
標點符號、數字跟空白都照原樣保留。注音可以連著寫、不加空格,會從最長的音節開始讀,跟拼音沒加隔音符號時
|
|
217
|
+
一樣。拼音裡只要有標聲調符號,沒標的音節就當輕聲(`xièxie`);整段都沒標聲調的拼音,就全部當作沒有聲調
|
|
218
|
+
(`ni hao` → `ㄋㄧ ㄏㄠ`)。要指定哪一種,可以用 `unmarked: :notation` 或 `unmarked: :toneless`。
|
|
219
|
+
|
|
220
|
+
### 錯誤
|
|
221
|
+
|
|
222
|
+
讀不懂的輸入會丟出 `TaiwanPhonetics::Error`。錯誤訊息會說哪裡有問題,`kind` 是下面其中一個 symbol,
|
|
223
|
+
完整清單在 `TaiwanPhonetics::Error::KINDS`:
|
|
224
|
+
|
|
225
|
+
| `kind` | 什麼時候會出現 |
|
|
226
|
+
| --------------------------- | -------------------------------------------------------------------- |
|
|
227
|
+
| `:not_a_string` | 文字,或是給 `sandhi` 的字、讀音不是 String |
|
|
228
|
+
| `:invalid_encoding` | 文字不是有效的 Unicode |
|
|
229
|
+
| `:han_character` | 文字裡有漢字 |
|
|
230
|
+
| `:unknown_system` | `to:` 或 `from:` 不是支援的系統 |
|
|
231
|
+
| `:invalid_option` | `tones:`、`unmarked:`,或 `Syllable` 的 `tone`、`erhua` 的值不能用 |
|
|
232
|
+
| `:foreign_bopomofo` | 用了標其他語言的注音符號,像是 ㄪ 或 ㆠ |
|
|
233
|
+
| `:tone_digits_after_zhuyin` | 注音後面直接接數字(`ㄋㄧ3`) |
|
|
234
|
+
| `:mixed_tone_notation` | 同一段文字裡聲調符號跟數字混用 |
|
|
235
|
+
| `:missing_tone_digit` | 用數字標調的拼音裡,有音節沒標數字(`ni3 hao`) |
|
|
236
|
+
| `:ambiguous` | 這個拼法在指定的系統裡對到兩個音節(威妥瑪拼音的 `lo`) |
|
|
237
|
+
| `:no_spelling` | 目標系統沒有這個音節或它的兒化拼法 |
|
|
238
|
+
| `:not_in_inventory` | 不是 418 個音節之一,或這個音節不能有這個聲調、不能兒化 |
|
|
239
|
+
| `:wrong_script` | 文字不是 `from:` 指定的系統 |
|
|
240
|
+
| `:toneless_sandhi` | 給 `sandhi` 的讀音裡有音節沒標聲調 |
|
|
241
|
+
| `:sandhi_shape` | 給 `sandhi` 的不是一串由 `[字, 讀音]` 組成的短語,或字跟音節對不起來 |
|
|
242
|
+
| `:malformed` | 其他讀不懂的情況 |
|
|
243
|
+
|
|
244
|
+
### 拼音系統
|
|
245
|
+
|
|
246
|
+
| 系統 | `to:` / `from:` | 在臺灣哪裡看得到 |
|
|
247
|
+
| ---------- | --------------- | --------------------------------------------------------------- |
|
|
248
|
+
| 注音 | `:zhuyin` | 學校、字典、鍵盤 |
|
|
249
|
+
| 漢語拼音 | `:pinyin` | 2009 年起的官方拼音:路牌、捷運站名,也是護照姓名的四種拼法之一 |
|
|
250
|
+
| 通用拼音 | `:tongyong` | 2002–2008 年的官方拼音;高雄跟南部的路牌 |
|
|
251
|
+
| 威妥瑪拼音 | `:wade_giles` | 很多護照上的姓名、舊地名 |
|
|
252
|
+
| 注音二式 | `:mps2` | 1986–2002 年的官方拼音;部分人名 |
|
|
253
|
+
|
|
254
|
+
`tones:` 可以是 `:marks`(預設;威妥瑪拼音沒有聲調符號,預設是 `:numbers`)、`:numbers` 或 `:none`。
|
|
255
|
+
注音跟漢語拼音可以不寫 `from:`;其他系統的拼法會互相重疊,一定要指定。系統名稱也可以直接寫中文,像
|
|
256
|
+
`to: "通用拼音"`;除了表格裡的名稱,也認得「拼音」、「威妥瑪」跟「國語注音符號第二式」。平常看到的
|
|
257
|
+
Taipei、Taichung 這類拼法,是把威妥瑪拼音的送氣符號跟聲調拿掉,所以轉不回原來的音:`Taipei` 會讀成
|
|
258
|
+
ㄉㄞ ㄅㄟ。
|
|
259
|
+
|
|
260
|
+
### 變調
|
|
261
|
+
|
|
262
|
+
```ruby
|
|
263
|
+
TaiwanPhonetics.sandhi([[["我", "ㄨㄛˇ"], ["很好", "ㄏㄣˇ ㄏㄠˇ"]]]).map { |t| t.surface.to_zhuyin }
|
|
264
|
+
# => ["ㄨㄛˇ", "ㄏㄣˊ", "ㄏㄠˇ"]
|
|
265
|
+
TaiwanPhonetics.sandhi([[["一起", "yī qǐ"], ["去", "qù"]], [["不要", "bù yào"]]]).map { |t| t.surface.to_pinyin }
|
|
266
|
+
# => ["yì", "qǐ", "qù", "bú", "yào"]
|
|
267
|
+
TaiwanPhonetics.sandhi([[["不要", "ㄅㄨˋ ㄧㄠˋ"]]]).map(&:changed?) # => [true, false]
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
輸入是一串短語;每個短語是一串詞,每個詞是一組「字、讀音」。回傳的每個結果都有字(`text`)、字典上的音節
|
|
271
|
+
(`underlying`)、實際念的音節(`surface`),還有 `changed?`:聲調有變就是 true。變調不會跨過短語。
|
|
272
|
+
|
|
273
|
+
1. 「一」在四聲前念二聲,在其他聲調前念四聲;在短語最後、多字詞的最後(唯一)、「第」後面、數字旁邊,還有
|
|
274
|
+
日期裡(一月一日),維持一聲。
|
|
275
|
+
2. 「不」在四聲前念二聲,其他時候念四聲。
|
|
276
|
+
3. 「一」、「不」後面接輕聲的時候,讀音寫的是二聲或四聲就照寫的念,其他都念四聲;「個」照它原本的四聲算
|
|
277
|
+
(一個 yí ge)。「一」、「不」本身寫成輕聲的,就維持輕聲。
|
|
278
|
+
4. 三聲在三聲前面變二聲:先處理每個詞裡面,再從右到左處理詞跟詞之間。
|
|
279
|
+
|
|
280
|
+
第 4 條的結果要看怎麼斷詞:分成「我|很好」是 3-2-3,像說得慢、說得清楚的時候;整句當成一個詞「我很好」是
|
|
281
|
+
2-2-3,像平常講得比較快的時候。
|
|
282
|
+
|
|
283
|
+
### 不做的事
|
|
284
|
+
|
|
285
|
+
- 只做臺灣:不處理中國、新加坡或其他地區的用法,也不處理簡體字。
|
|
286
|
+
- 不讀漢字,也不幫你判斷破音字怎麼念:讀音要自己給。
|
|
287
|
+
- 不做斷詞;變調要用的詞界要自己給。
|
|
288
|
+
- 只接受全字庫的 418 個音節,像 ㄎㄟ、ㄊㄟ 就不接受。通用拼音、威妥瑪拼音跟注音二式沒有 ㄍㄨㄜ、單獨的
|
|
289
|
+
ㄑ 跟兒化的拼法;威妥瑪拼音跟注音二式也沒有 ㄝ。
|
|
290
|
+
|
|
291
|
+
由 Den Patin 丁丹旭(<hi@dpat.in>)在臺灣臺北開發與維護。獨立研究者,做語言學跟軟體工程。
|
|
292
|
+
|
|
293
|
+
### 授權條款
|
|
294
|
+
|
|
295
|
+
MIT,詳見 [LICENSE](LICENSE)。音節清單來自數位發展部,CNS11643中文標準交換碼全字庫網站,
|
|
296
|
+
<https://www.cns11643.gov.tw>;測試也拿外交部領事事務局的護照外文姓名拼音對照表來比對。詳見
|
|
297
|
+
[NOTICES.md](NOTICES.md)。
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TaiwanPhonetics
|
|
4
|
+
module Bopomofo
|
|
5
|
+
EMPTY_RIME_LETTER = "ㄭ"
|
|
6
|
+
FOREIGN = /[ㄪㄫㄬㄮㄯㆠ-ㆿ]/
|
|
7
|
+
LOOK_ALIKES = {
|
|
8
|
+
"´" => "ˊ",
|
|
9
|
+
"`" => "ˋ",
|
|
10
|
+
"`" => "ˋ",
|
|
11
|
+
"^" => "ˇ",
|
|
12
|
+
"·" => "˙",
|
|
13
|
+
"・" => "˙",
|
|
14
|
+
"•" => "˙",
|
|
15
|
+
"‧" => "˙",
|
|
16
|
+
"一" => "ㄧ",
|
|
17
|
+
"丨" => "ㄧ",
|
|
18
|
+
"︱" => "ㄧ"
|
|
19
|
+
}.freeze
|
|
20
|
+
NEUTRAL = Inventory::NEUTRAL_MARK
|
|
21
|
+
|
|
22
|
+
module_function
|
|
23
|
+
|
|
24
|
+
def syllables(word, unmarked: nil)
|
|
25
|
+
text = normalize(word)
|
|
26
|
+
found = divide(text, 0, {}, unmarked == :toneless)
|
|
27
|
+
return found if found
|
|
28
|
+
|
|
29
|
+
single(text)
|
|
30
|
+
raise Error.new("cannot divide #{word.inspect} into zhuyin syllables", kind: :malformed)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def single(text)
|
|
34
|
+
neutral = text.start_with?(NEUTRAL)
|
|
35
|
+
rest = text.delete_prefix(NEUTRAL)
|
|
36
|
+
if rest.end_with?(NEUTRAL)
|
|
37
|
+
raise Error.new("two neutral tone marks in #{text.inspect}", kind: :malformed) if neutral
|
|
38
|
+
|
|
39
|
+
neutral = true
|
|
40
|
+
rest = rest.delete_suffix(NEUTRAL)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
erhua = rest.length > 1 && rest.end_with?(Inventory::ERHUA)
|
|
44
|
+
rest = rest.delete_suffix(Inventory::ERHUA) if erhua
|
|
45
|
+
tone = Inventory::TONES_READ[rest[-1]]
|
|
46
|
+
rest = rest[0...-1] if tone
|
|
47
|
+
raise Error.new("neutral and full tone marked in #{text.inspect}", kind: :malformed) if tone && neutral
|
|
48
|
+
|
|
49
|
+
parts = Inventory.components(rest)
|
|
50
|
+
raise Error.new("malformed zhuyin syllable #{text.inspect}", kind: :malformed) if parts.nil? || rest.empty?
|
|
51
|
+
|
|
52
|
+
initial, medial, final = parts
|
|
53
|
+
Syllable.new(initial: initial, medial: medial, final: final, tone: neutral ? 5 : tone || 1, erhua: erhua)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def normalize(word)
|
|
57
|
+
if (foreign = word[FOREIGN])
|
|
58
|
+
raise(
|
|
59
|
+
Error.new(
|
|
60
|
+
"#{foreign} in #{word.inspect} is a Bopomofo letter for a language other than Mandarin",
|
|
61
|
+
kind: :foreign_bopomofo
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
text = word.gsub(/[#{LOOK_ALIKES.keys.join}]/o, LOOK_ALIKES)
|
|
67
|
+
text.gsub(/(.?)#{EMPTY_RIME_LETTER}/o) do
|
|
68
|
+
initial = Regexp.last_match(1)
|
|
69
|
+
unless Inventory::EMPTY_RIME.include?(initial)
|
|
70
|
+
raise Error.new("ㄭ follows only ㄓㄔㄕㄖㄗㄘㄙ, in #{word.inspect}", kind: :malformed)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
initial
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def divide(text, start, memo, toneless)
|
|
78
|
+
return [] if start == text.length
|
|
79
|
+
return memo[start] if memo.key?(start)
|
|
80
|
+
|
|
81
|
+
memo[start] = candidates(text, start, toneless)
|
|
82
|
+
.lazy
|
|
83
|
+
.filter_map { |syllable, stop|
|
|
84
|
+
rest = divide(text, stop, memo, toneless)
|
|
85
|
+
[syllable, *rest] if rest
|
|
86
|
+
}
|
|
87
|
+
.first
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def candidates(text, start, toneless)
|
|
91
|
+
lead = text[start] == NEUTRAL
|
|
92
|
+
from = lead ? start + 1 : start
|
|
93
|
+
found = []
|
|
94
|
+
3.downto(1) do |length|
|
|
95
|
+
parts = Inventory.components(text[from, length].to_s)
|
|
96
|
+
next unless parts && text[from, length].length == length && Inventory::LETTERS.key?(parts)
|
|
97
|
+
|
|
98
|
+
endings(text, from + length, lead).each do |tone, erhua, stop|
|
|
99
|
+
tone ||= toneless ? nil : 1
|
|
100
|
+
syllable = build(parts, tone, erhua)
|
|
101
|
+
found << [syllable, stop] if syllable
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
found.each_with_index.sort_by { |(_, stop), position| [-stop, position] }.map(&:first)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def endings(text, at, lead)
|
|
109
|
+
tone = lead ? nil : Inventory::TONES_READ[text[at]]
|
|
110
|
+
after_tone = tone ? at + 1 : at
|
|
111
|
+
options = [[tone, false, after_tone]]
|
|
112
|
+
options << [tone, true, after_tone + 1] if text[after_tone] == Inventory::ERHUA
|
|
113
|
+
options.map! { |found, erhua, stop| [lead ? 5 : found, erhua, stop] }
|
|
114
|
+
options + trailing_neutral(text, options, lead)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def trailing_neutral(text, options, lead)
|
|
118
|
+
return [] if lead
|
|
119
|
+
|
|
120
|
+
options.filter_map do |tone, erhua, stop|
|
|
121
|
+
[5, erhua, stop + 1] if tone.nil? && text[stop] == NEUTRAL && stop + 1 == text.length
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def build(parts, tone, erhua)
|
|
126
|
+
initial, medial, final = parts
|
|
127
|
+
Syllable.new(initial: initial, medial: medial, final: final, tone: tone, erhua: erhua)
|
|
128
|
+
rescue Error
|
|
129
|
+
nil
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TaiwanPhonetics
|
|
4
|
+
class Error < StandardError
|
|
5
|
+
KINDS = %i[
|
|
6
|
+
not_a_string
|
|
7
|
+
invalid_encoding
|
|
8
|
+
han_character
|
|
9
|
+
unknown_system
|
|
10
|
+
invalid_option
|
|
11
|
+
foreign_bopomofo
|
|
12
|
+
tone_digits_after_zhuyin
|
|
13
|
+
mixed_tone_notation
|
|
14
|
+
missing_tone_digit
|
|
15
|
+
ambiguous
|
|
16
|
+
no_spelling
|
|
17
|
+
not_in_inventory
|
|
18
|
+
wrong_script
|
|
19
|
+
toneless_sandhi
|
|
20
|
+
sandhi_shape
|
|
21
|
+
malformed
|
|
22
|
+
]
|
|
23
|
+
.freeze
|
|
24
|
+
|
|
25
|
+
attr_reader :kind
|
|
26
|
+
|
|
27
|
+
def initialize(message, kind:)
|
|
28
|
+
raise ArgumentError, "unknown error kind #{kind.inspect}" unless KINDS.include?(kind)
|
|
29
|
+
|
|
30
|
+
super(message)
|
|
31
|
+
@kind = kind
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|