module Ting

Constants

ILLEGAL_COMBINATIONS

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

SYLLABLE_CACHE
SYLLABLE_REGEXP

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

VERSION

Public Class Methods

all_syllables( ) { |syllable| ... } click to toggle source
# File lib/ting/groundwork.rb, line 172
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) click to toggle source
# File lib/ting.rb, line 74
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) click to toggle source
# File lib/ting.rb, line 39
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) click to toggle source
# File lib/ting.rb, line 35
def from(from, from_tone)
  Converter.new(from, from_tone, nil, nil)
end
pretty_tones(string) click to toggle source
# File lib/ting.rb, line 55
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) click to toggle source
# File lib/ting.rb, line 27
def reader(format, tones)
  Reader.new(format,tones)
end
valid_combinations( ) { |initial, final| ... } click to toggle source

Yields a block for any valid initial/final pair

# File lib/ting/groundwork.rb, line 160
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) click to toggle source
# File lib/ting.rb, line 31
def writer(format, tones)
  Writer.new(format,tones)
end