-
Notifications
You must be signed in to change notification settings - Fork 0
/
Normalize.php
537 lines (468 loc) · 13.4 KB
/
Normalize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
namespace ArabyPHP\Normalize;
class Normalize
{
/**
* @var array
*/
private $_unshapeMap = [];
/**
* @var array
*/
private $_unshapeKeys = [];
/**
* @var array
*/
private $_unshapeValues = [];
/**
* @var array
*/
private $_chars = [];
/**
* @var array
*/
private $_charGroups = [];
/**
* @var array
*/
private $_charArNames = [];
/**
* Load the Unicode constants that will be used ibn substitutions
* and normalisations.
*
* @ignore
*/
public function __construct()
{
include __DIR__.'/data/ArUnicode.constants.php';
$this->_unshapeMap = $ligature_map;
$this->_unshapeKeys = array_keys($this->_unshapeMap);
$this->_unshapeValues = array_values($this->_unshapeMap);
$this->_chars = $char_names;
$this->_charGroups = $char_groups;
$this->_charArNames = $char_ar_names;
}
/**
* Strip all tatweel characters from an Arabic text.
*
* @param string $text The text to be stripped.
*
* @return string the stripped text.
*/
public function stripTatweel($text)
{
return str_replace($this->_chars['TATWEEL'], '', $text);
}
/**
* Strip all tashkeel characters from an Arabic text.
*
* @param string $text The text to be stripped.
*
* @return string the stripped text.
*
*/
public function stripTashkeel($text)
{
$tashkeel = [
$this->_chars['FATHATAN'],
$this->_chars['DAMMATAN'],
$this->_chars['KASRATAN'],
$this->_chars['FATHA'],
$this->_chars['DAMMA'],
$this->_chars['KASRA'],
$this->_chars['SUKUN'],
$this->_chars['SHADDA'],
];
return str_replace($tashkeel, '', $text);
}
/**
* Normalise all Hamza characters to their corresponding aleph
* character in an Arabic text.
*
* @param string $text The text to be normalised.
*
* @return string the normalised text.
*/
public function normaliseHamza($text)
{
$replace = [
$this->_chars['WAW_HAMZA'] = $this->_chars['WAW'],
$this->_chars['YEH_HAMZA'] = $this->_chars['YEH'],
];
$alephs = [
$this->_chars['ALEF_MADDA'],
$this->_chars['ALEF_HAMZA_ABOVE'],
$this->_chars['ALEF_HAMZA_BELOW'],
$this->_chars['HAMZA_ABOVE'],
$this->_chars['HAMZA_BELOW'],
];
$text = str_replace(array_keys($replace), array_values($replace), $text);
$text = str_replace($alephs, $this->_chars['ALEF'], $text);
return $text;
}
/**
* Unicode uses some special characters where the lamaleph and any
* hamza above them are combined into one code point. Some input
* system use them. This function expands these characters.
*
* @param string $text The text to be normalised.
*
* @return string the normalised text.
*/
public function normaliseLamaleph($text)
{
$text = str_replace($this->_chars['LAM_ALEPH'], $simple_LAM_ALEPH, $text);
$text = str_replace($this->_chars['LAM_ALEPH_HAMZA_ABOVE'], $simple_LAM_ALEPH_HAMZA_ABOVE, $text);
$text = str_replace($this->_chars['LAM_ALEPH_HAMZA_BELOW'], $simple_LAM_ALEPH_HAMZA_BELOW, $text);
$text = str_replace($this->_chars['LAM_ALEPH_MADDA_ABOVE'], $simple_LAM_ALEPH_MADDA_ABOVE, $text);
return $text;
}
/**
* Return unicode char by its code point.
*
* @param char $u code point
*
* @return string the result character.
*/
public function unichr($u)
{
return mb_convert_encoding('&#'.intval($u).';', 'UTF-8', 'HTML-ENTITIES');
}
/**
* Takes a string, it applies the various filters in this class
* to return a unicode normalised string suitable for activities
* such as searching, indexing, etc.
*
* @param string $text the text to be normalised.
*
* @return string the result normalised string.
*/
public function normalise($text)
{
$text = $this->stripTashkeel($text);
$text = $this->stripTatweel($text);
$text = $this->normaliseHamza($text);
$text = $this->normaliseLamaleph($text);
return $text;
}
/**
* Takes Arabic text in its joined form, it untangles the characters
* and unshapes them.
*
* This can be used to process text that was processed through OCR
* or by extracting text from a PDF document.
*
* Note that the result text may need further processing. In most
* cases, you will want to use the utf8Strrev function from
* this class to reverse the string.
*
* Most of the work of setting up the characters for this function
* is done through the ArUnicode.constants.php constants and
* the constructor loading.
*
* @param string $text the text to be unshaped.
*
* @return string the result normalised string.
*/
public function unshape($text)
{
return str_replace($this->_unshapeKeys, $this->_unshapeValues, $text);
}
/**
* Take a UTF8 string and reverse it.
*
* @param string $str the string to be reversed.
* @param bool $reverse_numbers whether to reverse numbers.
*
* @return string The reversed string.
*/
public function utf8Strrev($str, $reverse_numbers = false)
{
preg_match_all('/./us', $str, $ar);
if ($reverse_numbers) {
return implode('', array_reverse($ar[0]));
} else {
$temp = [];
foreach ($ar[0] as $value) {
if (is_numeric($value) && !empty($temp[0]) && is_numeric($temp[0])) {
foreach ($temp as $key => $value2) {
if (is_numeric($value2)) {
$pos = ($key + 1);
} else {
break;
}
}
$temp2 = array_splice($temp, $pos);
$temp = array_merge($temp, [$value], $temp2);
} else {
array_unshift($temp, $value);
}
}
return implode('', $temp);
}
}
/**
* Checks for Arabic Tashkeel marks (i.e. FATHA, DAMMA, KASRA, SUKUN,
* SHADDA, FATHATAN, DAMMATAN, KASRATAN).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Tashkeel mark
*/
public function isTashkeel($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['TASHKEEL'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Harakat marks (i.e. FATHA, DAMMA, KASRA, SUKUN, TANWIN).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Harakat mark
*/
public function isHaraka($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['HARAKAT'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic short Harakat marks (i.e. FATHA, DAMMA, KASRA, SUKUN).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic short Harakat mark
*/
public function isShortharaka($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['SHORTHARAKAT'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Tanwin marks (i.e. FATHATAN, DAMMATAN, KASRATAN).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Tanwin mark
*/
public function isTanwin($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['TANWIN'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Ligatures like LamAlef (i.e. LAM ALEF, LAM ALEF HAMZA
* ABOVE, LAM ALEF HAMZA BELOW, LAM ALEF MADDA ABOVE).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Ligatures like LamAlef
*/
public function isLigature($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['LIGUATURES'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Hamza forms (i.e. HAMZA, WAW HAMZA, YEH HAMZA, HAMZA ABOVE,
* HAMZA BELOW, ALEF HAMZA BELOW, ALEF HAMZA ABOVE).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Hamza form
*/
public function isHamza($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['HAMZAT'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Alef forms (i.e. ALEF, ALEF MADDA, ALEF HAMZA ABOVE,
* ALEF HAMZA BELOW,ALEF WASLA, ALEF MAKSURA).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Alef form
*
*/
public function isAlef($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['ALEFAT'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Weak letters (i.e. ALEF, WAW, YEH, ALEF_MAKSURA).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Weak letter
*
*/
public function isWeak($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['WEAK'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Yeh forms (i.e. YEH, YEH HAMZA, SMALL YEH, ALEF MAKSURA).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Yeh form
*
*/
public function isYehlike($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['YEHLIKE'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Waw like forms (i.e. WAW, WAW HAMZA, SMALL WAW).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Waw like form
*
*/
public function isWawlike($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['WAWLIKE'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Teh forms (i.e. TEH, TEH MARBUTA).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Teh form
*
*/
public function isTehlike($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['TEHLIKE'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Small letters (i.e. SMALL ALEF, SMALL WAW, SMALL YEH).
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Small letter
*
*/
public function isSmall($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['SMALL'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Moon letters.
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Moon letter
*
*/
public function isMoon($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['MOON'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Checks for Arabic Sun letters.
*
* @param string $archar Arabic unicode char
*
* @return bool True if it is Arabic Sun letter
*
*/
public function isSun($archar)
{
$key = array_search($archar, $this->_chars);
if (in_array($key, $this->_charGroups['SUN'])) {
$value = true;
} else {
$value = false;
}
return $value;
}
/**
* Return Arabic letter name in arabic.
*
* @param string $archar Arabic unicode char
*
* @return string Arabic letter name in arabic
*
*/
public function charName($archar)
{
$key = array_search($archar, $this->_chars);
$name = $this->_charArNames["$key"];
return $name;
}
}