I want to make a simple Python script that will map each Arabic letter to phoneme sound symbols. I have a file that has a bunch of words that the script will read to convert them to phonemes, and I have the following dictionary in my code:
Content in my .txt
file:
السلام عليكم
السلام عليكم و رحمة الله
السلام عليكم و رحمة الله و بركاته
الحمد لله
كيف حالك
كيف الحال
The dictionary in my code:
ar_let_phon_maplist = {u'ﺍ':'A:', u'ﺏ':'B', u'ﺕ':'T', u'ﺙ':'TH', u'ﺝ':'J', u'ﺡ':'H', u'ﺥ':'KH', u'ﻩ':'H', u'ﻉ':'(ayn) ’', u'ﻍ':'GH', u'ﻑ':'F', u'ﻕ':'q', u'ﺹ':u'ṣ', u'ﺽ':u'ḍ', u'ﺩ':'D', u'ﺫ':'DH', u'ﻁ':u'ṭ', u'ﻙ':'K', u'ﻡ':'M', u'ﻥ':'N', u'ﻝ':'L', u'ﻱ':'Y', u'ﺱ':'S', u'ﺵ':'SH', u'ﻅ':u'ẓ', u'ﺯ':'Z', u'ﻭ':'W', u'ﺭ':'R'}
I have a nested loop where I'm reading each line, converting each character:
with codecs.open(sys.argv[1], 'r', encoding='utf-8') as file:
lines = file.readlines()
line_counter = 0
for line in lines:
print "Phonetics In Line " + str(line_counter)
print line + " ",
for word in line:
for character in word:
if character == '\n':
print ""
elif character == ' ':
print " "
else:
print ar_let_phon_maplist[character] + " ",
line_counter +=1
And this is the error I'm getting:
Phonetics In Line 0
السلام عليكم
Traceback (most recent call last):
File "grapheme2phoneme.py", line 25, in <module>
print ar_let_phon_maplist[character] + " ",
KeyError: u'\u0627'
And then I checked if the file type is UTF-8 using the Linux command:
file words.txt
The output I got:
words.txt: UTF-8 Unicode text
Any solution for this problem, why it's not mapping to an Unicode object that is in the dictionary since also the character I'm using as key in ar_let_phon_maplist[character]
line is Unicode?
Is there something wrong with my code?