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
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TaiwanPhonetics
|
|
4
|
+
module Inventory
|
|
5
|
+
PATTERN = /\A(?<initial>[ㄅ-ㄙ])?(?<medial>[ㄧㄨㄩ])?(?<final>[ㄚ-ㄦ])?\z/
|
|
6
|
+
|
|
7
|
+
SYLLABLES = %w[
|
|
8
|
+
ㄧ ㄧㄚ ㄧㄛ ㄧㄝ ㄧㄞ ㄧㄠ ㄧㄡ ㄧㄢ ㄧㄣ ㄧㄤ ㄧㄥ ㄨ ㄨㄚ ㄨㄛ ㄨㄞ ㄨㄟ ㄨㄢ ㄨㄣ ㄨㄤ ㄨㄥ ㄩ ㄩㄝ ㄩㄢ ㄩㄣ ㄩㄥ
|
|
9
|
+
ㄚ ㄛ ㄜ ㄝ ㄞ ㄟ ㄠ ㄡ ㄢ ㄣ ㄤ ㄥ ㄦ
|
|
10
|
+
ㄅㄧ ㄅㄧㄝ ㄅㄧㄠ ㄅㄧㄢ ㄅㄧㄣ ㄅㄧㄥ ㄅㄨ ㄅㄚ ㄅㄛ ㄅㄞ ㄅㄟ ㄅㄠ ㄅㄢ ㄅㄣ ㄅㄤ ㄅㄥ
|
|
11
|
+
ㄆㄧ ㄆㄧㄚ ㄆㄧㄝ ㄆㄧㄠ ㄆㄧㄢ ㄆㄧㄣ ㄆㄧㄥ ㄆㄨ ㄆㄚ ㄆㄛ ㄆㄞ ㄆㄟ ㄆㄠ ㄆㄡ ㄆㄢ ㄆㄣ ㄆㄤ ㄆㄥ
|
|
12
|
+
ㄇㄧ ㄇㄧㄝ ㄇㄧㄠ ㄇㄧㄡ ㄇㄧㄢ ㄇㄧㄣ ㄇㄧㄥ ㄇㄨ ㄇㄚ ㄇㄛ ㄇㄜ ㄇㄞ ㄇㄟ ㄇㄠ ㄇㄡ ㄇㄢ ㄇㄣ ㄇㄤ ㄇㄥ
|
|
13
|
+
ㄈㄧㄠ ㄈㄨ ㄈㄚ ㄈㄛ ㄈㄟ ㄈㄡ ㄈㄢ ㄈㄣ ㄈㄤ ㄈㄥ
|
|
14
|
+
ㄉㄧ ㄉㄧㄚ ㄉㄧㄝ ㄉㄧㄠ ㄉㄧㄡ ㄉㄧㄢ ㄉㄧㄥ ㄉㄨ ㄉㄨㄛ ㄉㄨㄟ ㄉㄨㄢ ㄉㄨㄣ ㄉㄨㄥ
|
|
15
|
+
ㄉㄚ ㄉㄜ ㄉㄞ ㄉㄟ ㄉㄠ ㄉㄡ ㄉㄢ ㄉㄣ ㄉㄤ ㄉㄥ
|
|
16
|
+
ㄊㄧ ㄊㄧㄝ ㄊㄧㄠ ㄊㄧㄢ ㄊㄧㄥ ㄊㄨ ㄊㄨㄛ ㄊㄨㄟ ㄊㄨㄢ ㄊㄨㄣ ㄊㄨㄥ ㄊㄚ ㄊㄜ ㄊㄞ ㄊㄠ ㄊㄡ ㄊㄢ ㄊㄤ ㄊㄥ
|
|
17
|
+
ㄋㄧ ㄋㄧㄝ ㄋㄧㄠ ㄋㄧㄡ ㄋㄧㄢ ㄋㄧㄣ ㄋㄧㄤ ㄋㄧㄥ ㄋㄨ ㄋㄨㄛ ㄋㄨㄟ ㄋㄨㄢ ㄋㄨㄣ ㄋㄨㄥ ㄋㄩ ㄋㄩㄝ
|
|
18
|
+
ㄋㄚ ㄋㄜ ㄋㄞ ㄋㄟ ㄋㄠ ㄋㄡ ㄋㄢ ㄋㄣ ㄋㄤ ㄋㄥ
|
|
19
|
+
ㄌㄧ ㄌㄧㄚ ㄌㄧㄝ ㄌㄧㄠ ㄌㄧㄡ ㄌㄧㄢ ㄌㄧㄣ ㄌㄧㄤ ㄌㄧㄥ ㄌㄨ ㄌㄨㄛ ㄌㄨㄢ ㄌㄨㄣ ㄌㄨㄥ ㄌㄩ ㄌㄩㄝ ㄌㄩㄢ
|
|
20
|
+
ㄌㄚ ㄌㄛ ㄌㄜ ㄌㄞ ㄌㄟ ㄌㄠ ㄌㄡ ㄌㄢ ㄌㄤ ㄌㄥ
|
|
21
|
+
ㄍㄨ ㄍㄨㄚ ㄍㄨㄛ ㄍㄨㄜ ㄍㄨㄞ ㄍㄨㄟ ㄍㄨㄢ ㄍㄨㄣ ㄍㄨㄤ ㄍㄨㄥ ㄍㄚ ㄍㄜ ㄍㄞ ㄍㄟ ㄍㄠ ㄍㄡ ㄍㄢ ㄍㄣ ㄍㄤ ㄍㄥ
|
|
22
|
+
ㄎㄨ ㄎㄨㄚ ㄎㄨㄛ ㄎㄨㄞ ㄎㄨㄟ ㄎㄨㄢ ㄎㄨㄣ ㄎㄨㄤ ㄎㄨㄥ ㄎㄚ ㄎㄜ ㄎㄞ ㄎㄠ ㄎㄡ ㄎㄢ ㄎㄣ ㄎㄤ ㄎㄥ
|
|
23
|
+
ㄏㄨ ㄏㄨㄚ ㄏㄨㄛ ㄏㄨㄞ ㄏㄨㄟ ㄏㄨㄢ ㄏㄨㄣ ㄏㄨㄤ ㄏㄨㄥ ㄏㄚ ㄏㄜ ㄏㄞ ㄏㄟ ㄏㄠ ㄏㄡ ㄏㄢ ㄏㄣ ㄏㄤ ㄏㄥ
|
|
24
|
+
ㄐㄧ ㄐㄧㄚ ㄐㄧㄝ ㄐㄧㄠ ㄐㄧㄡ ㄐㄧㄢ ㄐㄧㄣ ㄐㄧㄤ ㄐㄧㄥ ㄐㄩ ㄐㄩㄝ ㄐㄩㄢ ㄐㄩㄣ ㄐㄩㄥ
|
|
25
|
+
ㄑ ㄑㄧ ㄑㄧㄚ ㄑㄧㄝ ㄑㄧㄠ ㄑㄧㄡ ㄑㄧㄢ ㄑㄧㄣ ㄑㄧㄤ ㄑㄧㄥ ㄑㄩ ㄑㄩㄝ ㄑㄩㄢ ㄑㄩㄣ ㄑㄩㄥ
|
|
26
|
+
ㄒㄧ ㄒㄧㄚ ㄒㄧㄝ ㄒㄧㄠ ㄒㄧㄡ ㄒㄧㄢ ㄒㄧㄣ ㄒㄧㄤ ㄒㄧㄥ ㄒㄩ ㄒㄩㄝ ㄒㄩㄢ ㄒㄩㄣ ㄒㄩㄥ
|
|
27
|
+
ㄓ ㄓㄨ ㄓㄨㄚ ㄓㄨㄛ ㄓㄨㄞ ㄓㄨㄟ ㄓㄨㄢ ㄓㄨㄣ ㄓㄨㄤ ㄓㄨㄥ ㄓㄚ ㄓㄜ ㄓㄞ ㄓㄟ ㄓㄠ ㄓㄡ ㄓㄢ ㄓㄣ ㄓㄤ ㄓㄥ
|
|
28
|
+
ㄔ ㄔㄨ ㄔㄨㄚ ㄔㄨㄛ ㄔㄨㄞ ㄔㄨㄟ ㄔㄨㄢ ㄔㄨㄣ ㄔㄨㄤ ㄔㄨㄥ ㄔㄚ ㄔㄜ ㄔㄞ ㄔㄠ ㄔㄡ ㄔㄢ ㄔㄣ ㄔㄤ ㄔㄥ
|
|
29
|
+
ㄕ ㄕㄨ ㄕㄨㄚ ㄕㄨㄛ ㄕㄨㄞ ㄕㄨㄟ ㄕㄨㄢ ㄕㄨㄣ ㄕㄨㄤ ㄕㄚ ㄕㄜ ㄕㄞ ㄕㄟ ㄕㄠ ㄕㄡ ㄕㄢ ㄕㄣ ㄕㄤ ㄕㄥ
|
|
30
|
+
ㄖ ㄖㄨ ㄖㄨㄛ ㄖㄨㄟ ㄖㄨㄢ ㄖㄨㄣ ㄖㄨㄥ ㄖㄜ ㄖㄠ ㄖㄡ ㄖㄢ ㄖㄣ ㄖㄤ ㄖㄥ
|
|
31
|
+
ㄗ ㄗㄨ ㄗㄨㄛ ㄗㄨㄟ ㄗㄨㄢ ㄗㄨㄣ ㄗㄨㄥ ㄗㄚ ㄗㄜ ㄗㄞ ㄗㄟ ㄗㄠ ㄗㄡ ㄗㄢ ㄗㄣ ㄗㄤ ㄗㄥ
|
|
32
|
+
ㄘ ㄘㄨ ㄘㄨㄛ ㄘㄨㄟ ㄘㄨㄢ ㄘㄨㄣ ㄘㄨㄥ ㄘㄚ ㄘㄜ ㄘㄞ ㄘㄠ ㄘㄡ ㄘㄢ ㄘㄣ ㄘㄤ ㄘㄥ
|
|
33
|
+
ㄙ ㄙㄨ ㄙㄨㄛ ㄙㄨㄟ ㄙㄨㄢ ㄙㄨㄣ ㄙㄨㄥ ㄙㄚ ㄙㄜ ㄙㄞ ㄙㄟ ㄙㄠ ㄙㄡ ㄙㄢ ㄙㄣ ㄙㄤ ㄙㄥ
|
|
34
|
+
].freeze
|
|
35
|
+
|
|
36
|
+
TONES = [1, 2, 3, 4, 5].freeze
|
|
37
|
+
TONE_MARKS = {1 => "", 2 => "ˊ", 3 => "ˇ", 4 => "ˋ"}.freeze
|
|
38
|
+
TONES_READ = {"ˉ" => 1, "ˊ" => 2, "ˇ" => 3, "ˋ" => 4}.freeze
|
|
39
|
+
NEUTRAL_MARK = "˙"
|
|
40
|
+
ERHUA = "ㄦ"
|
|
41
|
+
|
|
42
|
+
NEUTRAL_ONLY = %w[ㄑ].freeze
|
|
43
|
+
WITHOUT_ERHUA = %w[ㄑ ㄜ ㄦ].freeze
|
|
44
|
+
|
|
45
|
+
INITIALS = {
|
|
46
|
+
"ㄅ" => "b", "ㄆ" => "p", "ㄇ" => "m", "ㄈ" => "f", "ㄉ" => "d", "ㄊ" => "t", "ㄋ" => "n",
|
|
47
|
+
"ㄌ" => "l", "ㄍ" => "g", "ㄎ" => "k", "ㄏ" => "h", "ㄐ" => "j", "ㄑ" => "q", "ㄒ" => "x",
|
|
48
|
+
"ㄓ" => "zh", "ㄔ" => "ch", "ㄕ" => "sh", "ㄖ" => "r", "ㄗ" => "z", "ㄘ" => "c", "ㄙ" => "s"
|
|
49
|
+
}.freeze
|
|
50
|
+
|
|
51
|
+
RIMES = {
|
|
52
|
+
nil => {
|
|
53
|
+
nil => "", "ㄚ" => "a", "ㄛ" => "o", "ㄜ" => "e", "ㄝ" => "ê", "ㄞ" => "ai", "ㄟ" => "ei",
|
|
54
|
+
"ㄠ" => "ao", "ㄡ" => "ou", "ㄢ" => "an", "ㄣ" => "en", "ㄤ" => "ang", "ㄥ" => "eng", "ㄦ" => "er"
|
|
55
|
+
}.freeze,
|
|
56
|
+
"ㄧ" => {
|
|
57
|
+
nil => "i", "ㄚ" => "ia", "ㄛ" => "io", "ㄝ" => "ie", "ㄞ" => "iai", "ㄠ" => "iao",
|
|
58
|
+
"ㄡ" => "iu", "ㄢ" => "ian", "ㄣ" => "in", "ㄤ" => "iang", "ㄥ" => "ing"
|
|
59
|
+
}.freeze,
|
|
60
|
+
"ㄨ" => {
|
|
61
|
+
nil => "u", "ㄚ" => "ua", "ㄛ" => "uo", "ㄜ" => "ue", "ㄞ" => "uai", "ㄟ" => "ui",
|
|
62
|
+
"ㄢ" => "uan", "ㄣ" => "un", "ㄤ" => "uang", "ㄥ" => "ong"
|
|
63
|
+
}.freeze,
|
|
64
|
+
"ㄩ" => {nil => "ü", "ㄝ" => "üe", "ㄢ" => "üan", "ㄣ" => "ün", "ㄥ" => "iong"}.freeze
|
|
65
|
+
}.freeze
|
|
66
|
+
|
|
67
|
+
ZERO_INITIAL = {
|
|
68
|
+
"ㄧ" => {
|
|
69
|
+
nil => "yi", "ㄚ" => "ya", "ㄛ" => "yo", "ㄝ" => "ye", "ㄞ" => "yai", "ㄠ" => "yao",
|
|
70
|
+
"ㄡ" => "you", "ㄢ" => "yan", "ㄣ" => "yin", "ㄤ" => "yang", "ㄥ" => "ying"
|
|
71
|
+
}.freeze,
|
|
72
|
+
"ㄨ" => {
|
|
73
|
+
nil => "wu", "ㄚ" => "wa", "ㄛ" => "wo", "ㄞ" => "wai", "ㄟ" => "wei",
|
|
74
|
+
"ㄢ" => "wan", "ㄣ" => "wen", "ㄤ" => "wang", "ㄥ" => "weng"
|
|
75
|
+
}.freeze,
|
|
76
|
+
"ㄩ" => {nil => "yu", "ㄝ" => "yue", "ㄢ" => "yuan", "ㄣ" => "yun", "ㄥ" => "yong"}.freeze
|
|
77
|
+
}.freeze
|
|
78
|
+
|
|
79
|
+
EMPTY_RIME = %w[ㄓ ㄔ ㄕ ㄖ ㄗ ㄘ ㄙ].freeze
|
|
80
|
+
PALATALS = %w[ㄐ ㄑ ㄒ].freeze
|
|
81
|
+
|
|
82
|
+
module_function
|
|
83
|
+
|
|
84
|
+
def components(letters)
|
|
85
|
+
match = PATTERN.match(letters)
|
|
86
|
+
match && [match[:initial], match[:medial], match[:final]].freeze
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def parts(syllable) = [syllable.initial, syllable.medial, syllable.final]
|
|
90
|
+
|
|
91
|
+
def letters(syllable) = LETTERS.fetch(parts(syllable))
|
|
92
|
+
|
|
93
|
+
def spell(initial, medial, final)
|
|
94
|
+
return ZERO_INITIAL.fetch(medial).fetch(final) if initial.nil? && medial
|
|
95
|
+
|
|
96
|
+
rime = RIMES.fetch(medial).fetch(final)
|
|
97
|
+
rime = "i" if rime.empty? && EMPTY_RIME.include?(initial)
|
|
98
|
+
rime = rime.tr("ü", "u") if PALATALS.include?(initial)
|
|
99
|
+
"#{INITIALS.fetch(initial, "")}#{rime}"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
LETTERS = SYLLABLES.to_h { |letters| [components(letters), letters] }.freeze
|
|
103
|
+
SPELLINGS = LETTERS.keys.to_h { |parts| [parts, spell(*parts).freeze] }.freeze
|
|
104
|
+
end
|
|
105
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TaiwanPhonetics
|
|
4
|
+
module Pinyin
|
|
5
|
+
PLACEMENT = lambda do |spelling|
|
|
6
|
+
spelling.index("a") || spelling.index(/[eê]/) || spelling.index("ou") || spelling.rindex(/[iouü]/)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
REPLACEMENTS = {
|
|
10
|
+
"u:" => "u\u0308",
|
|
11
|
+
"v" => "u\u0308",
|
|
12
|
+
"ŋ" => "ng",
|
|
13
|
+
"e^" => "e\u0302",
|
|
14
|
+
"z\u0302" => "zh",
|
|
15
|
+
"c\u0302" => "ch",
|
|
16
|
+
"s\u0302" => "sh",
|
|
17
|
+
"\u0306" => "\u030C"
|
|
18
|
+
}.freeze
|
|
19
|
+
|
|
20
|
+
PASSPORT = {
|
|
21
|
+
"lyu" => ["ㄌ", "ㄩ", nil],
|
|
22
|
+
"nyu" => ["ㄋ", "ㄩ", nil],
|
|
23
|
+
"lyue" => %w[ㄌ ㄩ ㄝ],
|
|
24
|
+
"nyue" => %w[ㄋ ㄩ ㄝ],
|
|
25
|
+
"lyuan" => %w[ㄌ ㄩ ㄢ],
|
|
26
|
+
"lue" => %w[ㄌ ㄩ ㄝ],
|
|
27
|
+
"nue" => %w[ㄋ ㄩ ㄝ]
|
|
28
|
+
}.transform_values(&:freeze).freeze
|
|
29
|
+
|
|
30
|
+
UNCONTRACTED = {%w[ㄧ ㄡ] => "iou", %w[ㄨ ㄟ] => "uei", %w[ㄨ ㄣ] => "uen"}.freeze
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
def aliases
|
|
35
|
+
Inventory::LETTERS
|
|
36
|
+
.keys
|
|
37
|
+
.each_with_object(PASSPORT.dup) { |(initial, medial, final), found|
|
|
38
|
+
rime = UNCONTRACTED[[medial, final]]
|
|
39
|
+
found["#{Inventory::INITIALS[initial]}#{rime}"] = [initial, medial, final].freeze if initial && rime
|
|
40
|
+
}
|
|
41
|
+
.freeze
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TaiwanPhonetics
|
|
4
|
+
module Sandhi
|
|
5
|
+
Token = Data.define(:text, :underlying, :surface) do
|
|
6
|
+
def changed? = underlying.tone != surface.tone
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
Unit = Data.define(:text, :syllable, :phrase, :word, :word_size)
|
|
10
|
+
|
|
11
|
+
YI = "一"
|
|
12
|
+
BU = "不"
|
|
13
|
+
ORDINAL = "第"
|
|
14
|
+
COUNTING = %w[〇 零 一 二 三 四 五 六 七 八 九 十].freeze
|
|
15
|
+
MONTH = "月"
|
|
16
|
+
DATE_DAYS = %w[日 號].freeze
|
|
17
|
+
NEUTRAL_UNDERLYING = {"個" => 4}.freeze
|
|
18
|
+
ERHUA_CHARACTER = "兒"
|
|
19
|
+
NEUTRAL = 5
|
|
20
|
+
private_constant(
|
|
21
|
+
:Unit,
|
|
22
|
+
:YI,
|
|
23
|
+
:BU,
|
|
24
|
+
:ORDINAL,
|
|
25
|
+
:COUNTING,
|
|
26
|
+
:MONTH,
|
|
27
|
+
:DATE_DAYS,
|
|
28
|
+
:NEUTRAL_UNDERLYING,
|
|
29
|
+
:ERHUA_CHARACTER,
|
|
30
|
+
:NEUTRAL
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
module_function
|
|
34
|
+
|
|
35
|
+
def call(phrases)
|
|
36
|
+
units = layout(phrases)
|
|
37
|
+
base = units.map { |unit| unit.syllable.tone }
|
|
38
|
+
tones = base.dup
|
|
39
|
+
yi_bu(units, base, tones)
|
|
40
|
+
third_tone(units, base, tones)
|
|
41
|
+
units.each_with_index.map do |unit, index|
|
|
42
|
+
surface = Syllable.new(**unit.syllable.to_h, tone: tones[index])
|
|
43
|
+
Token.new(text: unit.text, underlying: unit.syllable, surface: surface)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def layout(phrases)
|
|
48
|
+
raise Error.new("phrases must be an Array of phrases", kind: :sandhi_shape) unless phrases.is_a?(Array)
|
|
49
|
+
|
|
50
|
+
word = -1
|
|
51
|
+
phrases.each_with_index.flat_map do |phrase, phrase_index|
|
|
52
|
+
unless phrase.is_a?(Array)
|
|
53
|
+
raise Error.new("a phrase must be an Array of [text, reading] words", kind: :sandhi_shape)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
phrase.flat_map do |entry|
|
|
57
|
+
word += 1
|
|
58
|
+
pieces = align(*pair(entry))
|
|
59
|
+
pieces.map { |text, syllable| Unit.new(text, syllable, phrase_index, word, pieces.length) }
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def pair(entry)
|
|
65
|
+
unless entry.is_a?(Array) && entry.length == 2
|
|
66
|
+
raise Error.new("a word must be a [text, reading] pair, got #{entry.inspect}", kind: :sandhi_shape)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
unless entry.all?(String)
|
|
70
|
+
raise Error.new("a word must be a pair of Strings, got #{entry.inspect}", kind: :not_a_string)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
entry
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def align(text, reading)
|
|
77
|
+
characters = text.chars
|
|
78
|
+
pieces = TaiwanPhonetics.syllables(reading).map do |syllable|
|
|
79
|
+
if syllable.tone.nil?
|
|
80
|
+
raise(
|
|
81
|
+
Error.new(
|
|
82
|
+
"#{reading.inspect} has a syllable without a tone; sandhi needs tones",
|
|
83
|
+
kind: :toneless_sandhi
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
character = characters.shift
|
|
89
|
+
if character.nil?
|
|
90
|
+
raise Error.new("#{reading.inspect} has more syllables than #{text.inspect}", kind: :sandhi_shape)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
character += characters.shift if syllable.erhua? && characters.first == ERHUA_CHARACTER
|
|
94
|
+
[character, syllable]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
unless characters.empty?
|
|
98
|
+
raise Error.new("#{text.inspect} has more characters than #{reading.inspect}", kind: :sandhi_shape)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
pieces
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def yi_bu(units, base, tones)
|
|
105
|
+
units.each_index do |index|
|
|
106
|
+
next if base[index] == NEUTRAL
|
|
107
|
+
|
|
108
|
+
following = following_tone(units, base, index)
|
|
109
|
+
case units[index].text
|
|
110
|
+
when YI
|
|
111
|
+
tones[index] = (following.nil? || plain_yi?(units, index)) ? 1 : shifted(base[index], following)
|
|
112
|
+
when BU
|
|
113
|
+
tones[index] = shifted(base[index], following)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def shifted(own, following)
|
|
119
|
+
return 2 if following == 4
|
|
120
|
+
return own if following == NEUTRAL && [2, 4].include?(own)
|
|
121
|
+
|
|
122
|
+
4
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def following_tone(units, base, index)
|
|
126
|
+
return nil unless same_phrase?(units, index)
|
|
127
|
+
|
|
128
|
+
tone = base[index + 1]
|
|
129
|
+
(tone == NEUTRAL) ? NEUTRAL_UNDERLYING.fetch(units[index + 1].text, tone) : tone
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def plain_yi?(units, index)
|
|
133
|
+
before = (index.zero? || !same_phrase?(units, index - 1)) ? nil : units[index - 1].text[-1]
|
|
134
|
+
after = units[index + 1].text[0]
|
|
135
|
+
counted = before == ORDINAL || COUNTING.include?(before) || COUNTING.include?(after)
|
|
136
|
+
dated = after == MONTH || (before == MONTH && DATE_DAYS.include?(after))
|
|
137
|
+
|
|
138
|
+
counted || dated || word_end?(units, index)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def third_tone(units, base, tones)
|
|
142
|
+
third = -> (index) { base[index] == 3 && ![YI, BU].include?(units[index].text) }
|
|
143
|
+
(units.length - 1).times do |index|
|
|
144
|
+
tones[index] = 2 if same_word?(units, index) && third.call(index) && third.call(index + 1)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
(units.length - 2).downto(0) do |index|
|
|
148
|
+
next if same_word?(units, index) || !same_phrase?(units, index)
|
|
149
|
+
|
|
150
|
+
tones[index] = 2 if third.call(index) && tones[index + 1] == 3
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def same_phrase?(units, index)
|
|
155
|
+
following = units[index + 1]
|
|
156
|
+
!following.nil? && following.phrase == units[index].phrase
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def same_word?(units, index)
|
|
160
|
+
following = units[index + 1]
|
|
161
|
+
!following.nil? && following.word == units[index].word
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def word_end?(units, index)
|
|
165
|
+
units[index].word_size > 1 && !same_word?(units, index)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
private_class_method(
|
|
169
|
+
:layout,
|
|
170
|
+
:pair,
|
|
171
|
+
:align,
|
|
172
|
+
:yi_bu,
|
|
173
|
+
:shifted,
|
|
174
|
+
:following_tone,
|
|
175
|
+
:plain_yi?,
|
|
176
|
+
:third_tone,
|
|
177
|
+
:same_phrase?,
|
|
178
|
+
:same_word?,
|
|
179
|
+
:word_end?
|
|
180
|
+
)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TaiwanPhonetics
|
|
4
|
+
class Scheme
|
|
5
|
+
TONE_MARKS = {"\u0304" => 1, "\u0301" => 2, "\u030C" => 3, "\u0300" => 4}.freeze
|
|
6
|
+
MARK_CHARACTERS = TONE_MARKS.invert.freeze
|
|
7
|
+
MODIFIERS = {"\u0308" => {"u" => "ü"}, "\u0302" => {"e" => "ê"}}.freeze
|
|
8
|
+
DIGITS = {
|
|
9
|
+
"0" => 5,
|
|
10
|
+
"1" => 1,
|
|
11
|
+
"2" => 2,
|
|
12
|
+
"3" => 3,
|
|
13
|
+
"4" => 4,
|
|
14
|
+
"5" => 5,
|
|
15
|
+
"⁰" => 5,
|
|
16
|
+
"¹" => 1,
|
|
17
|
+
"²" => 2,
|
|
18
|
+
"³" => 3,
|
|
19
|
+
"⁴" => 4,
|
|
20
|
+
"⁵" => 5
|
|
21
|
+
}.freeze
|
|
22
|
+
APOSTROPHES = "'’ʼ`‘"
|
|
23
|
+
VOWEL_START = /\A[aeêo]/
|
|
24
|
+
TONE_OPTIONS = %i[marks numbers none].freeze
|
|
25
|
+
DIACRITICS = [0x302, 0x308].pack("U*").freeze
|
|
26
|
+
|
|
27
|
+
attr_reader :label, :spellings
|
|
28
|
+
|
|
29
|
+
def initialize(label:, spellings:, marked:, unmarked:, placement:, **options)
|
|
30
|
+
@label = label
|
|
31
|
+
@spellings = spellings
|
|
32
|
+
@marked = marked
|
|
33
|
+
@unmarked = unmarked
|
|
34
|
+
@placement = placement
|
|
35
|
+
@suffix = options[:neutral_suffix]
|
|
36
|
+
@erhua = options.fetch(:erhua, false)
|
|
37
|
+
@apostrophe_letter = options.fetch(:apostrophe_letter, false)
|
|
38
|
+
@markable = options.fetch(:markable, /[aeêiouü]/)
|
|
39
|
+
@replacements = options.fetch(:replacements, {})
|
|
40
|
+
@numbered_unmarked = options.fetch(:numbered_unmarked, nil)
|
|
41
|
+
@plain = options.fetch(:plain, false)
|
|
42
|
+
@default_tones = marked.empty? ? :numbers : :marks
|
|
43
|
+
@tokens, @ambiguous = index(options.fetch(:aliases, {}))
|
|
44
|
+
@longest = @tokens.keys.map(&:length).max
|
|
45
|
+
freeze
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def spell(syllable, tones)
|
|
49
|
+
tones ||= @default_tones
|
|
50
|
+
unless TONE_OPTIONS.include?(tones)
|
|
51
|
+
raise Error.new("tones: must be one of #{TONE_OPTIONS.inspect}, got #{tones.inspect}", kind: :invalid_option)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
spelling = @spellings[Inventory.parts(syllable)] or
|
|
55
|
+
raise(Error.new("#{Inventory.letters(syllable)} has no spelling in #{@label}", kind: :no_spelling))
|
|
56
|
+
if syllable.erhua?
|
|
57
|
+
raise Error.new("#{@label} has no erhua spelling", kind: :no_spelling) unless @erhua
|
|
58
|
+
|
|
59
|
+
spelling = "#{spelling}r"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
tone = syllable.tone
|
|
63
|
+
return spelling if tones == :none || tone.nil?
|
|
64
|
+
return "#{spelling}#{tone}" if tones == :numbers
|
|
65
|
+
raise Error.new("#{@label} writes tones as numbers, not marks", kind: :invalid_option) if @marked.empty?
|
|
66
|
+
return "#{spelling}#{@suffix}" if tone == 5 && @suffix
|
|
67
|
+
return spelling unless @marked.include?(tone)
|
|
68
|
+
|
|
69
|
+
mark(spelling, tone)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def mark_index(spelling) = @placement.call(spelling)
|
|
73
|
+
|
|
74
|
+
def mark(spelling, tone)
|
|
75
|
+
index = mark_index(spelling)
|
|
76
|
+
"#{spelling[0..index]}#{MARK_CHARACTERS.fetch(tone)}#{spelling[(index + 1)..]}".unicode_normalize(:nfc)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def mode(words)
|
|
80
|
+
marks = words.any? { |word| marks?(word) }
|
|
81
|
+
numbers = words.any? { |word| word.match?(/[0-9⁰¹²³⁴⁵]/) }
|
|
82
|
+
raise Error.new("tone marks and tone digits mixed in one text", kind: :mixed_tone_notation) if marks && numbers
|
|
83
|
+
|
|
84
|
+
if numbers
|
|
85
|
+
:numbers
|
|
86
|
+
elsif marks
|
|
87
|
+
:marks
|
|
88
|
+
else
|
|
89
|
+
:plain
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def syllables(word, mode, unmarked)
|
|
94
|
+
groups(read(normalize(word), word)).each_with_object([]) do |(letters, ending), found|
|
|
95
|
+
if letters.map(&:first).join == "r" && @erhua && ending.nil? && found.last && !found.last.erhua?
|
|
96
|
+
found[-1] = found.last.with(erhua: true)
|
|
97
|
+
next
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
found.concat(group(letters, ending, mode, unmarked, word))
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
private
|
|
105
|
+
|
|
106
|
+
def marks?(word)
|
|
107
|
+
text = normalize(word)
|
|
108
|
+
text.match?(/[#{TONE_MARKS.keys.join}]/o) || (!@suffix.nil? && text.include?(@suffix))
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def index(aliases)
|
|
112
|
+
tokens = {}
|
|
113
|
+
ambiguous = {}
|
|
114
|
+
@spellings.each do |parts, spelling|
|
|
115
|
+
add(tokens, ambiguous, spelling, [parts, false, false])
|
|
116
|
+
next unless @erhua && !Inventory::WITHOUT_ERHUA.include?(Inventory::LETTERS[parts])
|
|
117
|
+
|
|
118
|
+
add(tokens, ambiguous, "#{spelling}r", [parts, true, true])
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
aliases.each { |spelling, parts| tokens[spelling] ||= [parts, false, true].freeze }
|
|
122
|
+
plain(tokens, ambiguous) if @plain
|
|
123
|
+
[tokens.freeze, ambiguous.freeze]
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def plain(tokens, ambiguous)
|
|
127
|
+
@spellings
|
|
128
|
+
.each_with_object(Hash.new { |hash, key| hash[key] = [] }) { |(parts, spelling), found|
|
|
129
|
+
bare = spelling.unicode_normalize(:nfd).delete(DIACRITICS).unicode_normalize(:nfc)
|
|
130
|
+
found[bare] << parts unless bare == spelling
|
|
131
|
+
}
|
|
132
|
+
.each do |bare, readings|
|
|
133
|
+
next if tokens.key?(bare) || ambiguous.key?(bare)
|
|
134
|
+
|
|
135
|
+
entries = readings.uniq.map { |parts| [parts, false, true].freeze }
|
|
136
|
+
entries.one? ? tokens[bare] = entries.first : ambiguous[bare] = entries.freeze
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def add(tokens, ambiguous, spelling, entry)
|
|
141
|
+
if tokens.key?(spelling) && tokens[spelling] != entry
|
|
142
|
+
ambiguous[spelling] = [*ambiguous.fetch(spelling, [tokens[spelling]]), entry].freeze
|
|
143
|
+
else
|
|
144
|
+
tokens[spelling] = entry.freeze
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def normalize(word)
|
|
149
|
+
text = word.unicode_normalize(:nfd).downcase.gsub(/[ɑɡ]/, "ɑ" => "a", "ɡ" => "g")
|
|
150
|
+
@replacements.reduce(text) { |result, (from, to)| result.gsub(from, to) }
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def read(text, word)
|
|
154
|
+
text.each_char.with_object([]) do |char, items|
|
|
155
|
+
last = items.last
|
|
156
|
+
if char.match?(/[a-z]/) || (@apostrophe_letter && APOSTROPHES.include?(char))
|
|
157
|
+
items << [:letter, @apostrophe_letter && APOSTROPHES.include?(char) ? "'" : char, nil]
|
|
158
|
+
elsif APOSTROPHES.include?(char)
|
|
159
|
+
items << [:split]
|
|
160
|
+
elsif (tone = TONE_MARKS[char])
|
|
161
|
+
unless last&.first == :letter && last[1].match?(@markable) && last[2].nil?
|
|
162
|
+
raise Error.new("stray tone mark in #{word.inspect}", kind: :malformed)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
last[2] = tone
|
|
166
|
+
elsif (modified = MODIFIERS.dig(char, last&.[](1).to_s)) && last.first == :letter
|
|
167
|
+
last[1] = modified
|
|
168
|
+
elsif (tone = DIGITS[char])
|
|
169
|
+
items << [:ending, tone]
|
|
170
|
+
elsif char == @suffix
|
|
171
|
+
items << [:ending, 5]
|
|
172
|
+
else
|
|
173
|
+
raise(
|
|
174
|
+
Error.new(
|
|
175
|
+
"unexpected #{char.unicode_normalize(:nfc).inspect} in #{@label} #{word.inspect}",
|
|
176
|
+
kind: :malformed
|
|
177
|
+
)
|
|
178
|
+
)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def groups(items)
|
|
184
|
+
groups = [[[], nil]]
|
|
185
|
+
items.each do |item|
|
|
186
|
+
case item.first
|
|
187
|
+
when :letter
|
|
188
|
+
groups.last.first << [item[1], item[2]]
|
|
189
|
+
when :split
|
|
190
|
+
groups << [[], nil]
|
|
191
|
+
when :ending
|
|
192
|
+
raise Error.new("a tone digit or mark without a syllable", kind: :malformed) if groups.last.first.empty?
|
|
193
|
+
|
|
194
|
+
groups.last[1] = item[1]
|
|
195
|
+
groups << [[], nil]
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
groups.reject { |letters, _| letters.empty? }
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def group(letters, ending, mode, unmarked, word)
|
|
203
|
+
plain = letters.map(&:first).join
|
|
204
|
+
marked = letters.map { |_, tone| tone ? 1 : 0 }
|
|
205
|
+
tokens = (@tokens.key?(plain) && marked.sum < 2) ? [plain] : segment(plain, marked, word)
|
|
206
|
+
offset = 0
|
|
207
|
+
tokens.each_with_index.map do |token, position|
|
|
208
|
+
tone = letters[offset, token.length].filter_map(&:last).first
|
|
209
|
+
offset += token.length
|
|
210
|
+
if position == tokens.length - 1 && ending
|
|
211
|
+
raise Error.new("two tones on #{token.inspect} in #{word.inspect}", kind: :malformed) if tone
|
|
212
|
+
|
|
213
|
+
tone = ending
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
build(token, tone || unmarked_tone(mode, unmarked, token, word), word)
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def unmarked_tone(mode, unmarked, token, word)
|
|
221
|
+
return nil if unmarked == :toneless
|
|
222
|
+
return @unmarked if unmarked == :notation
|
|
223
|
+
return @unmarked if mode == :marks
|
|
224
|
+
return nil if mode == :plain
|
|
225
|
+
return @numbered_unmarked if @numbered_unmarked
|
|
226
|
+
|
|
227
|
+
raise(
|
|
228
|
+
Error.new(
|
|
229
|
+
"every syllable needs a tone digit in #{word.inspect}, #{token.inspect} has none",
|
|
230
|
+
kind: :missing_tone_digit
|
|
231
|
+
)
|
|
232
|
+
)
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def segment(plain, marked, word)
|
|
236
|
+
best = Array.new(plain.length + 1)
|
|
237
|
+
best[0] = [[0, 0, 0, 0], []]
|
|
238
|
+
plain.length.times do |start|
|
|
239
|
+
base = best[start] or next
|
|
240
|
+
@longest.downto(1) do |length|
|
|
241
|
+
token = plain[start, length]
|
|
242
|
+
next unless token.length == length && segmentable?(token) && marked[start, length].sum < 2
|
|
243
|
+
|
|
244
|
+
score = base.first.zip(cost(token, start)).map(&:sum)
|
|
245
|
+
stop = start + length
|
|
246
|
+
best[stop] = [score, base.last + [token]] if best[stop].nil? || (score <=> best[stop].first).negative?
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
found = best.last&.last
|
|
251
|
+
return found if found
|
|
252
|
+
|
|
253
|
+
raise Error.new("cannot divide #{plain.inspect} into #{@label} syllables in #{word.inspect}", kind: :malformed)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def cost(token, start)
|
|
257
|
+
entry = @tokens[token]
|
|
258
|
+
erhua = entry&.[](1) ? 1 : 0
|
|
259
|
+
variant = entry&.[](2) ? 1 : 0
|
|
260
|
+
orphan = (start.positive? && token.match?(VOWEL_START)) ? 1 : 0
|
|
261
|
+
[orphan, erhua, variant, 1]
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def segmentable?(token)
|
|
265
|
+
entry = @tokens[token] || (@ambiguous.key?(token) && @ambiguous[token].first)
|
|
266
|
+
entry && !Inventory::NEUTRAL_ONLY.include?(Inventory::LETTERS[entry.first])
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def build(token, tone, word)
|
|
270
|
+
if @ambiguous.key?(token)
|
|
271
|
+
readings = @ambiguous[token].map { |parts, _| Inventory::LETTERS[parts] }.join(" or ")
|
|
272
|
+
raise Error.new("#{token.inspect} in #{word.inspect} is ambiguous in #{@label}: #{readings}", kind: :ambiguous)
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
(initial, medial, final), erhua, = @tokens.fetch(token)
|
|
276
|
+
Syllable.new(initial: initial, medial: medial, final: final, tone: tone, erhua: erhua)
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
end
|