removing Arabic Diacritic using Python

Question:

I want to filter my text by removing Arabic Diacritic using Python

for example

Text : اللَّهمَّ اغْفِرْ لنَا ولوالدِينَا
after filltring : اللهم اغفر لنا ولوالدينا

I have found that this can be done using CAMeL Tools but I am not sure how

Asked By: user15295803

||

Answers:

You can use the library pyArabic like this:

import pyarabic.araby as araby

before_filter="اللَّهمَّ اغْفِرْ لنَا ولوالدِينَا"
after_filter = araby.strip_diacritics(before_filter)

print(after_filter)
# will print : اللهم اغفر لنا ولوالدينا

You can try different stip filters:

araby.strip_harakat(before_filter)  # 'اللّهمّ اغفر لنا ولوالدينا'
araby.strip_lastharaka(before_filter)  # 'اللَّهمَّ اغْفِرْ لنَا ولوالدِينَا'
araby.strip_shadda(before_filter)  # 'اللَهمَ اغْفِرْ لنَا ولوالدِينَا'
araby.strip_small(before_filter)  # 'اللَّهمَّ اغْفِرْ لنَا ولوالدِينَا'
araby.strip_tashkeel(before_filter)  # 'اللَّهمَّ اغْفِرْ لنَا ولوالدِينَا'
araby.strip_tatweel(before_filter)  # 'اللَّهمَّ اغْفِرْ لنَا ولوالدِينَا'
Answered By: Seddik Mekki

Oneliner:

text = 'text with Arabic Diacritics to be removed'    
text = ''.join([t for t in text if t not in ['ِ', 'ُ', 'ٓ', 'ٰ', 'ْ', 'ٌ', 'ٍ', 'ً', 'ّ', 'َ']])
print(text)

if you want the full list of Arabic Diacritics you can also get it from pyArabic, standalone example:

import unicodedata
try:
    unichr
except NameError:
    unichr = chr

text = 'اللَّهمَّ اغْفِرْ لنَا ولوالدِينَا '    
text = ''.join([t for t in text if t not in [unichr(x) for x in range(0x0600, 0x06ff) if unicodedata.category(unichr(x)) == "Mn"]])
print(text)
Answered By: Nasser Al-Wohaibi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.