Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string_to_pinyin.rb

Instance Method Summary collapse

Instance Method Details

#to_pinyinObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/string_to_pinyin.rb', line 7

def to_pinyin
  idx_file_path = File.expand_path('../../data/idx99-tone.txt',__FILE__)
  result = ""
  self.scan(/./) do |char|
    match = %x[grep -m1 '^#{char}' #{idx_file_path}]
    if match.empty?
      result = result + char
    else
      pinyin =  match.gsub("\n","").split("\t")[1]
      result = result + pinyin + " "
    end
  end
  return result.rstrip
end