Skip to content

Commit 0fee16e

Browse files
committed
Merge pull request #4 from mozillazg/develop
v0.4.0
2 parents b7fbbf0 + e6080b6 commit 0fee16e

File tree

6 files changed

+304
-80
lines changed

6 files changed

+304
-80
lines changed

CHANGELOG.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
# Changelog
22

3+
4+
## 0.4.0 (2016-01-29)
5+
6+
* **NEW** `Args` 结构体新增 field: `Fallback func(r rune, a Args) []string`
7+
用于处理没有拼音的字符(默认忽略没有拼音的字符):
8+
```go
9+
a := pinyin.NewArgs()
10+
a.Fallback = func(r rune, a pinyin.Args) []string {
11+
return []string{string(r + 1)}
12+
}
13+
fmt.Println(pinyin.Pinyin("中国人abc", a))
14+
// Output: [[zhong] [guo] [ren] [b] [c] [d]]
15+
16+
// or
17+
pinyin.Fallback = func(r rune, a pinyin.Args) []string {
18+
return []string{string(r)}
19+
}
20+
fmt.Println(pinyin.Pinyin("中国人abc", pinyin.NewArgs()))
21+
// Output: [[zhong] [guo] [ren] [a] [b] [c]]
22+
```
23+
24+
325
## 0.3.0 (2015-12-29)
426

527
* fix "当字符串中有非中文的时候,会出现下标越界的情况"(影响 `pinyin.LazyPinyin``pinyin.Slug` ([#1](https://github.com/mozillazg/go-pinyin/issues/1)))
628
* 调整对非中文字符的处理:当遇到没有拼音的字符时,直接忽略
7-
```
29+
```go
830
// before
931
fmt.Println(pinyin.Pinyin("中国人abc", pinyin.NewArgs()))
1032
[[zhong] [guo] [ren] [] [] []]

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 mozillazg
3+
Copyright (c) 2016 mozillazg
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

doc.go

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

example_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pinyin_test
22

33
import (
44
"fmt"
5+
56
"github.com/mozillazg/go-pinyin"
67
)
78

@@ -85,6 +86,36 @@ func ExamplePinyin_heteronym() {
8586
// Output: [[zho1ng zho4ng] [guo2] [re2n]]
8687
}
8788

89+
func ExamplePinyin_fallbackCustom1() {
90+
hans := "中国人abc"
91+
a := pinyin.NewArgs()
92+
a.Fallback = func(r rune, a pinyin.Args) []string {
93+
return []string{string(r + 1)}
94+
}
95+
fmt.Println(pinyin.Pinyin(hans, a))
96+
// Output: [[zhong] [guo] [ren] [b] [c] [d]]
97+
}
98+
99+
func ExamplePinyin_fallbackCustom2() {
100+
hans := "中国人アイウ"
101+
a := pinyin.NewArgs()
102+
a.Fallback = func(r rune, a pinyin.Args) []string {
103+
data := map[rune][]string{
104+
'ア': {"a"},
105+
'イ': {"i"},
106+
'ウ': {"u"},
107+
}
108+
s, ok := data[r]
109+
if ok {
110+
return s
111+
} else {
112+
return []string{}
113+
}
114+
}
115+
fmt.Println(pinyin.Pinyin(hans, a))
116+
// Output: [[zhong] [guo] [ren] [a] [i] [u]]
117+
}
118+
88119
func ExampleLazyPinyin() {
89120
hans := "中国人"
90121
a := pinyin.NewArgs()

pinyin.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77

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

1616
// 拼音风格(推荐)
@@ -63,6 +63,10 @@ type Args struct {
6363
Style int // 拼音风格(默认: Normal)
6464
Heteronym bool // 是否启用多音字模式(默认:禁用)
6565
Separator string // Slug 中使用的分隔符(默认:-)
66+
67+
// 处理没有拼音的字符(默认忽略没有拼音的字符)
68+
// 函数返回的 slice 的长度为0 则表示忽略这个字符
69+
Fallback func(r rune, a Args) []string
6670
}
6771

6872
// 默认配置:风格
@@ -74,9 +78,14 @@ var Heteronym = false
7478
// 默认配置: `Slug` 中 Join 所用的分隔符
7579
var Separator = "-"
7680

81+
// 默认配置: 如何处理没有拼音的字符(忽略这个字符)
82+
var Fallback = func(r rune, a Args) []string {
83+
return []string{}
84+
}
85+
7786
// NewArgs 返回包含默认配置的 `Args`
7887
func NewArgs() Args {
79-
return Args{Style, Heteronym, Separator}
88+
return Args{Style, Heteronym, Separator, Fallback}
8089
}
8190

8291
// 获取单个拼音中的声母
@@ -117,7 +126,7 @@ func toFixed(p string, a Args) string {
117126
// 返回使用数字标识声调的字符
118127
m = symbol
119128
default:
120-
// // 声调在头上
129+
// 声调在头上
121130
}
122131
return m
123132
})
@@ -143,15 +152,24 @@ func applyStyle(p []string, a Args) []string {
143152

144153
// SinglePinyin 把单个 `rune` 类型的汉字转换为拼音.
145154
func SinglePinyin(r rune, a Args) []string {
155+
if a.Fallback == nil {
156+
a.Fallback = Fallback
157+
}
146158
value, ok := PinyinDict[int(r)]
147159
pys := []string{}
148160
if ok {
149161
pys = strings.Split(value, ",")
162+
} else {
163+
pys = a.Fallback(r, a)
164+
}
165+
if len(pys) > 0 {
150166
if !a.Heteronym {
151-
pys = strings.Split(value, ",")[:1]
167+
pys = pys[:1]
152168
}
169+
return applyStyle(pys, a)
170+
} else {
171+
return pys
153172
}
154-
return applyStyle(pys, a)
155173
}
156174

157175
// Pinyin 汉字转拼音,支持多音字模式.

0 commit comments

Comments
 (0)