Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating Pig Latin into English Using Python 3

As you will see in my code below, I have already made a program that translates from English into Pig Latin. It follows two rules:

  • If the word begins with a vowel, "way" should be appended (example: apple becomes appleway)
  • If the word begins with a sequence of consonants, this sequence should be moved to the end, prefixed with "a" and followed by "ay" (example: please becomes easeaplay)

I know this is an odd way to do it, but just humour me.

The issue: when translating into English, I can't think of a way to let the code know at exactly which point it should separate the root of the original word from the suffix because some words begin with 1 consonant, others with 2 consonants, etc.

Any help would be appreciated. Please bear in mind that I am a novice.

vowels = ('AEIOUaeiou')

def toPigLatin(s):
    sentence = s.split(" ")
    latin = ""
    for word in sentence:
        if word[0] in vowels:
            latin += word + "way" + " "
        else:
            vowel_index = 0
            for letter in word:
                if letter not in vowels: 
                    vowel_index += 1
                    continue
                else: 
                    break
            latin += word[vowel_index:] + "a" + word[:vowel_index] + "ay" + " "
    return latin[:len(latin) - 1]

def toEnglish(s):
    sentence = s.split(" ")
    english = ""
    for word in sentence:
        if word[:len(word) - 4:-1] == 'yaw':
            english += word[:len(word) - 3] + " "
        else: 
            #here's where I'm stuck
    return english

Thanks in advance!

like image 269
aidandeno Avatar asked Mar 31 '14 14:03

aidandeno


People also ask

How do you translate Pig Latin to English?

Pig Latin is a pseudo-language or argot where we use a formal technique altering English words. The basic rule is to switch the first consonant or consonant cluster to the end of the term and then adding suffix “ay” to form a new word. For instance, the word 'pig' would become igp+ay which becomes igpay.

How do you translate code in Python?

To translate the text from one language to another using Python, we are going to use a package named googletrans that has been called internally to the Google Translate API. However, we do not need to worry about the internal calling of the API; instead, we can go ahead and directly use the package.

What is Pig Latin in Python?

Pig Latin is loosely defined as taking the first letter of each word, putting it at the end of the word, and adding "ay" to the end of each word. I can't figure out how to separate the first letter from each word in the string much less add it to the end.


2 Answers

Kevin wis right about completely unambiguous translation being impossible, but I think this is probably what you're looking for:

def toEnglish(s):
    sentence = s.split(" ")
    english = ""
    for word in sentence:
        if word[:len(word) - 4:-1] == 'yaw':
            english += word[:len(word) - 3] + " "
        else: 
            noay = word[:len(word) - 2]
            firstconsonants = noay.split("a")[-1]
            consonantsremoved = noay[:len(noay) - (len(firstconsonants)+1)]
            english += firstconsonants + consonantsremoved + " "
    return english
like image 111
austin-schick Avatar answered Nov 06 '22 07:11

austin-schick


Although this will not succeed for every pig latin word, as Kevin and ghoti have noted, you can get pretty close by looking for that special 'a' you are adding.

First, get rid of the extra 'ay' at the end, since it will throw off the search for your special 'a':

>>> tmp = word[:-2]
>>> tmp
'easeapl'

Then, use find or index in order to get the location of that special 'a'. Note that it will search from the beginning of the string, so you should reverse the string temporarily:

>>> tmp = tmp[::-1]
>>> tmp
'lpaesae'
>>> ind = tmp.index('a')
>>> ind
2

Now, take the part to the left of that index and the part to the right of that index, reverse each of them, and reassemble. These slices start to get pretty tricky, so bear with me:

>>> prefix = tmp[ind-1::-1]
>>> prefix
'pl'
>>> suffix = tmp[:ind:-1]
>>> suffix
'ease'
>>> english = prefix + suffix
>>> english
'please'

For some more info on slicing, see this great answer.

like image 40
2rs2ts Avatar answered Nov 06 '22 07:11

2rs2ts