1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/pinyin. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overtrue\Pinyin; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/* |
17
|
|
|
* Chinese to pinyin translator. |
18
|
|
|
* |
19
|
|
|
* @author overtrue <[email protected]> |
20
|
|
|
* @copyright 2015 overtrue <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @link https://github.com/overtrue/pinyin |
23
|
1 |
|
* @link http://overtrue.me |
24
|
1 |
|
*/ |
25
|
1 |
|
|
26
|
|
|
define('PINYIN_NONE', 'none'); |
27
|
|
|
define('PINYIN_ASCII', 'ascii'); |
28
|
|
|
define('PINYIN_UNICODE', 'unicode'); |
29
|
|
|
|
30
|
|
|
class Pinyin |
31
|
|
|
{ |
32
|
|
|
const NONE = 'none'; |
33
|
|
|
|
34
|
|
|
const ASCII = 'ascii'; |
35
|
|
|
|
36
|
|
|
const UNICODE = 'unicode'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Dict loader. |
40
|
|
|
* |
41
|
|
|
* @var \Overtrue\Pinyin\DictLoaderInterface |
42
|
|
|
*/ |
43
|
|
|
protected $loader; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Punctuations map. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $punctuations = array( |
51
|
|
|
',' => ',', |
52
|
|
|
'。' => '.', |
53
|
|
|
'!' => '!', |
54
|
|
|
'?' => '?', |
55
|
|
|
':' => ':', |
56
|
|
|
'“' => '"', |
57
|
|
|
'”' => '"', |
58
|
|
|
'‘' => "'", |
59
|
|
|
'’' => "'", |
60
|
|
|
); |
61
|
|
|
|
62
|
21 |
|
/** |
63
|
|
|
* Constructor. |
64
|
21 |
|
* |
65
|
21 |
|
* @param string $loaderName |
66
|
|
|
*/ |
67
|
|
|
public function __construct($loaderName = null) |
68
|
|
|
{ |
69
|
|
|
$this->loader = $loaderName ?: 'Overtrue\\Pinyin\\FileDictLoader'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Convert string to pinyin. |
74
|
|
|
* |
75
|
10 |
|
* @param string $string |
76
|
|
|
* @param string $option |
77
|
10 |
|
* |
78
|
|
|
* @return array |
79
|
10 |
|
*/ |
80
|
|
|
public function convert($string, $option = self::NONE) |
81
|
|
|
{ |
82
|
|
|
$pinyin = $this->romanize($string); |
83
|
|
|
|
84
|
|
|
return $this->splitWords($pinyin, $option); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Convert string (person name) to pinyin. |
89
|
|
|
* |
90
|
2 |
|
* @param string $stringName |
91
|
|
|
* @param string $option |
92
|
2 |
|
* |
93
|
|
|
* @return array |
94
|
2 |
|
*/ |
95
|
|
|
public function name($stringName, $option = self::NONE) |
96
|
|
|
{ |
97
|
|
|
$pinyin = $this->romanize($stringName, true); |
98
|
|
|
|
99
|
|
|
return $this->splitWords($pinyin, $option); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return a pinyin permalink from string. |
104
|
|
|
* |
105
|
2 |
|
* @param string $string |
106
|
|
|
* @param string $delimiter |
107
|
2 |
|
* |
108
|
2 |
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function permalink($string, $delimiter = '-') |
111
|
2 |
|
{ |
112
|
|
|
if (!in_array($delimiter, array('_', '-', '.', ''), true)) { |
113
|
|
|
throw new InvalidArgumentException("Delimiter must be one of: '_', '-', '', '.'."); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return implode($delimiter, $this->convert($string, false)); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return first letters. |
121
|
|
|
* |
122
|
2 |
|
* @param string $string |
123
|
|
|
* @param string $delimiter |
124
|
|
|
* |
125
|
2 |
|
* @return string |
126
|
2 |
|
*/ |
127
|
|
|
public function abbr($string, $delimiter = '') |
128
|
|
|
{ |
129
|
|
|
return implode($delimiter, array_map(function ($pinyin) { |
130
|
|
|
return $pinyin[0]; |
131
|
|
|
}, $this->convert($string, false))); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Chinese phrase to pinyin. |
136
|
|
|
* |
137
|
|
|
* @param string $string |
138
|
2 |
|
* @param string $delimiter |
139
|
|
|
* @param string $option |
140
|
2 |
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function phrase($string, $delimiter = ' ', $option = self::NONE) |
144
|
|
|
{ |
145
|
|
|
return implode($delimiter, $this->convert($string, $option)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Chinese to pinyin sentense. |
150
|
|
|
* |
151
|
9 |
|
* @param string $sentence |
152
|
|
|
* @param bool $withTone |
153
|
9 |
|
* |
154
|
9 |
|
* @return string |
155
|
9 |
|
*/ |
156
|
|
|
public function sentence($sentence, $withTone = false) |
157
|
9 |
|
{ |
158
|
|
|
$marks = array_keys($this->punctuations); |
159
|
9 |
|
$punctuationsRegex = preg_quote(implode(array_merge($marks, $this->punctuations)), '/'); |
160
|
9 |
|
$regex = '/[^üāēīōūǖáéíóúǘǎěǐǒǔǚàèìòùǜɑa-z0-9'.$punctuationsRegex.'\s_]+/iu'; |
161
|
|
|
|
162
|
9 |
|
$pinyin = preg_replace($regex, '', $this->romanize($sentence)); |
163
|
|
|
|
164
|
|
|
$punctuations = array_merge($this->punctuations, array("\t" => ' ', ' ' => ' ')); |
165
|
|
|
$pinyin = trim(str_replace(array_keys($punctuations), $punctuations, $pinyin)); |
166
|
|
|
|
167
|
|
|
return $withTone ? $pinyin : $this->format($pinyin, false); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Loader setter. |
172
|
1 |
|
* |
173
|
|
|
* @param \Overtrue\Pinyin\DictLoaderInterface $loader |
174
|
1 |
|
* |
175
|
|
|
* @return $this |
176
|
1 |
|
*/ |
177
|
|
|
public function setLoader(DictLoaderInterface $loader) |
178
|
|
|
{ |
179
|
|
|
$this->loader = $loader; |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
21 |
|
/** |
185
|
|
|
* Return dict loader,. |
186
|
21 |
|
* |
187
|
20 |
|
* @return \Overtrue\Pinyin\DictLoaderInterface |
188
|
|
|
*/ |
189
|
20 |
|
public function getLoader() |
190
|
20 |
|
{ |
191
|
20 |
|
if (!($this->loader instanceof DictLoaderInterface)) { |
192
|
|
|
$dataDir = dirname(__DIR__).'/data/'; |
193
|
21 |
|
|
194
|
|
|
$loaderName = $this->loader; |
195
|
|
|
$this->loader = new $loaderName($dataDir); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this->loader; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Preprocess. |
203
|
21 |
|
* |
204
|
|
|
* @param string $string |
205
|
|
|
* |
206
|
8 |
|
* @return string |
207
|
21 |
|
*/ |
208
|
|
|
protected function prepare($string) |
209
|
21 |
|
{ |
210
|
|
|
$string = preg_replace_callback('~[a-z0-9_-]+~i', function ($matches) { |
211
|
|
|
return "\t".$matches[0]; |
212
|
|
|
}, $string); |
213
|
|
|
|
214
|
|
|
return preg_replace("~[^\p{Han}\p{P}\p{Z}\p{M}\p{N}\p{L}\t]~u", '', $string); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Convert Chinese to pinyin. |
219
|
|
|
* |
220
|
21 |
|
* @param string $string |
221
|
|
|
* @param bool $isName |
222
|
21 |
|
* |
223
|
|
|
* @return string |
224
|
21 |
|
*/ |
225
|
|
|
protected function romanize($string, $isName = false) |
226
|
21 |
|
{ |
227
|
2 |
|
$string = $this->prepare($string); |
228
|
2 |
|
|
229
|
|
|
$dictLoader = $this->getLoader(); |
230
|
|
|
|
231
|
21 |
|
if ($isName) { |
232
|
21 |
|
$string = $this->convertSurname($string, $dictLoader); |
233
|
|
|
} |
234
|
21 |
|
|
235
|
|
|
$dictLoader->map(function ($dictionary) use (&$string) { |
236
|
|
|
$string = strtr($string, $dictionary); |
237
|
|
|
}); |
238
|
|
|
|
239
|
|
|
return $string; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Convert Chinese Surname to pinyin. |
244
|
|
|
* |
245
|
|
|
* @param string $string |
246
|
|
|
* @param \Overtrue\Pinyin\DictLoaderInterface $dictLoader |
247
|
2 |
|
* |
248
|
2 |
|
* @return string |
249
|
2 |
|
*/ |
250
|
2 |
|
protected function convertSurname($string, $dictLoader) |
251
|
2 |
|
{ |
252
|
|
|
$dictLoader->mapSurname(function ($dictionary) use (&$string) { |
253
|
2 |
|
foreach ($dictionary as $surname => $pinyin) { |
254
|
2 |
|
if (0 === strpos($string, $surname)) { |
255
|
|
|
$string = $pinyin.mb_substr($string, mb_strlen($surname, 'UTF-8'), mb_strlen($string, 'UTF-8') - 1, 'UTF-8'); |
256
|
2 |
|
|
257
|
|
|
break; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
}); |
261
|
|
|
|
262
|
|
|
return $string; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Split pinyin string to words. |
267
|
12 |
|
* |
268
|
|
|
* @param string $pinyin |
269
|
12 |
|
* @param string $option |
270
|
|
|
* |
271
|
12 |
|
* @return array |
272
|
10 |
|
*/ |
273
|
10 |
|
public function splitWords($pinyin, $option) |
274
|
10 |
|
{ |
275
|
10 |
|
$split = array_filter(preg_split('/[^üāēīōūǖáéíóúǘǎěǐǒǔǚàèìòùǜɑa-z\d]+/iu', $pinyin)); |
276
|
|
|
|
277
|
12 |
|
if (self::UNICODE !== $option) { |
278
|
|
|
foreach ($split as $index => $pinyin) { |
279
|
|
|
$split[$index] = $this->format($pinyin, self::ASCII === $option); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return array_values($split); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Format. |
288
|
17 |
|
* |
289
|
|
|
* @param string $pinyin |
290
|
|
|
* @param bool $tone |
291
|
17 |
|
* |
292
|
17 |
|
* @return string |
293
|
17 |
|
*/ |
294
|
17 |
|
protected function format($pinyin, $tone = false) |
295
|
17 |
|
{ |
296
|
17 |
|
$replacements = array( |
297
|
|
|
'üē' => array('ue', 1), 'üé' => array('ue', 2), 'üě' => array('ue', 3), 'üè' => array('ue', 4), |
298
|
17 |
|
'ā' => array('a', 1), 'ē' => array('e', 1), 'ī' => array('i', 1), 'ō' => array('o', 1), 'ū' => array('u', 1), 'ǖ' => array('v', 1), |
299
|
17 |
|
'á' => array('a', 2), 'é' => array('e', 2), 'í' => array('i', 2), 'ó' => array('o', 2), 'ú' => array('u', 2), 'ǘ' => array('v', 2), |
300
|
16 |
|
'ǎ' => array('a', 3), 'ě' => array('e', 3), 'ǐ' => array('i', 3), 'ǒ' => array('o', 3), 'ǔ' => array('u', 3), 'ǚ' => array('v', 3), |
301
|
16 |
|
'à' => array('a', 4), 'è' => array('e', 4), 'ì' => array('i', 4), 'ò' => array('o', 4), 'ù' => array('u', 4), 'ǜ' => array('v', 4), |
302
|
17 |
|
); |
303
|
|
|
|
304
|
17 |
|
foreach ($replacements as $unicde => $replacement) { |
305
|
|
|
if (false !== strpos($pinyin, $unicde)) { |
306
|
|
|
$pinyin = str_replace($unicde, $replacement[0], $pinyin).($tone ? $replacement[1] : ''); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $pinyin; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: