pinyin

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package pinyin provides Chinese character to Pinyin conversion.

Index

Constants

View Source
const (
	Version   = "0.2.1"
	Author    = "mozillazg, 闲耘"
	License   = "MIT"
	Copyright = "Copyright (c) 2014 mozillazg, 闲耘"
)

Package metadata.

View Source
const (
	Normal      = 0 // Plain style, no tone marks. e.g. pin yin
	Tone        = 1 // Tone on first vowel of final. e.g. pīn yīn
	Tone2       = 2 // Tone as digit [0-4] after syllable. e.g. pi1n yi1n
	Initials    = 3 // Initial consonants only. e.g. "zh g"
	FirstLetter = 4 // First letter of each syllable only. e.g. p y
	Finals      = 5 // Finals only, no tone. e.g. ong uo
	FinalsTone  = 6 // Finals with tone on first vowel. e.g. ōng uó
	FinalsTone2 = 7 // Finals with tone as digit [0-4]. e.g. o1ng uo2
)

Pinyin style constants (recommended).

View Source
const (
	NORMAL       = 0
	TONE         = 1
	TONE2        = 2
	INITIALS     = 3
	FIRST_LETTER = 4
	FINALS       = 5
	FINALS_TONE  = 6
	FINALS_TONE2 = 7
)

Pinyin style constants (backward compatible aliases).

Variables

View Source
var Heteronym = false

Heteronym enables multi-reading character mode when true.

View Source
var PinyinDict = map[int]string{}/* 42013 elements not displayed */

Pinyin dictionary scraped from zdic.net. Contains the following Unicode ranges:

CJK基本:[4E00-9FFF]       CJK扩展A:[3400-4DBF]
CJK扩展B:[20000-2A6DF]    CJK扩展C:[2A700-2B73F]
CJK扩展D:[2B740-2B81D]    CJK兼容扩展:[2F800-2FA1F]
CJK部首扩展:[2E80-2EFF]   CJK笔画:[31C0-31EF]
CJK康熙部首:[2F00-2FDF]   CJK兼容:[F900-FAFF]

Characters without pinyin are commented out.

View Source
var Separator = "-"

Separator is the default join separator for Slug.

View Source
var Style = Normal

Style is the default pinyin style.

Functions

func LazyPinyin

func LazyPinyin(s string, a Args) []string

LazyPinyin converts Chinese to pinyin, returning a flat slice. Unlike Pinyin, it does not support heteronyms and uses only the first reading per character.

func Pinyin

func Pinyin(s string, a Args) [][]string

Pinyin converts Chinese characters to pinyin, with heteronym support.

func SinglePinyin

func SinglePinyin(r rune, a Args) []string

SinglePinyin converts a single Chinese character (rune) to pinyin.

func Slug

func Slug(s string, a Args) string

Slug joins LazyPinyin results with the configured separator.

func SortInitials

func SortInitials(strs []string)

SortInitials sorts the string slice by pinyin initial letters (in-place).

Types

type Args

type Args struct {
	Style     int    // Pinyin style (default: Normal)
	Heteronym bool   // Enable heteronym mode for multi-reading characters (default: false)
	Separator string // Separator used in Slug (default: "-")
}

Args holds pinyin conversion options.

func NewArgs

func NewArgs() Args

NewArgs returns Args with default configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL