Skip to content

Commit

Permalink
Merge pull request #4 from mozillazg/develop
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
mozillazg committed Jan 29, 2016
2 parents b7fbbf0 + e6080b6 commit 0fee16e
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 80 deletions.
24 changes: 23 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
# Changelog


## 0.4.0 (2016-01-29)

* **NEW** `Args` 结构体新增 field: `Fallback func(r rune, a Args) []string`
用于处理没有拼音的字符(默认忽略没有拼音的字符):
```go
a := pinyin.NewArgs()
a.Fallback = func(r rune, a pinyin.Args) []string {
return []string{string(r + 1)}
}
fmt.Println(pinyin.Pinyin("中国人abc", a))
// Output: [[zhong] [guo] [ren] [b] [c] [d]]

// or
pinyin.Fallback = func(r rune, a pinyin.Args) []string {
return []string{string(r)}
}
fmt.Println(pinyin.Pinyin("中国人abc", pinyin.NewArgs()))
// Output: [[zhong] [guo] [ren] [a] [b] [c]]
```


## 0.3.0 (2015-12-29)

* fix "当字符串中有非中文的时候,会出现下标越界的情况"(影响 `pinyin.LazyPinyin``pinyin.Slug` ([#1](https://github.com/mozillazg/go-pinyin/issues/1)))
* 调整对非中文字符的处理:当遇到没有拼音的字符时,直接忽略
```
```go
// before
fmt.Println(pinyin.Pinyin("中国人abc", pinyin.NewArgs()))
[[zhong] [guo] [ren] [] [] []]
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 mozillazg
Copyright (c) 2016 mozillazg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 33 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
// Package pinyin : 汉语拼音转换工具
//
// Usage
//
// package main
//
// import (
// "fmt"
// "github.com/mozillazg/go-pinyin"
// )
//
// func main() {
// hans := "中国人"
// a := pinyin.NewArgs()
// // 默认输出 [[zhong] [guo] [ren]]
// fmt.Println(pinyin.Pinyin(hans, a))
//
// // 包含声调 [[zhōng] [guó] [rén]]
// a.Style = pinyin.Tone
// fmt.Println(pinyin.Pinyin(hans, a))
//
// // 声调用数字表示 [[zho1ng] [guo2] [re2n]]
// a.Style = pinyin.Tone2
// fmt.Println(pinyin.Pinyin(hans, a))
//
// // 开启多音字模式 [[zhong zhong] [guo] [ren]]
// a = pinyin.NewArgs()
// a.Heteronym = true
// fmt.Println(pinyin.Pinyin(hans, a))
// // [[zho1ng zho4ng] [guo2] [re2n]]
// a.Style = pinyin.Tone2
// fmt.Println(pinyin.Pinyin(hans, a))
// }
//
package pinyin
31 changes: 31 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pinyin_test

import (
"fmt"

"github.com/mozillazg/go-pinyin"
)

Expand Down Expand Up @@ -85,6 +86,36 @@ func ExamplePinyin_heteronym() {
// Output: [[zho1ng zho4ng] [guo2] [re2n]]
}

func ExamplePinyin_fallbackCustom1() {
hans := "中国人abc"
a := pinyin.NewArgs()
a.Fallback = func(r rune, a pinyin.Args) []string {
return []string{string(r + 1)}
}
fmt.Println(pinyin.Pinyin(hans, a))
// Output: [[zhong] [guo] [ren] [b] [c] [d]]
}

func ExamplePinyin_fallbackCustom2() {
hans := "中国人アイウ"
a := pinyin.NewArgs()
a.Fallback = func(r rune, a pinyin.Args) []string {
data := map[rune][]string{
'ア': {"a"},
'イ': {"i"},
'ウ': {"u"},
}
s, ok := data[r]
if ok {
return s
} else {
return []string{}
}
}
fmt.Println(pinyin.Pinyin(hans, a))
// Output: [[zhong] [guo] [ren] [a] [i] [u]]
}

func ExampleLazyPinyin() {
hans := "中国人"
a := pinyin.NewArgs()
Expand Down
30 changes: 24 additions & 6 deletions pinyin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

// Meta
const (
Version = "0.3.0"
Version = "0.4.0"
Author = "mozillazg, 闲耘"
License = "MIT"
Copyright = "Copyright (c) 2014 mozillazg, 闲耘"
Copyright = "Copyright (c) 2016 mozillazg, 闲耘"
)

// 拼音风格(推荐)
Expand Down Expand Up @@ -63,6 +63,10 @@ type Args struct {
Style int // 拼音风格(默认: Normal)
Heteronym bool // 是否启用多音字模式(默认:禁用)
Separator string // Slug 中使用的分隔符(默认:-)

// 处理没有拼音的字符(默认忽略没有拼音的字符)
// 函数返回的 slice 的长度为0 则表示忽略这个字符
Fallback func(r rune, a Args) []string
}

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

// 默认配置: 如何处理没有拼音的字符(忽略这个字符)
var Fallback = func(r rune, a Args) []string {
return []string{}
}

// NewArgs 返回包含默认配置的 `Args`
func NewArgs() Args {
return Args{Style, Heteronym, Separator}
return Args{Style, Heteronym, Separator, Fallback}
}

// 获取单个拼音中的声母
Expand Down Expand Up @@ -117,7 +126,7 @@ func toFixed(p string, a Args) string {
// 返回使用数字标识声调的字符
m = symbol
default:
// // 声调在头上
// 声调在头上
}
return m
})
Expand All @@ -143,15 +152,24 @@ func applyStyle(p []string, a Args) []string {

// SinglePinyin 把单个 `rune` 类型的汉字转换为拼音.
func SinglePinyin(r rune, a Args) []string {
if a.Fallback == nil {
a.Fallback = Fallback
}
value, ok := PinyinDict[int(r)]
pys := []string{}
if ok {
pys = strings.Split(value, ",")
} else {
pys = a.Fallback(r, a)
}
if len(pys) > 0 {
if !a.Heteronym {
pys = strings.Split(value, ",")[:1]
pys = pys[:1]
}
return applyStyle(pys, a)
} else {
return pys
}
return applyStyle(pys, a)
}

// Pinyin 汉字转拼音,支持多音字模式.
Expand Down
Loading

0 comments on commit 0fee16e

Please sign in to comment.