I am working on a smart parking system and I have the following problem:
I have a CSV file that contains the license plate numbers of cars in different formats. I am trying to unify them but I do not know how to convert Arabic license plates into English or vise versa.
For example: "٤٦٠٤صمد"
.
I want to convert this to English using python.
Asked
Active
Viewed 181 times
-2

martineau
- 119,623
- 25
- 170
- 301
-
A Google search or PyPi could easily give you solutions on how to do this in Python: https://pypi.org/project/googletrans/ but you haven't shown any code so there is no legitimate answer that can be provided – ViaTech Dec 28 '21 at 21:00
-
Adding to @ViaTech 's comment, there is also https://pypi.org/project/deepl/ which uses deepl rather than google. I've never tried with Arabic, nor with license plates, but I find that deepl often gives better translations than google. – Stef Dec 28 '21 at 21:03
-
Although if the license plate is just a sequence of letters and numbers, it's probably better to simply use a dict that maps the Arabic characters to the English characters. See for instance [How to make a dictionary that contains an Arabic diacritic as a key in python?](https://stackoverflow.com/questions/37389694/how-to-make-a-dictionary-that-contains-an-arabic-diacritic-as-a-key-in-python) – Stef Dec 28 '21 at 21:05
-
1@Stef That would be [transliteration](https://en.wikipedia.org/wiki/Transliteration), and not translation, though. – Nick ODell Dec 28 '21 at 21:08
-
3@NickODell The question post uses the words "mapped" and "convert", not "translate" or "translation", and they're explicitly mentioning license plates, so I'm assuming that's what they want. – Stef Dec 28 '21 at 21:09
-
https://pypi.org/project/PyArabic/ might also be useful. And python's [standard library module unicodedata](https://docs.python.org/3/library/unicodedata.html) with a bit of knowledge from [Wikipedia: Arabic script in Unicode](https://en.wikipedia.org/wiki/Arabic_script_in_Unicode) – Stef Dec 28 '21 at 21:11
-
What English string would you want to get from your Arabic string `٤٦٠٤صمد`? For instance, `s = '٤٦٠٤صمد'; print( [unicodedata.name(c) for c in s] )` prints `['ARABIC-INDIC DIGIT FOUR', 'ARABIC-INDIC DIGIT SIX', 'ARABIC-INDIC DIGIT ZERO', 'ARABIC-INDIC DIGIT FOUR', 'ARABIC LETTER SAD', 'ARABIC LETTER MEEM', 'ARABIC LETTER DAL']` – Stef Dec 28 '21 at 21:14
-
For instance, ٣١٦٢لهح I want to convert it to 3162LHJ @Stef – Mariam Mostafa Dec 28 '21 at 21:27
-
@MariamMostafa `t = ' ٣١٦٢لهح'; print( [unicodedata.name(c) for c in t] )` prints `['SPACE', 'ARABIC-INDIC DIGIT THREE', 'ARABIC-INDIC DIGIT ONE', 'ARABIC-INDIC DIGIT SIX', 'ARABIC-INDIC DIGIT TWO', 'ARABIC LETTER LAM', 'ARABIC LETTER HEH', 'ARABIC LETTER HAH']`. From there, it's just a matter of processing this list of strings by dropping the first two words from each string, and mapping "HAH" to "J', 'HEH' to 'H, 'LAM' to 'L', etc – Stef Dec 28 '21 at 22:05
1 Answers
0
I think the transliteration can be performed with the unicodedata
module from the standard library.
Getting the character name: unicodedata.name
import unicodedata
alphabet = {
'ZERO': '0', 'ONE': '1', 'TWO': '2', 'THREE': '3', 'SIX': '6',
'LAM': 'L', 'HAH': 'J', 'HEH': 'H',
} # you complete this dict with all digits and letters
def arabic_to_english(license_plate):
l = [unicodedata.name(c) for c in license_plate]
return ''.join(alphabet[s.rsplit(maxsplit=1)[-1]] for s in l)
print( arabic_to_english( '٣١٦٢لهح' ) )
# 3162LHJ
Getting digits: unicodedata.digit
You can map Arabic digits directly:
print( unicodedata.digit('٢') )
# 2
Unfortunately, there doesn't appear to be a standard mapping from Arabic letters to latin letters, so it appears you're stuck with unicodedata.name
for the letters.
Writing your own dict
Or you could write the alphabet yourself directly as a dict:
alphabet = { 'ح': 'J', 'ه': 'H', } # ...
Perhaps with help from this Arabic keyboard: https://www.lexilogos.com/clavier/araby.htm

Stef
- 13,242
- 2
- 17
- 28
-
Why even bother with parsing the Unicode names? You already create a dictionary, just map each letter to its English counterpart... – Tomerikoo Dec 28 '21 at 22:31
-
@Tomerikoo That's a good point. But it assumes I already know some Arabic ;-) – Stef Dec 28 '21 at 22:45