hanzi

Convert Hanzi to pinyin. Unlike other similar gems, this includes tones and can accurately translate common words (others fail at things like 为什么). Uses cc-cedict for data. Does not yet have fallback for individual hanzi not in cedict.

Example usage:

Hanzi.load_data
Hanzi.to_pinyin('走红') # zou3hong2
Hanzi.to_pinyin('簡單') # jian3dan1
Hanzi.to_pinyin('为什么') # wei4shen2me5
Hanzi.to_pinyin('no! 为什么!') # no! wei4shen2me5!
Hanzi.to_pinyin('你好, 我是康昱辰。') # ni3hao3, wo3shi4kang1yu4chen2。

Hanzi.to_english('孔子') # Confucius (551-479 BC), Chinese thinker and social philosopher, also known as 孔夫子[Kong3 fu1 zi3]
Hanzi.to_english('老子喜欢') # nil (can only translate exact word matches)

To run tests:

rake test

Contributing to hanzi

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.

  • Fork the project.

  • Start a feature/bugfix branch.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2013 Steve Jackson. See LICENSE.txt for further details. Uses cc-cedict.org for data.