|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/pinyin. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2016 Seven Du <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Overtrue\Pinyin; |
|
10
|
|
|
|
|
11
|
|
|
use Closure; |
|
12
|
|
|
use Exception; |
|
13
|
|
|
use SplFileObject; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Generator syntax(yield) Dict File loader. |
|
17
|
|
|
*/ |
|
18
|
|
|
class GeneratorFileDictLoader implements DictLoaderInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Data directory. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $path; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Words segment name. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $segmentName = 'words_%s'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* SplFileObjects. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $handles = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $path |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($path) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->path = $path; |
|
49
|
|
|
|
|
50
|
|
|
for ($i = 0; $i < 100; ++$i) { |
|
51
|
|
|
$segment = $this->path.'/'.sprintf($this->segmentName, $i); |
|
52
|
|
|
|
|
53
|
|
|
if (file_exists($segment) && is_file($segment)) { |
|
54
|
|
|
array_push($this->handles, $this->openFile($segment)); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Construct a new file object. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $filename file path |
|
63
|
|
|
* @return SplFileObject |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function openFile($filename, $mode = 'r') |
|
66
|
|
|
{ |
|
67
|
|
|
return new SplFileObject($filename, $mode); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* get Generator syntax. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $handles SplFileObjects |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getGenerator(array $handles) |
|
76
|
|
|
{ |
|
77
|
|
|
foreach ($handles as $handle) { |
|
78
|
|
|
$handle->seek(0); |
|
79
|
|
|
while ($handle->eof() === false) { |
|
80
|
|
|
$string = str_replace(['\'', ' ', PHP_EOL, ','], '', $handle->fgets()); |
|
81
|
|
|
|
|
82
|
|
|
if (strpos($string, '=>') === false) { |
|
83
|
|
|
continue; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
list($string, $pinyin) = explode('=>', $string); |
|
87
|
|
|
|
|
88
|
|
|
yield $string => $pinyin; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Load dict. |
|
95
|
|
|
* |
|
96
|
|
|
* @param Closure $callback |
|
97
|
|
|
*/ |
|
98
|
|
|
public function map(Closure $callback) |
|
99
|
|
|
{ |
|
100
|
|
|
foreach ($this->getGenerator($this->handles) as $string => $pinyin) { |
|
101
|
|
|
$callback([$string => $pinyin]); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Load surname dict. |
|
107
|
|
|
* |
|
108
|
|
|
* @param Closure $callback |
|
109
|
|
|
*/ |
|
110
|
|
|
public function mapSurname(Closure $callback) |
|
111
|
|
|
{ |
|
112
|
|
|
$surnames = $this->path.'/surnames'; |
|
113
|
|
|
|
|
114
|
|
|
foreach ($this->getGenerator([ $this->openFile($surnames) ]) as $string => $pinyin) { |
|
115
|
|
|
$callback([$string => $pinyin]); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|