HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Translating from English to Entean

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
enteanfromtranslatingenglish

Problem

I wrote a python script that converts English to Entean language from an anime called "The Devil is a part timer". I am sloppy at coding and know that the script can be made better.

In the anime, the language in Ente Isla is called Entean, which is basically most of the consonants in the English alphabet switched around. Vowels and the consonants 'L', 'N', 'Q' keep their original position. ABCDEFGHIJKLMNOPQRSTUVWXYZ is changed to AZYXEWVTISRLPNOMQKJHUGFDCB.
For example, 'human' is 'tupan', and 'world' becomes 'foklx'.

Here is the code:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".lower()
switched = "AZYXEWVTISRLPNOMQKJHUGFDCB".lower()

usr_txt = input("Enter Englsih text:")

print(usr_txt)

def get_letter(eng_letter):
    if alphabet.find(letter):
        if eng_letter.isupper():
            alpha_place = alphabet.find(letter.lower())
            return(switched[alpha_place].upper())
        else:
            alpha_place = alphabet.find(letter.lower())
            return(switched[alpha_place])
    else:
        return(eng_letter)

ent_word = ""
ent_output = ""

for word in usr_txt.split(" "): 
    ent_word = ""
    for letter in word:
        ent_letter = get_letter(letter)
        ent_word += ent_letter
    ent_output += " " + ent_word

print(ent_output.strip())


If you interested the updated code can be found at https://github.com/Logmytech/English-to-Entean

Solution

Use Dictionaries

We are basically just mapping from one letter to the next. The most naturally way to do that is to actually use a map:

to_entean = {...}


The easiest way to build up the dictionary is to use zip():

to_entean = {eng: ent
    for eng, ent in zip(string.ascii_uppercase, 'AZYXEWVTISRLPNOMQKJHUGFDCB')
    }


Now to_entean['A'] == 'A' and to_entean['B'] == 'Z', etc. This simplifies your get_letter function to:

def get_letter(letter):
    ent = to_entean.get(letter.upper(), letter)
    if not letter.isupper():
        ent = ent.lower()
    return ent


String Concatentation

String concatenation is slow. Also it reads terribly. What we want to do is call get_letter for every letter in word and then join them all together. That's what join() is for:

ent_word = ''.join(get_letter(letter) for letter in word)


Why split?

Since get_letter() handles spaces already, we don't have to split into words. Just transform the whole thing:

print(''.join(get_letter(letter) for letter in usr_txt))


Which, if you prefer, can also be spelled:

print(''.join(map(get_letter, usr_txt)))


Typo

You have a typo here:

usr_txt = input("Enter Englsih text:")
                       ^^^^^^^

Code Snippets

to_entean = {...}
to_entean = {eng: ent
    for eng, ent in zip(string.ascii_uppercase, 'AZYXEWVTISRLPNOMQKJHUGFDCB')
    }
def get_letter(letter):
    ent = to_entean.get(letter.upper(), letter)
    if not letter.isupper():
        ent = ent.lower()
    return ent
ent_word = ''.join(get_letter(letter) for letter in word)
print(''.join(get_letter(letter) for letter in usr_txt))

Context

StackExchange Code Review Q#111814, answer score: 17

Revisions (0)

No revisions yet.