| 1 | go-pinyin |
| 2 | ========= |
| 3 | |
| 4 | [](https://github.com/mozillazg/go-pinyin/actions/workflows/ci.yml) |
| 5 | [](https://coveralls.io/r/mozillazg/go-pinyin?branch=master) |
| 6 | [](https://goreportcard.com/report/github.com/mozillazg/go-pinyin) |
| 7 | [](https://godoc.org/github.com/mozillazg/go-pinyin) |
| 8 | |
| 9 | 汉语拼音转换工具 Go 版。 |
| 10 | |
| 11 | |
| 12 | Installation |
| 13 | ------------ |
| 14 | |
| 15 | ``` |
| 16 | go get github.com/mozillazg/go-pinyin |
| 17 | ``` |
| 18 | |
| 19 | install CLI tool: |
| 20 | |
| 21 | ``` |
| 22 | # go version>=1.17 |
| 23 | go install github.com/mozillazg/go-pinyin/cli/pinyin@latest |
| 24 | |
| 25 | # go version<1.17 |
| 26 | go get -u github.com/mozillazg/go-pinyin/cli/pinyin |
| 27 | |
| 28 | $ pinyin 中国人 |
| 29 | zhōng guó rén |
| 30 | ``` |
| 31 | |
| 32 | |
| 33 | Documentation |
| 34 | -------------- |
| 35 | |
| 36 | API documentation can be found here: |
| 37 | https://godoc.org/github.com/mozillazg/go-pinyin |
| 38 | |
| 39 | |
| 40 | Usage |
| 41 | ------ |
| 42 | |
| 43 | ```go |
| 44 | package main |
| 45 | |
| 46 | import ( |
| 47 | "fmt" |
| 48 | "github.com/mozillazg/go-pinyin" |
| 49 | ) |
| 50 | |
| 51 | func main() { |
| 52 | hans := "中国人" |
| 53 | |
| 54 | // 默认 |
| 55 | a := pinyin.NewArgs() |
| 56 | fmt.Println(pinyin.Pinyin(hans, a)) |
| 57 | // [[zhong] [guo] [ren]] |
| 58 | |
| 59 | // 包含声调 |
| 60 | a.Style = pinyin.Tone |
| 61 | fmt.Println(pinyin.Pinyin(hans, a)) |
| 62 | // [[zhōng] [guó] [rén]] |
| 63 | |
| 64 | // 声调用数字表示 |
| 65 | a.Style = pinyin.Tone2 |
| 66 | fmt.Println(pinyin.Pinyin(hans, a)) |
| 67 | // [[zho1ng] [guo2] [re2n]] |
| 68 | |
| 69 | // 开启多音字模式 |
| 70 | a = pinyin.NewArgs() |
| 71 | a.Heteronym = true |
| 72 | fmt.Println(pinyin.Pinyin(hans, a)) |
| 73 | // [[zhong] [guo] [ren]] |
| 74 | a.Style = pinyin.Tone2 |
| 75 | fmt.Println(pinyin.Pinyin(hans, a)) |
| 76 | // [[zho1ng zho4ng] [guo2] [re2n]] |
| 77 | |
| 78 | fmt.Println(pinyin.LazyPinyin(hans, pinyin.NewArgs())) |
| 79 | // [zhong guo ren] |
| 80 | |
| 81 | fmt.Println(pinyin.Convert(hans, nil)) |
| 82 | // [[zhong] [guo] [ren]] |
| 83 | |
| 84 | fmt.Println(pinyin.LazyConvert(hans, nil)) |
| 85 | // [zhong guo ren] |
| 86 | } |
| 87 | ``` |
| 88 | |
| 89 | 注意: |
| 90 | |
| 91 | * 默认情况下会忽略没有拼音的字符(可以通过自定义 `Fallback` 参数的值来自定义如何处理没有拼音的字符, |
| 92 | 详见 [示例](https://godoc.org/github.com/mozillazg/go-pinyin#example-Pinyin--FallbackCustom1))。 |
| 93 | * 根据 [《汉语拼音方案》](http://www.moe.gov.cn/s78/A19/yxs_left/moe_810/s230/195802/t19580201_186000.html) y,w,ü (yu) 都不是声母, |
| 94 | 以及不是所有拼音都有声母,如果这不是你预期的话,你可能需要的是首字母风格 `FirstLetter` |
| 95 | ( [详细信息](https://github.com/mozillazg/python-pinyin#%E4%B8%BA%E4%BB%80%E4%B9%88%E6%B2%A1%E6%9C%89-y-w-yu-%E5%87%A0%E4%B8%AA%E5%A3%B0%E6%AF%8D) )。 |
| 96 | |
| 97 | |
| 98 | Related Projects |
| 99 | ----------------- |
| 100 | |
| 101 | * [hotoo/pinyin](https://github.com/hotoo/pinyin): 汉语拼音转换工具 Node.js/JavaScript 版。 |
| 102 | * [mozillazg/python-pinyin](https://github.com/mozillazg/python-pinyin): 汉语拼音转换工具 Python 版。 |
| 103 | * [mozillazg/rust-pinyin](https://github.com/mozillazg/rust-pinyin): 汉语拼音转换工具 Rust 版。 |
| 104 | |
| 105 | |
| 106 | pinyin data |
| 107 | ----------------- |
| 108 | |
| 109 | * 使用 [pinyin-data](https://github.com/mozillazg/pinyin-data) 的拼音数据 |
| 110 | |
| 111 | |
| 112 | License |
| 113 | --------- |
| 114 | |
| 115 | Under the MIT License. |