汉语拼音转换工具 Rust 版
Add this to your Cargo.toml
:
[dependencies]
pinyin = "0.6"
and this to your crate root:
extern crate pinyin;
API documentation can be found here: https://docs.rs/pinyin/
extern crate pinyin;
pub fn main() {
let hans = "中国人";
let mut args = pinyin::Args::new();
// 默认输出 [["zhong"] ["guo"] ["ren"]]
println!("{:?}", pinyin::pinyin(hans, &args));
// ["zhong", "guo", "ren"]
println!("{:?}", pinyin::lazy_pinyin(hans, &args));
// 包含声调 [["zhōng"], ["guó"], ["rén"]]
args.style = pinyin::Style::Tone;
println!("{:?}", pinyin::pinyin(hans, &args));
// 声调用数字表示 [["zho1ng"] ["guo2"] ["re2n"]]
args.style = pinyin::Style::Tone2;
println!("{:?}", pinyin::pinyin(hans, &args));
// 开启多音字模式
args = pinyin::Args::new();
args.heteronym = true;
// [["zhong"] ["guo"] ["ren"]]
println!("{:?}", pinyin::pinyin(hans, &args));
// [["zho1ng", "zho4ng"] ["guo2"] ["re2n"]]
args.style = pinyin::Style::Tone2;
println!("{:?}", pinyin::pinyin(hans, &args));
}
$ make build
$ make test
使用来自 pinyin-data 的拼音数据。
- hotoo/pinyin: 汉语拼音转换工具 Node.js/JavaScript 版。
- mozillazg/python-pinyin: 汉语拼音转换工具 Python 版。
- mozillazg/go-pinyin: 汉语拼音转换工具 Go 版。