Search code examples
python-3.xdictionarydiacriticsarabic-support

How to make a dictionary that contains an Arabic diacritic as a key in python


I am trying to make a program that converts the Arabic diacritics and letters into the Latin script. The letters work well in the program, but the diacritics can not be converted as I get an error every time I run the program.

At the beginning, I put the diacritics alone as keys but that did not work with me. please, see the last key, it contains َ ,which is a diacritic, but do not work properly as the letters:

def convert(lit):
    ArEn = {'ا':'A', 'ل':'L', "و": "W", "َ":"a"}
    end_word=[]
    for i in range(len(lit)):
        end_word.append(ArEn[lit[i]])
        jon = ""

    print(jon.join(end_word))

convert("الوَ")

However, I tried to fix the problem by using letters attached with diacritics as keys, but the program resulted in the same error:

the dictionary:

ArEn = {'ا':'A', 'ل':'L', "وَ":"Wa"}

the error:

    Traceback (most recent call last):
  File "C:\Users\Abdulaziz\Desktop\converter AR to EN SC.py", line 10, in <module>
    convert("الوَ")
  File "C:\Users\Abdulaziz\Desktop\converter AR to EN SC.py", line 5, in convert
    end_word.append(ArEn[lit[i]])
KeyError: 'و'

Solution

  • Update: I just noticed, after years, that the letters and diacritics are put together in the first try. When I separated them, the program worked.

    I just solved the problem! I am not really sure if it is a mistake in python or something else, but as far as I know python does not support Arabic very well. Or maybe I made a problem in the program above.

    I kept writing the same program and suddenly it worked very well. I even added different diacritics and they worked properly.

        def convert(lit):
        ArEn = {'ا':'A', 'ل':'L', "و":"W", "َ":"a", "ُ":"w", "":""}
        end_word=[]
        for i in range(len(lit)):
            end_word.append(ArEn[lit[i]])
            jon = ""
    
        print(jon.join(end_word))
    
    convert("اُلوَ")
    

    the reult is

    AwLWa