|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the mucts/pinyin. |
|
4
|
|
|
* |
|
5
|
|
|
* This source file is subject to the MIT license that is bundled |
|
6
|
|
|
* with this source code in the file LICENSE. |
|
7
|
|
|
* |
|
8
|
|
|
* @version 1.0 |
|
9
|
|
|
* @author herry<[email protected]> |
|
10
|
|
|
* @copyright © 2020 MuCTS.com All Rights Reserved. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace MuCTS\Pinyin\Loaders; |
|
14
|
|
|
|
|
15
|
|
|
use Closure; |
|
16
|
|
|
use Generator; |
|
17
|
|
|
use MuCTS\Pinyin\Exceptions\PinyinException; |
|
18
|
|
|
use MuCTS\Pinyin\Interfaces\DictLoader; |
|
19
|
|
|
use SplFileObject; |
|
20
|
|
|
|
|
21
|
|
|
class GeneratorFile implements DictLoader |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Words segment name. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected string $segmentName = 'words_*'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* SplFileObjects. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected static array $handles = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* surnames. |
|
39
|
|
|
* |
|
40
|
|
|
* @var mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
protected static $surnamesHandle; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $path |
|
48
|
|
|
* @throws PinyinException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($path) |
|
51
|
|
|
{ |
|
52
|
|
|
$segments = glob($path . '/' . $this->segmentName); |
|
53
|
|
|
if ($segments == false) { |
|
54
|
|
|
throw new PinyinException('CC-CEDICT dictionary data does not exist'); |
|
55
|
|
|
} |
|
56
|
|
|
while (($segment = array_shift($segments))) { |
|
57
|
|
|
array_push(static::$handles, $this->openFile($segment)); |
|
58
|
|
|
} |
|
59
|
|
|
static::$surnamesHandle = $this->openFile($path . '/surnames'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Construct a new file object. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $filename file path |
|
66
|
|
|
* @param string $mode file open mode |
|
67
|
|
|
* |
|
68
|
|
|
* @return SplFileObject |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function openFile($filename, $mode = 'r') |
|
71
|
|
|
{ |
|
72
|
|
|
return new SplFileObject($filename, $mode); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* get Generator syntax. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $handles SplFileObjects |
|
79
|
|
|
* |
|
80
|
|
|
* @return Generator |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getGenerator(array $handles) |
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($handles as $handle) { |
|
85
|
|
|
$handle->seek(0); |
|
86
|
|
|
while (false === $handle->eof()) { |
|
87
|
|
|
$string = str_replace(['\'', ' ', PHP_EOL, ','], '', $handle->fgets()); |
|
88
|
|
|
|
|
89
|
|
|
if (false === strpos($string, '=>')) { |
|
90
|
|
|
continue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
list($string, $pinyin) = explode('=>', $string); |
|
94
|
|
|
|
|
95
|
|
|
yield $string => $pinyin; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Traverse the stream. |
|
102
|
|
|
* |
|
103
|
|
|
* @param Generator $generator |
|
104
|
|
|
* @param Closure $callback |
|
105
|
|
|
* |
|
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
|
|
|
$this->traversing($this->getGenerator([static::$surnamesHandle]), $callback); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|