Skip to content

Commit b7fbbf0

Browse files
committed
Merge pull request #2 from mozillazg/develop
v0.3.0
2 parents 40555cf + 66e2b1d commit b7fbbf0

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

CHANGELOG.md

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

3+
## 0.3.0 (2015-12-29)
4+
5+
* fix "当字符串中有非中文的时候,会出现下标越界的情况"(影响 `pinyin.LazyPinyin``pinyin.Slug` ([#1](https://github.com/mozillazg/go-pinyin/issues/1)))
6+
* 调整对非中文字符的处理:当遇到没有拼音的字符时,直接忽略
7+
```
8+
// before
9+
fmt.Println(pinyin.Pinyin("中国人abc", pinyin.NewArgs()))
10+
[[zhong] [guo] [ren] [] [] []]
11+
12+
// after
13+
fmt.Println(pinyin.Pinyin("中国人abc", pinyin.NewArgs()))
14+
[[zhong] [guo] [ren]]
15+
```
16+
17+
318
## 0.2.1 (2015-08-26)
419

520
* `yu`, `y`, `w` 不是声母

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,4 @@ Related Projects
7272

7373
* [hotoo/pinyin](https://github.com/hotoo/pinyin): 汉语拼音转换工具 Node.js/JavaScript 版。
7474
* [mozillazg/python-pinyin](https://github.com/mozillazg/python-pinyin): 汉语拼音转换工具 Python 版。
75+
* [mozillazg/rust-pinyin](https://github.com/mozillazg/rust-pinyin): 汉语拼音转换工具 Rust 版。

pinyin.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@ import (
77

88
// Meta
99
const (
10-
Version = "0.2.1"
10+
Version = "0.3.0"
1111
Author = "mozillazg, 闲耘"
1212
License = "MIT"
1313
Copyright = "Copyright (c) 2014 mozillazg, 闲耘"
1414
)
1515

1616
// 拼音风格(推荐)
1717
const (
18-
Normal = 0 // 普通风格,不带声调(默认风格)。如: pin yin
19-
Tone = 1 // 声调风格1,拼音声调在韵母第一个字母上。如: pīn yīn
20-
Tone2 = 2 // 声调风格2,即拼音声调在各个拼音之后,用数字 [0-4] 进行表示。如: pi1n yi1n
21-
Initials = 3 // 声母风格,只返回各个拼音的声母部分。如: 中国 的拼音 zh g
22-
FirstLetter = 4 // 首字母风格,只返回拼音的首字母部分。如: p y
18+
Normal = 0 // 普通风格,不带声调(默认风格)。如: zhong guo
19+
Tone = 1 // 声调风格1,拼音声调在韵母第一个字母上。如: zhōng guó
20+
Tone2 = 2 // 声调风格2,即拼音声调在各个拼音之后,用数字 [0-4] 进行表示。如: zho1ng guo2
21+
Initials = 3 // 声母风格,只返回各个拼音的声母部分。如: zh g
22+
FirstLetter = 4 // 首字母风格,只返回拼音的首字母部分。如: z g
2323
Finals = 5 // 韵母风格1,只返回各个拼音的韵母部分,不带声调。如: ong uo
2424
FinalsTone = 6 // 韵母风格2,带声调,声调在韵母第一个字母上。如: ōng uó
2525
FinalsTone2 = 7 // 韵母风格2,带声调,声调在各个拼音之后,用数字 [0-4] 进行表示。如: o1ng uo2
2626
)
2727

2828
// 拼音风格(兼容之前的版本)
2929
const (
30-
NORMAL = 0 // 普通风格,不带声调(默认风格)。如: pin yin
31-
TONE = 1 // 声调风格1,拼音声调在韵母第一个字母上。如: pīn yīn
32-
TONE2 = 2 // 声调风格2,即拼音声调在各个拼音之后,用数字 [0-4] 进行表示。如: pi1n yi1n
33-
INITIALS = 3 // 声母风格,只返回各个拼音的声母部分。如: 中国 的拼音 zh g
34-
FIRST_LETTER = 4 // 首字母风格,只返回拼音的首字母部分。如: p y
35-
FINALS = 5 // 韵母风格1,只返回各个拼音的韵母部分,不带声调。如: ong uo
36-
FINALS_TONE = 6 // 韵母风格2,带声调,声调在韵母第一个字母上。如: ōng uó
37-
FINALS_TONE2 = 7 // 韵母风格2,带声调,声调在各个拼音之后,用数字 [0-4] 进行表示。如: o1ng uo2
30+
NORMAL = Normal
31+
TONE = Tone
32+
TONE2 = Tone2
33+
INITIALS = Initials
34+
FIRST_LETTER = FirstLetter
35+
FINALS = Finals
36+
FINALS_TONE = FinalsTone
37+
FINALS_TONE2 = FinalsTone2
3838
)
3939

4040
// 声母表
@@ -159,7 +159,10 @@ func Pinyin(s string, a Args) [][]string {
159159
hans := []rune(s)
160160
pys := [][]string{}
161161
for _, r := range hans {
162-
pys = append(pys, SinglePinyin(r, a))
162+
py := SinglePinyin(r, a)
163+
if len(py) > 0 {
164+
pys = append(pys, py)
165+
}
163166
}
164167
return pys
165168
}

pinyin_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestPinyin(t *testing.T) {
9191
func TestNoneHans(t *testing.T) {
9292
s := "abc"
9393
v := Pinyin(s, NewArgs())
94-
value := [][]string{[]string{}, []string{}, []string{}}
94+
value := [][]string{}
9595
if !reflect.DeepEqual(v, value) {
9696
t.Errorf("Expected %s, got %s", value, v)
9797
}
@@ -104,6 +104,13 @@ func TestLazyPinyin(t *testing.T) {
104104
if !reflect.DeepEqual(v, value) {
105105
t.Errorf("Expected %s, got %s", value, v)
106106
}
107+
108+
s = "中国人abc"
109+
v = LazyPinyin(s, Args{})
110+
value = []string{"zhong", "guo", "ren"}
111+
if !reflect.DeepEqual(v, value) {
112+
t.Errorf("Expected %s, got %s", value, v)
113+
}
107114
}
108115

109116
func TestSlug(t *testing.T) {
@@ -126,6 +133,13 @@ func TestSlug(t *testing.T) {
126133
if v != value {
127134
t.Errorf("Expected %s, got %s", value, v)
128135
}
136+
137+
s = "中国人abc,,中"
138+
v = Slug(s, a)
139+
value = "zhong-guo-ren-zhong"
140+
if v != value {
141+
t.Errorf("Expected %s, got %s", value, v)
142+
}
129143
}
130144

131145
func TestFinal(t *testing.T) {

0 commit comments

Comments
 (0)