Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters