Module: Pinny::ModuleMethods

Included in:
Pinny
Defined in:
lib/pinny.rb

Instance Method Summary collapse

Instance Method Details

#add_tone_mark(word) ⇒ Object



48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



29
30
31
32
33
34
35
# 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) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/pinny.rb', line 25

def is_pinyin?(word)
  word =~ /^\w+[1-5]$/
end

#lowest_vowel(word) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
# 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