gpy

package module
v0.42.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2021 License: MIT Imports: 4 Imported by: 18

README

gpy

Build Status 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

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

License

Under the MIT License, base on go-pinyin.

Documentation

Overview

Package gpy : Chinese Pinyin conversion tool; 汉语拼音转换工具.

Installation:

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

Usage :

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

Index

Examples

Constants

View Source
const (
	// Version get the gpy version
	Version = "v0.40.0.133"
	// License get the license
	License = "MIT"
)

Meta

View Source
const (
	Normal      = 0 // 普通风格,不带声调(默认风格)。如: zhong guo
	Tone        = 1 // 声调风格1,拼音声调在韵母第一个字母上。如: zhōng guó
	Tone2       = 2 // 声调风格2,即拼音声调在各个韵母之后,用数字 [1-4] 进行表示。如: zho1ng guo2
	Tone3       = 8 // 声调风格3,即拼音声调在各个拼音之后,用数字 [1-4] 进行表示。如: zhong1 guo2
	Initials    = 3 // 声母风格,只返回各个拼音的声母部分。如: zh g
	FirstLetter = 4 // 首字母风格,只返回拼音的首字母部分。如: z g
	Finals      = 5 // 韵母风格,只返回各个拼音的韵母部分,不带声调。如: ong uo
	FinalsTone  = 6 // 韵母风格1,带声调,声调在韵母第一个字母上。如: ōng uó
	FinalsTone2 = 7 // 韵母风格2,带声调,声调在各个韵母之后,用数字 [1-4] 进行表示。如: o1ng uo2
	FinalsTone3 = 9 // 韵母风格3,带声调,声调在各个拼音之后,用数字 [1-4] 进行表示。如: ong1 uo2
)

拼音风格(推荐)

Variables

View Source
var (
	// Style 默认配置:风格
	Style = Normal
	// Heteronym 默认配置:是否启用多音字模式
	Heteronym = false
	// Separator 默认配置: `Slug` 中 Join 所用的分隔符
	Separator = "-"

	// Fallback 默认配置: 如何处理没有拼音的字符(忽略这个字符)
	Fallback = func(r rune, a Args) []string {
		return []string{}
	}
)
View Source
var PinyinDict = map[int]string{}/* 41451 elements not displayed */

PinyinDict is data map Warning: Auto-generated file, don't edit.

View Source
var PinyinDictAdd = map[int]string{
	0: "",
}

PinyinDictAdd pinyin dict addition

Functions

func AddDict added in v0.41.1

func AddDict(text int, py string)

AddDict add a token into pinyin dictionary.

func Convert

func Convert(s string, a *Args) [][]string

Convert 跟 Pinyin 的唯一区别就是 a 参数可以是 nil

Example
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	fmt.Println("default:", gpy.Convert(hans, nil))
}
Output:

default: [[zhong] [guo] [hua]]

func GetVersion

func GetVersion() string

GetVersion get the version

func HanPinyin

func HanPinyin(s string, arg ...Args) [][]string

HanPinyin 汉字转拼音,支持多音字模式.

Example (Default)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	fmt.Println("default:", gpy.HanPinyin(hans, a))
}
Output:

default: [[zhong] [guo] [hua]]

func IsChineseChar

func IsChineseChar(str string) bool

IsChineseChar to determine whether the Chinese string 判断是否为中文字符串

func LazyConvert

func LazyConvert(s string, a *Args) []string

LazyConvert 跟 LazyPinyin 的唯一区别就是 a 参数可以是 nil

func LazyPinyin

func LazyPinyin(s string, arg ...Args) []string

LazyPinyin 汉字转拼音,与 `Pinyin` 的区别是: 返回值类型不同,并且不支持多音字模式,每个汉字只取第一个音.

Example
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	fmt.Println(gpy.LazyPinyin(hans, a))
}
Output:

[zhong guo hua]

func Pinyin

func Pinyin(s string, arg ...Args) [][]string

Pinyin 汉字转拼音,支持多音字模式、拼音与英文等字母混合.

Example (Default)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	fmt.Println("default:", gpy.Pinyin(hans, a))
}
Output:

default: [[zhong] [guo] [hua]]
Example (FallbackCustom1)
package main

