module Pinny::ModuleMethods

Public Instance Methods

add_tone_mark(word) click to toggle source
# File lib/pinny.rb, line 48
def add_tone_mark(word)
  if t = extract_tone(word)
    v = lowest_vowel(word)
    r = TONES[v] && TONES[v][t]
    word.sub(v, r)
  else
    word
  end
end
extract_tone(word) click to toggle source
# File lib/pinny.rb, line 29
def extract_tone(word)
  tone = word.scan(/[1-4]$/).first

  word.sub!(/\d$/, "")

  tone && tone.to_i
end
is_pinyin?(word) click to toggle source
# File lib/pinny.rb, line 25
def is_pinyin?(word)
  word =~ /^\w+[1-5]$/
end
lowest_vowel(word) click to toggle source
# File lib/pinny.rb, line 37
def lowest_vowel(word)
  case word
  when /a/i then "a"
  when /e/i then "e"
  when /i/i then "i"
  when /o/i then "o"
  when /u/i then "u"
  when /v/i then "v"
  end
end
to_pinyin(string) click to toggle source
# File lib/pinny.rb, line 15
def to_pinyin(string)
  string.split(/\b/).map do |word|
    if is_pinyin?(word)
      add_tone_mark(word)
    else
      word
    end
  end.join("")
end