Skip to content

Commit f7e5b20

Browse files
committed
更新词库、优化代码
1 parent 0d80d25 commit f7e5b20

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ PHP Chinese Tool class, support Chinese pinyin, pinyin participle, simplified an
77

88
由于中文的博大精深,字有多音字,简体字和繁体字也有多种对应。并且本类库返回的所有结果,均为包含所有组合的数组。
99

10-
本类库字典数据总共收录 41578 个汉字,包括:3919 个简体字,1734 个繁体字,35925 个其它汉字。
10+
本类库字典数据总共收录 41852 个汉字,包括:3955 个简体字,1761 个繁体字,36136 个其它汉字。
1111

1212
加载后会占用 16 MB 内存,在访问量大的接口要使用此类汉字转拼音、繁简转换功能时,推荐用 Swoole 开发一个异步服务程序,只需加载一次数据,就可以持续高效地为你提供服务。
1313

@@ -216,11 +216,11 @@ array(2) {
216216
```php
217217
use \Yurun\Util\Chinese;
218218
$string2 = 'xianggang';
219-
echo '"', $string2, '"的分词结果', PHP_EOL;
219+
echo '"', $string2, '"的分词结果:', PHP_EOL;
220220
var_dump(Chinese::splitPinyin($string2));
221221
/**
222-
输出结果
223-
"xianggang"的分词结果
222+
输出结果:
223+
"xianggang"的分词结果:
224224
array(2) {
225225
[0]=>
226226
string(12) "xi ang gang "
@@ -235,18 +235,18 @@ array(2) {
235235
```php
236236
use \Yurun\Util\Chinese;
237237
$string3 = '中华人民共和国!恭喜發財!';
238-
echo '"', $string3, '"的简体转换', PHP_EOL;
238+
echo '"', $string3, '"的简体转换:', PHP_EOL;
239239
var_dump(Chinese::toSimplified($string3));
240-
echo '"', $string3, '"的繁体转换', PHP_EOL;
240+
echo '"', $string3, '"的繁体转换:', PHP_EOL;
241241
var_dump(Chinese::toTraditional($string3));
242242
/**
243-
输出结果
244-
"中华人民共和国!恭喜發財!"的简体转换
243+
输出结果:
244+
"中华人民共和国!恭喜發財!"的简体转换:
245245
array(1) {
246246
[0]=>
247247
string(39) "中华人民共和国!恭喜发财!"
248248
}
249-
"中华人民共和国!恭喜發財!"的繁体转换
249+
"中华人民共和国!恭喜發財!"的繁体转换:
250250
array(1) {
251251
[0]=>
252252
string(39) "中華人民共和國!恭喜發財!"

data/chineseData.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/Chinese/Pinyin.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public static function parseResult($list, $mode, $wordSplit)
8181
for($i = 0; $i < $oldResultCount; ++$i)
8282
{
8383
$j = $index * $oldResultCount + $i;
84-
// $pinyinSounds[$j] .= $pinyin . $wordSplit;
8584
$pinyinSounds[$j][] = $pinyin;
8685
}
8786
}
@@ -180,11 +179,15 @@ public static function getResult($string)
180179

181180
protected static function parseSoundItem($array, $mode)
182181
{
183-
static $pattern;
182+
static $pattern, $splitSeparator;
184183
if(null === $pattern)
185184
{
186185
$pattern = '/([' . implode('', array_keys(Chinese::$chineseData['pinyin']['sound'])) . '])/u';
187186
}
187+
if(null === $splitSeparator)
188+
{
189+
$splitSeparator = '/' . uniqid('', true) . '/';
190+
}
188191
$isPinyin = (($mode & static::CONVERT_MODE_PINYIN) === static::CONVERT_MODE_PINYIN);
189192
$isPinyinSoundNumber = (($mode & static::CONVERT_MODE_PINYIN_SOUND_NUMBER) === static::CONVERT_MODE_PINYIN_SOUND_NUMBER);
190193
$isPinyinFirst = (($mode & static::CONVERT_MODE_PINYIN_FIRST) === static::CONVERT_MODE_PINYIN_FIRST);
@@ -204,20 +207,19 @@ protected static function parseSoundItem($array, $mode)
204207
$result['pinyinFirst'] = [];
205208
}
206209

210+
if($isPinyin)
211+
{
212+
$result['pinyin'] = explode($splitSeparator, preg_replace_callback(
213+
$pattern,
214+
function ($matches){
215+
return Chinese::$chineseData['pinyin']['sound'][$matches[0]]['ab'];
216+
},
217+
implode($splitSeparator, $array)
218+
));
219+
}
220+
207221
foreach($array as $pinyinSoundItem)
208222
{
209-
if($isPinyin)
210-
{
211-
$pinyin = preg_replace_callback(
212-
$pattern,
213-
function ($matches){
214-
return Chinese::$chineseData['pinyin']['sound'][$matches[0]]['ab'];
215-
},
216-
$pinyinSoundItem,
217-
1
218-
);
219-
$result['pinyin'][] = $pinyin;
220-
}
221223
if($isPinyinSoundNumber)
222224
{
223225
$tone = null;

src/Chinese/PinyinSplit.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ private function parseItem($text)
8383

8484
private function extractPinyins($lastStr = '', $lastStrSpace = '', $index = 0)
8585
{
86-
// exit;
8786
$result = '';
8887
$stackLength = count($this->stacks);
8988
$bigHasResult = false;

0 commit comments

Comments
 (0)