Skip to content

Commit ed286cc

Browse files
committed
Cleanup. overtrue#68
1 parent 0b0f679 commit ed286cc

File tree

5 files changed

+102
-66
lines changed

5 files changed

+102
-66
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ composer require "overtrue/pinyin:~3.0"
1919

2020
## 使用
2121

22+
可选两种转换方案:
23+
24+
- 内存型,适用于服务器内存空间较富余,优点:转换快
25+
- 小内存型(默认),适用于内存比较紧张的环境,优点:占用内存小,转换不如内存型快
26+
2227
### 拼音数组
2328

2429
```php
2530
use Overtrue\Pinyin\Pinyin;
2631

27-
$pinyin = new Pinyin();
32+
$pinyin = new Pinyin(); // 小内存型(默认)
33+
// 或者
34+
$pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader'); // 内存型
2835

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

src/FileToMemoryDictLoader.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/MemoryFileDictLoader.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/pinyin.
5+
*
6+
* (c) 2016 overtrue <i@overtrue.me>
7+
*/
8+
9+
namespace Overtrue\Pinyin;
10+
11+
use Closure;
12+
13+
/**
14+
* Memory Dict File loader.
15+
*/
16+
class MemoryFileDictLoader implements DictLoaderInterface
17+
{
18+
/**
19+
* Words segment name.
20+
*
21+
* @var string
22+
*/
23+
protected $segmentName = 'words_%s';
24+
25+
/**
26+
* Segment files.
27+
*
28+
* @var array
29+
*/
30+
protected $segments = array();
31+
32+
/**
33+
* Surname cache.
34+
*
35+
* @var array
36+
*/
37+
protected $surnames = array();
38+
39+
/**
40+
* Constructor.
41+
*
42+
* @param string $path
43+
*/
44+
public function __construct($path)
45+
{
46+
$this->path = $path;
47+
48+
for ($i = 0; $i < 100; ++$i) {
49+
$segment = $path.'/'.sprintf($this->segmentName, $i);
50+
51+
if (file_exists($segment)) {
52+
$this->segments[] = (array) include $segment;
53+
}
54+
}
55+
}
56+
57+
/**
58+
* Load dict.
59+
*
60+
* @param Closure $callback
61+
*/
62+
public function map(Closure $callback)
63+
{
64+
foreach ($this->segments as $dictionary) {
65+
$callback($dictionary);
66+
}
67+
}
68+
69+
/**
70+
* Load surname dict.
71+
*
72+
* @param Closure $callback
73+
*/
74+
public function mapSurname(Closure $callback)
75+
{
76+
if (empty($this->surnames)) {
77+
$surnames = $this->path.'/surnames';
78+
79+
if (file_exists($surnames)) {
80+
$this->surnames = (array) include $surnames;
81+
}
82+
}
83+
84+
$callback($this->surnames);
85+
}
86+
}

src/Pinyin.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class Pinyin
5656
/**
5757
* Constructor.
5858
*
59-
* @param mixed $loader
59+
* @param string $loaderName
6060
*/
61-
public function __construct($loader = null)
61+
public function __construct($loaderName = null)
6262
{
63-
$this->loader = $loader;
63+
$this->loader = $loaderName ?: 'Overtrue\\Pinyin\\FileDictLoader';
6464
}
6565

6666
/**
@@ -168,15 +168,11 @@ public function setLoader(DictLoaderInterface $loader)
168168
*/
169169
public function getLoader()
170170
{
171-
172171
if (!($this->loader instanceof DictLoaderInterface)) {
173172
$dataDir = dirname(__DIR__).'/data/';
174-
if(is_string($this->loader)) {
175-
$loaderName = $this->loader;
176-
$this->loader = new $loaderName($dataDir);
177-
} else {
178-
$this->loader = new FileDictLoader($dataDir);
179-
}
173+
174+
$loaderName = $this->loader;
175+
$this->loader = new $loaderName($dataDir);
180176
}
181177

182178
return $this->loader;

tests/FileToMemoryDictLoaderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* @author Garveen <acabin@live.com>
77
*/
88

9-
use Overtrue\Pinyin\FileToMemoryDictLoader;
9+
use Overtrue\Pinyin\MemoryFileDictLoader;
1010
use Overtrue\Pinyin\Pinyin;
1111

1212
class FileToMemoryDictLoaderTest extends AbstractDictLoaderTest
1313
{
1414
protected function setUp()
1515
{
16-
$this->pinyin = new Pinyin('Overtrue\Pinyin\FileToMemoryDictLoader');
16+
$this->pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
1717
}
1818
}

0 commit comments

Comments
 (0)