Skip to content

Commit

Permalink
更新生成器类型加载器,减少重复代码,优化代码结构,更新文档。 (overtrue#78)
Browse files Browse the repository at this point in the history
* 更新生成器类型加载器,减少重复代码,优化代码结构,更新文档。

* 删除没必要存在的变量~
  • Loading branch information
medz authored and overtrue committed Jan 12, 2017
1 parent f886daa commit c463dd1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ composer require "overtrue/pinyin:~3.0"

## 使用

可选两种转换方案
可选转换方案

- 内存型,适用于服务器内存空间较富余,优点:转换快
- 小内存型(默认),适用于内存比较紧张的环境,优点:占用内存小,转换不如内存型快
- I/O型,适用于虚拟机,内存限制比较严格环境。优点:非常微小内存消耗。缺点:转换慢,不如内存型转换快,php >= 5.5

### 拼音数组

```php
use Overtrue\Pinyin\Pinyin;

$pinyin = new Pinyin(); // 小内存型(默认)
// 或者
$pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader'); // 内存型
// 小内存型
$pinyin = new Pinyin(); // 默认
// 内存型
// $pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
// I/O型
// $pinyin = new Pinyin('Overtrue\Pinyin\GeneratorFileDictLoader');

$pinyin->convert('带着希望去旅行,比到达终点更美好');
// ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]
Expand All @@ -43,6 +47,11 @@ $pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCI
//["dai4","zhe","xi1","wang4","qu4","lv3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]
```

- 小内存型: 将字典分片载入内存
- 内存型: 将所有字典预先载入内存
- I/O型: 不载入内存,将字典使用文件流打开逐行便利并运用php5.5生成器(yield)特性分配单行内存


选项:

| 选项 | 描述 |
Expand Down
38 changes: 29 additions & 9 deletions src/GeneratorFileDictLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Closure;
use Exception;
use SplFileObject;
use Generator;

/**
* Generator syntax(yield) Dict File loader.
Expand All @@ -36,7 +37,14 @@ class GeneratorFileDictLoader implements DictLoaderInterface
*
* @var array
*/
protected $handles = [];
protected static $handles = [];

/**
* surnames.
*
* @var SplFileObject
*/
protected static $surnamesHandle;

/**
* Constructor.
Expand All @@ -51,7 +59,7 @@ public function __construct($path)
$segment = $this->path.'/'.sprintf($this->segmentName, $i);

if (file_exists($segment) && is_file($segment)) {
array_push($this->handles, $this->openFile($segment));
array_push(static::$handles, $this->openFile($segment));
}
}
}
Expand Down Expand Up @@ -91,28 +99,40 @@ protected function getGenerator(array $handles)
}

/**
* Load dict.
* Traverse the stream.
*
* @param Generator $generator
* @param Closure $callback
* @author Seven Du <[email protected]>
*/
public function map(Closure $callback)
protected function traversing(Generator $generator, Closure $callback)
{
foreach ($this->getGenerator($this->handles) as $string => $pinyin) {
foreach ($generator as $string => $pinyin) {
$callback([$string => $pinyin]);
}
}

/**
* Load dict.
*
* @param Closure $callback
*/
public function map(Closure $callback)
{
$this->traversing($this->getGenerator(static::$handles), $callback);
}

/**
* Load surname dict.
*
* @param Closure $callback
*/
public function mapSurname(Closure $callback)
{
$surnames = $this->path.'/surnames';

foreach ($this->getGenerator([ $this->openFile($surnames) ]) as $string => $pinyin) {
$callback([$string => $pinyin]);
if (!static::$surnamesHandle instanceof SplFileObject) {
static::$surnamesHandle = $this->openFile($this->path.'/surnames');
}

$this->traversing($this->getGenerator([static::$surnamesHandle]), $callback);
}
}

0 comments on commit c463dd1

Please sign in to comment.