Go 语言汉字转拼音工具

gpy

CircleCI Status Build Status codecov Go Report Card GoDoc

汉语拼音转换工具 Go 版。

简体中文

Installation

go get -u github.com/go-ego/gpy

install CLI tool:

go get -u github.com/go-ego/gpy/tools/pinyin
$ pinyin 中国话
zhōng guó huà 

$ pinyin -s zhao 中国话
zhong guo hua

Documentation

API documentation can be found here: godoc

Usage

package main

import (
	"fmt"

	"github.com/go-ego/gse"

	"github.com/go-ego/gpy"
	"github.com/go-ego/gpy/phrase"
)

var test = `西雅图都会区; 长夜漫漫, winter is coming!`

func main() {
	args := gpy.Args{
		Style:     gpy.Tone,
		Heteronym: true}

	py := gpy.Pinyin(test, args)
	fmt.Println("gpy:", py)

	s := gpy.ToString(py)
	fmt.Println("gpy string:", s)

	phrase.LoadGseDict()
	go func() {
		fmt.Println("gpy phrase1:", phrase.Paragraph(test))
	}()
	fmt.Println("gpy phrase2:", phrase.Paragraph(test))

	seg := gse.New("zh, dict.txt")
	// phrase.DictAdd["都会区"] = "dū huì qū"
	phrase.AddDict("都会区", "dū huì qū")

	fmt.Println("gpy phrase:", phrase.Paragraph(test, seg))
	fmt.Println("pinyin: ", phrase.Pinyin(test))
	fmt.Println("Initial: ", phrase.Initial("都会区"))
}
package main

import (
	"fmt"

	"github.com/go-ego/gpy"
)

func main() {
	hans := "中国话"

	// 默认
	a := gpy.NewArgs()
	fmt.Println(gpy.Pinyin(hans, a))
	// [[zhong] [guo] [hua]]

	// 包含声调
	a.Style = gpy.Tone
	fmt.Println(gpy.Pinyin(hans, a))
	// [[zhōng] [guó] [huà]]

	// 声调用数字表示
	a.Style = gpy.Tone2
	fmt.Println(gpy.Pinyin(hans, a))
	// [[zho1ng] [guo2] [hua4]]

	// 开启多音字模式
	a = gpy.NewArgs()
	a.Heteronym = true
	fmt.Println(gpy.Pinyin(hans, a))
	// [[zhong zhong] [guo] [hua]]
	a.Style = gpy.Tone2
	fmt.Println(gpy.Pinyin(hans, a))
	// [[zho1ng zho4ng] [guo2] [hua4]]

	fmt.Println(gpy.LazyPinyin(hans, gpy.NewArgs()))
	// [zhong guo hua]

	fmt.Println(gpy.Convert(hans, nil))
	// [[zhong] [guo] [hua]]

	fmt.Println(gpy.LazyConvert(hans, nil))
	// [zhong guo hua]
}

Related Projects

License

Under the MIT License, base on go-pinyin.

