汉字转拼音

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详细信息 )。

Related Projects

pinyin data

License

Under the MIT License.

Comments
  • 基于范例的“拼音风格”命令行选项

    基于范例的“拼音风格”命令行选项

    实用工具pinyin目前直接采用源代码定义的拼音风格名作为命令行选项,似乎存在以下问题:

    • 选项名过长,且大小写字母数字混合,拼写不便。一般来说,命令行选项极少采用upper camel casing的形式,即使简单地strconv.ToLower()一下再比较,用户体验也会好一些。
    • 命名略显仓促,用户难以记忆,代码难以重构和扩展。例如,Normal改称Toneless似乎更精确;哪些风格带有复数s,什么是2什么是3,为什么没有1,用户回忆起来恐怕也得“顿一下”。

    如果仅作为第三方库给悲催的程序员调用,以上问题(除了代码维护)似乎也无所谓。但既然提供了命令行工具,个人倾向于对用户更温柔些。

    在此建议改用基于范例的风格选项,好处是:容易拼写,也容易记忆;用户注意力集中到问题域,不容易写错;隐藏了实现方式,利于代码维护;容易基于现有代码修改,不易改错。

    建议采用的范例字是“赵”,好处是:好记(百家姓首位);拼音短;涵盖了现有的全部风格;挺欢乐……

    对应如下:

    拼音风格 | 命令行选项 | 附注 --- | --- | --- Normal | zhao | Tone | zh4ao | * Tone2 | zha4o | Tone3 | zhao4 | Initials | zh | FirstLetter | z | Finals | ao | FinalsTone | 4ao | * FinalsTone2 | a4o | FinalsTone3 | ao4 |

    附注标*处的说明:zhào和ào不容易输入,命令行选项就要采取折衷,这是用户唯一需要“硬记”的地方。其中Tone是默认选项,一般不需要指定;必须指定时,也只要记得“4在a前面”(for all?),这也是a4o、ao4的自然外推。

  • undefined: gojieba.NewJieba

    undefined: gojieba.NewJieba

    Getting this error running the example code:

    go: finding github.com/mozillazg/go-pinyin v0.16.0 go: downloading github.com/mozillazg/go-pinyin v0.16.0 go: extracting github.com/mozillazg/go-pinyin v0.16.0 go: downloading github.com/yanyiwu/gojieba v1.1.0 go: extracting github.com/yanyiwu/gojieba v1.1.0 go: finding github.com/yanyiwu/gojieba v1.1.0

    github.com/mozillazg/go-pinyin

    ../gopath503249456/pkg/mod/github.com/mozillazg/[email protected]/phrase.go:10:10: undefined: gojieba.NewJieba

  • 多音字判断是否正确

    多音字判断是否正确

    使用go-pinyin库做了json接口,测试了几个汉字,感觉不太对 config := pinyin.NewArgs() config.Heteronym = true config.Style = pinyin.Tone

    {"han":"我她","pinyin":[["wǒ"],["tā","jiě","chí"]]} {"han":"我大","pinyin":[["wǒ"],["dà","dài","tài"]]} {"han":"我太","pinyin":[["wǒ"],["tài","tā"]]}

  • 许多多音字错误

    许多多音字错误

    setting := pinyin.NewArgs()

    setting.Heteronym = true

    setting.Style=pinyin.Tone2

    fmt.Println(pinyin.Pinyin("广州深圳",setting))


    返回值为

    [[gua3ng ya3n a1n] [zho1u] [she1n] [zhe4n qua3n cho2u hua2i]]

    很显然 ,多音字是不正确的

  • Upgrade to GitHub-native Dependabot

    Upgrade to GitHub-native Dependabot

    Dependabot Preview will be shut down on August 3rd, 2021. In order to keep getting Dependabot updates, please merge this PR and migrate to GitHub-native Dependabot before then.

    Dependabot has been fully integrated into GitHub, so you no longer have to install and manage a separate app. This pull request migrates your configuration from Dependabot.com to a config file, using the new syntax. When merged, we'll swap out dependabot-preview (me) for a new dependabot app, and you'll be all set!

    With this change, you'll now use the Dependabot page in GitHub, rather than the Dependabot dashboard, to monitor your version updates, and you'll configure Dependabot through the new config file rather than a UI.

    If you've got any questions or feedback for us, please let us know by creating an issue in the dependabot/dependabot-core repository.

    Learn more about migrating to GitHub-native Dependabot

    Please note that regular @dependabot commands do not work on this pull request.

  • 烦请作者添加英文原样输出支持,谢谢~

    烦请作者添加英文原样输出支持,谢谢~

    package converter
    
    import "github.com/stretchr/testify/assert"
    
    func (e *EncoderSuite) Test_ChineseToPinYinString() {
    	grids := []struct {
    		in       string
    		sep      string
    		expected string
    	}{
    		{in: "hello, 中国", sep: "", expected: "hello,zhongguo"},
    	}
    
    	for _, grid := range grids {
    		actual := ChineseToPinYinString(grid.in, grid.sep)
    		assert.Equal(e.T(), grid.expected, actual, "has error, want:%+v, but got: %+v, resource data value of the: %+v\n", grid.expected, actual, grid.in)
    	}
    }
    
  • Bump github.com/mattn/go-isatty from 0.0.10 to 0.0.12

    Bump github.com/mattn/go-isatty from 0.0.10 to 0.0.12

    Bumps github.com/mattn/go-isatty from 0.0.10 to 0.0.12.

    Commits
    • 7b513a9 Merge pull request #49 from tklauser/use-x-sys
    • fb38f00 Add gomodTidy to postUpdateOptions in renovate.json (#50)
    • 6ede958 go mod tidy
    • 27288b1 Use golang.org/x/sys/unix for IsTerminal on *BSD
    • 65118e8 Re-use implementation in isatty_tcgets.go for android
    • 8a28c40 Update golang.org/x/sys commit hash to b775942 (#47)
    • 219d1dc Update README.md
    • 4a8ed5b Enable codecov
    • b194945 Update golang.org/x/sys commit hash to 548cf77 (#46)
    • 8266834 Add renovate.json (#45)
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in your Dependabot dashboard:

    • Update frequency (including time of day and day of week)
    • Pull request limits (per update run and/or open at any time)
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
  • Bump github.com/yanyiwu/gojieba from 1.1.0 to 1.1.1

    Bump github.com/yanyiwu/gojieba from 1.1.0 to 1.1.1

    Bumps github.com/yanyiwu/gojieba from 1.1.0 to 1.1.1.

    Changelog

    Sourced from github.com/yanyiwu/gojieba's changelog.

    v1.1.1

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in your Dependabot dashboard:

    • Update frequency (including time of day and day of week)
    • Pull request limits (per update run and/or open at any time)
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
  • Bump github.com/mattn/go-isatty from 0.0.10 to 0.0.11

    Bump github.com/mattn/go-isatty from 0.0.10 to 0.0.11

    Bumps github.com/mattn/go-isatty from 0.0.10 to 0.0.11.

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Note: This repo was added to Dependabot recently, so you'll receive a maximum of 5 PRs for your first few update runs. Once an update run creates fewer than 5 PRs we'll remove that limit.

    You can always request more updates by clicking Bump now in your Dependabot dashboard.

    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language
    • @dependabot badge me will comment on this PR with code to add a "Dependabot enabled" badge to your readme

    Additionally, you can set the following in your Dependabot dashboard:

    • Update frequency (including time of day and day of week)
    • Pull request limits (per update run and/or open at any time)
    • Out-of-range updates (receive only lockfile updates, if desired)
    • Security updates (receive only security updates, if desired)
  • About the pinyin dictionary

    About the pinyin dictionary

  • 配置多音字后发现有不属于该汉字的发音

    配置多音字后发现有不属于该汉字的发音

    我的代码如下:

    hans := "一余五哎欧额"              
    hans2 := "刘茜"         
    hans3 := "王"
    py := pinyin.NewArgs()
    py.Heteronym = true 
    fmt.Println(pinyin.Pinyin(hans, py))
    fmt.Println(pinyin.Pinyin(hans2, py))  
    fmt.Println(pinyin.Pinyin(hans3, py))
    

    输出如下:

    [[yi yi yi] [yu tu xu yu] [wu] [ai] [ou] [e]] //  余的读音?
    [[liu] [qian xi]]
    [[wang wang yu]] // 王有yu的读音吗?
    

    我简单查了下资料【余】【王】好像没有上述那么多的发音? 请大佬看看,是我代码有问题还是需要做什么?

  • Problem with 声母风格&韵母风格

    Problem with 声母风格&韵母风格

    Hi @mozillazg,

    I noticed something is wrong with 声母风格&韵母风格 while doing #26. (Ref: https://github.com/go-cc/cc2py/blob/master/cc2py_test.go#L32-L39)

    Please take a look at https://github.com/go-cc/cc-table/blob/master/text/tools/go-pinyin.go

    You can see that the output for "yín" in 声母风格 is blank and in 韵母风格 it is "iín". I.e., both cases are wrong.

    Would you look into it please? Thx.