Pinyin.rs
中文转拼音 Rust 库和命令行工具,词语优先匹配,支持多音字、姓氏、护照姓名、slug、缩写和多种声调格式。词库来自 mozillazg/pinyin-data 及项目补丁数据。
Features
- 词语优先:最长词匹配,
你好世界会输出nǐ hǎo shì jiè,不会逐字泄露好的多音字。 - 常用 API:句子、短语、姓名、护照姓名、permalink、首字母缩写、单字和 heteronym。
- 格式完整:支持声调符号、数字声调、无声调,支持
ü/v/yu/u输出。 - CLI 可用:支持参数输入和 stdin 管道输入。
- 构建干净:词库在 Cargo
OUT_DIR生成,构建过程不会改写源码目录。
Install
Requires Rust 1.95 or newer.
Library:
[]
= "0.1"
The package is named pinyin-converter; the Rust library name is pinyin:
use Pinyin;
CLI:
Quick Start
use ;
CLI
# nǐ hǎo shì jiè
# ni3 hao3
# lv xing
# dai-zhe-xi-wang-qu-lv-xing
# b j d x
# shàn mǒu mǒu
# lyu xiu cai
|
# zhōng guó
Run pinyin --help for all options.
API
Low-Level Conversion
convert(input) returns matched pinyin segments and keeps unmatched characters:
use convert;
assert_eq!;
assert_eq!;
Use convert_safe or convert_with_config when input limits should be validated:
use ;
let config = new.with_max_length;
let result = convert_with_config.unwrap;
assert_eq!;
Pinyin
use Pinyin;
sentence; // nǐ hǎo , shì jiè
phrase; // nǐ hǎo shì jiè
permalink; // dai-zhe-xi-wang-qu-lv-xing
permalink_with.unwrap; // ni_hao_shi_jie
abbr; // b j d x
name; // shàn mǒu mǒu
name_abbr; // s m m
passport_name; // lyu xiu cai
chars; // zhòng qìng
heteronym; // [('重', ["zhòng", "chóng", ...])]
Converter
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Development
The build script reads files under sources/ and writes generated data into Cargo's OUT_DIR. Do not edit generated files under target/.
License
MIT