-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arabic.php
155 lines (128 loc) · 5.86 KB
/
Arabic.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
<?php
declare(strict_types=1);
namespace VPremiss\Arabicable;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use VPremiss\Arabicable\Enums\ArabicLinguisticConcept;
use VPremiss\Arabicable\Enums\ArabicSpecialCharacters;
use VPremiss\Arabicable\Enums\CommonArabicTextType;
use VPremiss\Arabicable\Facades\ArabicFilter;
use VPremiss\Arabicable\Models\ArabicPlural;
use VPremiss\Arabicable\Models\CommonArabicText;
use VPremiss\Arabicable\Support\Concerns;
use VPremiss\Crafty\Facades\CraftyPackage;
// TODO consider some python pyarabic package methods and consts maybe?
class Arabic
{
use Concerns\HandlesNumerals;
use Concerns\HandlesPunctuation;
use Concerns\HandlesSpaces;
public function removeHarakat(string $text): string
{
return strtr(
$text,
array_fill_keys(
ArabicSpecialCharacters::Harakat->get(),
'',
),
);
}
public function normalizeHuroof(string $text): string
{
$huroof = ['أ', 'ى', 'إ', 'ٕ ', 'ﭐ', 'ﭑ'];
$text = strtr($text, array_fill_keys($huroof, 'ا'));
$huroof = ['ﭒ', 'ﭓ', 'ﭔ', 'ٕﭕ', 'ﭖ'];
$text = strtr($text, array_fill_keys($huroof, 'ب'));
$huroof = ['ئ', 'ؤ', 'آ'];
$text = strtr($text, array_fill_keys($huroof, 'ء'));
return $text;
}
public function getSingulars(string|array $plurals, bool $uniqueFiltered = true): array
{
// ? Ensure $plurals is an array
$plurals = Arr::wrap($plurals);
// ? Cache all plurals as they're not updated much often
$cacheKey = CraftyPackage::getConfiguration('arabicable.arabic_plural.cache_key');
$allPlurals = Cache::rememberForever($cacheKey, fn () => ArabicPlural::query()->get());
// ? Filter plurals for search first
$filteredPlurals = collect($plurals)
->map(fn ($plural) => ArabicFilter::forSearch($plural))
->toArray();
// ? Get the singulars only now
$singulars = $allPlurals
->whereIn(ar_searchable('plural'), $filteredPlurals)
->pluck('singular');
return $uniqueFiltered ? $singulars->unique()->toArray() : $singulars->toArray();
}
public function getPlurals(string|array $singulars, bool $uniqueFiltered = true): array
{
// ? Ensure $singulars is an array
$singulars = Arr::wrap($singulars);
// ? Cache all plurals as they're not updated much often
$cacheKey = CraftyPackage::getConfiguration('arabicable.arabic_plural.cache_key');
$allPlurals = Cache::rememberForever($cacheKey, fn () => ArabicPlural::query()->get());
// ? Filter singulars for search first
$filteredSingulars = collect($singulars)
->map(fn ($singular) => ArabicFilter::forSearch($singular))
->toArray();
// ? Get the plurals only now
$plurals = $allPlurals
->whereIn(ar_searchable('singular'), $filteredSingulars)
->pluck('plural');
return $uniqueFiltered ? $plurals->unique()->toArray() : $plurals->toArray();
}
// TODO implement only and except approach
public function removeCommons(string|array $words, array $excludedTypes = [], bool $asString = false): string|array
{
$baseCacheKey = CraftyPackage::getConfiguration('arabicable.common_arabic_text.cache_key');
$types = CommonArabicTextType::cases();
// ? Cache each type of common text separately and store in an associative array
$cachedCommonTexts = [];
foreach ($types as $type) {
$typeKey = $baseCacheKey . '.' . $type->value;
$cachedCommonTexts[$type->value] = Cache::rememberForever($typeKey, function () use ($type) {
return CommonArabicText::where('type', $type->value)
->get()
->sortByDesc(fn ($model) => mb_strlen($model->{ar_searchable('content')}));
});
}
// ? Ensure processing as a string
$wordsSentence = is_string($words) ? $words : implode(' ', $words);
// ? Filter using an array though
$originalWordsArray = explode(' ', $wordsSentence);
$filteredWordsArray = array_map(fn ($word) => ArabicFilter::forSearch($word), $originalWordsArray);
// ? Process and remove common texts except the excluded types
foreach ($cachedCommonTexts as $type => $texts) {
if (in_array(CommonArabicTextType::from($type), $excludedTypes)) {
continue;
}
foreach ($texts as $model) {
$pattern = '/\b' . preg_quote($model->{ar_searchable('content')}, '/') . '\b/u';
foreach ($filteredWordsArray as $index => $word) {
if (preg_match($pattern, $word)) {
$originalWordsArray[$index] = '|'; // ? Using a special splitting mark
}
}
}
}
// ? Normalize spaces, split into sentences, and get rid of singular left-over letters
$wordsSentence = implode(' ', $originalWordsArray);
$wordsSentence = self::normalizeSpaces($wordsSentence);
$sentences = array_filter(preg_split('/\s*\|\s*/', $wordsSentence), fn ($sentence) => mb_strlen(trim($sentence)) > 1);
if ($asString) {
return implode(' ', $sentences);
} else {
return $sentences;
}
}
public function clearConceptCache(ArabicLinguisticConcept $concept): void
{
$cacheType = $concept === ArabicLinguisticConcept::Plurals ? 'arabic_plural' : 'common_arabic_text';
$baseCacheKey = CraftyPackage::getConfiguration("arabicable.$cacheType.cache_key");
$types = CommonArabicTextType::cases();
foreach ($types as $type) {
$typeKey = $baseCacheKey . '.' . $type->value;
Cache::forget($typeKey);
}
}
}