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.
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TaiwanPhonetics
4
+ module Spellings
5
+ TONGYONG = {
6
+ initials: {
7
+ "ㄅ" => "b", "ㄆ" => "p", "ㄇ" => "m", "ㄈ" => "f", "ㄉ" => "d", "ㄊ" => "t", "ㄋ" => "n",
8
+ "ㄌ" => "l", "ㄍ" => "g", "ㄎ" => "k", "ㄏ" => "h", "ㄐ" => "j", "ㄑ" => "c", "ㄒ" => "s",
9
+ "ㄓ" => "jh", "ㄔ" => "ch", "ㄕ" => "sh", "ㄖ" => "r", "ㄗ" => "z", "ㄘ" => "c", "ㄙ" => "s"
10
+ },
11
+ rimes: {
12
+ nil => {
13
+ nil => "ih", "ㄚ" => "a", "ㄛ" => "o", "ㄜ" => "e", "ㄝ" => "ê", "ㄞ" => "ai", "ㄟ" => "ei",
14
+ "ㄠ" => "ao", "ㄡ" => "ou", "ㄢ" => "an", "ㄣ" => "en", "ㄤ" => "ang", "ㄥ" => "eng", "ㄦ" => "er"
15
+ },
16
+ "ㄧ" => {
17
+ nil => "i", "ㄚ" => "ia", "ㄛ" => "io", "ㄝ" => "ie", "ㄞ" => "iai", "ㄠ" => "iao",
18
+ "ㄡ" => "iou", "ㄢ" => "ian", "ㄣ" => "in", "ㄤ" => "iang", "ㄥ" => "ing"
19
+ },
20
+ "ㄨ" => {
21
+ nil => "u", "ㄚ" => "ua", "ㄛ" => "uo", "ㄞ" => "uai", "ㄟ" => "uei",
22
+ "ㄢ" => "uan", "ㄣ" => "un", "ㄤ" => "uang", "ㄥ" => "ong"
23
+ },
24
+ "ㄩ" => {nil => "yu", "ㄝ" => "yue", "ㄢ" => "yuan", "ㄣ" => "yun", "ㄥ" => "yong"}
25
+ },
26
+ zero: {
27
+ "ㄧ" => {
28
+ nil => "yi", "ㄚ" => "ya", "ㄛ" => "yo", "ㄝ" => "ye", "ㄞ" => "yai", "ㄠ" => "yao",
29
+ "ㄡ" => "you", "ㄢ" => "yan", "ㄣ" => "yin", "ㄤ" => "yang", "ㄥ" => "ying"
30
+ },
31
+ "ㄨ" => {
32
+ nil => "wu", "ㄚ" => "wa", "ㄛ" => "wo", "ㄞ" => "wai", "ㄟ" => "wei",
33
+ "ㄢ" => "wan", "ㄣ" => "wun", "ㄤ" => "wang", "ㄥ" => "wong"
34
+ },
35
+ "ㄩ" => {nil => "yu", "ㄝ" => "yue", "ㄢ" => "yuan", "ㄣ" => "yun", "ㄥ" => "yong"}
36
+ },
37
+ overrides: {%w[ㄈ] + [nil, "ㄥ"] => "fong"}
38
+ }.freeze
39
+
40
+ MPS2 = {
41
+ initials: {
42
+ "ㄅ" => "b", "ㄆ" => "p", "ㄇ" => "m", "ㄈ" => "f", "ㄉ" => "d", "ㄊ" => "t", "ㄋ" => "n",
43
+ "ㄌ" => "l", "ㄍ" => "g", "ㄎ" => "k", "ㄏ" => "h", "ㄐ" => "j", "ㄑ" => "ch", "ㄒ" => "sh",
44
+ "ㄓ" => "j", "ㄔ" => "ch", "ㄕ" => "sh", "ㄖ" => "r", "ㄗ" => "tz", "ㄘ" => "ts", "ㄙ" => "s"
45
+ },
46
+ rimes: {
47
+ nil => {
48
+ nil => "", "ㄚ" => "a", "ㄛ" => "o", "ㄜ" => "e", "ㄞ" => "ai", "ㄟ" => "ei",
49
+ "ㄠ" => "au", "ㄡ" => "ou", "ㄢ" => "an", "ㄣ" => "en", "ㄤ" => "ang", "ㄥ" => "eng", "ㄦ" => "er"
50
+ },
51
+ "ㄧ" => {
52
+ nil => "i", "ㄚ" => "ia", "ㄛ" => "io", "ㄝ" => "ie", "ㄞ" => "iai", "ㄠ" => "iau",
53
+ "ㄡ" => "iou", "ㄢ" => "ian", "ㄣ" => "in", "ㄤ" => "iang", "ㄥ" => "ing"
54
+ },
55
+ "ㄨ" => {
56
+ nil => "u", "ㄚ" => "ua", "ㄛ" => "uo", "ㄞ" => "uai", "ㄟ" => "uei",
57
+ "ㄢ" => "uan", "ㄣ" => "uen", "ㄤ" => "uang", "ㄥ" => "ung"
58
+ },
59
+ "ㄩ" => {nil => "iu", "ㄝ" => "iue", "ㄢ" => "iuan", "ㄣ" => "iun", "ㄥ" => "iung"}
60
+ },
61
+ zero: {
62
+ "ㄧ" => {
63
+ nil => "yi", "ㄚ" => "ya", "ㄛ" => "yo", "ㄝ" => "ye", "ㄞ" => "yai", "ㄠ" => "yau",
64
+ "ㄡ" => "you", "ㄢ" => "yan", "ㄣ" => "yin", "ㄤ" => "yang", "ㄥ" => "ying"
65
+ },
66
+ "ㄨ" => {
67
+ nil => "wu", "ㄚ" => "wa", "ㄛ" => "wo", "ㄞ" => "wai", "ㄟ" => "wei",
68
+ "ㄢ" => "wan", "ㄣ" => "wen", "ㄤ" => "wang", "ㄥ" => "weng"
69
+ },
70
+ "ㄩ" => {nil => "yu", "ㄝ" => "yue", "ㄢ" => "yuan", "ㄣ" => "yun", "ㄥ" => "yung"}
71
+ },
72
+ empty_rime: {"ㄓ" => "jr", "ㄔ" => "chr", "ㄕ" => "shr", "ㄖ" => "r", "ㄗ" => "tz", "ㄘ" => "tsz", "ㄙ" => "sz"},
73
+ overrides: {}
74
+ }.freeze
75
+
76
+ WADE_GILES = {
77
+ initials: {
78
+ "ㄅ" => "p", "ㄆ" => "p'", "ㄇ" => "m", "ㄈ" => "f", "ㄉ" => "t", "ㄊ" => "t'", "ㄋ" => "n",
79
+ "ㄌ" => "l", "ㄍ" => "k", "ㄎ" => "k'", "ㄏ" => "h", "ㄐ" => "ch", "ㄑ" => "ch'", "ㄒ" => "hs",
80
+ "ㄓ" => "ch", "ㄔ" => "ch'", "ㄕ" => "sh", "ㄖ" => "j", "ㄗ" => "ts", "ㄘ" => "ts'", "ㄙ" => "s"
81
+ },
82
+ rimes: {
83
+ nil => {
84
+ nil => "", "ㄚ" => "a", "ㄛ" => "o", "ㄜ" => "ê", "ㄞ" => "ai", "ㄟ" => "ei",
85
+ "ㄠ" => "ao", "ㄡ" => "ou", "ㄢ" => "an", "ㄣ" => "ên", "ㄤ" => "ang", "ㄥ" => "êng", "ㄦ" => "êrh"
86
+ },
87
+ "ㄧ" => {
88
+ nil => "i", "ㄚ" => "ia", "ㄛ" => "io", "ㄝ" => "ieh", "ㄞ" => "iai", "ㄠ" => "iao",
89
+ "ㄡ" => "iu", "ㄢ" => "ien", "ㄣ" => "in", "ㄤ" => "iang", "ㄥ" => "ing"
90
+ },
91
+ "ㄨ" => {
92
+ nil => "u", "ㄚ" => "ua", "ㄛ" => "uo", "ㄞ" => "uai", "ㄟ" => "ui",
93
+ "ㄢ" => "uan", "ㄣ" => "un", "ㄤ" => "uang", "ㄥ" => "ung"
94
+ },
95
+ "ㄩ" => {nil => "ü", "ㄝ" => "üeh", "ㄢ" => "üan", "ㄣ" => "ün", "ㄥ" => "iung"}
96
+ },
97
+ zero: {
98
+ "ㄧ" => {
99
+ nil => "i", "ㄚ" => "ya", "ㄛ" => "yo", "ㄝ" => "yeh", "ㄞ" => "yai", "ㄠ" => "yao",
100
+ "ㄡ" => "yu", "ㄢ" => "yen", "ㄣ" => "yin", "ㄤ" => "yang", "ㄥ" => "ying"
101
+ },
102
+ "ㄨ" => {
103
+ nil => "wu", "ㄚ" => "wa", "ㄛ" => "wo", "ㄞ" => "wai", "ㄟ" => "wei",
104
+ "ㄢ" => "wan", "ㄣ" => "wên", "ㄤ" => "wang", "ㄥ" => "wêng"
105
+ },
106
+ "ㄩ" => {nil => "yü", "ㄝ" => "yüeh", "ㄢ" => "yüan", "ㄣ" => "yün", "ㄥ" => "yung"}
107
+ },
108
+ empty_rime: {
109
+ "ㄓ" => "chih", "ㄔ" => "ch'ih", "ㄕ" => "shih", "ㄖ" => "jih", "ㄗ" => "tzu", "ㄘ" => "tz'u", "ㄙ" => "ssu"
110
+ },
111
+ after: {
112
+ %w[ㄨ ㄛ] => %w[ㄉ ㄊ ㄋ ㄌ ㄓ ㄔ ㄖ ㄗ ㄘ ㄙ].to_h { |initial| [initial, "o"] },
113
+ %w[ㄨ ㄟ] => %w[ㄍ ㄎ].to_h { |initial| [initial, "uei"] }
114
+ },
115
+ overrides: {}
116
+ }.freeze
117
+
118
+ WADE_GILES_ALIASES = {
119
+ "ko" => ["ㄍ", nil, "ㄜ"], "k'o" => ["ㄎ", nil, "ㄜ"], "ho" => ["ㄏ", nil, "ㄜ"], "yi" => [nil, "ㄧ", nil]
120
+ }.transform_values(&:freeze).freeze
121
+
122
+ MPS2_PLACEMENT = ->(spelling) { Pinyin::PLACEMENT.call(spelling) || (spelling.length - 1) }
123
+
124
+ module_function
125
+
126
+ def build(rules)
127
+ Inventory::LETTERS.keys.each_with_object({}) do |parts, table|
128
+ spelling = spell(rules, *parts)
129
+ table[parts] = spelling.freeze if spelling
130
+ end.freeze
131
+ end
132
+
133
+ def spell(rules, initial, medial, final)
134
+ return rules[:overrides][[initial, medial, final]] if rules[:overrides].key?([initial, medial, final])
135
+ return rules.dig(:zero, medial, final) if initial.nil? && medial
136
+ return rules.dig(:rimes, nil, final) if initial.nil?
137
+ return empty_rime(rules, initial) if medial.nil? && final.nil?
138
+
139
+ rime = rules.dig(:rimes, medial, final) or return nil
140
+ rime = rules.dig(:after, [medial, final], initial) || rime
141
+ "#{rules[:initials][initial]}#{rime}"
142
+ end
143
+
144
+ def empty_rime(rules, initial)
145
+ return nil unless Inventory::EMPTY_RIME.include?(initial)
146
+
147
+ rules[:empty_rime]&.[](initial) || "#{rules[:initials][initial]}#{rules[:rimes][nil][nil]}"
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TaiwanPhonetics
4
+ Syllable = Data.define(:initial, :medial, :final, :tone, :erhua) do
5
+ def initialize(initial: nil, medial: nil, final: nil, tone: 1, erhua: false)
6
+ letters = Inventory::LETTERS[[initial, medial, final]]
7
+ if letters.nil?
8
+ raise Error.new("not a syllable of the inventory: #{[initial, medial, final].join}", kind: :not_in_inventory)
9
+ end
10
+
11
+ unless tone.nil? || (tone.is_a?(Integer) && Inventory::TONES.include?(tone))
12
+ raise Error.new("tone must be an Integer from 1 to 5 or nil, got #{tone.inspect}", kind: :invalid_option)
13
+ end
14
+
15
+ if tone && tone != 5 && Inventory::NEUTRAL_ONLY.include?(letters)
16
+ raise Error.new("#{letters} carries only the neutral tone", kind: :not_in_inventory)
17
+ end
18
+
19
+ unless [true, false].include?(erhua)
20
+ raise Error.new("erhua must be true or false, got #{erhua.inspect}", kind: :invalid_option)
21
+ end
22
+
23
+ if erhua && Inventory::WITHOUT_ERHUA.include?(letters)
24
+ raise Error.new("#{letters} takes no erhua suffix", kind: :not_in_inventory)
25
+ end
26
+
27
+ super
28
+ end
29
+
30
+ def spell(system = :zhuyin, tones: nil) = Systems.spell(self, system, tones)
31
+
32
+ def to_zhuyin = spell(:zhuyin)
33
+
34
+ def to_pinyin(numbered: false) = spell(:pinyin, tones: numbered ? :numbers : :marks)
35
+
36
+ def erhua? = erhua
37
+
38
+ def to_s = to_zhuyin
39
+ end
40
+ end
@@ -0,0 +1,144 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TaiwanPhonetics
4
+ module Systems
5
+ NAMES = %i[zhuyin pinyin tongyong wade_giles mps2].freeze
6
+ UNMARKED = [nil, :toneless, :notation].freeze
7
+ CHINESE_NAMES = {
8
+ "注音" => :zhuyin,
9
+ "漢語拼音" => :pinyin,
10
+ "拼音" => :pinyin,
11
+ "通用拼音" => :tongyong,
12
+ "威妥瑪" => :wade_giles,
13
+ "威妥瑪拼音" => :wade_giles,
14
+ "注音二式" => :mps2,
15
+ "國語注音符號第二式" => :mps2
16
+ }.freeze
17
+
18
+ SCHEMES = {
19
+ pinyin: Scheme.new(
20
+ label: "Hanyu Pinyin",
21
+ spellings: Inventory::SPELLINGS,
22
+ marked: [1, 2, 3, 4],
23
+ unmarked: 5,
24
+ placement: Pinyin::PLACEMENT,
25
+ erhua: true,
26
+ aliases: Pinyin.aliases,
27
+ replacements: Pinyin::REPLACEMENTS
28
+ ),
29
+ tongyong: Scheme.new(
30
+ label: "Tongyong Pinyin",
31
+ spellings: Spellings.build(Spellings::TONGYONG),
32
+ marked: [2, 3, 4],
33
+ unmarked: 1,
34
+ placement: Pinyin::PLACEMENT,
35
+ neutral_suffix: "˚",
36
+ replacements: {"\u0306" => "\u030C"}
37
+ ),
38
+ wade_giles: Scheme.new(
39
+ label: "Wade–Giles",
40
+ spellings: Spellings.build(Spellings::WADE_GILES),
41
+ marked: [],
42
+ unmarked: 1,
43
+ placement: nil,
44
+ apostrophe_letter: true,
45
+ numbered_unmarked: 1,
46
+ plain: true,
47
+ aliases: Spellings::WADE_GILES_ALIASES,
48
+ replacements: {"u:" => "u\u0308", "\u0306" => ""}
49
+ ),
50
+ mps2: Scheme.new(
51
+ label: "MPS II",
52
+ spellings: Spellings.build(Spellings::MPS2),
53
+ marked: [1, 2, 3, 4],
54
+ unmarked: 5,
55
+ placement: Spellings::MPS2_PLACEMENT,
56
+ markable: /[aeêioruz]/,
57
+ replacements: {"\u0306" => "\u030C"}
58
+ )
59
+ }.freeze
60
+
61
+ module_function
62
+
63
+ def resolve(name)
64
+ return name if NAMES.include?(name)
65
+ return name.to_sym if name.is_a?(String) && NAMES.map(&:to_s).include?(name)
66
+
67
+ CHINESE_NAMES.fetch(name) do
68
+ raise Error.new("unknown system #{name.inspect}; use one of #{NAMES.inspect}", kind: :unknown_system)
69
+ end
70
+ end
71
+
72
+ def scheme(name) = SCHEMES.fetch(resolve(name))
73
+
74
+ def spell(syllable, system, tones)
75
+ system = resolve(system)
76
+ return scheme(system).spell(syllable, tones) unless system == :zhuyin
77
+ unless [nil, :marks, :none].include?(tones)
78
+ raise Error.new("zhuyin writes tones as marks; tones: must be :marks or :none", kind: :invalid_option)
79
+ end
80
+
81
+ body = if tones == :none || syllable.tone.nil?
82
+ Inventory.letters(syllable)
83
+ elsif syllable.tone == 5
84
+ "#{Inventory::NEUTRAL_MARK}#{Inventory.letters(syllable)}"
85
+ else
86
+ "#{Inventory.letters(syllable)}#{Inventory::TONE_MARKS.fetch(syllable.tone)}"
87
+ end
88
+
89
+ syllable.erhua? ? "#{body}#{Inventory::ERHUA}" : body
90
+ end
91
+
92
+ def options(from, unmarked)
93
+ unless UNMARKED.include?(unmarked)
94
+ raise Error.new("unmarked: must be nil, :toneless or :notation, got #{unmarked.inspect}", kind: :invalid_option)
95
+ end
96
+
97
+ from.nil? ? nil : resolve(from)
98
+ end
99
+
100
+ def check_tones(system, tones)
101
+ return if tones.nil?
102
+
103
+ allowed = (system == :zhuyin) ? %i[marks none] : Scheme::TONE_OPTIONS
104
+ allowed -= [:marks] if system == :wade_giles
105
+ return if allowed.include?(tones)
106
+
107
+ raise(
108
+ Error.new(
109
+ "tones: for #{system} must be one of #{allowed.inspect}, got #{tones.inspect}",
110
+ kind: :invalid_option
111
+ )
112
+ )
113
+ end
114
+
115
+ def read(text, from, unmarked)
116
+ from = options(from, unmarked)
117
+ tokens = Text.tokens(text)
118
+ latin = from.nil? ? scheme(:pinyin) : (from == :zhuyin ? nil : scheme(from))
119
+ words = tokens.grep(Text::Word)
120
+ check_scripts(words, from)
121
+ mode = latin&.mode(words.select { |word| word.script == :latin }.map(&:text))
122
+ tokens.map do |token|
123
+ next token unless token.is_a?(Text::Word)
124
+ next Bopomofo.syllables(token.text, unmarked: unmarked) if token.script == :zhuyin
125
+
126
+ latin.syllables(token.text, mode, unmarked)
127
+ end
128
+ end
129
+
130
+ def check_scripts(words, from)
131
+ return if from.nil?
132
+
133
+ wrong = words.find { |word| (word.script == :zhuyin) != (from == :zhuyin) }
134
+ return unless wrong
135
+
136
+ raise(
137
+ Error.new(
138
+ "#{wrong.text.inspect} is not #{(from == :zhuyin) ? "zhuyin" : scheme(from).label}",
139
+ kind: :wrong_script
140
+ )
141
+ )
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TaiwanPhonetics
4
+ module Text
5
+ Word = Data.define(:text, :script)
6
+
7
+ FULL_WIDTH = /[0-9A-Za-z']/
8
+ ZHUYIN_CORE = "ㄅ-ㄯㆠ-ㆿˉˊˇˋ˙"
9
+ ZHUYIN_CHAR = "#{ZHUYIN_CORE}´``^·・•‧一丨︱"
10
+ LATIN_CHAR = "[\\p{L}\\p{M}0-9⁰¹²³⁴⁵˚&&[^\\p{Han}#{ZHUYIN_CORE}]]"
11
+ APOSTROPHES = "'’ʼ`‘"
12
+ TOKEN = /
13
+ (?<zhuyin>[#{ZHUYIN_CHAR}]*[#{ZHUYIN_CORE}][#{ZHUYIN_CHAR}]*)
14
+ |(?<latin>#{LATIN_CHAR}(?:#{LATIN_CHAR}|[#{APOSTROPHES}](?=#{LATIN_CHAR})|(?<=[uU]):|(?<=[eE])\^)*)
15
+ |(?<other>.)
16
+ /mx
17
+ NUMBER = /\A[0-9⁰¹²³⁴⁵]+\z/
18
+
19
+ module_function
20
+
21
+ def tokens(text)
22
+ raise Error.new("expected a String, got #{text.class}", kind: :not_a_string) unless text.is_a?(String)
23
+
24
+ folded = utf8(text).gsub(FULL_WIDTH) { |char| (char.ord - 0xFEE0).chr(Encoding::UTF_8) }
25
+ list = []
26
+ folded.scan(TOKEN) { list << classify(Regexp.last_match, list.last) }
27
+ list
28
+ end
29
+
30
+ def utf8(text)
31
+ converted = text.encode(Encoding::UTF_8)
32
+ return converted if converted.valid_encoding?
33
+
34
+ raise Error.new("text is not valid #{text.encoding}", kind: :invalid_encoding)
35
+ rescue EncodingError
36
+ raise Error.new("text is not valid Unicode", kind: :invalid_encoding)
37
+ end
38
+
39
+ def classify(match, previous)
40
+ return Word.new(match[:zhuyin], :zhuyin) if match[:zhuyin]
41
+
42
+ if (latin = match[:latin])
43
+ return Word.new(latin, :latin) unless latin.match?(NUMBER)
44
+ if previous.is_a?(Word) && previous.script == :zhuyin
45
+ raise(
46
+ Error.new(
47
+ "tone digits after zhuyin are not a zhuyin notation: #{latin}",
48
+ kind: :tone_digits_after_zhuyin
49
+ )
50
+ )
51
+ end
52
+
53
+ return latin
54
+ end
55
+
56
+ other = match[:other]
57
+ if other.match?(/\p{Han}/)
58
+ raise Error.new("#{other} is a Han character; convert readings, not characters", kind: :han_character)
59
+ end
60
+
61
+ other
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TaiwanPhonetics
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "taiwan_phonetics/version"
4
+ require_relative "taiwan_phonetics/error"
5
+ require_relative "taiwan_phonetics/inventory"
6
+ require_relative "taiwan_phonetics/syllable"
7
+ require_relative "taiwan_phonetics/pinyin"
8
+ require_relative "taiwan_phonetics/spellings"
9
+ require_relative "taiwan_phonetics/scheme"
10
+ require_relative "taiwan_phonetics/systems"
11
+ require_relative "taiwan_phonetics/bopomofo"
12
+ require_relative "taiwan_phonetics/text"
13
+ require_relative "taiwan_phonetics/sandhi"
14
+
15
+ module TaiwanPhonetics
16
+ private_constant :Inventory, :Pinyin, :Spellings, :Scheme, :Systems, :Bopomofo, :Text
17
+
18
+ module_function
19
+
20
+ def convert(text, to:, from: nil, tones: nil, unmarked: nil)
21
+ to = Systems.resolve(to)
22
+ Systems.check_tones(to, tones)
23
+
24
+ output = +""
25
+ previous_word = false
26
+ Systems.read(text, from, unmarked).each do |item|
27
+ if item.is_a?(Array)
28
+ output << " " if previous_word
29
+ output << item.map { |syllable| syllable.spell(to, tones: tones) }.join(" ")
30
+ else
31
+ output << item
32
+ end
33
+
34
+ previous_word = item.is_a?(Array)
35
+ end
36
+
37
+ output
38
+ end
39
+
40
+ def to_pinyin(text, numbered: false) = convert(text, to: :pinyin, tones: numbered ? :numbers : :marks)
41
+
42
+ def to_zhuyin(text) = convert(text, to: :zhuyin)
43
+
44
+ def syllables(text, from: nil, unmarked: nil) = Systems.read(text, from, unmarked).grep(Array).flatten
45
+
46
+ def syllable(text, from: nil, unmarked: nil)
47
+ Systems.options(from, unmarked)
48
+ raise Error.new("expected a String, got #{text.class}", kind: :not_a_string) unless text.is_a?(String)
49
+ raise Error.new("expected one syllable, got #{text.inspect}", kind: :malformed) if text.match?(/[[:space:]]/)
50
+
51
+ items = Systems.read(text, from, unmarked)
52
+ found = items.grep(Array).flatten
53
+ return found.first if found.length == 1 && items.length == 1
54
+
55
+ raise Error.new("expected one syllable in #{text.inspect}, found #{found.length}", kind: :malformed)
56
+ end
57
+
58
+ def valid?(text, from: nil)
59
+ Systems.options(from, nil)
60
+ begin
61
+ syllable(text, from: from)
62
+ true
63
+ rescue Error
64
+ false
65
+ end
66
+ end
67
+
68
+ def sandhi(phrases) = Sandhi.call(phrases)
69
+ end
@@ -0,0 +1,58 @@
1
+ module TaiwanPhonetics
2
+ VERSION: String
3
+
4
+ type system = :zhuyin | :pinyin | :tongyong | :wade_giles | :mps2 | ::String
5
+ type tones = :marks | :numbers | :none | nil
6
+ type unmarked = :toneless | :notation | nil
7
+ type word = [::String, ::String]
8
+ type phrase = ::Array[word]
9
+
10
+ class Error < ::StandardError
11
+ KINDS: ::Array[::Symbol]
12
+
13
+ attr_reader kind: ::Symbol
14
+
15
+ def initialize: (::String, kind: ::Symbol) -> void
16
+ end
17
+
18
+ def self.convert: (::String, to: system, ?from: system?, ?tones: tones, ?unmarked: unmarked) -> ::String
19
+ def self.to_pinyin: (::String, ?numbered: bool) -> ::String
20
+ def self.to_zhuyin: (::String) -> ::String
21
+ def self.syllables: (::String, ?from: system?, ?unmarked: unmarked) -> ::Array[Syllable]
22
+ def self.syllable: (::String, ?from: system?, ?unmarked: unmarked) -> Syllable
23
+ def self.valid?: (untyped, ?from: system?) -> bool
24
+ def self.sandhi: (::Array[phrase]) -> ::Array[Sandhi::Token]
25
+
26
+ class Syllable
27
+ attr_reader initial: ::String?
28
+ attr_reader medial: ::String?
29
+ attr_reader final: ::String?
30
+ attr_reader tone: ::Integer?
31
+ attr_reader erhua: bool
32
+
33
+ def self.new: (?initial: ::String?, ?medial: ::String?, ?final: ::String?, ?tone: ::Integer?, ?erhua: bool) -> Syllable
34
+
35
+ def spell: (?system, ?tones: tones) -> ::String
36
+ def to_zhuyin: () -> ::String
37
+ def to_pinyin: (?numbered: bool) -> ::String
38
+ def erhua?: () -> bool
39
+ def to_s: () -> ::String
40
+ def with: (**untyped) -> Syllable
41
+ def to_h: () -> ::Hash[::Symbol, untyped]
42
+ end
43
+
44
+ module Sandhi
45
+ class Token
46
+ attr_reader text: ::String
47
+ attr_reader underlying: Syllable
48
+ attr_reader surface: Syllable
49
+
50
+ def self.new: (text: ::String, underlying: Syllable, surface: Syllable) -> Token
51
+ def changed?: () -> bool
52
+ def with: (**untyped) -> Token
53
+ def to_h: () -> ::Hash[::Symbol, untyped]
54
+ end
55
+
56
+ def self.call: (::Array[phrase]) -> ::Array[Token]
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: taiwan-phonetics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Den Patin 丁丹旭
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rake
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '13.4'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '13.4'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rbs
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.1'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '4.1'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.13'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.13'
54
+ description: Converts readings of Taiwanese Mandarin between zhuyin, Hanyu Pinyin,
55
+ Tongyong Pinyin, Wade–Giles and MPS II, in both directions, with tone marks, tone
56
+ numbers or no tones. Reads the forms people actually type, validates syllables against
57
+ the 418 syllables of the CNS 11643 voice index, and applies the tone changes of
58
+ 一, 不 and third tones. Taiwan conventions only. No dependencies.
59
+ email: hi@dpat.in
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - CHANGELOG.md
65
+ - LICENSE
66
+ - NOTICES.md
67
+ - README.md
68
+ - lib/taiwan-phonetics.rb
69
+ - lib/taiwan_phonetics.rb
70
+ - lib/taiwan_phonetics/bopomofo.rb
71
+ - lib/taiwan_phonetics/error.rb
72
+ - lib/taiwan_phonetics/inventory.rb
73
+ - lib/taiwan_phonetics/pinyin.rb
74
+ - lib/taiwan_phonetics/sandhi.rb
75
+ - lib/taiwan_phonetics/scheme.rb
76
+ - lib/taiwan_phonetics/spellings.rb
77
+ - lib/taiwan_phonetics/syllable.rb
78
+ - lib/taiwan_phonetics/systems.rb
79
+ - lib/taiwan_phonetics/text.rb
80
+ - lib/taiwan_phonetics/version.rb
81
+ - sig/taiwan_phonetics.rbs
82
+ homepage: https://github.com/taiwancards/taiwan-phonetics
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ source_code_uri: https://github.com/taiwancards/taiwan-phonetics
87
+ changelog_uri: https://github.com/taiwancards/taiwan-phonetics/blob/main/CHANGELOG.md
88
+ bug_tracker_uri: https://github.com/taiwancards/taiwan-phonetics/issues
89
+ rubygems_mfa_required: 'true'
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '3.4'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 4.0.20
105
+ specification_version: 4
106
+ summary: 'Mandarin as written and spoken in Taiwan: zhuyin, Hanyu Pinyin, Tongyong
107
+ Pinyin, Wade–Giles, MPS II and tone sandhi'
108
+ test_files: []