Hi All,
I'm attempting to write a simple tool for transliterating Russian to English. I have the following structure:
The implementation of the function cyrillic_to_roman seems ugly to me (especially the conversion of x to its unicode encoding ch). Is there a way to clean this up? The character_map is a dictionary mapping cyrillic characters to the roman variants.
Thank you kindly.
I'm attempting to write a simple tool for transliterating Russian to English. I have the following structure:
Code:
character_map = {
...
}
def cyrillic_to_roman(x):
"""
Given a cyrillic character, returns the corresponding roman character.
"""
ch = x.encode('utf-8')
if character_map.has_key(ch):
return character_map[ch]
else:
return ch
def transliterate(text):
"""
Transliterates the given string from cyrillic to roman characters.
"""
romanized = [cyrillic_to_roman(ch) for ch in text]
return "".join(romanized)
The implementation of the function cyrillic_to_roman seems ugly to me (especially the conversion of x to its unicode encoding ch). Is there a way to clean this up? The character_map is a dictionary mapping cyrillic characters to the roman variants.
Thank you kindly.