Documentation ¶
Overview ¶
Package pinyin : 汉语拼音转换工具.
Usage
package main import ( "fmt" "github.com/mozillazg/go-pinyin" ) func main() { hans := "中国人" // 默认 a := pinyin.NewArgs() fmt.Println(pinyin.Pinyin(hans, a)) // [[zhong] [guo] [ren]] // 包含声调 a.Style = pinyin.Tone fmt.Println(pinyin.Pinyin(hans, a)) // [[zhōng] [guó] [rén]] // 声调用数字表示 a.Style = pinyin.Tone2 fmt.Println(pinyin.Pinyin(hans, a)) // [[zho1ng] [guo2] [re2n]] // 开启多音字模式 a = pinyin.NewArgs() a.Heteronym = true fmt.Println(pinyin.Pinyin(hans, a)) // [[zhong zhong] [guo] [ren]] a.Style = pinyin.Tone2 fmt.Println(pinyin.Pinyin(hans, a)) // [[zho1ng zho4ng] [guo2] [re2n]] }
Index ¶
- Constants
- Variables
- func Convert(s string, a *Args) [][]string
- func LazyConvert(s string, a *Args) []string
- func LazyPinyin(s string, a Args) []string
- func Paragraph(p string) (s string)
- func Pinyin(s string, a Args) [][]string
- func SinglePinyin(r rune, a Args) []string
- func Slug(s string, a Args) string
- type Args
Examples ¶
Constants ¶
View Source
const ( Version = "0.16.0" Author = "mozillazg, 闲耘" License = "MIT" Copyright = "Copyright (c) 2016 mozillazg, 闲耘" )
Meta
View Source
const ( Normal = 0 // 普通风格,不带声调(默认风格)。如: zhong guo Tone = 1 // 声调风格1,拼音声调在韵母第一个字母上。如: zhōng guó Tone2 = 2 // 声调风格2,即拼音声调在各个韵母之后,用数字 [1-4] 进行表示。如: zho1ng guo2 Tone3 = 8 // 声调风格3,即拼音声调在各个拼音之后,用数字 [1-4] 进行表示。如: zhong1 guo2 Initials = 3 // 声母风格,只返回各个拼音的声母部分。如: zh g FirstLetter = 4 // 首字母风格,只返回拼音的首字母部分。如: z g Finals = 5 // 韵母风格,只返回各个拼音的韵母部分,不带声调。如: ong uo FinalsTone = 6 // 韵母风格1,带声调,声调在韵母第一个字母上。如: ōng uó FinalsTone2 = 7 // 韵母风格2,带声调,声调在各个韵母之后,用数字 [1-4] 进行表示。如: o1ng uo2 FinalsTone3 = 9 // 韵母风格3,带声调,声调在各个拼音之后,用数字 [1-4] 进行表示。如: ong1 uo2 )
拼音风格(推荐)
View Source
const ( NORMAL = Normal TONE = Tone TONE2 = Tone2 INITIALS = Initials FIRST_LETTER = FirstLetter FINALS = Finals FINALS_TONE = FinalsTone FINALS_TONE2 = FinalsTone2 )
拼音风格(兼容之前的版本)
Variables ¶
View Source
var Fallback = func(r rune, a Args) []string { return []string{} }
Fallback 默认配置: 如何处理没有拼音的字符(忽略这个字符)
View Source
var Heteronym = false
Heteronym 默认配置:是否启用多音字模式
View Source
var PinyinDict = map[int]string{}/* 41451 elements not displayed */
PinyinDict is data map Warning: Auto-generated file, don't edit.
View Source
var Separator = "-"
Separator 默认配置: `Slug` 中 Join 所用的分隔符
View Source
var Style = Normal
Style 默认配置:风格
Functions ¶
func Convert ¶ added in v0.9.0
Convert 跟 Pinyin 的唯一区别就是 a 参数可以是 nil
Example ¶
hans := "中国人" fmt.Println("default:", pinyin.Convert(hans, nil))
Output: default: [[zhong] [guo] [ren]]
func LazyConvert ¶ added in v0.9.0
LazyConvert 跟 LazyPinyin 的唯一区别就是 a 参数可以是 nil
func LazyPinyin ¶
LazyPinyin 汉字转拼音,与 `Pinyin` 的区别是: 返回值类型不同,并且不支持多音字模式,每个汉字只取第一个音.
Example ¶
hans := "中国人" a := pinyin.NewArgs() fmt.Println(pinyin.LazyPinyin(hans, a))
Output: [zhong guo ren]
func Paragraph ¶ added in v1.0.0
Paragraph convert a Chinese paragraph into pinyin, including letters, numbers, symbols
Example ¶
hans := "人民银行旁边一行人abc字母【路牌】,平行宇宙发行股票。" fmt.Println(pinyin.Paragraph(hans))
Output: ren min yin xing pang bian yi xing ren abc zi mu [lu pai], ping xing yu zhou fa xing gu piao.
func Pinyin ¶
Pinyin 汉字转拼音,支持多音字模式.
Example (Default) ¶
hans := "中国人" a := pinyin.NewArgs() fmt.Println("default:", pinyin.Pinyin(hans, a))
Output: default: [[zhong] [guo] [ren]]
Example (FallbackCustom1) ¶
hans := "中国人abc" a := pinyin.NewArgs() a.Fallback = func(r rune, a pinyin.Args) []string { return []string{string(r + 1)} } fmt.Println(pinyin.Pinyin(hans, a))
Output: [[zhong] [guo] [ren] [b] [c] [d]]
Example (FallbackCustom2) ¶
hans := "中国人アイウ" a := pinyin.NewArgs() a.Fallback = func(r rune, a pinyin.Args) []string { data := map[rune][]string{ 'ア': {"a"}, 'イ': {"i"}, 'ウ': {"u"}, } s, ok := data[r] if ok { return s } else { return []string{} } } fmt.Println(pinyin.Pinyin(hans, a))
Output: [[zhong] [guo] [ren] [a] [i] [u]]
Example (Finals) ¶
hans := "中国人" a := pinyin.NewArgs() a.Style = pinyin.Finals fmt.Println(pinyin.Pinyin(hans, a))
Output: [[ong] [uo] [en]]
Example (FinalsTone) ¶
hans := "中国人" a := pinyin.NewArgs() a.Style = pinyin.FinalsTone fmt.Println(pinyin.Pinyin(hans, a))
Output: [[ōng] [uó] [én]]
Example (FinalsTone2) ¶
hans := "中国人" a := pinyin.NewArgs() a.Style = pinyin.FinalsTone2 fmt.Println(pinyin.Pinyin(hans, a))
Output: [[o1ng] [uo2] [e2n]]
Example (FirstLetter) ¶
hans := "中国人" a := pinyin.NewArgs() a.Style = pinyin.FirstLetter fmt.Println(pinyin.Pinyin(hans, a))
Output: [[z] [g] [r]]
Example (Heteronym) ¶
hans := "中国人" a := pinyin.NewArgs() a.Heteronym = true a.Style = pinyin.Tone2 fmt.Println(pinyin.Pinyin(hans, a))
Output: [[zho1ng zho4ng] [guo2] [re2n]]
Example (Initials) ¶
hans := "中国人" a := pinyin.NewArgs() a.Style = pinyin.Initials fmt.Println("Initials:", pinyin.Pinyin(hans, a))
Output: Initials: [[zh] [g] [r]]
Example (Normal) ¶
hans := "中国人" a := pinyin.NewArgs() a.Style = pinyin.Normal fmt.Println("Normal:", pinyin.Pinyin(hans, a))
Output: Normal: [[zhong] [guo] [ren]]
Example (Tone) ¶
hans := "中国人" a := pinyin.NewArgs() a.Style = pinyin.Tone fmt.Println("Tone:", pinyin.Pinyin(hans, a))
Output: Tone: [[zhōng] [guó] [rén]]
Example (Tone2) ¶
hans := "中国人" a := pinyin.NewArgs() a.Style = pinyin.Tone2 fmt.Println("Tone2:", pinyin.Pinyin(hans, a))
Output: Tone2: [[zho1ng] [guo2] [re2n]]
Types ¶
Source Files ¶
Click to show internal directories.
Click to hide internal directories.