Skip to content
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
_tools
cmd/pinyin
.editorconfig
.gitignore
.gitmodules
.travis.yml
CHANGELOG.md V0.16.0 Dec 5, 2019
LICENSE
Makefile
README.md
benchmark_test.go
doc.go update doc Mar 12, 2016
example_test.go
go.mod
go.sum
paragraph.go
paragraph_test.go
phonetic_symbol.go
phrase.go Split addition phrase words into phrase_dict_addition.go Dec 5, 2019
phrase_dict.go
phrase_dict_addition.go Split addition phrase words into phrase_dict_addition.go Dec 5, 2019
phrase_test.go
pinyin.go
pinyin_dict.go
pinyin_test.go 新增 Tone3 和 FinalsTone3 拼音风格 Oct 23, 2016

README.md

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!
}

Related Projects

pinyin data

License

Under the MIT License.

You can’t perform that action at this time.