Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Add tools.

* Improve words.

* Loader interface.

* Remove Pinyin dir.

* Remove Old class.

* Rename tools.

* Refactory.

* Segment words.

* Tests.

* Update README.

* Typo

* Fix tests.

* Update .scrutinizer.yml
  • Loading branch information
overtrue committed Apr 25, 2016
1 parent 934851f commit 298a12a
Show file tree
Hide file tree
Showing 31 changed files with 413,293 additions and 108,020 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
* text=auto

/tests export-ignore
/tools export-ignore
/vendor export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
Expand Down
33 changes: 24 additions & 9 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
<?php
/**
* PHP-CS-fixer configuration.
*/

$finder = Symfony\CS\Finder\DefaultFinder::create()
->in(__DIR__ . '/src/Pinyin/')
;
$year = date('Y');

$header = <<<EOF
This file is part of the overtrue/pinyin.
(c) $year overtrue <i@overtrue.me>
EOF;

Symfony\CS\Fixer\Contrib\HeaderCommentFixer::setHeader($header);

return Symfony\CS\Config\Config::create()
->fixers(array('-symfony'))
->finder($finder)
->setUsingCache(true)
// use default SYMFONY_LEVEL and extra fixers:
->fixers(array(
'header_comment',
'long_array_syntax',
'ordered_use',
'strict',
'strict_param',
'phpdoc_order',
'php4_constructor',
))
->finder(
Symfony\CS\Finder\DefaultFinder::create()
->exclude('vendor')
->in(__DIR__.'/src')
)
;
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build:
tests:
override:
-
command: phpunit --coverage-clover=my-coverage-file
command: ./vendor/bin/phpunit --coverage-clover=my-coverage-file
coverage:
file: my-coverage-file
format: php-clover
5 changes: 3 additions & 2 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ enabled:
- strict_param

disabled:
- short_array_syntax
- long_array_syntax
- phpdoc_short_description

excluded:
- src/data
- src/data
- tools
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ php:

before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev
- composer install --no-interaction --dev

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

script: phpunit ./ --coverage-clover=coverage.clover
script: ./vendor/bin/phpunit --coverage-clover=coverage.clover

matrix:
fast_finish: true
184 changes: 37 additions & 147 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,190 +6,80 @@ Pinyin
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/overtrue/pinyin/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/overtrue/pinyin/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/overtrue/pinyin/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/overtrue/pinyin/?branch=master)

