-
Notifications
You must be signed in to change notification settings - Fork 0
/
Virastar.php
1052 lines (920 loc) · 37.3 KB
/
Virastar.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Alirezasedghi\Virastar;
use Exception;
class Virastar
{
private $charsPersian = 'ءاآأإئؤبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهیةيك';
// @REF: https://en.wikipedia.org/wiki/Persian_alphabet#Diacritics
// `\u064e\u0650\u064f\u064b\u064d\u064c\u0651\u06c0`
private $charsDiacritic = 'ًٌٍَُِّْ';
private $patternURI = '#[-a-zA-Z0-9@:%_\+.~\#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~\#?&//=]*)?#sim';
private $patternAfter = '\\s.,;،؛!؟?"\'()[\\]{}“”«»';
private $defaults = [
"cleanup_begin_and_end" => true,
"cleanup_extra_marks" => true,
"cleanup_kashidas" => true,
"cleanup_line_breaks" => true,
"cleanup_rlm" => true,
"cleanup_spacing" => true,
"cleanup_zwnj" => true,
"decode_html_entities" => true,
"fix_arabic_numbers" => true,
"fix_dashes" => true,
"fix_diacritics" => true,
"fix_english_numbers" => true,
"fix_english_quotes_pairs" => true,
"fix_english_quotes" => true,
"fix_hamzeh" => true,
"fix_hamzeh_arabic" => false,
"fix_misc_non_persian_chars" => true,
"fix_misc_spacing" => true,
"fix_numeral_symbols" => true,
"fix_prefix_spacing" => true,
"fix_persian_glyphs" => true,
"fix_punctuations" => true,
"fix_question_mark" => true,
"fix_spacing_for_braces_and_quotes" => true,
"fix_spacing_for_punctuations" => true,
"fix_suffix_misc" => true,
"fix_suffix_spacing" => true,
"fix_three_dots" => true,
"kashidas_as_parenthetic" => true,
"markdown_normalize_braces" => true,
"markdown_normalize_lists" => true,
"normalize_dates" => true,
"normalize_ellipsis" => true,
"normalize_eol" => true,
"preserve_braces" => false,
"preserve_brackets" => false,
"preserve_comments" => true,
"preserve_entities" => true,
"preserve_front_matter" => true,
"preserve_HTML" => true,
"preserve_nbsp" => true,
"preserve_URIs" => true,
"remove_diacritics" => false,
"skip_markdown_ordered_lists_numbers_conversion" => false
];
private $digits = '۱۲۳۴۵۶۷۸۹۰';
private $entities = [
'sbquo;' => '\x{201a}',
'lsquo;' => '\x{2018}',
'lsquor;' => '\x{201a}',
'ldquo;' => '\x{201c}',
'ldquor;' => '\x{201e}',
'rdquo;' => '\x{201d}',
'rdquor;' => '\x{201d}',
'rsquo;' => '\x{2019}',
'rsquor;' => '\x{2019}',
'apos;' => '\'',
'QUOT;' => '"',
'QUOT' => '"',
'quot;' => '"',
'quot' => '"',
'zwj;' => '\x{200d}',
'ZWNJ;' => '\x{200c}',
'zwnj;' => '\x{200c}',
'shy;' => '\x{00ad}' // wrongly used as zwnj
];
private $glyphs = [
'\x{200c}ه' => 'ﻫ',
'ی\x{200c}' => 'ﻰﻲ',
'ﺃ' => 'ﺄﺃ',
'ﺁ' => 'ﺁﺂ',
'ﺇ' => 'ﺇﺈ',
'ا' => 'ﺎا',
'ب' => 'ﺏﺐﺑﺒ',
'پ' => 'ﭖﭗﭘﭙ',
'ت' => 'ﺕﺖﺗﺘ',
'ث' => 'ﺙﺚﺛﺜ',
'ج' => 'ﺝﺞﺟﺠ',
'چ' => 'ﭺﭻﭼﭽ',
'ح' => 'ﺡﺢﺣﺤ',
'خ' => 'ﺥﺦﺧﺨ',
'د' => 'ﺩﺪ',
'ذ' => 'ﺫﺬ',
'ر' => 'ﺭﺮ',
'ز' => 'ﺯﺰ',
'ژ' => 'ﮊﮋ',
'س' => 'ﺱﺲﺳﺴ',
'ش' => 'ﺵﺶﺷﺸ',
'ص' => 'ﺹﺺﺻﺼ',
'ض' => 'ﺽﺾﺿﻀ',
'ط' => 'ﻁﻂﻃﻄ',
'ظ' => 'ﻅﻆﻇﻈ',
'ع' => 'ﻉﻊﻋﻌ',
'غ' => 'ﻍﻎﻏﻐ',
'ف' => 'ﻑﻒﻓﻔ',
'ق' => 'ﻕﻖﻗﻘ',
'ک' => 'ﮎﮏﮐﮑﻙﻚﻛﻜ',
'گ' => 'ﮒﮓﮔﮕ',
'ل' => 'ﻝﻞﻟﻠ',
'م' => 'ﻡﻢﻣﻤ',
'ن' => 'ﻥﻦﻧﻨ',
'ه' => 'ﻩﻪﻫﻬ',
'هٔ' => 'ﮤﮥ',
'و' => 'ﻭﻮ',
'ﺅ' => 'ﺅﺆ',
'ی' => 'ﯼﯽﯾﯿﻯﻰﻱﻲﻳﻴ',
'ئ' => 'ﺉﺊﺋﺌ',
'لا' => 'ﻼ',
'ﻹ' => 'ﻺ',
'ﻷ' => 'ﻸ',
'ﻵ' => 'ﻶ'
];
// Options
private $options = [];
/**
* Construction
*
* @throws Exception
*/
public function __construct($options = [])
{
if (!empty($options))
if (is_array($options))
$this->setOptions($options);
else
throw new Exception('Options should be an array.');
}
/**
* Set options or update it
*
* @param array $options
*/
public function setOptions(array $options)
{
$this->options = $this->parseOptions($options);
}
/**
* Get options
*
* @return array
*/
public function getOptions()
{
return !empty($this->options) ? $this->options : $this->defaults;
}
/**
* Parse options
*
* @param $options
* @return array
*/
public function parseOptions($options)
{
if (is_object($options)) {
$parsed_args = get_object_vars($options);
} elseif (is_array($options)) {
$parsed_args =& $options;
} else {
parse_str((string)$options, $parsed_args);
}
$defaults = $this->defaults;
if (!empty($parsed_args))
return array_merge($defaults, $parsed_args);
return $defaults;
}
/**
* Converts numeral and selected html character-sets into original characters
*
* @param $text
* @return string
*/
public function decodeHTMLEntities($text)
{
return preg_replace_callback('/&(#?[^;\W]+;?)/', function ($matched) {
$match = $matched[1] ?? '';
// $html[] = $matched;
return ' __HTML__PRESERVER__ ';
}, $text);
}
/**
* Cleanup Text
*
* @param $text
* @return string
* @throws Exception
*/
public function cleanup($text)
{
if (!is_string($text))
throw new Exception('Expected a String, but received ' . gettype($text));
// trim text
$text = trim($text);
// don't bother if its empty or whitespace
if (empty($text))
return $text;
$options = $this->getOptions();
// preserves front matter data in the text
if ($options["preserve_front_matter"]) {
$front_matter = [];
$text = preg_replace_callback('/^ ---[\S\s]*?---\n/', function ($matched) use ($front_matter) {
$front_matter[] = $matched;
return ' __FRONT__MATTER__PRESERVER__ ';
}, $text);
}
// preserves all html tags in the text
// @props: @wordpress/wordcount
if ($options["preserve_HTML"]) {
$html = [];
$text = preg_replace_callback('/<\/?[a-z][^>]*?>/i', function ($matched) use ($html) {
$html[] = $matched;
return ' __HTML__PRESERVER__ ';
}, $text);
}
// preserves all html comments in the text
// @props: @wordpress/wordcount
if ($options["preserve_comments"]) {
$comments = [];
$text = preg_replace_callback('/<!--[\s\S]*?-->/', function ($matched) use ($comments) {
$comments[] = $matched;
return ' __COMMENT__PRESERVER__ ';
}, $text);
}
// preserves strings inside square brackets (`[]`)
if ($options["preserve_brackets"]) {
$brackets = [];
$text = preg_replace_callback('/(\[.*?\])/', function ($matched) use ($brackets) {
$brackets[] = $matched;
return ' __BRACKETS__PRESERVER__ ';
}, $text);
}
// preserve strings inside curly braces (`{}`)
if ($options["preserve_braces"]) {
$braces = [];
$text = preg_replace_callback('/(\{.*?\})/', function ($matched) use ($braces) {
$braces[] = $matched;
return ' __BRACES__PRESERVER__ ';
}, $text);
}
// preserves all uri strings in the text
if ($options["preserve_URIs"]) {
$md_links = [];
$uris = [];
// stores Markdown links separately
$text = preg_replace_callback('/]\((.*?)\)/', function ($matched) use ($md_links) {
if (is_string($matched))
$matched = [$matched];
if (isset($matched[1])) {
$md_links[] = trim($matched[1]);
return '](__MD_LINK__PRESERVER__)'; // no padding!
}
return $matched[0];
}, $text);
$text = preg_replace_callback($this->patternURI, function ($matched) use ($uris) {
$uris[] = $matched;
return ' __URI__PRESERVER__ ';
}, $text);
}
// preserves all no-break space entities in the text
if ($options["preserve_nbsp"]) {
$nbsps = [];
$text = preg_replace_callback('/ | /iu', function ($matched) use ($nbsps) {
$nbsps[] = $matched;
return ' __NBSPS__PRESERVER__ ';
}, $text);
}
if ($options["decode_html_entities"]) {
$text = $this->decodeHTMLEntities($text);
}
// preserves all html entities in the text
// @props: @substack/node-ent
if ($options["preserve_entities"]) {
$entities = [];
$text = preg_replace_callback('/&(#?[^;\W]+;?)/', function ($matched) use ($entities) {
$entities[] = $matched;
return ' __ENTITIES__PRESERVER__ ';
}, $text);
}
if ($options["normalize_eol"]) {
$text = $this->normalizeEOL($text);
}
if ($options["fix_persian_glyphs"]) {
$text = $this->fixPersianGlyphs($text);
}
if ($options["fix_dashes"]) {
$text = $this->fixDashes($text);
}
if ($options["fix_three_dots"]) {
$text = $this->fixThreeDots($text);
}
if ($options["normalize_ellipsis"]) {
$text = $this->normalizeEllipsis($text);
}
if ($options["fix_english_quotes_pairs"]) {
$text = $this->fixEnglishQuotesPairs($text);
}
if ($options["fix_english_quotes"]) {
$text = $this->fixEnglishQuotes($text);
}
if ($options["fix_hamzeh"]) {
if ($options["fix_hamzeh_arabic"]) {
$text = $this->fixHamzehArabic($text);
}
$text = $this->fixHamzeh($text);
}
else if ($options["fix_suffix_spacing"]) {
if ($options["fix_hamzeh_arabic"]) {
$text = $this->fixHamzehArabicAlt($text);
}
$text = $this->fixSuffixSpacingHamzeh($text);
}
if ($options["cleanup_rlm"]) {
$text = $this->cleanupRLM($text);
}
if ($options["cleanup_zwnj"]) {
$text = $this->cleanupZWNJ($text);
}
if ($options["fix_arabic_numbers"]) {
$text = $this->fixArabicNumbers($text);
}
// word tokenizer
$text = preg_replace_callback('/(^|\s+)([[({"\'“«]?)(\S+)([\])}"\'”»]?)(?=($|\s+))/', function ($matches) use ($options) {
$matched = $matches[0] ?? '';
// $before = $matches[1] ?? '';
// $leading = $matches[2] ?? '';
$word = $matches[3] ?? '';
$trailing = $matches[4] ?? '';
$after = $matches[5] ?? '';
// should not replace to persian chars in english phrases
preg_match('/[a-zA-Z\-_]{2,}/u', $word, $word_match);
if ($word_match) {
return $matched;
}
// should not touch sprintf directives
unset($word_match);
preg_match_all("/%(?:\d+\$)?[+-]?(?:[ 0]|'.{1})?-?\d*(?:\.\d+)?[bcdeEufFgGosxX]/u", $word, $word_match);
if ($word_match && ( isset($word_match[0]) && !empty($word_match[0]) )) {
return $matched;
}
// should not touch numbers in html entities
unset($word_match);
preg_match('/&#\d+;/u', $word, $word_match);
if ($word_match) {
return $matched;
}
// skips converting english numbers of ordered lists in markdown
unset($word_match);
preg_match('/(?:(?:\r?\n)|(?:\r\n?)|(?:^|\n))\d+\.\s/', $matched . $trailing . $after, $word_match);
if ($options["skip_markdown_ordered_lists_numbers_conversion"] && $word_match) {
return $matched;
}
if ($options["fix_english_numbers"]) {
$matched = $this->fixEnglishNumbers($matched);
}
if ($options["fix_numeral_symbols"]) {
$matched = $this->fixNumeralSymbols($matched);
}
if ($options["fix_punctuations"]) {
$matched = $this->fixPunctuations($matched);
}
if ($options["fix_misc_non_persian_chars"]) {
$matched = $this->fixMiscNonPersianChars($matched);
}
if ($options["fix_question_mark"]) {
$matched = $this->fixQuestionMark($matched);
}
return $matched;
}, $text);
if ($options["normalize_dates"]) {
$text = $this->normalizeDates($text);
}
if ($options["fix_prefix_spacing"]) {
$text = $this->fixPrefixSpacing($text);
}
if ($options["fix_suffix_spacing"]) {
$text = $this->fixSuffixSpacing($text);
}
if ($options["fix_suffix_misc"]) {
$text = $this->fixSuffixMisc($text);
}
if ($options["fix_spacing_for_braces_and_quotes"]) {
$text = $this->fixBracesSpacing($text);
}
if ($options["cleanup_extra_marks"]) {
$text = $this->cleanupExtraMarks($text);
}
if ($options["fix_spacing_for_punctuations"]) {
$text = $this->fixPunctuationSpacing($text);
}
if ($options["kashidas_as_parenthetic"]) {
$text = $this->kashidasAsParenthetic($text);
}
if ($options["cleanup_kashidas"]) {
$text = $this->cleanupKashidas($text);
}
if ($options["markdown_normalize_braces"]) {
$text = $this->markdownNormalizeBraces($text);
}
if ($options["markdown_normalize_lists"]) {
$text = $this->markdownNormalizeLists($text);
}
// doing it again after `fixPunctuationSpacing()`
if ($options["fix_spacing_for_braces_and_quotes"]) {
$text = $this->fixBracesSpacingInside($text);
}
if ($options["fix_misc_spacing"]) {
$text = $this->fixMiscSpacing($text);
}
if ($options["remove_diacritics"]) {
$text = $this->removeDiacritics($text);
} else if ($options["fix_diacritics"]) {
$text = $this->fixDiacritics($text);
}
if ($options["cleanup_spacing"]) {
$text = $this->cleanupSpacing($text);
}
if ($options["cleanup_zwnj"]) {
$text = $this->cleanupZWNJLate($text);
}
if ($options["cleanup_line_breaks"]) {
$text = $this->cleanupLineBreaks($text);
}
// bringing back entities
if ($options["preserve_entities"]) {
$entities_array = $this->entities;
$text = preg_replace_callback('/[ ]?__ENTITIES__PRESERVER__[ ]?/', function () use ($entities_array) {
return array_shift($entities_array);
}, $text);
}
// bringing back nbsp
if ($options["preserve_nbsp"]) {
$nbsps = $nbsps ?? [];
$text = preg_replace_callback('/[ ]?__NBSPS__PRESERVER__[ ]?/', function () use ($nbsps) {
return array_shift($nbsps);
}, $text);
}
// bringing back URIs
if ($options["preserve_URIs"]) {
$md_links = $md_links ?? [];
// no padding!
$text = preg_replace_callback('/__MD_LINK__PRESERVER__/', function () use ($md_links) {
return array_shift($md_links);
}, $text);
$uris = $uris ?? [];
$text = preg_replace_callback('/[ ]?__URI__PRESERVER__[ ]?/', function () use ($uris) {
return array_shift($uris);
}, $text);
}
// bringing back braces
if ($options["preserve_braces"]) {
$braces = $braces ?? [];
$text = preg_replace_callback('/[ ]?__BRACES__PRESERVER__[ ]?/', function () use ($braces) {
return array_shift($braces);
}, $text);
}
// bringing back brackets
if ($options["preserve_brackets"]) {
$brackets = $brackets ?? [];
$text = preg_replace_callback('/[ ]?__BRACKETS__PRESERVER__[ ]?/', function () use ($brackets) {
return array_shift($brackets);
}, $text);
}
// bringing back HTML comments
if ($options["preserve_comments"]) {
$comments = $comments ?? [];
$text = preg_replace_callback('/[ ]?__COMMENT__PRESERVER__[ ]?/', function () use ($comments) {
return array_shift($comments);
}, $text);
}
// bringing back HTML tags
if ($options["preserve_HTML"]) {
$html = $html ?? [];
$text = preg_replace_callback('/[ ]?__HTML__PRESERVER__[ ]?/', function () use ($html) {
return array_shift($html);
}, $text);
}
// bringing back front matter
if ($options["preserve_front_matter"]) {
$front_matter = $front_matter ?? [];
$text = preg_replace_callback('/[ ]?__FRONT__MATTER__PRESERVER__[ ]?/', function () use ($front_matter) {
return array_shift($front_matter);
}, $text);
}
if ($options["cleanup_begin_and_end"]) {
$text = $this->cleanupBeginAndEnd($text);
} else {
// removes single space paddings around the string
$text = preg_replace('/^[ ]/', '', preg_replace('/[ ]$/', '', $text));
}
return $text;
}
protected function cleanupZWNJ($text)
{
// converts all soft hyphens (­) into zwnj
return preg_replace('/\x{00ad}/u', '\x{200c}',
// removes more than one zwnj
preg_replace('/\x{200c}{2,}/u', '\x{200c}',
// cleans zwnj before and after numbers, english words, spaces and punctuations
// preg_replace('~\x{200c}([\w\s0-9۰-۹[\](){}«»“”.…,:;?!$%@#*=+\-/\\،؛٫٬×٪؟ـ])~u', '$1', // \w is for any english word character in javascript, but it supports words in any language in php
preg_replace('~\x{200c}([\s0-9۰-۹[\](){}«»“”.…,:;?!$%@#*=+\-/\\،؛٫٬×٪؟ـ])~u', '$1',
// preg_replace('~([\w\s0-9۰-۹[\](){}«»“”.…,:;?!$%@#*=+\-/\\،؛٫٬×٪؟ـ])\x{200c}~u', '$1', // \w is for any english word character in javascript, but it supports words in any language in php
preg_replace('~([\s0-9۰-۹[\](){}«»“”.…,:;?!$%@#*=+\-/\\،؛٫٬×٪؟ـ])\x{200c}~u', '$1',
// removes unnecessary zwnj on start/end of each line
preg_replace('/(^\x{200c}|\x{200c}$)/um', '', $text)
)
)
)
);
}
// late checks for zwnj
protected function cleanupZWNJLate($text)
{
// cleans zwnj after characters that don't connect to the next
return preg_replace('/([إأةؤورزژاآدذ،؛,:«»\\/@#$٪×*()ـ\-=|])\x{200c}/u', '$1', $text);
}
protected function charReplace($text, $fromBatch, $toBatch)
{
$fromChars = mb_str_split($fromBatch);
$toChars = mb_str_split($toBatch);
foreach ($fromChars as $key => $value) {
$text = preg_replace("~" . $value . "~u", $toChars[$key] ?? '', $text);
}
return $text;
}
protected function arrReplace($text, $array)
{
foreach ($array as $key => $item) {
$text = preg_replace('/[' . $item . ']/u', $key, $text);
}
return $text;
}
protected function normalizeEOL($text)
{
// replace windows end of lines with unix eol (`\n`)
return preg_replace('/(\r?\n)|(\r\n?)/u', "\n", $text); // Replace windows end of lines with unix eol (`\n`)
}
protected function fixDashes($text)
{
// replaces triple dash to mdash
// replaces double dash to ndash
return preg_replace('/-{2}/', '–', preg_replace('/-{3}/', '—', $text));
}
protected function fixThreeDots($text)
{
// remove spaces between dots
// replaces three dots with ellipsis character
return preg_replace('/\.([ ]+)(?=[.])/', '.', preg_replace('/[ \t]*\.{3,}/', '…', $text));
}
protected function normalizeEllipsis($text)
{
// replaces more than one ellipsis with one
// replaces (space|tab|zwnj) after ellipsis with one space
// NOTE: allows for space before ellipsis
return preg_replace('/(…){2,}/', '…',
preg_replace('/([ ]{1,})*…[ \t\x{200c}]*/u', '$1… ', $text)
);
}
protected function fixEnglishQuotesPairs($text)
{
// replaces english quote pairs with their persian equivalent
return preg_replace('/(“)(.+?)(”)/', '«$2»', $text);
}
// replaces english quote marks with their persian equivalent
protected function fixEnglishQuotes($text)
{
return preg_replace('/(["\'`]+)(.+?)(\1)/', '«$2»', $text);
}
protected function fixHamzeh($text)
{
$replacement = '$1هٔ$3';
// replaces ه followed by (space|ZWNJ|lrm) follow by ی with هٔ
return preg_replace('/(\S)(ه[\s\x{200c}\x{200e}]+[یي])([\s\x{200c}\x{200e}])/u', $replacement, // heh + ye
// replaces ه followed by (space|ZWNJ|lrm|nothing) follow by ء with هٔ
preg_replace('/(\S)(ه[\s\x{200c}\x{200e}]?\x{0621})([\s\x{200c}\x{200e}])/u', $replacement, // heh + standalone hamza
// replaces هٓ or single-character ۀ with the standard هٔ
preg_replace('/(ۀ|هٓ)/u', 'هٔ', $text)
)
);
}
protected function fixHamzehArabic($text)
{
// converts arabic hamzeh ة to هٔ
return preg_replace('/(\S)ة([\s\x{200c}\x{200e}])/u', '$1هٔ$2', $text);
}
protected function fixHamzehArabicAlt($text)
{
// converts arabic hamzeh ة to هی
return preg_replace('/(\S)ة([\s\x{200c}\x{200e}])/u', '$1هی$2', $text);
}
protected function cleanupRLM($text)
{
// converts Right-to-left marks followed by persian characters to
// zero-width non-joiners (ZWNJ)
return preg_replace('/([^a-zA-Z\-_])(\x{200f})/u', '$1\x{200c}', $text);
}
// converts incorrect persian glyphs to standard characters
protected function fixPersianGlyphs($text)
{
return $this->arrReplace($text, $this->glyphs);
}
protected function fixMiscNonPersianChars($text)
{
return $this->charReplace($text, 'كڪيىۍېہە', 'ککییییههه');
}
// replaces english numbers with their persian equivalent
protected function fixEnglishNumbers($text)
{
return $this->charReplace($text, '1234567890', $this->digits);
}
// replaces arabic numbers with their persian equivalent
protected function fixArabicNumbers($text)
{
return $this->charReplace($text, '١٢٣٤٥٦٧٨٩٠', $this->digits);
}
// @REF: https://github.com/shkarimpour/pholiday/pull/5/files
protected function convertPersianNumbers($text)
{
return preg_replace_callback('/[\x{0660}-\x{0669}\x{06f0}-\x{06f9}]/', function ($matched) {
return ord($matched[0][0] ?? '') & 0xf;
}, $text);
}
protected function fixNumeralSymbols($text)
{
// replaces english percent signs (U+066A)
return preg_replace('/([۰-۹]) ?%/', '$1٪',
// replaces dots between numbers into decimal separator (U+066B)
preg_replace('/([۰-۹])\.(?=[۰-۹])/', '$1٫',
// replaces commas between numbers into thousands separator (U+066C)
preg_replace('/([۰-۹]),(?=[۰-۹])/', '$1٬', $text)
)
);
}
protected function normalizeDates($text)
{
// re-orders date parts with slash as delimiter
return preg_replace_callback('#([0-9۰-۹]{1,2})([/-])([0-9۰-۹]{1,2})\2([0-9۰-۹]{4})#', function ($matched) {
$day = $matched[1] ?? '';
// $delimiter = $matched[2] ?? '';
$month = $matched[3] ?? '';
$year = $matched[4] ?? '';
return $year . '/' . $month . '/' . $day;
}, $text);
}
protected function fixPunctuations($text)
{
return $this->charReplace($text, '٬,;', '،،؛');
}
// replaces question marks with its persian equivalent
protected function fixQuestionMark($text)
{
return preg_replace('/(\?)/', '\x{061F}', $text); // \x{061F} = ؟
}
// puts zwnj between the word and the prefix:
// - mi* nemi* bi*
// NOTE: there's a possible bug here: prefixes could be separate nouns
protected function fixPrefixSpacing($text)
{
$replacement = "$1\u{200c}$3";
return preg_replace('/((\s|^)ن?می) ([^ ])/u', $replacement, preg_replace('/((\s|^)بی) ([^ ])/u', $replacement, $text));
}
// puts zwnj between the word and the suffix
// NOTE: possible bug: suffixes could be nouns
protected function fixSuffixSpacing($text)
{
$replacement = "$1\u{200c}$2";
// must be done before others
// *ha *haye
return preg_replace('#([' . $this->charsPersian . $this->charsDiacritic . ']) (ها(ی)?[' . $this->patternAfter . '])#u', $replacement,
// *am *at *ash *ei *eid *eem *and *man *tan *shan
preg_replace('#([' . $this->charsPersian . $this->charsDiacritic . ']) ((ام|ات|اش|ای|اید|ایم|اند|مان|تان|شان)[' . $this->patternAfter . '])#u', $replacement,
// *tar *tari *tarin
preg_replace('#([' . $this->charsPersian . $this->charsDiacritic . ']) (تر((ی)|(ین))?[' . $this->patternAfter . '])#u', $replacement,
// *hayee *hayam *hayat *hayash *hayetan *hayeman *hayeshan
preg_replace('#([' . $this->charsPersian . $this->charsDiacritic . ']) ((هایی|هایم|هایت|هایش|هایمان|هایتان|هایشان)[' . $this->patternAfter . '])#u', $replacement, $text)
)
)
);
}
protected function fixSuffixSpacingHamzeh($text)
{
$replacement = '$1\x{0647}\x{200c}\x{06cc}$3';
// heh + ye
return preg_replace('/(\S)(ه[\s\x{200c}]+[یي])([\s\x{200c}])/u', $replacement,
// heh + standalone hamza
preg_replace('/(\S)(ه[\s\x{200c}]?\x{0621})([\s\x{200c}])/u', $replacement,
// heh + hamza above
preg_replace('/(\S)(ه[\s\x{200c}]?\x{0654})([\s\x{200c}])/u', $replacement, $text)
)
);
}
protected function fixSuffixMisc($text)
{
// replaces ه followed by ئ or ی, and then by ی, with ه\x{200c}ای,
// EXAMPLE: خانهئی becomes خانهای
// preg_replace('/(\S)ه[\x{200c}\x{200e}][ئی]ی([\s\x{200c}\x{200e}])/u', "$1ه\u{200c}ای$2", $text);
return preg_replace('/(\S)ه[\x{200c}\x{200e}][ئی]ی/u', "$1ه\u{200c}ای$2", $text);
}
protected function cleanupExtraMarks($text)
{
// removes space between different/same marks (combining for cleanup)
return preg_replace('#([؟?!])([ ]+)(?=[؟?!])#', '$1',
// replaces more than one exclamation mark with just one
preg_replace('/(!){2,}/u', '$1',
// replaces more than one english or persian question mark with just one
preg_replace('/(\x{061F}|\?){2,}/u', '$1', // \x{061F} = `؟`
// re-orders consecutive marks
preg_replace('/(!)([ \t]*)([\x{061F}?])/u', '$3$1', $text) // `?!` --> `!?`
)
)
);
}
// replaces kashidas to ndash in parenthetic
protected function kashidasAsParenthetic($text)
{
return preg_replace('/(\s)\x{0640}+/u', '$1–', preg_replace('/\x{0640}+(\s)/u', '–$1', $text));
}
protected function cleanupKashidas($text)
{
// converts kashida between numbers to ndash
return preg_replace('/([0-9۰-۹]+)ـ+([0-9۰-۹]+)/u', '$1–$2',
// removes all kashidas between non-whitespace characters
// MAYBE: more punctuations
preg_replace('/([^\s.])\x{0640}+(?![\s.])/u', '$1', $text)
);
}
protected function fixPunctuationSpacing($text)
{
// removes space before punctuations
return preg_replace('/[ \t\x{200c}]*([:;,؛،.؟?!]{1})/u', '$1',
// removes more than one space after punctuations
// except followed by new-lines (or preservers)
preg_replace('/([:;,؛،.؟?!]{1})[ \t\x{200c}]*(?!\n|_{2})/u', '$1 ',
// removes space after colon that separates time parts
preg_replace('/([0-9۰-۹]+):\s+([0-9۰-۹]+)/', '$1:$2',
// removes space after dots in numbers
preg_replace('/([0-9۰-۹]+)\. ([0-9۰-۹]+)/', '$1.$2',
// removes space before common domain tlds
preg_replace('~([\w\-_]+)\. (ir|com|org|net|info|edu|me)([\s/\])»:;.])~', '$1.$2$3',
// removes space between different/same marks (double-check)
preg_replace('/([؟?!])([ ]+)(?=[؟?!])/', '$1', $text)
)
)
)
)
);
}
protected function fixBracesSpacing($text)
{
$replacement = ' $1$2$3 ';
// removes inside spaces and more than one outside
// for `()`, `[]`, `{}`, `“”` and `«»`
return preg_replace("/[ \t\x{200c}]*(\()\s*([^)]+?)\s*?(\))[ \t\x{200c}]*/u", $replacement,
preg_replace("/[ \t\x{200c}]*(\[)\s*([^\]]+?)\s*?(\])[ \t\x{200c}]*/u", $replacement,
preg_replace("/[ \t\x{200c}]*(\{)\s*([^}]+?)\s*?(\})[ \t\x{200c}]*/u", $replacement,
preg_replace("/[ \t\x{200c}]*(“)\s*([^”]+?)\s*?(”)[ \t\x{200c}]*/u", $replacement,
preg_replace("/[ \t\x{200c}]*(«)\s*([^»]+?)\s*?(»)[ \t\x{200c}]*/u", $replacement, $text)
)
)
)
);
}
protected function fixBracesSpacingInside($text)
{
$replacement = '$1$2$3';
// removes inside spaces for `()`, `[]`, `{}`, `“”` and `«»`
return preg_replace("/(\()\s*([^)]+?)\s*?(\))/u", $replacement,
preg_replace("/(\[)\s*([^\]]+?)\s*?(\])/u", $replacement,
preg_replace("/(\{)\s*([^}]+?)\s*?(\})/u", $replacement,
preg_replace("/(“)\s*([^”]+?)\s*?(”)/u", $replacement,
preg_replace("/(«)\s*([^»]+?)\s*?(»)/u", $replacement,
// NOTE: must be here, weird not working if on `markdownNormalizeBraces()`
// removes Markdown link spaces inside normal ()
preg_replace("/(\(\[.*?\]\(.*?\))\s+(\))/u", '$1$2', $text)
)
)
)
)
);
}
protected function markdownNormalizeBraces($text)
{
// removes space between ! and opening brace on markdown images
// EXAMPLE: `! [alt] (src)` --> `![alt](src)`
return preg_replace("/! (\[.*?\])[ ]?(\(.*?\))[ ]?/", '!$1$2',
// remove spaces between [] and ()
// EXAMPLE: `[text] (link)` --> `[text](link)`
preg_replace("/(\[.*?\])[ \t]+(\(.*?\))/", '$1$2',
// removes spaces inside double () [] {}
// EXAMPLE: `[[ text ]]` --> `[[text]]`
preg_replace("/\(\([ \t]*(.*?)[ \t]*\)\)/", '(($1))',
preg_replace("/\[\[[ \t]*(.*?)[ \t]*\]\]/", '[[$1]]',
preg_replace("/\{\{[ \t]*(.*?)[ \t]*\}\}/", '{{$1}}',
preg_replace("/\{\{\{[ \t]*(.*?)[ \t]*\}\}\}/", '{{{$1}}}', // mustache escape
// removes spaces between double () [] {}
// EXAMPLE: `[[text] ]` --> `[[text]]`
preg_replace("/(\(\(.*\))[ \t]+(\))/", '$1$2',
preg_replace("/(\[\[.*\])[ \t]+(\])/", '$1$2',
preg_replace("/(\{\{.*\})[ \t]+(\})/", '$1$2', $text)
)
)
)
)
)
)
)
);
}
protected function markdownNormalizeLists($text)
{
// removes extra line between two items list
return preg_replace('/((\n|^)\*.*?)\n+(?=\n\*)/', '$1',
preg_replace('/((\n|^)-.*?)\n+(?=\n-)/', '$1',
preg_replace('/((\n|^)#.*?)\n+(?=\n#)/', '$1', $text)
)
);
}
protected function fixMiscSpacing($text)
{
// removes space before parentheses on misc cases
return preg_replace('/ \((ص|عج|س|ع|ره)\)/u', '($1)',
// removes space before braces containing numbers
preg_replace('/ \[([0-9۰-۹]+)\]/u', '[$1]', $text)
);
}
protected function fixDiacritics($text)
{
// cleans zwnj before diacritic characters
return preg_replace('#\x{200c}([' . $this->charsDiacritic . '])#u', '$1',
// cleans more than one diacritic characters
// props @languagetool-org
preg_replace('#(.*)([' . $this->charsDiacritic . ']){2,}(.*)#u', '$1$2$3',
// clean spaces before diacritic characters
preg_replace('#(\\S)[ ]+([' . $this->charsDiacritic . '])#u', '$1$2', $text)
)
);
}
protected function removeDiacritics($text)
{
// removes all diacritic characters
return preg_replace('/[' . $this->charsDiacritic . ']+/u', '', $text);
}
protected function cleanupSpacing($text)
{
// replaces more than one space with just a single one
// except before/after preservers and before new-lines
// .replace(/(?<![_]{2})([ ]{2,})(?![_]{2}|\n)/g, ' ') // WORKS: using lookbehind
return preg_replace('/([^_])([ ]{2,})(?![_]{2}|\n)/u', '$1 ',
// cleans whitespace/zwnj between new-lines
// @REF: https://stackoverflow.com/a/10965543/
preg_replace('/\n[\s\x{200c}]*\n/u', "\n\n", $text)
);
}
protected function cleanupLineBreaks($text)
{
// cleans more than two contiguous line-breaks
return preg_replace('/\n{2,}/u', "\n\n", $text);
}
protected function cleanupBeginAndEnd($text)
{
// removes space/tab/zwnj/nbsp from the beginning of the new-lines
return preg_replace('/([\n]+)[ \t\x{200c}\x{00a0}]*/u', '$1',
// remove spaces, tabs, zwnj, direction marks and new lines from
// the beginning and end of text
// @REF: http://stackoverflow.com/a/38490203
preg_replace('/^[\s\x{200c}\x{200e}\x{200f}]+|[\s\x{200c}\x{200e}\x{200f}]+$/u', '', $text)