import (
	"fmt"

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

func main() {
	hans := "中国话abc"
	a := gpy.NewArgs()
	a.Fallback = func(r rune, a gpy.Args) []string {
		return []string{string(r + 1)}
	}
	fmt.Println(gpy.HanPinyin(hans, a))
}
Output:

[[zhong] [guo] [hua] [b] [c] [d]]
Example (FallbackCustom2)
package main

import (
	"fmt"

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

func main() {
	hans := "中国话アイウ"
	a := gpy.NewArgs()
	a.Fallback = func(r rune, a gpy.Args) []string {
		data := map[rune][]string{
			'ア': {"a"},
			'イ': {"i"},
			'ウ': {"u"},
		}
		s, ok := data[r]
		if ok {
			return s
		} else {
			return []string{}
		}
	}
	fmt.Println(gpy.HanPinyin(hans, a))
}
Output:

[[zhong] [guo] [hua] [a] [i] [u]]
Example (Finals)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	a.Style = gpy.Finals
	fmt.Println(gpy.Pinyin(hans, a))
}
Output:

[[ong] [uo] [ua]]
Example (FinalsTone)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	a.Style = gpy.FinalsTone
	fmt.Println(gpy.Pinyin(hans, a))
}
Output:

[[ōng] [uó] [uà]]
Example (FinalsTone2)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	a.Style = gpy.FinalsTone2
	fmt.Println(gpy.Pinyin(hans, a))
}
Output:

[[o1ng] [uo2] [ua4]]
Example (FirstLetter)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	a.Style = gpy.FirstLetter
	fmt.Println(gpy.Pinyin(hans, a))
}
Output:

[[z] [g] [h]]
Example (Heteronym)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	a.Heteronym = true
	a.Style = gpy.Tone2
	fmt.Println(gpy.Pinyin(hans, a))
}
Output:

[[zho1ng zho4ng] [guo2] [hua4]]
Example (Initials)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	a.Style = gpy.Initials
	fmt.Println("Initials:", gpy.Pinyin(hans, a))
}
Output:

Initials: [[zh] [g] [h]]
Example (Normal)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	a.Style = gpy.Normal
	fmt.Println("Normal:", gpy.Pinyin(hans, a))
}
Output:

Normal: [[zhong] [guo] [hua]]
Example (Tone)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	a.Style = gpy.Tone
	fmt.Println("Tone:", gpy.Pinyin(hans, a))
}
Output:

Tone: [[zhōng] [guó] [huà]]
Example (Tone2)
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	a.Style = gpy.Tone2
	fmt.Println("Tone2:", gpy.Pinyin(hans, a))
}
Output:

Tone2: [[zho1ng] [guo2] [hua4]]

func Py

func Py(s string, a ...Args) string

Py return to string pinyin

func Remove added in v0.41.1

func Remove(text int)

Remove remove a token from pinyin dictionary.

func SinglePinyin

func SinglePinyin(r rune, a Args) []string

SinglePinyin 把单个 `rune` 类型的汉字转换为拼音.

func Slug

func Slug(s string, a Args) string

Slug join `LazyPinyin` 的返回值. 建议改用 https://github.com/mozillazg/go-slugify

Example
package main

import (
	"fmt"

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

var hans = "中国话"

func main() {
	a := gpy.NewArgs()
	fmt.Println(gpy.Slug(hans, a))
}
Output:

zhong-guo-hua

func ToFixed

func ToFixed(p string, a Args) string

ToFixed fixed pinyin style

func ToString

func ToString(p [][]string) (s string)

ToString trans pinyin [][]string to string

Types

type Args

type Args struct {
	Style     int    // 拼音风格(默认: Normal)
	Heteronym bool   // 是否启用多音字模式(默认:禁用)
	Separator string // Slug 中使用的分隔符(默认:-)

	// 处理没有拼音的字符(默认忽略没有拼音的字符)
	// 函数返回的 slice 的长度为0 则表示忽略这个字符
	Fallback func(r rune, a Args) []string
}

Args 配置信息

func NewArgs

func NewArgs() Args

NewArgs 返回包含默认配置的 `Args`

Directories

Path Synopsis
tools

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL