pinyin

package module
Version: v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2021 License: MIT Imports: 2 Imported by: 110

README

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

pinyin data

License

Under the MIT License.

Documentation

Overview

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

Index

Examples

Constants

View Source
const (
	Version   = "0.19.0"
	Author    = "mozillazg, 闲耘"
	License   = "MIT"
	Copyright = "Copyright (c) 2016 mozillazg, 闲耘"
)

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
)

拼音风格(推荐)

View Source
const (
	NORMAL       = Normal
	TONE         = Tone
	TONE2        = Tone2
	INITIALS     = Initials
	FIRST_LETTER = FirstLetter
	FINALS       = Finals
	FINALS_TONE  = FinalsTone
	FINALS_TONE2 = FinalsTone2
)

拼音风格(兼容之前的版本)

Variables

View Source
var Fallback = func(r rune, a Args) []string {
	return []string{}
}

Fallback 默认配置: 如何处理没有拼音的字符(忽略这个字符)

View Source
var Heteronym = false

Heteronym 默认配置:是否启用多音字模式

View Source
var PinyinDict = map[int]string{}/* 41497 elements not displayed */

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

View Source
var Separator = "-"

Separator 默认配置: `Slug` 中 Join 所用的分隔符

View Source
var Style = Normal

Style 默认配置:风格

Functions

func Convert added in v0.9.0

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

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

Example
hans := "中国人"
fmt.Println("default:", pinyin.Convert(hans, nil))
Output:

default: [[zhong] [guo] [ren]]

func LazyConvert added in v0.9.0

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

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

func LazyPinyin

func LazyPinyin(s string, a Args) []string

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

Example
hans := "中国人"
a := pinyin.NewArgs()
fmt.Println(pinyin.LazyPinyin(hans, a))
Output:

[zhong guo ren]

func Pinyin

func Pinyin(s string, a Args) [][]string

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

Example (Default)
hans := "中国人"
a := pinyin.NewArgs()
fmt.Println("default:", pinyin.Pinyin(hans, a))
Output:

default: [[zhong] [guo] [ren]]
Example (FallbackCustom1)
hans := "中国人abc"
a := pinyin.NewArgs()
a.Fallback = func(r rune, a pinyin.Args) []string {
	return []string{string(r)}
}
fmt.Println(pinyin.Pinyin(hans, a))
Output:

[[zhong] [guo] [ren] [a] [b] [c]]
Example (FallbackCustom2)
hans := "中国人アイウ"
a := pinyin.NewArgs()
a.Fallback = func(r rune, a pinyin.Args) []string {
	data := map[rune][]string{
		'ア': {"a"},
		'イ': {"i"},
		'ウ': {"u"},
	}
	s, ok := data[r]
	if ok {
		return s
	} else {
		return []string{}
	}
}
fmt.Println(pinyin.Pinyin(hans, a))
Output:

[[zhong] [guo] [ren] [a] [i] [u]]
Example (Finals)
hans := "中国人"
a := pinyin.NewArgs()
a.Style = pinyin.Finals
fmt.Println(pinyin.Pinyin(hans, a))
Output:

[[ong] [uo] [en]]
Example (FinalsTone)
hans := "中国人"
a := pinyin.NewArgs()
a.Style = pinyin.FinalsTone
fmt.Println(pinyin.Pinyin(hans, a))
Output:

[[ōng] [uó] [én]]
Example (FinalsTone2)
hans := "中国人"
a := pinyin.NewArgs()
a.Style = pinyin.FinalsTone2
fmt.Println(pinyin.Pinyin(hans, a))
Output:

[[o1ng] [uo2] [e2n]]
Example (FirstLetter)
hans := "中国人"
a := pinyin.NewArgs()
a.Style = pinyin.FirstLetter
fmt.Println(pinyin.Pinyin(hans, a))
Output:

[[z] [g] [r]]
Example (Heteronym)
hans := "中国人"
a := pinyin.NewArgs()
a.Heteronym = true
a.Style = pinyin.Tone2
fmt.Println(pinyin.Pinyin(hans, a))
Output:

[[zho1ng zho4ng] [guo2] [re2n]]
Example (Initials)
hans := "中国人"
a := pinyin.NewArgs()
a.Style = pinyin.Initials
fmt.Println("Initials:", pinyin.Pinyin(hans, a))
Output:

Initials: [[zh] [g] [r]]
Example (Normal)
hans := "中国人"
a := pinyin.NewArgs()
a.Style = pinyin.Normal
fmt.Println("Normal:", pinyin.Pinyin(hans, a))
Output:

Normal: [[zhong] [guo] [ren]]
Example (Tone)
hans := "中国人"
a := pinyin.NewArgs()
a.Style = pinyin.Tone
fmt.Println("Tone:", pinyin.Pinyin(hans, a))
Output:

Tone: [[zhōng] [guó] [rén]]
Example (Tone2)
hans := "中国人"
a := pinyin.NewArgs()
a.Style = pinyin.Tone2
fmt.Println("Tone2:", pinyin.Pinyin(hans, a))
Output:

Tone2: [[zho1ng] [guo2] [re2n]]

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
hans := "中国人"
a := pinyin.NewArgs()
fmt.Println(pinyin.Slug(hans, a))
Output:

zhong-guo-ren

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 added in v0.2.0

func NewArgs() Args

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

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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