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
|
|
|
use Generator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Generator syntax(yield) Dict File loader. |
18
|
|
|
*/ |
19
|
|
|
class GeneratorFileDictLoader implements DictLoaderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Data directory. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $path; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Words segment name. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $segmentName = 'words_%s'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* SplFileObjects. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected static $handles = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* surnames. |
44
|
|
|
* |
45
|
|
|
* @var SplFileObject |
46
|
|
|
*/ |
47
|
|
|
protected static $surnamesHandle; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor. |
51
|
|
|
* |
52
|
|
|
* @param string $path |
53
|
|
|
*/ |
54
|
|
|
public function __construct($path) |
55
|
|
|
{ |
56
|
|
|
$this->path = $path; |
57
|
|
|
|
58
|
|
|
for ($i = 0; $i < 100; ++$i) { |
59
|
|
|
$segment = $this->path.'/'.sprintf($this->segmentName, $i); |
60
|
|
|
|
61
|
|
|
if (file_exists($segment) && is_file($segment)) { |
62
|
|
|
array_push(static::$handles, $this->openFile($segment)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Construct a new file object. |
69
|
|
|
* |
70
|
|
|
* @param string $filename file path |
71
|
|
|
* @return SplFileObject |
72
|
|
|
*/ |
73
|
|
|
protected function openFile($filename, $mode = 'r') |
74
|
|
|
{ |
75
|
|
|
return new SplFileObject($filename, $mode); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* get Generator syntax. |
80
|
|
|
* |
81
|
|
|
* @param array $handles SplFileObjects |
82
|
|
|
*/ |
83
|
|
|
protected function getGenerator(array $handles) |
84
|
|
|
{ |
85
|
|
|
foreach ($handles as $handle) { |
86
|
|
|
$handle->seek(0); |
87
|
|
|
while ($handle->eof() === false) { |
88
|
|
|
$string = str_replace(['\'', ' ', PHP_EOL, ','], '', $handle->fgets()); |
89
|
|
|
|
90
|
|
|
if (strpos($string, '=>') === false) { |
91
|
|
|
continue; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
list($string, $pinyin) = explode('=>', $string); |
95
|
|
|
|
96
|
|
|
yield $string => $pinyin; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Traverse the stream. |
103
|
|
|
* |
104
|
|
|
* @param Generator $generator |
105
|
|
|
* @param Closure $callback |
106
|
|
|
* @author Seven Du <[email protected]> |
107
|
|
|
*/ |
108
|
|
|
protected function traversing(Generator $generator, Closure $callback) |
109
|
|
|
{ |
110
|
|
|
foreach ($generator as $string => $pinyin) { |
111
|
|
|
$callback([$string => $pinyin]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Load dict. |
117
|
|
|
* |
118
|
|
|
* @param Closure $callback |
119
|
|
|
*/ |
120
|
|
|
public function map(Closure $callback) |
121
|
|
|
{ |
122
|
|
|
$this->traversing($this->getGenerator(static::$handles), $callback); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Load surname dict. |
127
|
|
|
* |
128
|
|
|
* @param Closure $callback |
129
|
|
|
*/ |
130
|
|
|
public function mapSurname(Closure $callback) |
131
|
|
|
{ |
132
|
|
|
if (!static::$surnamesHandle instanceof SplFileObject) { |
133
|
|
|
$surnameFilename = $this->path.'/surnames'; |
134
|
|
|
static::$surnamesHandle = $this->openFile($surnameFilename); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->traversing($this->getGenerator([static::$surnamesHandle]), $callback); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|