go-pinyin

汉字转拼音

Github stars Tracking Chart

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]

	// 段落转换,支持完整支持多音字,保留符号
	fmt.Println(pinyin.Paragraph("交给团长,告诉他我们给予期望。前线的供给一定要能自给自足!"))
	// jiao gei tuan zhang, gao su ta wo men ji yu qi wang. qian xian de gong ji yi ding yao neng zi ji zi zu!
}

pinyin data

License

Under the MIT License.

Main metrics

Overview
Name With Ownermozillazg/go-pinyin
Primary LanguageGo
Program languageGo (Language Count: 2)
Platform
License:MIT License
所有者活动
Created At2014-11-09 14:04:33
Pushed At2023-07-12 14:32:55
Last Commit At2023-05-14 20:01:59
Release Count22
Last Release Namev0.20.0 (Posted on 2023-05-14 20:02:23)
First Release Namev0.1.0 (Posted on 2014-11-23 21:06:33)
用户参与
Stargazers Count1.7k
Watchers Count41
Fork Count201
Commits Count190
Has Issues Enabled
Issues Count37
Issue Open Count18
Pull Requests Count22
Pull Requests Open Count0
Pull Requests Close Count9
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private