基于 [CC-CEDICT](http://cc-cedict.org/wiki/) 词典的中文转拼音工具,更准确的汉字转拼音解决方案
基于 [CC-CEDICT](http://cc-cedict.org/wiki/) 词典的中文转拼音工具,更准确的支持多音字的汉字转拼音解决方案

```php
use Overtrue\Pinyin\Pinyin;

echo Pinyin::trans('带着希望去旅行,比到达终点更美好');
// dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo

//多音字
// 了
Pinyin::trans('了然'); // liǎo rán
Pinyin::trans('来了'); // lái le

// 还
Pinyin::trans('还有'); // hái yǒu
Pinyin::trans('交还'); // jiāo huán

// 什
Pinyin::trans('什么'); // shén me
Pinyin::trans('什锦'); // shí jǐn

// 便
Pinyin::trans('便当'); // biàn dāng
Pinyin::trans('便宜'); // pián yí

// 剥
Pinyin::trans('剥皮'); // bāo pí
Pinyin::trans('剥皮器'); // bō pí qì

// 不
Pinyin::trans('赔不是'); // péi bú shi
Pinyin::trans('跑了和尚,跑不了庙'); // pǎo le hé shàng , pǎo bù liǎo miào

// 降
Pinyin::trans('降温'); // jiàng wēn
Pinyin::trans('投降'); // tóu xiáng

// 都
Pinyin::trans('首都'); // shǒu dū
Pinyin::trans('都什么年代了'); // dōu shén me nián dài le

// 乐
Pinyin::trans('快乐'); // kuài lè
Pinyin::trans('音乐'); // yīn yuè

// 长
Pinyin::trans('成长'); // chéng zhǎng
Pinyin::trans('长江'); // cháng jiāng
## 安装

// 难
Pinyin::trans('难民'); // nàn mín
Pinyin::trans('难过'); // nán guò
...
使用 Composer 安装:

```


## 安装
1. 使用 Composer 安装:
```
composer require overtrue/pinyin:2.*
```
或者在你的项目 composer.json 加入:
```javascript
{
"require": {
"overtrue/pinyin": "2.*"
}
}
```

2. 直接下载文件 `src/Pinyin/Pinyin.php` 引入到项目中。

composer require "overtrue/pinyin:~3.0"
```

## 使用

### 拼音数组

```php
<?php
use Overtrue\Pinyin\Pinyin;

//获取拼音
echo Pinyin::trans('带着希望去旅行,比到达终点更美好');
// dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo

//获取首字母
echo Pinyin::letter('带着希望去旅行,比到达终点更美好');
// d z x w q l x b d d z d g m h

//当前也可以两个同时获取
echo Pinyin::parse('带着希望去旅行,比到达终点更美好');
// output:
// array(
// 'src' => '带着希望去旅行,比到达终点更美好',
// 'pinyin' => 'dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo',
// 'letter' => 'd z x w q l x b d d z d g m h',
// );

// 加载自定义补充词库
$appends = array(
'冷' => 're4',
);
Pinyin::appends($appends);
echo Pinyin::trans('冷');
// rè
```

$pinyin = new Pinyin();

### 设置
$pinyin->convert('带着希望去旅行,比到达终点更美好');
// ["dai", "zhe", "xi", "wang", "qu", "lu", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]

| 选项 | 描述 |
| ------------- | --------------------------------------------------- |
| `delimiter` | 分隔符,默认为一个空格 |
| `accent` | 是否输出音调 |
| `only_chinese` | 只保留 `$string` 中中文部分 |
| `uppercase` | 取首字母时的大写,默认 `false` |
| `charset` | 字符集,默认:`UTF-8` |
$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_UNICODE);
// ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]

$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCII);
//["dai4","zhe","xi1","wang4","qu4","lv3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]
```

*全局设置:* `Pinyin::set('delimiter', '-');`
选项:

*临时设置:* `Pinyin::trans($word, $settings)` 在调用的方法后传参
| 选项 | 描述 |
| ------------- | ---------------------------------------------------|
| `PINYIN_NONE` | 不带音调输出: `mei hao` |
| `PINYIN_ASCII` | 带数字式音调: `mei3 hao3` |
| `PINYIN_UNICODE` | UNICODE 式音调:`měi hǎo` |

example:
### 生成用于链接的拼音字符串

```php
$pinyin->permlink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lu-xing
$pinyin->permlink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lu.xing
```

Pinyin::set('delimiter', '-');//全局
echo Pinyin::trans('带着希望去旅行,比到达终点更美好');
### 获取首字符字符串

// dài-zhe-xī-wàng-qù-lǔ-xíng-bǐ-dào-dá-zhōng-diǎn-gèng-měi-hǎo
```
```php
$pinyin->abbr('带着希望去旅行'); // dzxwqlx
$pinyin->abbr('带着希望去旅行', '-'); // d-z-x-w-q-l-x
```

$setting = [
'delimiter' => '-',
'accent' => false,
];
### 翻译整段文字为拼音

echo Pinyin::trans('带着希望去旅行,比到达终点更美好', $setting);//这里的 setting 只是临时修改,并非全局设置

// dai-zhe-xi-wang-qu-lu-xing-bi-dao-da-zhong-dian-geng-mei-hao
```
将会保留中文字符:`,。 ! ? : “ ” ‘ ’` 并替换为对应的英文符号。

```php
Pinyin::set('accent', false);
echo Pinyin::trans('带着希望去旅行,比到达终点更美好');
$pinyin->sentence('带着希望去旅行,比到达终点更美好!');
// dai zhe xi wang qu lv xing, bi dao da zhong dian geng mei hao!

// dai zhe xi wang qu lu xing bi dao da zhong dian geng mei hao
$pinyin->sentence('带着希望去旅行,比到达终点更美好!', true);
// dài zhe xī wàng qù lǚ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo!
```

## 在 Laravel 中使用

独立的包在这里:[overtrue/laravel-pinyin](https://github.com/overtrue/laravel-pinyin)


### 使用

与上面的使用方法一样:

```php
use Overtrue\Pinyin\Pinyin;

//...

$pinyin = Pinyin::trans("带着希望去旅行,比到达终点更美好");

```

## TODO
- [x] <del>添加获取首字母;</del>
- [x] <del>添加补充词典;</del>
- [x] <del>添加音频表,根据音频提高未匹配词典时多音字准确度;</del>
- [x] <del>添加首字母输出大小写选项 `uppercase`;</del>
- [x] <del>支持载入自定义词库:`Pinyin::appends($appends = array())`;</del>
- [x] <del>支持 Laravel 5 的 service provider。[overtrue/laravel-pinyin](https://github.com/overtrue/laravel-pinyin)</del>

## Contribution
欢迎提意见及完善补充词库 `src/data/dict.php` :kiss:
欢迎提意见及完善补充词库 [`tools/patches/`](https://github.com/overtrue/pinyin/tree/v2/tools/patches) :kiss:

## 参考

- [CC-CEDICT](http://cc-cedict.org/wiki/)
- [現代漢語語音語料庫](http://mmc.sinica.edu.tw/intro_c_01.html)
- [汉典](http://www.zdic.net/)
- [详细参考资料](https://github.com/overtrue/pinyin-resources)

# License

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
],
"autoload": {
"psr-4": {
"Overtrue\\": "src/"
"Overtrue\\Pinyin\\": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "4.8.*"
"phpunit/phpunit": "~4.8"
}
}
Loading

0 comments on commit 298a12a

Please sign in to comment.