GitXplorerGitXplorer
m

go-pinyin

public
1650 stars
197 forks
18 issues

Commits

List of commits on branch master.
Verified
6347079abb0b6a8d067b3e7a3064755698fd870a

testing multiple Go versions

mmozillazg committed 2 years ago
Verified
c173b2265687ef5f0aa7dfd574296c6190c619f3

Update the cli installation command for go 1.17+ (#59)

llevinit committed 2 years ago
Verified
bb630d7ee0fbd559c4b71d89ecfb1dff45018308

rename cmd/pinyin to cli/pinyin

mmozillazg committed 2 years ago
Verified
acca03c01b1849911ad02d5f71172845e9909a1a

使用 pinyin-data v0.13.0 的拼音数据

mmozillazg committed 2 years ago
Verified
8930bc1fcb5693689dc35d98ad2a4153662e34b1

Upgrade to GitHub-native Dependabot (#52)

ddependabot-preview[bot] committed 3 years ago
Verified
b7cf8df4136acd9aafbb99ba78775e1f7c0a244e

only test on ubuntu-latest

mmozillazg committed 3 years ago

README

The README file for this repository.

go-pinyin

Build Status Coverage Status Go Report Card GoDoc

汉语拼音转换工具 Go 版。

Installation

go get github.com/mozillazg/go-pinyin

install CLI tool:

# go version>=1.17
go install github.com/mozillazg/go-pinyin/cli/pinyin@latest

# go version<1.17
go get -u github.com/mozillazg/go-pinyin/cli/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详细信息 )。

Related Projects

pinyin data

License

Under the MIT License.