ChinesePinyin
Translate chinese hanzi to pinyin.
The dict is borrowed from http://github.com/fayland/perl-lingua-han/tree/master/Lingua-Han-PinYin/
Install
gem install chinese_pinyin
or add in Gemfile.
gem 'chinese_pinyin'
Usage
By CLI
$ ch2py -h
Usage: ch2py [opts]
-c, --camelcase Camelcase of each word
-i, --stdin Read from stdard input
-t, --tone Show tone at end of word
-m, --tonemarks Show tone at top of letter, this
would cover -t option
-s, --splitter <splitter> Splitter of each word, use a space
by default
-v, --version Show version
-h, --help Show this help
$ ch2py ไธญๆ
zhong wen
By code
require 'chinese_pinyin'
Pinyin.t('ไธญๅฝ') => "zhong guo"
Pinyin.t('ไฝ ๅฅฝworld') => "ni hao world"
Pinyin.t('ไธญๅฝ', splitter: '-') => "zhong-guo"
Pinyin.t('ไธญๅฝ', splitter: '') => "zhongguo"
Pinyin.t('ไธญๅฝ', tone: true) => "zhong1 guo2"
Pinyin.t('ไธญๅฝ', tonemarks: true) => "zhลng guรณ"
Pinyin.t('ๅไบฌ') { |letters| letters[0].upcase } => 'BJ'
Pinyin.t('ๅไบฌ') { |letters, i| letters[0].upcase if i == 0 } => 'B'
Polyphone Issue
use Words.dat to override default behavior.
by default
Pinyin.t('ๅนฟๅท') => "yan zhou"
add file Words.dat
ๅนฟๅท|guang3 zhou1
set ENV['WORDS_FILE']
for Words.dat
ENV['WORDS_FILE'] = "Words.dat path"
Pinyin.t('ๅนฟๅท') => "guang zhou"