Documentation
¶
Overview ¶
Package pinyin converts Chinese characters to pinyin readings.
It is a pure-Go port of the getpinyin.c library, covering the entire CJK Unified Ideographs basic block (U+4E00–U+9FA5, 20,812 characters). Multi-tone characters (多音字) are supported: all readings are stored and can be retrieved individually or flattened.
Basic usage:
// Simple conversion (first reading per character)
fmt.Println(pinyin.Convert("你好世界"))
// Output: ni hao shi jie
// Keep non-Chinese characters
fmt.Println(pinyin.Convert("Hello世界", pinyin.WithKeepNonCJK(true)))
// Output: Hello shi jie
// Get all readings for multi-tone characters
for _, readings := range pinyin.ConvertAll("重庆") {
fmt.Println(readings)
}
// [chong]
// [qing]
// Single character lookup
fmt.Println(pinyin.Pinyin('乐')) // "le luo yao yue"
fmt.Println(pinyin.PinyinFirst('乐')) // "le"
Index ¶
- func Convert(s string, opts ...Option) string
- func ConvertAll(s string, opts ...Option) [][]string
- func ConvertSlice(s string, opts ...Option) []string
- func Count() int
- func HasPinyin(r rune) bool
- func IsCJK(r rune) bool
- func Pinyin(r rune) string
- func PinyinAll(r rune) []string
- func PinyinFirst(r rune) string
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Convert ¶
Convert converts a Chinese string to pinyin. Each Chinese character is replaced by its first pinyin reading; non-Chinese characters are dropped unless WithKeepNonCJK(true) is set. Consecutive non-CJK characters are kept as a single group. Readings are separated by a space (configurable via WithSeparator).
func ConvertAll ¶
ConvertAll converts a Chinese string to pinyin, returning all possible readings for each character. Non-Chinese characters produce a single-element slice containing the character itself (if WithKeepNonCJK is true) or are skipped.
func ConvertSlice ¶
ConvertSlice converts a Chinese string to a slice of pinyin readings, one element per character (using the first reading for multi-tone chars). Non-Chinese characters are skipped unless WithKeepNonCJK is true, in which case consecutive non-CJK characters are grouped into a single element.
func Pinyin ¶
Pinyin returns all pinyin readings for a single Chinese character, separated by spaces. Returns empty string for non-CJK characters.
func PinyinAll ¶
PinyinAll returns all pinyin readings as a slice for a character. Returns nil for non-CJK characters.
func PinyinFirst ¶
PinyinFirst returns the first (primary) pinyin reading for a character. Returns empty string for non-CJK characters.
Types ¶
type Option ¶
type Option func(*config)
Option configures Convert / ConvertAll behavior.
func WithAllTones ¶
WithAllTones makes Convert return all readings for multi-tone characters (joined by the separator), instead of just the first reading.
func WithHyphenSeparator ¶
func WithHyphenSeparator() Option
WithHyphenSeparator is a shortcut for WithSeparator("-").
func WithKeepNonCJK ¶
WithKeepNonCJK keeps non-Chinese characters (letters, digits, punctuation) in the output instead of dropping them.
func WithNoSeparator ¶
func WithNoSeparator() Option
WithNoSeparator is a shortcut for WithSeparator("").
func WithSeparator ¶
WithSeparator sets the separator between pinyin readings. Default is a single space.
func WithTitleCase ¶
WithTitleCase converts pinyin output to title case (first letter uppercase).
func WithUnderscoreSeparator ¶
func WithUnderscoreSeparator() Option
WithUnderscoreSeparator is a shortcut for WithSeparator("_").
func WithUppercase ¶
WithUppercase converts all pinyin output to uppercase.