ruby 汉字转拼音

Posted by wxianfeng Tue, 20 Nov 2012 04:30:00 GMT

很久以前从项目中抽取出来的 汉字转拼音的 gem, 今天介绍下使用方法.

功能

  • 首字母支持
  • 全拼支持
  • 多音字支持
  • 其它字符默认输出

Gemfile:

gem 'hanzi_to_pinyin', '0.8.0', require: 'hanzi_to_pinyin'

使用demo

$ HanziToPinyin.hanzi_to_pinyin("喜欢Ruby") => "xhruby"
$ HanziToPinyin.hanzi_2_pinyin("喜欢Ruby") => "xhruby"

$ HanziToPinyin.is_hanzi?("") => true
$ HanziToPinyin.is_hanzi?("a") => false

# 多音字,分隔 字字之间;分隔,字母丢弃
$ HanziToPinyin.hanzi_2_py("我们") => "wo;men"
$ HanziToPinyin.hanzi_2_py("查理Smith") => "cha,zha;li"
$ HanziToPinyin.hanzi_2_py("测试1") => "ce;shi;1"
$ HanziToPinyin.hanzi_2_py("测_试") => "ce;_;shi"
$ HanziToPinyin.hanzi_2_py("测-试") => "ce;-;shi"
$ HanziToPinyin.hanzi_2_py(2) =>  "2"

$ HanziToPinyin.is_number?("1".ord) => true
$ HanziToPinyin.is_number?("a".ord) => false

$ HanziToPinyin.is_underline?("_".ord) => true
$ HanziToPinyin.is_underline?("豆豆") => false
$ HanziToPinyin.is_dash?("-".ord) => true

更多介绍可以看 github 主页

HERE

另外也被翻译成了 nodejs module

nodejs

This entry was posted on Tue, 20 Nov 2012 04:30:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags , ,


Trackbacks

Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=254

  1. league of angels hack tool download league of angels hack tool download
    ruby 汉字转拼音 | 0x64

Comments

  1. Avatar
    Richard over {{count}} years ago:

    What i don’t understood is in reality how you are no longer actually much more neatly-liked than you may be right now.
    You’re very intelligent. You recognize therefore considerably with regards to this topic, produced me
    in my opinion imagine it from numerous varied angles.
    Its like men and women are not interested unless it is something to accomplish with Woman gaga!

    Your own stuffs outstanding. Always maintain it up!

  2. Avatar
    Richard over {{count}} years ago:

    What i don’t understood is in reality how you are no longer actually much more
    neatly-liked than you may be right now. You’re very intelligent.
    You recognize therefore considerably with regards to this topic, produced me in my
    opinion imagine it from numerous varied angles.
    Its like men and women are not interested unless it is
    something to accomplish with Woman gaga! Your own stuffs outstanding.
    Always maintain it up!

Leave a comment