
Security News
Astral Launches pyx: A Python-Native Package Registry
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
ruby-pinyin: zhī chí duō yīn zì de hàn zì zhuǎn pīn yīn gōng jù
ruby-pinyin可以把汉字转化为对应的拼音,并能够较好的处理多音字的情况。比如:
PinYin.of_string('南京市长江大桥', :ascii)
能够正确的将“长”转为"chang2", 而不是"zhang3".
gem install ruby-pinyin
或者把ruby-pinyin加入你的Gemfile:
gem 'ruby-pinyin'
# encoding: utf-8
require 'ruby-pinyin'
# return ['jie', 'cao']
PinYin.of_string('节操')
# return ['jie2', 'cao1']
PinYin.of_string('节操', true)
PinYin.of_string('节操', :ascii)
# return ["jié", "cāo"]
PinYin.of_string('节操', :unicode)
# 正确处理多音字: return ["nán", "jīng", "shì", "cháng", "jiāng", "dà", "qiáo"]
PinYin.of_string('南京市长江大桥', :unicode)
# return %w(gan xie party gan xie guo jia)
PinYin.of_string('感谢party感谢guo jia')
# return 'gan-xie-party-gan-xie-guo-jia'
PinYin.permlink('感谢party感谢guo jia')
# return 'gxpartygxguojia'
PinYin.abbr('感谢party感谢guo jia')
# return 'gan xie party, gan xie guo jia!'
# PinYin.sentence保留标点符号, 同时用对应英文标点代替中文标点
PinYin.sentence('感谢party, 感谢guo家!')
# override readings with your own data file
PinYin.override_files = [File.expand_path('../my.dat', __FILE__)]
更多的例子和参数请参考测试用例
ruby-pinyin有两个PinYin::Backend: PinYin::Backend::Simple
以及PinYin::Backend::MMSeg
. 默认是使用MMSeg backend, 支持多音字识别。如果你不需要多音字识别,或是对内存使用要求很高,或是有其它任何原因想要fallback到Simple backend, 可以如下配置:
PinYin.backend = PinYin::Backend::Simple.new
通过PinYin.override_files
可以自定义某些字的发音。自定义的数据以普通文本文件存放,每行定义一个字的发音,以ASCII空格将汉字的unicode编码和拼音隔开。格式可参考lib/ruby-pinyin/data/Mandarin.dat文件。
如果你喜欢这个项目,请通过(不限)以下方式帮助她!
ruby-pinyin中的拼音数据由作者整理自互联网,你可以在ruby-pinyin之外的地方任意使用,但是请注明数据来自ruby-pinyin :-)
FAQs
Unknown package
We found that ruby-pinyin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.