MCPcopy
hub / github.com/mozillazg/go-pinyin

github.com/mozillazg/go-pinyin @v0.21.0 sqlite

repository ↗ · DeepWiki ↗ · release v0.21.0 ↗
54 symbols 120 edges 9 files 11 documented · 20%
README

go-pinyin

Build Status Coverage Status Go Report Card GoDoc

汉语拼音转换工具 Go 版。

Installation

go get github.com/mozillazg/go-pinyin

install CLI tool:

# go version>=1.17
go install github.com/mozillazg/go-pinyin/cli/pinyin@latest

# go version<1.17
go get -u github.com/mozillazg/go-pinyin/cli/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] [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.

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

NewArgs
called by 13
pinyin.go
Pinyin
called by 5
pinyin.go
LazyPinyin
called by 5
pinyin.go
Slug
called by 4
pinyin.go
initial
called by 2
pinyin.go
final
called by 2
pinyin.go
Convert
called by 2
pinyin.go
LazyConvert
called by 2
pinyin.go

Shape

Function 49
Struct 4
FuncType 1

Languages

Go100%

Modules by API surface

pinyin_test.go15 symbols
example_test.go15 symbols
pinyin.go13 symbols
benchmark_test.go6 symbols
_tools/gen_pinyin_dict.go4 symbols
cli/pinyin/main.go1 symbols

Dependencies from manifests, versioned

github.com/mattn/go-isattyv0.0.18 · 1×
golang.org/x/sysv0.6.0 · 1×

For agents

$ claude mcp add go-pinyin \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact