Al Bhed-English translator code

Edit: It has been pointed out to me that I’ve made the ciphers the hard way. I’ll try the obvious way next, most of which I didn’t know was there.

So, it might be awhile before I learn to make a Python web app, which is apparently what I have to do in order to let you run the translator online (I think). So if you’d like to use it, here’s the code; the original reads the ciphers from a separate text file, but I’ve added them directly to the code for this copy so you don’t need to modify anything. You’ll need to have some version of Python 3 (not 2!) installed on your computer to run this. It’s written in Python 3.3.

Here’s the code itself (minus most comments and plus some line breaks for readability):

''' Al Bhed/English "Translator"
Python 3.3
by vyh, 3/31/2013 
Al Bhed is the language (cipher, really) of a race in Final Fantasy X.
Works in both directions. '''

def parseInput(s):
    parsedS, i, j, n = {0:[1,'']}, 0, 0, len(s)
    while i<n:
       if s[i]=='~':
            j += 1
            i += 1
            if i==n:
               break
           parsedS[j] = [flip(parsedS[j-1][0]),'']
        parsedS[j][1] += s[i]
        i += 1
    return parsedS

def flip(x):
    if x==0:
        return 1
    return 0

def translate(s,dictionary):
    s = parseInput(s)
    translation = ''
    for i in range(len(s)):
        toTranslate, subString = s[i][0], s[i][1]
        if toTranslate==1:
            for j in subString:
                if j in dictionary:
                    translation += dictionary[j]
                else:
                    translation += j
        else:
            translation += subString
    return translation

if __name__ == '__main__':
    dictionary = {1: {'e': 'a', 'd': 't', 'g': 'k', 'f': 'v', 
'a': 'y', 'c': 'l', 'b': 'p', 'm': 's', 'l': 'm', 'o': 'u', 'n': 'h', 
'i': 'e', 'h': 'r', 'k': 'g', 'j': 'z', 'u': 'i', 't': 'd', 'w': 'f', 
'v': 'j', 'q': 'x', 'p': 'b', 's': 'c', 'r': 'n', 'y': 'o', 'x': 'q', 
'z': 'w', 'E': 'A', 'D': 'T', 'G': 'K', 'F': 'V', 'A': 'Y', 'C': 'L', 
'B': 'P', 'M': 'S', 'L': 'M', 'O': 'U', 'N': 'H', 'I': 'E', 'H': 'R', 
'K': 'G', 'J': 'Z', 'U': 'I', 'T': 'D', 'W': 'F', 'V': 'J', 'Q': 'X', 
'P': 'B', 'S': 'C', 'R': 'N', 'Y': 'O', 'X': 'Q', 'Z': 'W'}, 
2: {'e': 'i', 'd': 't', 'g': 'k', 'f': 'w', 'a': 'e', 'c': 's', 
'b': 'p', 'm': 'l', 'l': 'c', 'o': 'y', 'n': 'r', 'i': 'u', 'h': 'n', 
'k': 'g', 'j': 'v', 'u': 'o', 't': 'd', 'w': 'z', 'v': 'f', 'q': 'x', 
'p': 'b', 's': 'm', 'r': 'h', 'y': 'a', 'x': 'q', 'z': 'j', 'E': 'I', 
'D': 'T', 'G': 'K', 'F': 'W', 'A': 'E', 'C': 'S', 'B': 'P', 'M': 'L', 
'L': 'C', 'O': 'Y', 'N': 'R', 'I': 'U', 'H': 'N', 'K': 'G', 'J': 'V', 
'U': 'O', 'T': 'D', 'W': 'Z', 'V': 'F', 'Q': 'X', 'P': 'B', 'S': 'M', 
'R': 'H', 'Y': 'A', 'X': 'Q', 'Z': 'J'}}
    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)? '))
        s = 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: ', s)
        print('Translated text: ', translate(s,dictionary[c]))
        if input('Translate more? y/n: ')!='y':
            break

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

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s