MoonBit port of pinyin4cj: Chinese-to-pinyin conversion
.
├── src
│ ├── data
│ │ ├── chinese_dict.mbt # 繁→简码点映射(2533 条)
│ │ ├── mutil_pinyin_dict.mbt # 词组拼音(843 条)
│ │ ├── pinyin_dict.mbt # 单字拼音(20903 条)
│ │ └── tongyong_pinyin_dict.mbt # 通用拼音(82 条)
│ ├── examples # 功能示例
│ │ ├── chinese_helper_example.mbt
│ │ ├── pinyin_helper_example.mbt
│ │ ├── pinyin_array_example.mbt
│ │ └── custom_dict_example.mbt
│ ├── chinese_helper.mbt # ChineseHelper:繁简转换、汉字判断
│ ├── pinyin_dicts.mbt # 四张字典视图常量
│ ├── pinyin_error.mbt # PinyinError 错误类型
│ ├── pinyin_format.mbt # PinyinFormat 格式枚举
│ ├── pinyin_helper.mbt # PinyinHelper:拼音转换核心 API
│ └── tone_conversion.mbt # 声调转换内部逻辑
├── doc
│ └── feature_api.md # API 文档
├── scripts
│ └── gen_pinyin_dict.py # 字典生成脚本
├── moon.mod # 模块元数据
├── CHANGELOG.md # 变更日志
├── LICENSE # MIT 协议
└── README.md| 类 | 方法 | 说明 |
|---|---|---|
| PinyinHelper | convert_to_pinyin_string | 词句转拼音字符串 |
| PinyinHelper | convert_to_pinyin_string_traditional | 繁体词句转拼音 |
| PinyinHelper | convert_to_pinyin_array | 单字转拼音数组 |
| PinyinHelper | get_short_pinyin | 首字母格式拼音 |
| PinyinHelper | has_multi_pinyin | 多音字检测 |
| PinyinHelper | to_tongyong_pinyin_string_array | 通用拼音数组 |
| PinyinHelper | add_pinyin_dict_resource | 添加自定义拼音字典 |
| PinyinHelper | add_mutil_pinyin_dict_resource | 添加自定义词组字典 |
| ChineseHelper | convert_to_simplified_chinese | 繁体转简体 |
| ChineseHelper | convert_to_traditional_chinese | 简体转繁体 |
| ChineseHelper | is_chinese | 汉字判断 |
| ChineseHelper | is_traditional_chinese | 繁体汉字判断 |
| ChineseHelper | contains_chinese | 包含汉字判断 |
| ChineseHelper | add_chinese_dict_resource | 添加自定义中文字典 |
moon check # 类型检查
moon test # 运行测试
moon fmt # 格式化
moon info # 生成接口文件let pinyin = ChineseHelper::convert_to_simplified_chinese("臺,喪,麗")
println(pinyin)
// 输出: 台,丧,丽let pinyin = ChineseHelper::convert_to_traditional_chinese("我是中国人")
println(pinyin)
// 输出: 我是中國人let pinyin = PinyinHelper::convert_to_pinyin_string("我是中国人", " ")
println(pinyin)
// 输出: wǒ shì zhōng zhòng guó rénlet pinyin = PinyinHelper::convert_to_pinyin_string("中国", " ", format=PinyinFormat::WithToneNumber)
println(pinyin)
// 输出: zhong1 zhong4 guo2let map : Map[String, String] = { "上": "shǎng" }
PinyinHelper::add_pinyin_dict_resource(map)
let pinyin = PinyinHelper::convert_to_pinyin_string("上午", " ")
println(pinyin)
// 输出: shǎng wǔlet map : Map[String, String] = { "阿弥陀佛": "ā,mí,tuó,fó" }
PinyinHelper::add_mutil_pinyin_dict_resource(map)
let pinyin = PinyinHelper::convert_to_pinyin_string("阿弥陀佛", " ")
println(pinyin)
// 输出: ā mí tuó fólet map : Map[Int, Int] = { 0x7691: 0x75C7 } // 癥 → 症
ChineseHelper::add_chinese_dict_resource(map)
let pinyin = ChineseHelper::convert_to_simplified_chinese("癥")
println(pinyin)
// 输出: 症let pinyin = PinyinHelper::convert_to_pinyin_array('长')
println(pinyin)
// 输出: ["cháng", "zhǎng"]let pinyin = PinyinHelper::convert_to_pinyin_array('嚴')
println(pinyin)
// 输出: ["yán"]let simplePinyin = PinyinHelper::to_tongyong_pinyin_string_array('傳')
let traditionalPinyin = PinyinHelper::to_tongyong_pinyin_string_array('传')
println(simplePinyin)
println(traditionalPinyin)
// 输出: ["chuan2", "jhuan4"]
// ["chuan2", "jhuan4"]| 字典 | 条目数 | 类型 | 源库对应 |
|---|---|---|---|
| chinese_map | 2533 | Map[Int, Int] | CHINESE_MAP |
| pinyin_table | 20903 | Map[String, String] | PINYIN_TABLE |
| mutil_pinyin_table | 843 | Map[String, String] | MUTIL_PINYIN_TABLE |
| tongyong_pinyin_table | 82 | Map[String, String] | TONGYONG_PINYIN_TABLE |
python scripts/gen_pinyin_dict.py| 编号 | 依赖构建工具 | 版本号 |
|---|---|---|
| 1 | moon | 0.1.20260713 |
| 项 | 内容 |
|---|---|
| 原项目名称 | pinyin4cj |
| 原项目链接 | https://gitcode.com/pinyin4cj/pinyin4cj |
| 原项目语言 | Cangjie(仓颉) |
| 原项目许可证 | MIT |
| 移植范围 | pinyin_helper + chinese_helper 全部 API,完整移植 |
| 修改适配 | camelCase→snake_case、异常改用 raise、字典内嵌为字面量、src/ 布局 |
| 未支持功能 | 无(完整移植) |
.
├── src
│ ├── data
│ │ ├── chinese_dict.mbt
│ │ ├── mutil_pinyin_dict.mbt
│ │ ├── pinyin_dict.mbt
│ │ └── tongyong_pinyin_dict.mbt
│ ├── chinese_helper.mbt
│ ├── pinyin_dicts.mbt
│ ├── pinyin_error.mbt
│ ├── pinyin_format.mbt
│ ├── pinyin_helper.mbt
│ └── tone_conversion.mbt
├── doc
│ └── feature_api.md
├── scripts
│ └── gen_pinyin_dict.py
├── moon.mod
├── CHANGELOG.md
├── LICENSE
└── README.mdmoon check
moon test///|
test "readme_convert_to_simplified" {
inspect(
ChineseHelper::convert_to_simplified_chinese("臺,喪,麗"),
content="台,丧,丽",
)
}///|
test "readme_convert_to_traditional" {
inspect(
ChineseHelper::convert_to_traditional_chinese("我是中国人"),
content="我是中國人",
)
}///|
test "readme_convert_to_pinyin_string" {
inspect(
PinyinHelper::convert_to_pinyin_string("我是中国人", " "),
content="wǒ shì zhōng zhòng guó rén",
)
}///|
test "readme_with_tone_number" {
inspect(
PinyinHelper::convert_to_pinyin_string(
"中国",
" ",
format=PinyinFormat::WithToneNumber,
),
content="zhong1 zhong4 guo2",
)
}///|
test "readme_convert_to_pinyin_array" {
debug_inspect(
PinyinHelper::convert_to_pinyin_array('长'),
content="[\"cháng\", \"zhǎng\"]",
)
}///|
test "readme_tongyong_pinyin" {
debug_inspect(
PinyinHelper::to_tongyong_pinyin_string_array('传'),
content="[\"chuan2\", \"jhuan4\"]",
)
}///|
test "readme_get_short_pinyin" {
inspect(PinyinHelper::get_short_pinyin("中国"), content="z z g")
}///|
test "readme_has_multi_pinyin" {
inspect(PinyinHelper::has_multi_pinyin('中'), content="true")
}///|
test "readme_is_chinese" {
inspect(ChineseHelper::is_chinese('中'), content="true")
inspect(ChineseHelper::contains_chinese("abc中"), content="true")
}| 字典 | 条目数 | 类型 | 源库对应 |
|---|---|---|---|
| chinese_map | 2533 | Map[Int, Int] | CHINESE_MAP |
| pinyin_table | 20903 | Map[String, String] | PINYIN_TABLE |
| mutil_pinyin_table | 843 | Map[String, String] | MUTIL_PINYIN_TABLE |
| tongyong_pinyin_table | 82 | Map[String, String] | TONGYONG_PINYIN_TABLE |
python scripts/gen_pinyin_dict.py| 编号 | 依赖构建工具 | 版本号 |
|---|---|---|
| 1 | moon | 0.1.20260713 |
pub(all) suberror PinyinError {
PinyinError(String)
}pub struct ChineseHelper {
}pub(all) enum PinyinFormat {
WithToneMark
WithoutTone
WithToneNumber
FirstLetter
}pub struct PinyinHelper {
}fn PinyinHelper::convert_to_pinyin_string(str : String, separator : String, format? : PinyinFormat) -> String raise PinyinErrorfn PinyinHelper::convert_to_pinyin_string_traditional(str : String, separator : String, format? : PinyinFormat) -> String raise PinyinErrorfn find_array_key_by_value(ch : Char) -> IntMoonBit port of pinyin4cj: Chinese-to-pinyin conversion