Skip to content

Commit c9122b8

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents a1cd0b7 + fea3a56 commit c9122b8

18 files changed

+41485
-16
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "_tools/pinyin-data"]
22
path = _tools/pinyin-data
33
url = https://github.com/mozillazg/pinyin-data.git
4+
[submodule "_tools/phrase-data"]
5+
path = _tools/phrase-data
6+
url = https://github.com/hotoo/pinyin.git

.travis.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
language: go
22
go:
3-
- '1.7'
4-
- '1.8'
5-
- '1.9.x'
6-
- '1.10.x'
7-
- '1.11.x'
8-
- '1.12.x'
9-
- 'master'
3+
- "1.9.x"
4+
- "1.10.x"
5+
- "1.11.x"
6+
- "1.12.x"
7+
- "1.13.x"
108

119
sudo: false
1210

1311
before_install:
1412
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
15-
- go get github.com/mattn/go-isatty
1613
- go get github.com/axw/gocov/gocov
1714
- go get github.com/mattn/goveralls
15+
- go get ./cmd/pinyin
1816

1917
script:
2018
- go run cmd/pinyin/main.go abc
2119
- go run cmd/pinyin/main.go -s zhao abc
2220
- echo "abc" | go run cmd/pinyin/main.go
2321
- echo "abc" > abc.txt && go run cmd/pinyin/main.go < abc.txt
2422
- $HOME/gopath/bin/goveralls -service=travis-ci -v -package .
25-
26-
matrix:
27-
allow_failures:
28-
- go: master

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [0.16.0] (2019-12-05)
4+
5+
* **NEW** 增加 ``func Paragraph(p string) string`` 用于便捷处理大段文字
6+
(thanks [@huacnlee] via [#37][#37])
7+
38
## [0.15.0] (2019-04-06)
49

510
* **Changed** 使用 [pinyin-data][pinyin-data] v0.7.0 的拼音数据
@@ -193,9 +198,11 @@
193198

194199
[pinyin-data]: https://github.com/mozillazg/pinyin-data
195200
[@wdscxsj]: https://github.com/wdscxsj
201+
[@huacnlee]: https://github.com/huacnlee
196202
[#19]: https://github.com/mozillazg/go-pinyin/pull/19
197203
[#20]: https://github.com/mozillazg/go-pinyin/pull/20
198204
[#30]: https://github.com/mozillazg/go-pinyin/pull/30
205+
[#37]: https://github.com/mozillazg/go-pinyin/pull/37
199206

200207
[0.1.1]: https://github.com/mozillazg/go-pinyin/compare/v0.1.0...v0.1.1
201208
[0.2.0]: https://github.com/mozillazg/go-pinyin/compare/v0.1.1...v0.2.0
@@ -213,3 +220,4 @@
213220
[0.13.0]: https://github.com/mozillazg/go-pinyin/compare/v0.12.0...v0.13.0
214221
[0.14.0]: https://github.com/mozillazg/go-pinyin/compare/v0.13.0...v0.14.0
215222
[0.15.0]: https://github.com/mozillazg/go-pinyin/compare/v0.14.0...v0.15.0
223+
[0.16.0]: https://github.com/mozillazg/go-pinyin/compare/v0.15.0...v0.16.0

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ test:
1010

1111
.PHONY: gen_pinyin_dict
1212
gen_pinyin_dict:
13-
@go run _tools/gen_pinyin_dict.go _tools/pinyin-data/pinyin.txt pinyin_dict.go
13+
@go run _tools/gen_pinyin_dict/main.go _tools/pinyin-data/pinyin.txt pinyin_dict.go
14+
15+
.PHONY: gen_phrase_dict
16+
gen_phrase_dict:
17+
@go run _tools/gen_phrase_dict/main.go _tools/phrase-data/data/phrases-dict.js phrase_dict.go
18+
@goreturns -w phrase_dict.go
1419

1520
.PHONY: lint
1621
lint:

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func main() {
7878

7979
fmt.Println(pinyin.LazyConvert(hans, nil))
8080
// [zhong guo ren]
81+
82+
// 段落转换,支持完整支持多音字,保留符号
83+
fmt.Println(pinyin.Paragraph("交给团长,告诉他我们给予期望。前线的供给一定要能自给自足!"))
84+
// 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!
8185
}
8286
```
8387

_tools/gen_phrase_dict/main.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"flag"
6+
"fmt"
7+
"io"
8+
"os"
9+
"strings"
10+
)
11+
12+
type cmdArgs struct {
13+
inputFile string
14+
outputFile string
15+
}
16+
17+
func genCode(inFile *os.File, outFile *os.File) {
18+
rd := bufio.NewReader(inFile)
19+
output := `package pinyin
20+
21+
// phraseDict is data map
22+
//
23+
// Generate from:
24+
// https://github.com/hotoo/pinyin/blob/master/data/phrases-dict.js
25+
//
26+
// Warning: Auto-generated file, don't edit.
27+
// If you want add more words, use phrase_dict_addition.go
28+
var phraseDict = map[string]string{
29+
`
30+
lines := []string{}
31+
32+
for {
33+
line, err := rd.ReadString('\n')
34+
if err == io.EOF {
35+
break
36+
} else if err != nil {
37+
panic(err)
38+
}
39+
40+
// Remove prefix space
41+
line = strings.TrimSpace(line)
42+
43+
// `"后来居上": [["hòu"], ["lái"], ["jū"], ["shàng"]],` to `"后来居上": "hòu lái jū shàng",`
44+
if !strings.HasPrefix(line, `"`) {
45+
continue
46+
}
47+
48+
line = strings.ReplaceAll(line, `[`, "")
49+
line = strings.ReplaceAll(line, `]`, "")
50+
line = strings.ReplaceAll(line, `", "`, " ")
51+
52+
lines = append(lines, line)
53+
}
54+
55+
output += strings.Join(lines, "\n")
56+
output += "\n}\n"
57+
outFile.WriteString(output)
58+
return
59+
}
60+
61+
func parseCmdArgs() cmdArgs {
62+
flag.Parse()
63+
inputFile := flag.Arg(0)
64+
outputFile := flag.Arg(1)
65+
return cmdArgs{inputFile, outputFile}
66+
}
67+
68+
func main() {
69+
args := parseCmdArgs()
70+
usage := "gen_phrase_dict INPUT OUTPUT"
71+
inputFile := args.inputFile
72+
outputFile := args.outputFile
73+
if inputFile == "" || outputFile == "" {
74+
fmt.Println(usage)
75+
os.Exit(1)
76+
}
77+
78+
inFp, err := os.Open(inputFile)
79+
if err != nil {
80+
fmt.Printf("open file %s error", inputFile)
81+
panic(err)
82+
}
83+
outFp, err := os.Create(outputFile)
84+
if err != nil {
85+
fmt.Printf("open file %s error", outputFile)
86+
panic(err)
87+
}
88+
defer inFp.Close()
89+
defer outFp.Close()
90+
91+
genCode(inFp, outFp)
92+
}
File renamed without changes.

_tools/phrase-data

Submodule phrase-data added at f2b37ad

example_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,9 @@ func ExampleSlug() {
135135
fmt.Println(pinyin.Slug(hans, a))
136136
// Output: zhong-guo-ren
137137
}
138+
139+
func ExampleParagraph() {
140+
hans := "人民银行旁边一行人abc字母【路牌】,平行宇宙发行股票。"
141+
fmt.Println(pinyin.Paragraph(hans))
142+
// Output: ren min yin xing pang bian yi xing ren abc zi mu [lu pai], ping xing yu zhou fa xing gu piao.
143+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
module github.com/mozillazg/go-pinyin
2+
3+
require (
4+
github.com/mattn/go-isatty v0.0.10
5+
github.com/yanyiwu/gojieba v1.1.0
6+
)

0 commit comments

Comments
 (0)