Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mozillazg/develop
v0.1.0
- Loading branch information
Showing
8 changed files
with
376 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
language: go | ||
go: | ||
- 1.2 | ||
- 1.3 | ||
- 1.4 | ||
- 1.5 | ||
- 1.6 | ||
- tip | ||
|
||
sudo: false | ||
|
||
before_install: | ||
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi | ||
- go get github.com/axw/gocov/gocov | ||
- go get github.com/mattn/goveralls | ||
|
||
install: | ||
- go get . | ||
- go get ./stpinyin | ||
|
||
script: | ||
- stpinyin -V | ||
- stpinyin abc | ||
- echo "abc" | stpinyin | ||
- echo "abc" > abc.txt && stpinyin < abc.txt | ||
- $HOME/gopath/bin/goveralls -service=travis-ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## 0.1.0 (2016-08-13) | ||
|
||
* Initial Release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
help: | ||
@echo "test run test" | ||
@echo "lint run lint" | ||
|
||
.PHONY: test | ||
test: | ||
go test -v -cover | ||
|
||
.PHONY: lint | ||
lint: | ||
gofmt -s -w . stpinyin | ||
golint . | ||
golint stpinyin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,57 @@ | ||
# stpinyin | ||
ta1 -> tā | ||
stpinyin | ||
============== | ||
|
||
[![Build Status](https://travis-ci.org/mozillazg/stpinyin.svg?branch=master)](https://travis-ci.org/mozillazg/stpinyin) | ||
[![Coverage Status](https://coveralls.io/repos/mozillazg/stpinyin/badge.svg?branch=master)](https://coveralls.io/r/mozillazg/stpinyin?branch=master) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/mozillazg/stpinyin)](https://goreportcard.com/report/github.com/mozillazg/stpinyin) | ||
[![GoDoc](https://godoc.org/github.com/mozillazg/stpinyin?status.svg)](https://godoc.org/github.com/mozillazg/stpinyin) | ||
|
||
Convert pinyin like this: `you1 -> yōu` | ||
|
||
Installation | ||
------------ | ||
|
||
``` | ||
go get -u github.com/mozillazg/stpinyin | ||
``` | ||
|
||
Install CLI tool: | ||
|
||
``` | ||
go get -u github.com/mozillazg/stpinyin/stpinyin | ||
$ stpinyin you1 hang2 | ||
yōu háng | ||
``` | ||
|
||
|
||
Documentation | ||
-------------- | ||
|
||
API documentation can be found here: | ||
https://godoc.org/github.com/mozillazg/stpinyin | ||
|
||
|
||
Usage | ||
------ | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mozillazg/stpinyin" | ||
) | ||
|
||
func main() { | ||
s := "you1" | ||
fmt.Println(stpinyin.Convert(s)) | ||
// Output: yōu | ||
} | ||
``` | ||
|
||
License | ||
--------- | ||
|
||
Under the MIT License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package stpinyin_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mozillazg/stpinyin" | ||
) | ||
|
||
func ExampleConvert() { | ||
s := "you1" | ||
fmt.Println(stpinyin.Convert(s)) | ||
// Output: yōu | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Package stpinyin implements convert pinyin like this: you1 -> yōu | ||
package stpinyin | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// Version export version info | ||
func Version() string { | ||
return "0.1.0" | ||
} | ||
|
||
var toneMap = map[string]string{ | ||
"a1": "ā", | ||
"a2": "á", | ||
"a3": "ǎ", | ||
"a4": "à", | ||
"e1": "ē", | ||
"e2": "é", | ||
"e3": "ě", | ||
"e4": "è", | ||
"o1": "ō", | ||
"o2": "ó", | ||
"o3": "ǒ", | ||
"o4": "ò", | ||
"i1": "ī", | ||
"i2": "í", | ||
"i3": "ǐ", | ||
"i4": "ì", | ||
"u1": "ū", | ||
"u2": "ú", | ||
"u3": "ǔ", | ||
"u4": "ù", | ||
"v0": "ü", | ||
"v2": "ǘ", | ||
"v3": "ǚ", | ||
"v4": "ǜ", | ||
"n2": "ń", | ||
"n3": "ň", | ||
"n4": "ǹ", | ||
"m2": "ḿ", | ||
} | ||
|
||
var tonePositions = [][2]string{ | ||
{"ang", "a"}, | ||
{"eng", "e"}, | ||
{"ong", "o"}, | ||
{"ing", "i"}, | ||
{"en", "e"}, | ||
{"an", "a"}, | ||
{"ou", "o"}, | ||
{"ai", "a"}, | ||
{"ei", "e"}, | ||
{"ao", "a"}, | ||
{"in", "i"}, | ||
{"un", "u"}, | ||
{"a", "a"}, | ||
{"o", "o"}, | ||
{"e", "e"}, | ||
{"i", "i"}, | ||
{"u", "u"}, | ||
{"v", "v"}, | ||
{"m", "m"}, | ||
{"n", "n"}, | ||
} | ||
|
||
var reToneNumber = regexp.MustCompile("^([^0-4]+)([0-4])$") | ||
|
||
// Convert implements you1 -> yōu | ||
func Convert(src string) (s string) { | ||
s = fixNumberPosition(src) | ||
for key, value := range toneMap { | ||
if strings.Contains(s, key) { | ||
s = strings.Replace(s, key, value, 1) | ||
break | ||
} | ||
} | ||
return s | ||
} | ||
|
||
const matchNumber = 3 | ||
|
||
// hang2 -> ha2ng | ||
func fixNumberPosition(src string) (s string) { | ||
matchs := reToneNumber.FindStringSubmatch(src) | ||
if len(matchs) != matchNumber { | ||
return src | ||
} | ||
|
||
letter := matchs[1] | ||
number := matchs[2] | ||
// hang2 -> ha2ng | ||
for _, item := range tonePositions { | ||
suffix := item[0] | ||
position := item[1] | ||
if strings.HasSuffix(letter, suffix) { | ||
prefix := letter[:len(letter)-len(suffix)] | ||
// ang2 -> a2ng | ||
suffix = strings.Replace(suffix, position, position+number, 1) | ||
return prefix + suffix | ||
} | ||
} | ||
return src | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/mattn/go-isatty" | ||
"github.com/mozillazg/stpinyin" | ||
) | ||
|
||
func main() { | ||
version := flag.Bool("V", false, "Output version info") | ||
flag.Parse() | ||
if *version { | ||
v := stpinyin.Version() | ||
fmt.Printf("stpinyin %s\n", v) | ||
os.Exit(0) | ||
} | ||
|
||
textSlice := flag.Args() | ||
stdin := []byte{} | ||
if !isatty.IsTerminal(os.Stdin.Fd()) { | ||
stdin, _ = ioutil.ReadAll(os.Stdin) | ||
} | ||
if len(stdin) > 0 { | ||
textSlice = append(textSlice, string(stdin)) | ||
} | ||
|
||
if len(textSlice) == 0 { | ||
fmt.Println("Usage: stpinyin STRING") | ||
os.Exit(1) | ||
} | ||
|
||
ret := []string{} | ||
for _, s := range textSlice { | ||
ret = append(ret, stpinyin.Convert(s)) | ||
} | ||
fmt.Println(strings.Join(ret, " ")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package stpinyin | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
) | ||
|
||
func check(t *testing.T, ret, expect string) { | ||
if ret != expect { | ||
t.Errorf("Expected %v, got %v", expect, ret) | ||
} | ||
} | ||
|
||
func TestVersion(t *testing.T) { | ||
ret := Version() | ||
check(t, ret, "0.1.0") | ||
} | ||
|
||
func TestConvert(t *testing.T) { | ||
for input, expect := range map[string]string{ | ||
"yi1": "yī", | ||
"wu1": "wū", | ||
"yu1": "yū", | ||
"a1": "ā", | ||
"ya1": "yā", | ||
"wa1": "wā", | ||
"o1": "ō", | ||
"wo1": "wō", | ||
"e2": "é", | ||
"ye2": "yé", | ||
"yue1": "yuē", | ||
"ai1": "āi", | ||
"wai1": "wāi", | ||
"ei2": "éi", | ||
"wei1": "wēi", | ||
"ao2": "áo", | ||
"yao1": "yāo", | ||
"ou1": "ōu", | ||
"you1": "yōu", | ||
"an1": "ān", | ||
"yan1": "yān", | ||
"wan1": "wān", | ||
"yuan1": "yuān", | ||
"en1": "ēn", | ||
"yin1": "yīn", | ||
"wen1": "wēn", | ||
"yun4": "yùn", | ||
"ang2": "áng", | ||
"yang1": "yāng", | ||
"wang1": "wāng", | ||
"heng1": "hēng", | ||
"ying1": "yīng", | ||
"weng1": "wēng", | ||
"hong1": "hōng", | ||
"yong1": "yōng", | ||
"n4": "ǹ", | ||
"n2g": "ńg", | ||
"m2": "ḿ", | ||
"mou2": "móu", | ||
"móu": "móu", | ||
} { | ||
log.Printf("input %r, expect %r", input, expect) | ||
ret := Convert(input) | ||
check(t, ret, expect) | ||
} | ||
} | ||
|
||
func Test_fixNumberPosition(t *testing.T) { | ||
for input, expect := range map[string]string{ | ||
"yi1": "yi1", | ||
"wu1": "wu1", | ||
"yu1": "yu1", | ||
"a1": "a1", | ||
"ya1": "ya1", | ||
"wa1": "wa1", | ||
"o1": "o1", | ||
"wo1": "wo1", | ||
"e2": "e2", | ||
"ye1": "ye1", | ||
"yue1": "yue1", | ||
"ai1": "a1i", | ||
"wai1": "wa1i", | ||
"ei2": "e2i", | ||
"wei1": "we1i", | ||
"ao2": "a2o", | ||
"yao1": "ya1o", | ||
"ou1": "o1u", | ||
"you1": "yo1u", | ||
"an1": "a1n", | ||
"yan1": "ya1n", | ||
"wan1": "wa1n", | ||
"yuan1": "yua1n", | ||
"en1": "e1n", | ||
"yin1": "yi1n", | ||
"wen1": "we1n", | ||
"yun4": "yu4n", | ||
"ang2": "a2ng", | ||
"yang1": "ya1ng", | ||
"wang1": "wa1ng", | ||
"heng1": "he1ng", | ||
"ying1": "yi1ng", | ||
"weng1": "we1ng", | ||
"hong1": "ho1ng", | ||
"yong1": "yo1ng", | ||
"n2": "n2", | ||
"n2g": "n2g", | ||
"m2": "m2", | ||
"m": "m", | ||
"g3": "g3", | ||
} { | ||
log.Printf("input %r, expect %r", input, expect) | ||
ret := fixNumberPosition(input) | ||
check(t, ret, expect) | ||
} | ||
} |