Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 763576c

Browse files
committed
Initial gpy
1 parent 6507668 commit 763576c

File tree

14 files changed

+42829
-1
lines changed

14 files changed

+42829
-1
lines changed

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: go
2+
go:
3+
- 1.6.x
4+
- 1.7.x
5+
- 1.8.x
6+
- tip
7+
8+
sudo: false
9+
10+
before_install:
11+
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
12+
- go get -u github.com/mattn/go-isatty
13+
- go get -u github.com/axw/gocov/gocov
14+
- go get -u github.com/mattn/goveralls
15+
16+
script:
17+
- go run pinyin/main.go abc
18+
- go run pinyin/main.go -s Normal abc
19+
- echo "abc" | go run pinyin/main.go
20+
- echo "abc" > abc.txt && go run pinyin/main.go < abc.txt
21+
- $HOME/gopath/bin/goveralls -service=travis-ci -v -package .

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Changelog
2+
3+
## 0.1.0 (2017-6-23)
4+
* Initial Release

Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
help:
2+
@echo "test run test"
3+
@echo "lint run lint"
4+
@echo "gen_pinyin_dict gen pinyin dict"
5+
6+
.PHONY: test
7+
test:
8+
@echo "run test"
9+
@go test -v -cover
10+
11+
.PHONY: gen_pinyin_dict
12+
gen_pinyin_dict:
13+
@go run tools/gen_pinyin_dict.go tools/pinyin-data/pinyin.txt pinyin_dict.go
14+
15+
.PHONY: lint
16+
lint:
17+
gofmt -s -w . pinyin tools
18+
golint .
19+
golint pinyin
20+
golint tools

