Module: Ting

Defined in:
lib/ting.rb,
lib/ting/tones.rb,
lib/ting/tones.rb,
lib/ting/reader.rb,
lib/ting/writer.rb,
lib/ting/version.rb,
lib/ting/procable.rb,
lib/ting/converter.rb,
lib/ting/exception.rb,
lib/ting/tones/ipa.rb,
lib/ting/conversion.rb,
lib/ting/groundwork.rb,
lib/ting/conversions.rb,
lib/ting/tones/marks.rb,
lib/ting/memoize_call.rb,
lib/ting/tones/accents.rb,
lib/ting/tones/numbers.rb,
lib/ting/tones/no_tones.rb,
lib/ting/tones/supernum.rb,
lib/ting/conversions/hanyu.rb,
lib/ting/hanyu_pinyin_parser.rb

Defined Under Namespace

Modules: Conversions, Procable, Tones Classes: Conversion, Converter, Final, HanyuPinyinParser, Initial, MemoizeCall, ParseError, Reader, Syllable, Tone, Writer

Constant Summary collapse

SYLLABLE_CACHE =
Hash.new do |hsh, syll|
  hsh[syll] = Ting.writer(:hanyu, :accents).(
    Ting.reader(:hanyu, :numbers).(syll.downcase)
  )
end
SYLLABLE_REGEXP =

The longest syllables are six letters long (chuang, shuang, zhuang).

/[A-Za-züÜ]{1,6}\d?/
VERSION =
'0.12.0'
ILLEGAL_COMBINATIONS =

Some groups of initials and finals may not be combined This list is not exhaustive but is sufficient to resolve ambiguity

[
 [Initial::Group_0, Final::Group_0],
 [Initial::Group_1, Final::Group_0],
 [Initial::Group_2, Final::Group_0],
 [Initial::Group_3, Final::Group_0],
 [Initial::Group_4, Final::Group_0],

 [Initial::Group_4, Final::Group_U],
 [Initial::Group_4, Final::Group_A],

 [Initial::Group_3, Final::Group_I],
 [Initial::Group_5, Final::Group_I],
 [Initial::Group_6, Final::Group_I],

 [Initial::Group_1, Final::Group_V],
 [Initial::Group_3, Final::Group_V],

 # For "咯 / lo5" to parse correctly we need to list "Le + O" as valid,
 [Initial::Group_2 - [Initial::Le], [Final::O]],  #Only bo, po, mo and fo are valid -o combinations
 [Initial::Group_3, [Final::O]],
 [Initial::Group_4, [Final::O]],
 [Initial::Group_5, [Final::O]],
 [Initial::Group_6, [Final::O]],

 [[Initial::Empty], [Final::Ong]]
   # TODO: Ong is actually the same as Ueng, in Hanyu Pinyin : -ong or weng
]

Class Method Summary collapse

Class Method Details

.all_syllables(&blk) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/ting/groundwork.rb', line 173

def all_syllables( &blk )
  return to_enum(__method__) unless block_given?
  valid_combinations.map do |i,f|
    1.upto(5) do |t|
      yield Syllable.new(i,f,t,false)
      yield Syllable.new(i,f,t,true)
    end
  end
end

.bpmf(string) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/ting.rb', line 75

def bpmf(string)
  string.gsub('u:', 'ü').scan(SYLLABLE_REGEXP).map do |m|
    Ting.writer(:zhuyin, :marks).(
      Ting.reader(:hanyu, :numbers).(m.downcase)
    )
  end.join(' ')
end

.camelize(str) ⇒ Object



40
41
42
43
44
45
# File 'lib/ting.rb', line 40

def camelize(str)
  str = str.dup
  str.gsub!(/(?:_+|-+)([a-z])/){ $1.upcase }
  str.gsub!(/(\A|\s)([a-z])/){ $1 + $2.upcase }
  str
end

.from(from, from_tone) ⇒ Object



36
37
38
# File 'lib/ting.rb', line 36

def from(from, from_tone)
  Converter.new(from, from_tone, nil, nil)
end

.pretty_tones(string) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ting.rb', line 56

def pretty_tones(string)
  string = string.gsub('u:', 'ü') # (note that this implicitly dups the string)
  # Scan through the string, replacing syllable by syllable.
  pos = 0
  while match = string.match(SYLLABLE_REGEXP, pos)
    syllable = match[0]
    replacement = SYLLABLE_CACHE[syllable]
    match_pos = match.begin(0)
    # If this syllable starts with a vowel and is preceded by a letter (not whitespace or
    # control characters), insert an apostrophe before it.
    if match_pos > 0 && string[match_pos - 1] =~ /[[:alpha:]]/ && syllable =~ /^[AEOaoe]/
      replacement = "'" + replacement
    end
    string[match_pos, syllable.length] = replacement
    pos = match_pos + replacement.length
  end
  string
end

.reader(format, tones) ⇒ Object



28
29
30
# File 'lib/ting.rb', line 28

def reader(format, tones)
  Reader.new(format,tones)
end

.valid_combinations(&blk) ⇒ Object

Yields a block for any valid initial/final pair



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ting/groundwork.rb', line 161

def valid_combinations( &blk )
  return to_enum(__method__) unless block_given?
  inp = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'data', 'valid_pinyin.yaml')))
  inp.each do |final, initials|
    final = Final.const_get(final)
    initials.each do |initial, pinyin|
      initial = Initial.const_get(initial)
      yield [initial, final]
    end
  end
end

.writer(format, tones) ⇒ Object



32
33
34
# File 'lib/ting.rb', line 32

def writer(format, tones)
  Writer.new(format,tones)
end