Advertisement
quark_zju

pinyin2hanzi.rb

May 6th, 2011
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.28 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. def pinyin_to_hanzi(pinyin)
  4.     `curl -s "http://olime.baidu.com/py?py=#{pinyin}" | iconv -f gb18030 -t utf8`[/"[^"]*/][1..-1]
  5. end
  6.  
  7. STDIN.lines do |l|
  8.     pinyins = l.gsub(' ', "").split /[^a-z]/
  9.     puts pinyins.inject([]) { |a, p| a << pinyin_to_hanzi(p) }.join ' '
  10. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement