[Awesome Ruby Gem] Use chinese_pinyin gem to translate chinese hanzi to pinyin

chinese_pinyin

ChinesePinyin is a tools to translate chinese hanzi to pinyin.

The dict is borrowed from perl-lingua-han/Lingua-Han-PinYin at master · fayland/perl-lingua-han · GitHub - https://github.com/fayland/perl-lingua-han/tree/master/Lingua-Han-PinYin

Installation

You can install it as a gem:

1
$ gem install chinese_pinyin

or add it into a Gemfile (Bundler):

1
2
3
4
5
# Gemfile

# GitHub - flyerhzm/chinese_pinyin: translate chinese hanzi to pinyin
# https://github.com/flyerhzm/chinese_pinyin
gem 'chinese_pinyin', '1.1.0'

Then, run bundle install.

1
$ bundle install

Usage

By CLI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ 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

1
2
3
4
5
6
7
8
9
10
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

1
Pinyin.t('广州') => "yan zhou"

add file Words.dat

1
广州|guang3 zhou1

set ENV['WORDS_FILE'] for Words.dat

1
2
ENV['WORDS_FILE'] = "Words.dat path"
Pinyin.t('广州') => "guang zhou"

References

[1] GitHub - flyerhzm/chinese_pinyin: translate chinese hanzi to pinyin - https://github.com/flyerhzm/chinese_pinyin

[2] chinese_pinyin | RubyGems.org | your community gem host - https://rubygems.org/gems/chinese_pinyin/

[3] perl-lingua-han/Lingua-Han-PinYin at master · fayland/perl-lingua-han · GitHub - https://github.com/fayland/perl-lingua-han/tree/master/Lingua-Han-PinYin