pinyin

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-pinyin

Go

汉语拼音转换工具 Go 版。

这是一个 go-pinyin fork 版本,用来支持段落、短语、多一字准确转换的支持。

Installation

go get -u github.com/longbridge/go-pinyin

install CLI tool:

go get -u github.com/longbridge/go-pinyin/cmd/pinyin
$ pinyin 中国人
zhōng guó rén

Documentation

API documentation can be found here: https://pkg.go.dev/github.com/longbridge/go-pinyin

Usage

package main

import (
	"fmt"
	"github.com/longbridge/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]

	// 段落转换,支持完整支持多音字,保留符号
	fmt.Println(pinyin.Paragraph("交给团长,告诉他我们给予期望。前线的供给一定要能自给自足!"))
	// jiao gei tuan zhang, gao su ta wo men ji yu qi wang. qian xian de gong ji yi ding yao neng zi ji zi zu!
}

注意:

  • 默认情况下会忽略没有拼音的字符(可以通过自定义 Fallback 参数的值来自定义如何处理没有拼音的字符, 详见 示例)。
  • 根据 《汉语拼音方案》 y,w,ü (yu) 都不是声母, 以及不是所有拼音都有声母,如果这不是你预期的话,你可能需要的是首字母风格 FirstLetter ( 详细信息 )。

pinyin data

License

Under the MIT License.

Documentation

Overview

Package pinyin : 汉语拼音转换工具.

Usage

package main

import (
	"fmt"
	"github.com/longbridge/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   = "1.1.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{}/* 41455 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
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	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
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	hans := "中国人"
	a := pinyin.NewArgs()
	fmt.Println(pinyin.LazyPinyin(hans, a))
}
Output:
[zhong guo ren]

func Paragraph added in v1.2.0

func Paragraph(p string) (s string)

Paragraph convert a Chinese paragraph into pinyin, including letters, numbers, symbols

Example
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	hans := "人民银行旁边一行人abc字母【路牌】,平行宇宙发行股票。"
	fmt.Println(pinyin.Paragraph(hans))
}
Output:
ren min yin xing pang bian yi xing ren abc zi mu [lu pai], ping xing yu zhou fa xing gu piao.

func Pinyin

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

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

Example (Default)
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	hans := "中国人"
	a := pinyin.NewArgs()
	fmt.Println("default:", pinyin.Pinyin(hans, a))
}
Output:
default: [[zhong] [guo] [ren]]
Example (FallbackCustom1)
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	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)
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	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)
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

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

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

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

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

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

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	hans := "中国人"
	a := pinyin.NewArgs()
	a.Style = pinyin.FirstLetter
	fmt.Println(pinyin.Pinyin(hans, a))
}
Output:
[[z] [g] [r]]
Example (Heteronym)
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	hans := "中国人"
	a := pinyin.NewArgs()
	a.Heteronym = true
	a.Style = pinyin.Tone2
	fmt.Println(pinyin.Pinyin(hans, a))
}
Output:
[[zho1ng zho4ng] [guo2] [re2n]]
Example (Initials)
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

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

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

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

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

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

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	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
package main

import (
	"fmt"

	"github.com/longbridge/go-pinyin"
)

func main() {
	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
_tools
gen_phrase_dict command
gen_pinyin_dict command
cmd
pinyin command

Jump to

Keyboard shortcuts

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