-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
324 lines (278 loc) · 12.7 KB
/
test.py
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
import arab_tools
import data
from data_types import NameProfile
from trans import Profile, ner_available, transliterate, transliterate_names
profile_pausa = Profile(pausa=True)
def test_separate_join():
assert arab_tools.separate("مَدِينَة") == (
["م", "د", "ي", "ن", "ة"],
[data.fatha, data.kasra, "", data.fatha, ""],
)
assert (
arab_tools.join(
["م", "د", "ي", "ن", "ة"], [data.fatha, data.kasra, "", data.fatha, ""]
)
== "مَدِينَة"
)
# muhammad
muhammad = "مُحَمَّد"
assert muhammad == arab_tools.join(
*arab_tools.separate(muhammad)
), arab_tools.separate(muhammad)
assert arab_tools.inject("ا", "الله", 3) == "اللاه"
rasm, harakat = arab_tools.separate("أَنا")
rasm.pop(-1)
harakat.pop(-1)
harakat[-1] = data.fatha
assert arab_tools.join(rasm, harakat) == "أَنَ"
def test_transliteration_robustness():
assert transliterate("") == ""
assert transliterate(" ") == ""
assert transliterate("?") == "?"
assert transliterate("؟") == "?"
assert transliterate("؟ \n") == "?"
assert transliterate("أَ") in ("ʾ", "a")
assert transliterate("آ") == "ā"
assert transliterate("ذَهَبَ إِ") in ("ḏahaba ʾ", "ḏahaba i")
assert transliterate(" َكبَر") == "akbar"
def test_alif():
assert transliterate("قُرآن") == "qurʾān"
def test_half_vowels():
assert transliterate("و") == "w"
assert transliterate("شو") == "šū"
assert transliterate("شَو") == "šaw"
assert transliterate("شُو") == "šū"
assert transliterate("شوك") == "šūk"
def test_prefixes():
# sa
# assert transliterate("سَأَكون")
# lil
assert transliterate("للامتِحان") == "li-l-imtiḥān"
# ka
assert transliterate("كَتَبَ كَمُعَلِّم") == "kataba ka-muʿallim"
assert transliterate("كاليَوم") == "ka-l-yawm"
# bil
assert transliterate("بِالْبَيت") == "bi-l-bayt"
# wa-lil
assert transliterate("وَلِالاستِشراق") == "wa-li-l-istišrāq"
def test_sun_assimilation():
# https://de.wikipedia.org/wiki/Sonnenbuchstabe
no_diphthong_tests = {
"التَعلِيم": "at-taʿlīm",
"الثَورَة": "aṯ-ṯawra",
"الدَولَة": "ad-dawla",
"الذَرَّة": "aḏ-ḏarra",
"الرَئيس": "ar-raʾīs",
"الزَيت": "az-zayt",
"السُكَّر": "as-sukkar",
"الشَمس": "aš-šams",
# "الصُندُق": "aṣ-ṣundūq", # doesn't work, sunduq is not recognized
"الضَيف": "aḍ-ḍayf",
# "الطَاوِيلَة": "aṭ-ṭāwila", # tawila is not recognized
"الظُهر": "aẓ-ẓuhr",
"اللُغَة": "al-luġa",
"النَوم": "an-nawm",
}
diphthong_tests = {
"الدَولَة": "ad-daula",
"الزَيت": "az-zait",
"الضَيف": "aḍ-ḍaif",
"النَوم": "an-naum",
}
for arab, latin in no_diphthong_tests.items():
assert transliterate(arab) == latin
for arab, latin in diphthong_tests.items():
assert transliterate(arab, Profile(diphthongs=True)) == latin
def test_ta_marbutah():
global profile_pausa
profile_tm = Profile(ta_marbutah=True)
profile_pausa_tm = Profile(pausa=True, ta_marbutah=True)
assert transliterate("المَدِينَة") == "al-madīna"
assert transliterate("المَدِينَة", profile_tm) == "al-madīnah"
assert (
transliterate("الشَجَرَةُ في الحَديقَةِ كَبيرَةٌ")
== "aš-šaǧaratu fī l-ḥadīqati kabīratun"
)
if ner_available:
assert transliterate("مَدِينَةُ القَاهِرَةِ") == "madīnatu l-Qāhirati"
assert transliterate("مَدِينَةُ القَاهِرَةِ", profile_pausa) == "madīnat al-Qāhira"
assert transliterate("مَدِينَةُ القَاهِرَةِ", profile_tm) == "madīnatu l-Qāhirati"
assert transliterate("مَدِينَةُ القَاهِرَةِ", profile_pausa_tm) == "madīnat al-Qāhirah"
assert transliterate("صَلاة") == "ṣalāh"
assert transliterate("ﻣِﺮﺁة") == "mirʾāh"
def test_diphthong():
profile = Profile(diphthongs=True)
# bayt
assert transliterate("بَيت") == "bayt"
assert transliterate("بَيت", profile) == "bait"
# dawl
assert transliterate("دَول") == "dawl"
assert transliterate("دَول", profile) == "daul"
# awwal
assert transliterate("أَوَّل") == "awwal"
assert transliterate("أَوَّل", profile) == "auwal"
def test_double_vowels():
profile_double_vowels = Profile(double_vowels=False)
# quwwah
assert transliterate("قُوَّة") == "quwwa"
assert transliterate("قُوَّة", profile_double_vowels) == "qūwa"
# niyyah
assert transliterate("نِيَّة") == "niyya"
assert transliterate("نِيَّة", profile_double_vowels) == "nīya"
def test_hamzatul_wasl():
assert transliterate("ابن") == "ibn"
assert transliterate("ابنُ") == "ibnu"
assert transliterate("اسم") == "ism"
assert transliterate("اسمُ") == "ismu"
assert transliterate("الَّذينَ") == "allaḏīna"
assert transliterate("اِنْكَسَرَ") == "inkasara"
assert transliterate("انْكَسَرَ") == "inkasara"
assert transliterate("ٱِنْكَسَرَ") == "inkasara"
assert transliterate("اسْتِشْرَاق") == "istišrāq"
assert transliterate("فَانتَقَلَ") == "fa-ntaqala" # with prefix
assert transliterate("اخرُج") == "uḫruǧ"
assert transliterate("هُم الكُتّاب") == "hum ul-kuttāb"
assert transliterate("عَن الْكِتابُ", profile_pausa) == "ʿan il-kitāb"
assert transliterate("مِنْ البَيت") == "min al-bayt"
# this looks like it ends in a vowel, but it doesn't
assert transliterate("الشَّاشَةُ اِنْكَسَرَتْ", profile_pausa) == "aš-šāša inkasarat"
assert transliterate("هَذَا احْتِمَالٌ عَظِيمٌ", profile_pausa) == "hāḏā ḥtimāl ʿaẓīm"
assert transliterate("الْمَكْتَبَةُ الْكَبِيرَةُ") == "al-maktabatu l-kabīratu"
assert transliterate("الجُندِيّ العَرَبِيّ") == "al-ǧundī al-ʿarabī"
assert transliterate("ﻓﻲ اﻟﺒَﻴﺖ") == "fī l-bayt"
def test_pronouns_and_prepositions():
assert transliterate("أَنْتَ") == "anta"
assert transliterate("أَنْتِ") == "anti"
assert transliterate("هُوَ") == "huwa"
assert transliterate("هِيَ") == "hiya"
assert transliterate("نَحْنُ") == "naḥnu"
assert transliterate("أَنْتُمْ") == "antum"
assert transliterate("هُمْ") == "hum"
assert transliterate("هُنَّ") == "hunna"
assert transliterate("لِي") == "lī"
assert transliterate("لَكَ") == "laka"
assert transliterate("لَكِ") == "laki"
assert transliterate("لَهُ") == "lahu"
assert transliterate("لَهَا") == "lahā"
assert transliterate("لَنَا") == "lanā"
assert transliterate("لَكُم") == "lakum"
assert transliterate("لَهُم") == "lahum"
assert transliterate("لَهُنَّ") == "lahunna"
def test_endings():
# shukran und taqriban
assert transliterate("شُكْرًا", profile_pausa) == "šukran"
assert transliterate("تَقْرِيباً", profile_pausa) == "taqrīban"
# waw + alif -> waw
assert transliterate("ﻗﺎﻟﻮا") == "qālū"
assert transliterate("رَأَوا") == "raʾaw"
assert transliterate("رَأَوا", Profile(diphthongs=True)) == "raʾau"
def test_nisba():
# nabi
assert transliterate("نَبِيٌّ", profile_pausa) == "nabī"
assert transliterate("نَبِيٌّ", Profile(pausa=True, double_vowels=False)) == "nabī"
assert transliterate("نَبِيٌّ", Profile(pausa=True, nisba=False)) == "nabiyy"
assert (
transliterate("نَبِيٌّ", Profile(pausa=True, nisba=False, double_vowels=False))
== "nabīy"
)
# al-arabi
assert transliterate("العَرَبِيّ", profile_pausa) == "al-ʿarabī"
assert (
transliterate("العَرَبِيّ", Profile(pausa=True, double_vowels=False)) == "al-ʿarabī"
)
assert transliterate("العَرَبِيّ", Profile(pausa=True, nisba=False)) == "al-ʿarabī"
assert (
transliterate("العَرَبِيّ", Profile(pausa=True, nisba=False, double_vowels=False))
== "al-ʿarabī"
)
def test_hu_hi():
# abuhu
assert transliterate("لِأَبُوهُ") == "li-abūhu"
assert transliterate("لِأَبُوهُ", Profile(hu_hi=False)) == "li-abūhu"
# baytuhu
assert transliterate("بَيْتُهُ") == "baytuhū"
assert transliterate("بَيْتُهُ", Profile(hu_hi=False)) == "baytuhu"
# dhahaba ilayhi
assert transliterate("ذَهَبَ إِلَيْهِ", Profile(hu_hi=False)) == "ḏahaba ilayhi"
assert transliterate("ذَهَبَ إِلَيْهِ") == "ḏahaba ilayhi"
def test_special_words():
# Allah
assert transliterate("ﷲ") == "Allāh" # ligature
assert transliterate("الله") == "Allāh"
assert transliterate("والله") == "wa-llāh"
assert transliterate("لله", profile_pausa) == "li-llāh"
assert transliterate("عَبد الله") == "ʿabd Allāh"
assert transliterate("عَبدُ الله") == "ʿabdu llāh"
assert transliterate("هُم الله") == "hum ullāh"
# ilah
assert transliterate("إِلَه") == "ilāh"
# rahman
assert transliterate("رَحْمَن") == "raḥmān"
# ana
assert transliterate("أَنَا") == "ana"
# hadha
assert transliterate("هَذَا") == "hāḏā"
assert transliterate("هَذِهِ") == "hāḏihi"
# dhalika
assert transliterate("ذَلِكَ") == "ḏālika"
assert transliterate("كَذَلِكَ") == "kaḏālika"
# hakadha
assert transliterate("هَكَذَا") == "hākaḏā"
# lakin
assert transliterate("لَكِن") == "lākin"
assert transliterate("لَكِنَّ") == "lākinna"
# ulaika
assert transliterate("أُولَئِكَ") == "ulāʾika"
assert transliterate("أُولَٰئِكَ") == "ulāʾika"
# Taha
assert transliterate("طَهَ") == "Ṭāhā"
# Ibrahim
assert transliterate("إِبْرَاهِيم") == "Ibrāhīm"
# Amr
assert transliterate("عَمرَو") == "ʿAmr"
def _test_ibrahim_text():
p = Profile(pausa=True, double_vowels=False)
with open("data/ibrahim-in.txt", encoding="utf-8") as inp, open(
"data/ibrahim-out.txt", encoding="utf-8"
) as outp:
arab = "\n".join(line for line in inp.readlines() if not line.startswith("#"))
latin = "\n".join(line for line in outp.readlines() if not line.startswith("#"))
assert transliterate(arab, p) == latin.strip()
# Still errors:
# It's acually wan-niṣf, not wālniṣf
# Ibrahim is not capitalized
assert transliterate("براون آند كو", Profile(diphthongs=True)) == "brāun ānd kū"
def test_names():
tests = {
# "عَيْنُ التَّابْغَةِ": "ʿAyn at-Tābġa",
"النِّيلُ": "an-Nīl",
"ابْنُ بَطّوطَةَ": "Ibn Baṭṭūṭa",
"أَبُو عَبْدِ الله مُحَمَّدٌ ابْنُ بَطّوطَةَ": "Abū ʿAbdallāh Muḥammad ibn Baṭṭūṭa",
"أَبو حامِد مُحَمَّد الغَزّالِي": "Abū Ḥāmid Muḥammad al-Ġazzālī",
"كِتاب الأَغاني لِلإِمام أَبي الفَرَج": "Kitāb al-Aġānī li-l-Imām Abī l-Faraǧ",
}
for arab, latin in tests.items():
assert transliterate_names(arab) == latin
assert transliterate_names("أَبُو بَكْر") == "Abū Bakr"
assert (
transliterate_names(
"أَبُو عَبْدِ الله مُحَمَّدٌ ابْنُ بَطّوطَةَ", profile=NameProfile(short_ibn=True)
)
== "Abū ʿAbdallāh Muḥammad b. Baṭṭūṭa"
)
assert (
transliterate_names("ابْنُ بَطّوطَةَ", profile=NameProfile(short_ibn=True))
== "Ibn Baṭṭūṭa"
)
assert (
transliterate_names(
"كِتاب الأَغاني لِلإِمام أَبي الفَرَج", profile=NameProfile(is_book=True)
)
== "Kitāb al-Aġānī li-l-imām abī l-faraǧ"
)
if __name__ == "__main__":
# test_prepositions()
# test_transliteration_robustness()
# test_ibrahim_text()
# test_hamzatul_wasl()
print(transliterate("اسْتِشْرَاقٌ"))