Code Monkey home page Code Monkey logo

chinese_pinyin's Introduction

ChinesePinyin

Build Status

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"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.