pinyin

package module
v0.19.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2021 License: MIT Imports: 5 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-pinyin

Build Status Coverage Status Go Report Card GoDoc

汉语拼音转换工具 Go 版。

Installation

go get -u github.com/mozillazg/go-pinyin

install CLI tool:

go get -u github.com/mozillazg/go-pinyin/cmd/pinyin
$ pinyin 中国人
zhōng guó rén

Documentation

API documentation can be found here: https://godoc.org/github.com/mozillazg/go-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]]

	fmt.Println(pinyin.LazyPinyin(hans, pinyin.NewArgs()))
	// [zhong guo ren]

	fmt.Println(pinyin.Convert(hans, nil))
	// [[zhong] [guo] [ren]]

	fmt.Println(pinyin.LazyConvert(hans, nil))
	// [zhong guo ren]
}

注意:

  • 默认情况下会忽略没有拼音的字符(可以通过自定义 Fallback 参数的值来自定义如何处理没有拼音的字符, 详见 示例)。
  • 根据 《汉语拼音方案》 y,w,ü (yu) 都不是声母, 以及不是所有拼音都有声母,如果这不是你预期的话,你可能需要的是首字母风格 FirstLetter ( 详细信息 )。

pinyin data

License

Under the MIT License.

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]]
}
Example (Output)

for standalone test, change package to main and the next func def to, func main() {

package main

import (
	"fmt"

	pinyin "github.com/go-cc/cc-pinyin"
)

func main() {
	s := `名著:《红楼梦》〖清〗曹雪芹 著、高鹗 续/『人民文学』出版社/1996—9月30日/59.70【元】,《三国演义》〖明〗罗贯中。`
	_ = s
	hans := "中国人的〖中国银行〗,很.行.。"
	// Separator 默认配置:所用的分隔符
	var Separator = " "
	var a pinyin.Pinyin

	// 默认
	a = pinyin.NewPinyin(pinyin.Normal, pinyin.Normal, Separator, false, false)
	//a.Separator = "_"
	fmt.Println(a.Convert(hans))

	// 包含声调
	a = pinyin.NewPinyin(pinyin.Tone3, pinyin.Normal, Separator, false, false)
	fmt.Println(a.Convert(hans))

	// 声调用数字表示
	a = pinyin.NewPinyin(pinyin.Tone2, pinyin.Normal, Separator, false, false)
	fmt.Println(a.Convert(hans))

	// 声调在拼音后用数字表示
	a = pinyin.NewPinyin(pinyin.Tone1, pinyin.Normal, Separator, false, false)
	fmt.Println(a.Convert(hans))

	// 开启多音字模式
	a = pinyin.NewPinyin(pinyin.Tone1, pinyin.Normal, Separator, true, false)
	fmt.Println(a.Convert(hans))
	a = pinyin.NewPinyin(pinyin.Tone3, pinyin.Normal, Separator, true, false)
	fmt.Println(a.Convert(hans))

	// 11: 双显风格,返回 汉字 + 拼音
	a = pinyin.NewPinyin(pinyin.Tone3, pinyin.Both, Separator, false, true)
	fmt.Println(a.Convert("中国银行。"))

	
Output:
zhong guo ren de 〖zhong guo yin xing 〗,hen .xing .。
zhōng guó rén de 〖zhōng guó yín xíng 〗,hěn .xíng .。
zho1ng guo2 re2n de 〖zho1ng guo2 yi2n xi2ng 〗,he3n .xi2ng .。
zhong1 guo2 ren2 de 〖zhong1 guo2 yin2 xing2 〗,hen3 .xing2 .。
zho1ng/zho4ng guo2 ren2 de/di1/di2/di4 〖zho1ng/zho4ng guo2 yin2 xi2ng/ha2ng/he2ng/xi4ng/ha4ng 〗,hen3 .xi2ng/ha2ng/he2ng/xi4ng/ha4ng .。
zhōng/zhòng guó rén de/dī/dí/dì 〖zhōng/zhòng guó yín xíng/háng/héng/xìng/hàng 〗,hěn .xíng/háng/héng/xìng/hàng .。
中(Zhōng) 国(Guó) 银(Yín) 行(Xíng) 。

Index

Examples

Constants

View Source
const (
	Tone1 // 声调风格1,即拼音声调在各个拼音之后,用数字 [1-4] 进行表示。如: zhong1 guo2
	Tone2 // 声调风格2,即拼音声调在各个韵母之后,用数字 [1-4] 进行表示。如: zho1ng guo2
	Tone3 // 声调风格3,拼音声调在韵母上。如: zhōng guó
)

-- 声调风格 Tone

View Source
const (
	FirstLetter   = iota + 1 // 1: 首字母风格,只返回拼音的首字母部分。如: z g
	Initials                 // 2: 声母风格,只返回各个拼音的声母部分。如: zh g
	ZeroConsonant = iota + 6 // 8: 支持零声母功能, wén -> -> uén
	Finals                   // 9: 韵母风格,只返回各个拼音的韵母部分。如: ong uo, wén -> w -> én
	Both          = 11       // 11: 双显风格,返回 汉字 + 拼音
)

-- 部分返回 Truncate

View Source
const (
	Normal = iota // 普通风格,不带声调(默认风格)。如: zhong guo
)

== 拼音风格

Variables

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

PinyinDict is data map Warning: Auto-generated file, don't edit.

View Source
var (
	VERSION = "0.20.0"
)

VERSION defines the running build id.

Functions

This section is empty.

Types

type Pinyin

type Pinyin struct {
	Style
	Separator string // 使用的分隔符(默认:" ")
	// contains filtered or unexported fields
}

Pinyin with 配置信息

func NewPinyin added in v0.19.1

func NewPinyin(tone, truncate int, separator string, _polyphone, _capitalized bool) Pinyin

NewPinyin 返回包含默认配置的 `Pinyin`

func (Pinyin) Convert added in v0.19.1

func (a Pinyin) Convert(s string) string

Convert 汉字转拼音,支持多音字模式. If enabled Polyphone, then separate the returns with '/'. E.g., for input like "我的银行不行", the output is wo de yin hang/xing bu hang/xing.

type Shaper added in v0.19.1

type Shaper struct {
	*shaper.Shaper
}

Shaper extends shaper.Shaper

func NewShaper added in v0.19.1

func NewShaper() *Shaper

NewShaper makes a new Shaper filter

func (*Shaper) ApplyToneShaping added in v0.19.1

func (sp *Shaper) ApplyToneShaping(a Pinyin) *Shaper

func (*Shaper) ApplyTruncate added in v0.19.1

func (sp *Shaper) ApplyTruncate(a Pinyin) *Shaper

type Style added in v0.2.0

type Style struct {
	// contains filtered or unexported fields
}

Style 配置拼音风格 (声调风格 + 部分返回)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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