Comments
  • Fixes #14, Adding more options to tools/pinyin

    Fixes #14, Adding more options to tools/pinyin

    Please provide Issues links to:

    • Issues: #14

    Provide test code:

    $ pinyin 中国话
    zhōng guó huà 
    
    $ pinyin -s zhao 中国话
    zhong guo hua 
    
    $ pinyin -d '' 中国话
    zhōngguóhuà
    
    $ pinyin -d '' -c 中国话
    ZhōngGuóHuà
    
    $ pinyin -s zhao -d '' -c 中国话
    ZhongGuoHua
    
    $ PINYIN_S=zhao PINYIN_D='_' pinyin 中国话
    zhong_guo_hua_
    
    $ PINYIN_S=zhao PINYIN_D='_' pinyin -c 中国话
    Zhong_Guo_Hua_
    
     $ pinyin -h
    Usage of pinyin:
      -c	Capitalize. 启用首字符大写模式
      -d string
        	Delimiter. 间隔符号 (default " ")
      -e	Heteronym. 启用多音字模式
      -help
        	Help. 显示帮助
      -p	Phrase. 启用短语
      -s string
        	Style. 指定拼音风格。可选值:zhao, zh4ao, zha4o, zhao4, zh, z, ao, 4ao, a4o, ao4 (default "zh4ao")
      -v	Verbose. 启用详细输出模式
    
    $ pinyin -help
    
    Usage:
     pinyin [flags..] HANS [HANS ...]
    
    Flags:
    
      -s	Style. 指定拼音风格。可选值:zhao, zh4ao, zha4o, zhao4, zh, z, ao, 4ao, a4o, ao4 (PINYIN_S)
      -d	Delimiter. 间隔符号 (PINYIN_D)
      -c	Capitalize. 启用首字符大写模式 (PINYIN_C)
      -e	Heteronym. 启用多音字模式 (PINYIN_E)
      -p	Phrase. 启用短语 (PINYIN_P)
      -v	Verbose. 启用详细输出模式 (PINYIN_V)
      -help	Help. 显示帮助 (PINYIN_HELP)
    
    Details:
    
      -c	Capitalize. 启用首字符大写模式
      -d string
        	Delimiter. 间隔符号 (default " ")
      -e	Heteronym. 启用多音字模式
      -help
        	Help. 显示帮助
      -p	Phrase. 启用短语
      -s string
        	Style. 指定拼音风格。可选值:zhao, zh4ao, zha4o, zhao4, zh, z, ao, 4ao, a4o, ao4 (default "zh4ao")
      -v	Verbose. 启用详细输出模式
    
    -s 或 -d 也可以由 PINYIN_S 或 PINYIN_D 环境变量指定
    
    

    Description

    Adding more options to tools/pinyin

  • `pinyin -p` has debug output

    `pinyin -p` has debug output

    • Ego version (or commit ref): v0.40.0
    • Go version: go1.14.3
    • Gcc version: gcc version 9.3.0
    • Operating system and bit: 5.4.0-33-generic x86_64

    Description

    Is the extra output from pinyin -p intended?

    $ pinyin -p 银行
    2020/10/08 22:29:30 Load the gse dictionary: "/home/tong/l/ws/Go/pkg/mod/github.com/go-ego/[email protected]/data/dict/dictionary.txt" 
    2020/10/08 22:29:32 Gse dictionary loaded finished.
    yin xing
    

    This looks like debug output, and should have a way to disable it.

  • Adding more options to tools/pinyin

    Adding more options to tools/pinyin

    • Ego version (or commit ref): v0.40.0
    • Go version: go1.14.3
    • Gcc version: gcc version 9.3.0
    • Operating system and bit: 5.4.0-33-generic x86_64

    Description

    I need a special format of the tools/pinyin output to be:

    $ pinyin 中国话
    ZhongGuoHua
    

    Justification: this is mainly to rename Chinese-named files into their pinyin counterparts. Thus

    • the space is unwelcome, and because of this
    • need capital each letter to easily tell them apart

    I.e., two more switches will be needed/added, for space and capitalization. This would be the 99.9% use case for me to use this tool.

    I was planning to fork and add the feature on my own, but think I'd better ask first and see if such added output format would be welcome upstream -- I can do all the modification and send in PR myself, if idea accepted.

  • 可以考虑支持拼音首字母吗?

    可以考虑支持拼音首字母吗?

    1. Please speak English, this is the language everybody of us can speak and write.
    2. Please ask questions or config/deploy problems on our Gitter channel: https://gitter.im/go-ego/ego
    3. Please take a moment to search that an issue doesn't already exist.
    4. Please give all relevant information below for bug reports, incomplete details will be handled as an invalid report.

    You MUST delete the content above including this line before posting, otherwise your issue will be invalid.

    • Ego version (or commit ref):
    • Go version:
    • Gcc version:
    • Operating system and bit:
    • Can you reproduce the bug at Examples:
      • [ ] Yes (provide example code)
      • [ ] No
      • [ ] Not relevant
    • Provide example code:
      
      
    • Log gist:

    Description

    ...

  • Able to suppress logs, closes #16

    Able to suppress logs, closes #16

    Please provide Issues links to:

    • Issues: #16

    Provide test code:

    $ pinyin -p 银行
    yín xíng
    
    $ pinyin -v -p 银行
    2020/10/11 22:26:21 Load the gse dictionary: "/home/tong/l/ws/Go/pkg/mod/github.com/go-ego/[email protected]/data/dict/dictionary.txt" 
    2020/10/11 22:26:23 Gse dictionary loaded finished.
    yín xíng
    

    Description

    See #16

  • - update README with correct command & output

    - update README with correct command & output

    Description

    go get -v -u github.com/go-ego/gpy/tools/pinyin
    
    $ gpy
    bash: gpy: command not found
    

    I.e..

    • gpy command is not found after doing go get github.com/go-ego/gpy/tools/pinyin
    • and its current default output do not contain 声调
  • Able to suppress logs, closes #16

    Able to suppress logs, closes #16

    Please provide Issues links to:

    • Issues: #16

    Provide test code:

    $ pinyin -p 银行
    yín xíng
    
    $ pinyin -v -p 银行
    2020/10/11 22:26:21 Load the gse dictionary: "/home/tong/l/ws/Go/pkg/mod/github.com/go-ego/[email protected]/data/dict/dictionary.txt" 
    2020/10/11 22:26:23 Gse dictionary loaded finished.
    yín xíng
    

    Description

    See #16

  • - fixes #17, make cli flag working

    - fixes #17, make cli flag working

    Please provide Issues links to:

    • Issues: #17

    Provide test code:

     $ pinyin 中国话
    zhōng guó huà 
    
    $ pinyin -s zhao 中国话
    zhong guo hua
    
    $ pinyin -s zh 中国话
    zh g h 
    

    Description

    #17 fixed as above.

  • tools/pinyin flags not working

    tools/pinyin flags not working

    • Ego version (or commit ref): v0.40.0
    • Go version: go1.14.3
    • Gcc version: gcc version 9.3.0
    • Operating system and bit: 5.4.0-33-generic x86_64

    Description

    Note that

    style = flag.String("s", "zh4ao", str)

    I.e., the default output should be with the tone.

    However, the pinyin output is without the tone.

    Also:

    $ pinyin -s zh 中国话
    zhong guo hua 
    

    whereas it should be zh g h

  • `pinyin -p` output sometimes incorrect

    `pinyin -p` output sometimes incorrect

    • Ego version (or commit ref): v0.40.0
    • Go version: go1.14.3
    • Gcc version: gcc version 9.3.0
    • Operating system and bit: 5.4.0-33-generic x86_64

    Description

    The output of pinyin -p sometimes are not correct.

    Correct example:

    $ pinyin -p 银行
    2020/10/11 13:47:09 Load the gse dictionary: "/home/tong/l/ws/Go/pkg/mod/github.com/go-ego/[email protected]/data/dict/dictionary.txt" 
    2020/10/11 13:47:12 Gse dictionary loaded finished.
    yín xíng
    

    Incorrect example:

    $ pinyin -p 中国话
    2020/10/11 13:46:54 Load the gse dictionary: "/home/tong/l/ws/Go/pkg/mod/github.com/go-ego/[email protected]/data/dict/dictionary.txt" 
    2020/10/11 13:46:56 Gse dictionary loaded finished.
    zhng gu huà
    

    somehow the 韵母 of the first two letters are incorrect.

    Please double-check. thx.

  • Heteronym mode

    Heteronym mode

    Question

    What does the Heteronym (多音字模式) do?

    $ pinyin 银行
    yin xing 
    
    $ pinyin -e 银行
    yin xing 
    

    I was hoping that when the Heteronym (多音字模式) is on, the second output would be

    yin hang