module Pinyin

Constants

AntiPinyin
AntiRegex
Pinyin
Regex
Vowels

Public Class Methods

accent_map() click to toggle source
# File lib/pinyinator.rb, line 82
def self.accent_map
  unless @accent_map
    @accent_map = {}
    stars = ('a*i a*o e*i ia* ia*o ie* io* iu* ' +
             'A*I A*O E*I IA* IA*O IE* IO* IU* ' +
             'o*u ua* ua*i ue* ui* uo* üe* ' +
             'O*U UA* UA*I UE* UI* UO* ÜE* ' +
             'A* E* I* O* U* Ü* ' +
             'a* e* i* o* u* ü*').split(' ')
    nostars = stars.map {|s| s.sub(/\*/, '')}

    nostars.each_with_index do |k, i| 
      @accent_map[k] = stars[i] 
    end
  end
  @accent_map   
end
from_pinyin(string) click to toggle source
# File lib/pinyinator.rb, line 120
def self.from_pinyin(string)
  string.scan(AntiRegex).each do |match|
    pinyin = match[0]
    string = string.sub(pinyin, AntiPinyin[pinyin])
  end
  string
end
replacement_for(word, tone) click to toggle source
# File lib/pinyinator.rb, line 100
def self.replacement_for(word, tone)
  word = word.gsub('v', 'ü').gsub('V', 'Ü')
  accent_map.each_pair do |base, vowel|
    if word.index(base)
      vowel_char = vowel.scan(/\w\*/)[0]
      vowel_num = Vowels[vowel_char]
      accented_vowel_char = Pinyin[tone][vowel_num]
      return replaced_word = word.sub(base, vowel).sub(vowel_char, accented_vowel_char)
    end 
  end
  match
end
to_pinyin(string) click to toggle source
# File lib/pinyinator.rb, line 113
def self.to_pinyin(string) 
  string.scan(Regex).each do |word, tone|
    string = string.sub(word + tone, replacement_for(word, tone))
  end
  string 
end

Public Instance Methods

from_pinyin() click to toggle source
# File lib/pinyinator.rb, line 134
def from_pinyin
  Pinyin::from_pinyin(self.to_s)
end
to_pinyin() click to toggle source

instance methods

# File lib/pinyinator.rb, line 130
def to_pinyin
  Pinyin::to_pinyin(self.to_s)
end