README.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,96 @@
1-
# gpy
1+
# gpy
2+
=========
3+
4+
[![Build Status](https://travis-ci.org/go-ego/gpy.svg?branch=master)](https://travis-ci.org/go-ego/gpy)
5+
[![Coverage Status](https://coveralls.io/repos/go-ego/gpy/badge.svg?branch=master)](https://coveralls.io/r/go-ego/gpy?branch=master)
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/go-ego/gpy)](https://goreportcard.com/report/github.com/go-ego/gpy)
7+
[![GoDoc](https://godoc.org/github.com/go-ego/gpy?status.svg)](https://godoc.org/github.com/go-ego/gpy)
8+
9+
汉语拼音转换工具 Go 版。
10+
11+
12+
Installation
13+
------------
14+
15+
```
16+
go get -u github.com/go-ego/gpy
17+
```
18+
19+
install CLI tool:
20+
21+
```
22+
go get -u github.com/go-ego/gpy/pinyin
23+
$ pinyin 中国人
24+
zhōng guó rén
25+
```
26+
27+
28+
Documentation
29+
--------------
30+
31+
API documentation can be found here:
32+
https://godoc.org/github.com/go-ego/gpy
33+
34+
35+
Usage
36+
------
37+
38+
```go
39+
package main
40+
41+
import (
42+
"fmt"
43+
"github.com/go-ego/gpy"
44+
)
45+
46+
func main() {
47+
hans := "中国人"
48+
49+
// 默认
50+
a := pinyin.NewArgs()
51+
fmt.Println(pinyin.Pinyin(hans, a))
52+
// [[zhong] [guo] [ren]]
53+
54+
// 包含声调
55+
a.Style = pinyin.Tone
56+
fmt.Println(pinyin.Pinyin(hans, a))
57+
// [[zhōng] [guó] [rén]]
58+
59+
// 声调用数字表示
60+
a.Style = pinyin.Tone2
61+
fmt.Println(pinyin.Pinyin(hans, a))
62+
// [[zho1ng] [guo2] [re2n]]
63+
64+
// 开启多音字模式
65+
a = pinyin.NewArgs()
66+
a.Heteronym = true
67+
fmt.Println(pinyin.Pinyin(hans, a))
68+
// [[zhong zhong] [guo] [ren]]
69+
a.Style = pinyin.Tone2
70+
fmt.Println(pinyin.Pinyin(hans, a))
71+
// [[zho1ng zho4ng] [guo2] [re2n]]
72+
73+
fmt.Println(pinyin.LazyPinyin(hans, pinyin.NewArgs()))
74+
// [zhong guo ren]
75+
76+
fmt.Println(pinyin.Convert(hans, nil))
77+
// [[zhong] [guo] [ren]]
78+
79+
fmt.Println(pinyin.LazyConvert(hans, nil))
80+
// [zhong guo ren]
81+
}
82+
```
83+
84+
85+
Related Projects
86+
-----------------
87+
88+
* [hotoo/pinyin](https://github.com/hotoo/pinyin): 汉语拼音转换工具 Node.js/JavaScript 版。
89+
* [mozillazg/python-pinyin](https://github.com/mozillazg/python-pinyin): 汉语拼音转换工具 Python 版。
90+
* [mozillazg/rust-pinyin](https://github.com/mozillazg/rust-pinyin): 汉语拼音转换工具 Rust 版。
91+
92+
93+
License
94+
---------
95+
96+
Under the MIT License, base on [go-pinyin](https://github.com/go-ego/gpy).

README_zh.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# gpy
2+
=========
3+
4+
[![Build Status](https://travis-ci.org/go-ego/gpy.svg?branch=master)](https://travis-ci.org/go-ego/gpy)
5+
[![Coverage Status](https://coveralls.io/repos/go-ego/gpy/badge.svg?branch=master)](https://coveralls.io/r/go-ego/gpy?branch=master)
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/go-ego/gpy)](https://goreportcard.com/report/github.com/go-ego/gpy)
7+
[![GoDoc](https://godoc.org/github.com/go-ego/gpy?status.svg)](https://godoc.org/github.com/go-ego/gpy)
8+
9+
汉语拼音转换工具 Go 版。
10+
11+
12+
Installation
13+
------------
14+
15+
```
16+
go get -u github.com/go-ego/gpy
17+
```
18+
19+
install CLI tool:
20+
21+
```
22+
go get -u github.com/go-ego/gpy/pinyin
23+
$ pinyin 中国人
24+
zhōng guó rén
25+
```
26+
27+
28+
Documentation
29+
--------------
30+
31+
API documentation can be found here:
32+
https://godoc.org/github.com/go-ego/gpy
33+
34+
35+
Usage
36+
------
37+
38+
```go
39+
package main
40+
41+
import (
42+
"fmt"
43+
"github.com/go-ego/gpy"
44+
)
45+
46+
func main() {
47+
hans := "中国人"
48+
49+
// 默认
50+
a := pinyin.NewArgs()
51+
fmt.Println(pinyin.Pinyin(hans, a))
52+
// [[zhong] [guo] [ren]]
53+
54+
// 包含声调
55+
a.Style = pinyin.Tone
56+
fmt.Println(pinyin.Pinyin(hans, a))
57+
// [[zhōng] [guó] [rén]]
58+
59+
// 声调用数字表示
60+
a.Style = pinyin.Tone2
61+
fmt.Println(pinyin.Pinyin(hans, a))
62+
// [[zho1ng] [guo2] [re2n]]
63+
64+
// 开启多音字模式
65+
a = pinyin.NewArgs()
66+
a.Heteronym = true
67+
fmt.Println(pinyin.Pinyin(hans, a))
68+
// [[zhong zhong] [guo] [ren]]
69+
a.Style = pinyin.Tone2
70+
fmt.Println(pinyin.Pinyin(hans, a))
71+
// [[zho1ng zho4ng] [guo2] [re2n]]
72+
73+
fmt.Println(pinyin.LazyPinyin(hans, pinyin.NewArgs()))
74+
// [zhong guo ren]
75+
76+
fmt.Println(pinyin.Convert(hans, nil))
77+
// [[zhong] [guo] [ren]]
78+
79+
fmt.Println(pinyin.LazyConvert(hans, nil))
80+
// [zhong guo ren]
81+
}
82+
```
83+
84+
85+
Related Projects
86+
-----------------
87+
88+
* [hotoo/pinyin](https://github.com/hotoo/pinyin): 汉语拼音转换工具 Node.js/JavaScript 版。
89+
* [mozillazg/python-pinyin](https://github.com/mozillazg/python-pinyin): 汉语拼音转换工具 Python 版。
90+
* [mozillazg/rust-pinyin](https://github.com/mozillazg/rust-pinyin): 汉语拼音转换工具 Rust 版。
91+
92+
93+
License
94+
---------
95+
96+
Under the MIT License, base on [go-pinyin](https://github.com/go-ego/gpy).

benchmark_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package gpy
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
var hans500 = strings.Replace(strings.Replace(`
9+
的、一、是、在、不、了、有、和、人、这、中、大、为、上、个、国、我、以、要、他、
10+
时、来、用、们、生、到、作、地、于、出、就、分、对、成、会、可、主、发、年、动、
11+
同、工、也、能、下、过、子、说、产、种、面、而、方、后、多、定、行、学、法、所、
12+
民、得、经、十、三、之、进、着、等、部、度、家、电、力、里、如、水、化、高、自、
13+
二、理、起、小、物、现、实、加、量、都、两、体、制、机、当、使、点、从、业、本、
14+
去、把、性、好、应、开、它、合、还、因、由、其、些、然、前、外、天、政、四、日、
15+
那、社、义、事、平、形、相、全、表、间、样、与、关、各、重、新、线、内、数、正、
16+
心、反、你、明、看、原、又、么、利、比、或、但、质、气、第、向、道、命、此、变、
17+
条、只、没、结、解、问、意、建、月、公、无、系、军、很、情、者、最、立、代、想、
18+
已、通、并、提、直、题、党、程、展、五、果、料、象、员、革、位、入、常、文、总、
19+
次、品、式、活、设、及、管、特、件、长、求、老、头、基、资、边、流、路、级、少、
20+
图、山、统、接、知、较、将、组、见、计、别、她、手、角、期、根、论、运、农、指、
21+
几、九、区、强、放、决、西、被、干、做、必、战、先、回、则、任、取、据、处、队、
22+
南、给、色、光、门、即、保、治、北、造、百、规、热、领、七、海、口、东、导、器、
23+
压、志、世、金、增、争、济、阶、油、思、术、极、交、受、联、什、认、六、共、权、
24+
收、证、改、清、己、美、再、采、转、更、单、风、切、打、白、教、速、花、带、安、
25+
场、身、车、例、真、务、具、万、每、目、至、达、走、积、示、议、声、报、斗、完、
26+
类、八、离、华、名、确、才、科、张、信、马、节、话、米、整、空、元、况、今、集、
27+
温、传、土、许、步、群、广、石、记、需、段、研、界、拉、林、律、叫、且、究、观、
28+
越、织、装、影、算、低、持、音、众、书、布、复、容、儿、须、际、商、非、验、连、
29+
断、深、难、近、矿、千、周、委、素、技、备、半、办、青、省、列、习、响、约、支、
30+
般、史、感、劳、便、团、往、酸、历、市、克、何、除、消、构、府、称、太、准、精、
31+
值、号、率、族、维、划、选、标、写、存、候、毛、亲、快、效、斯、院、查、江、型、
32+
眼、王、按、格、养、易、置、派、层、片、始、却、专、状、育、厂、京、识、适、属、
33+
圆、包、火、住、调、满、县、局、照、参、红、细、引、听、该、铁、价、严、龙、飞
34+
`, "、", "", -1), "\n", "", -1)
35+
36+
func benchmarkPinyin(b *testing.B, s string, args Args) {
37+
b.StopTimer()
38+
b.StartTimer()
39+
40+
for i := 0; i < b.N; i++ {
41+
Pinyin(s, args)
42+
}
43+
}
44+
45+
func benchmarkLazyPinyin(b *testing.B, s string, args Args) {
46+
b.StopTimer()
47+
b.StartTimer()
48+
49+
for i := 0; i < b.N; i++ {
50+
LazyPinyin(s, args)
51+
}
52+
}
53+
54+
func BenchmarkPinyinOne(b *testing.B) {
55+
args := NewArgs()
56+
benchmarkPinyin(b, "中", args)
57+
}
58+
59+
func BenchmarkPinyin500(b *testing.B) {
60+
args := NewArgs()
61+
benchmarkPinyin(b, hans500, args)
62+
}
63+
64+
func BenchmarkLazyPinyinOne(b *testing.B) {
65+
args := NewArgs()
66+
benchmarkLazyPinyin(b, "中", args)
67+
}
68+
69+
func BenchmarkLazyPinyin500(b *testing.B) {
70+
args := NewArgs()
71+
benchmarkLazyPinyin(b, hans500, args)
72+
}

doc.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
package gpy : 汉语拼音转换工具.
3+
4+
Usage
5+
6+
package main
7+
8+
import (
9+
"fmt"
10+
"github.com/go-ego/gpy"
11+
)
12+
13+
func main() {
14+
hans := "中国人"
15+
// 默认
16+
a := gpy.NewArgs()
17+
fmt.Println(gpy.gpy(hans, a))
18+
// [[zhong] [guo] [ren]]
19+
20+
// 包含声调
21+
a.Style = gpy.Tone
22+
fmt.Println(gpy.gpy(hans, a))
23+
// [[zhōng] [guó] [rén]]
24+
25+
// 声调用数字表示
26+
a.Style = gpy.Tone2
27+
fmt.Println(gpy.gpy(hans, a))
28+
// [[zho1ng] [guo2] [re2n]]
29+
30+
// 开启多音字模式
31+
a = gpy.NewArgs()
32+
a.Heteronym = true
33+
fmt.Println(gpy.gpy(hans, a))
34+
// [[zhong zhong] [guo] [ren]]
35+
a.Style = gpy.Tone2
36+
fmt.Println(gpy.gpy(hans, a))
37+
// [[zho1ng zho4ng] [guo2] [re2n]]
38+
}
39+
*/
40+
package gpy

0 commit comments

Comments
 (0)