Translate 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 to Pig Latin. It follows two rules:

  • If the word begins with a vowel, add โ€œpathโ€ (for example: an apple becomes an apple).
  • If a word begins with a consonant sequence, this sequence should be moved to the end with the prefix "a", followed by "ay" (example: please ease easelay).

I know this is a weird way to do this, but just humorously me.

Problem: when translating into English, I canโ€™t think of a way for the code to know exactly at what point it should separate the root of the source word from the suffix, because some words begin with 1 consonant, others with 2 consonants, etc.

Any help would be greatly appreciated. Please keep in mind that I'm new.

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 where I'm stuck return english 

Thanks in advance!

+6
source share
5 answers

Kevin is right almost completely an unambiguous translation is impossible, but I think this is probably what you are 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 
+2
source

Although this will not work out for every Latin word for a pig, as Kevin and Gotti have noted, you can come close to this special 'a' that you add.

First, get rid of the extra 'ay' at the end, as it will drop your search for your special 'a' :

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

Then use find or index to get the location of this special 'a' . Note that it will search from the beginning of the line, so you should temporarily change the line:

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

Now, take the part to the left of this index and the part to the right of this index, cancel each of them and build. These pieces 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 more information about the cut, see this excellent answer .

+1
source
 def toEnglish(s): sentence = s.split(" ") english = "" for word in sentence: if word[:len(word) - 4:-1] == 'yaw': english += word[:len(word) - 3] + " " else: index = -3 # count consonent between two last a for letter in reversed(word[:-3]): if letter is not 'a': index-=1 else: break english += word[index:-2] + word[:index-1] + " " return english 

Due to the trick :)

0
source

I see your problem.

the strap -> apstray seems pretty simple. But according to your rules, he can go to rap or traps.

Perhaps one way to do this is to use opportunities until you find a word that matches the word in English. You can make a list of words:

 with open("/usr/share/dict/words","r") as f: wordlist = f.read().splitlines() 

Then in toEnglish go through each iteration of the mutation (add if word in wordlist: to check each of the raps, traps, etc.). and settle for the word if it's on the list.

This method is guaranteed to fail in words such as โ€œbelt.โ€ :-) YMMV.

0
source

Ok, for me, I just do this:

 word = "python" word = word[1:len(word)] + word[0] + "ay" 
0
source

All Articles