Skip to content

Commit

Permalink
Cleanup. #68
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Nov 17, 2016
1 parent 0b0f679 commit ed286cc
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 66 deletions.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -19,12 +19,19 @@ composer require "overtrue/pinyin:~3.0"

## 使用

可选两种转换方案:

- 内存型,适用于服务器内存空间较富余,优点:转换快
- 小内存型(默认),适用于内存比较紧张的环境,优点:占用内存小,转换不如内存型快

### 拼音数组

```php
use Overtrue\Pinyin\Pinyin;

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

$pinyin->convert('带着希望去旅行,比到达终点更美好');
// ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]
Expand Down
53 changes: 0 additions & 53 deletions src/FileToMemoryDictLoader.php

This file was deleted.

86 changes: 86 additions & 0 deletions src/MemoryFileDictLoader.php
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the overtrue/pinyin.
*
* (c) 2016 overtrue <i@overtrue.me>
*/

namespace Overtrue\Pinyin;

use Closure;

/**
* Memory Dict File loader.
*/
class MemoryFileDictLoader implements DictLoaderInterface
{
/**
* Words segment name.
*
* @var string
*/
protected $segmentName = 'words_%s';

/**
* Segment files.
*
* @var array
*/
protected $segments = array();

/**
* Surname cache.
*
* @var array
*/
protected $surnames = array();

/**
* Constructor.
*
* @param string $path
*/
public function __construct($path)
{
$this->path = $path;

for ($i = 0; $i < 100; ++$i) {
$segment = $path.'/'.sprintf($this->segmentName, $i);

if (file_exists($segment)) {
$this->segments[] = (array) include $segment;
}
}
}

/**
* Load dict.
*
* @param Closure $callback
*/
public function map(Closure $callback)
{
foreach ($this->segments as $dictionary) {
$callback($dictionary);
}
}

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

if (file_exists($surnames)) {
$this->surnames = (array) include $surnames;
}
}

$callback($this->surnames);
}
}
16 changes: 6 additions & 10 deletions src/Pinyin.php
Expand Up @@ -56,11 +56,11 @@ class Pinyin
/**
* Constructor.
*
* @param mixed $loader
* @param string $loaderName
*/
public function __construct($loader = null)
public function __construct($loaderName = null)
{
$this->loader = $loader;
$this->loader = $loaderName ?: 'Overtrue\\Pinyin\\FileDictLoader';
}

/**
Expand Down Expand Up @@ -168,15 +168,11 @@ public function setLoader(DictLoaderInterface $loader)
*/
public function getLoader()
{

if (!($this->loader instanceof DictLoaderInterface)) {
$dataDir = dirname(__DIR__).'/data/';
if(is_string($this->loader)) {
$loaderName = $this->loader;
$this->loader = new $loaderName($dataDir);
} else {
$this->loader = new FileDictLoader($dataDir);
}

$loaderName = $this->loader;
$this->loader = new $loaderName($dataDir);
}

return $this->loader;
Expand Down
4 changes: 2 additions & 2 deletions tests/FileToMemoryDictLoaderTest.php
Expand Up @@ -6,13 +6,13 @@
* @author Garveen <acabin@live.com>
*/

use Overtrue\Pinyin\FileToMemoryDictLoader;
use Overtrue\Pinyin\MemoryFileDictLoader;
use Overtrue\Pinyin\Pinyin;

class FileToMemoryDictLoaderTest extends AbstractDictLoaderTest
{
protected function setUp()
{
$this->pinyin = new Pinyin('Overtrue\Pinyin\FileToMemoryDictLoader');
$this->pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
}
}

0 comments on commit ed286cc

Please sign in to comment.