Updated Al Bhed – English translator

The suggestion I received won’t work for the translator, it turns out. My friend pointed out the maketrans() and translate() string methods, which I didn’t know about, but those wouldn’t allow me to skip the sections of text that the translator is supposed to leave alone (Al Bhed leaves proper names be, don’t you know?).

But I did switch to building the dictionary within the code instead of either accessing an external file or defining the whole thing explicitly. So now I enter the lowercase of each and let the program add the caps and build the dicts itself.

I also realized that in the parsed version of the input, the key for each substring to be translated is always even & the key for each substring to leave alone is always odd, so I simplified from a {int: list of int, str} to a {int: str} dict and check the key’s modulus (remainder) over 2 to tell whether to translate. The updated file is in the same place; here’s the new code.

Edit: One more update — for x in y iteration rather than a while loop in the parsing function. It’s a teeny bit cleaner, I think.

''' Al Bhed/English "Translator"
Python 3.3
by vyh, updated 4/1/2013, created 3/31/2013 '''

def buildDictionary():
    e, ab = 'abcdefghijklmnopqrstuvwxyz', 'ypltavkrezgmshubxncdijfqow'
    e += e.upper()
    ab += ab.upper()
    engToAlBhed, alBhedToEng = {}, {}
    for i in range(len(e)):
        engToAlBhed[e[i]] = ab[i]
        alBhedToEng[ab[i]] = e[i]
    return (engToAlBhed, alBhedToEng)

def parseInput(s):
    parsedS, j, n = {0: ''}, 0, len(s)
    for i in range(n):
        if s[i]=='~':
            if i+1<n:
                j += 1
                parsedS[j] = ''
        parsedS[j] += s[i]
    return parsedS

def translate(text,dictionary):
    text = parseInput(text)
    translation = ''
    for key in range(len(text)):
        noTranslate, subString = key%2, text[key]
        if noTranslate:
            translation += subString
            for char in subString:
                if char in dictionary:
                    translation += dictionary[char]
                    translation += char
    return translation

if __name__ == '__main__':
    dictionary = {}
    dictionary[1], dictionary[2] = buildDictionary()
    while True:
        c = int(input('Welcome to the Al Bhed Translator! 
                    Your choices are:\n'\
                    '1. English to Al Bhed\n'\
                    '2. Al Bhed to English\n'\
                    'Which would you like to use (1 or 2)? '))
        text = input('Thank you! Enter the text to translate 
                    below. \n'\
                    'If a part is already translated, 
                    put a ~ before and after it.\n')
        print("Original text: ", text)
        print("Translated text: ", translate(text,dictionary[c]))
        if input('Translate more? y/n: ')!='y':

input('Press the enter key when you want to exit the program.')

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s