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!
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
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.
I do see your problem.
strap -> apstray seems pretty straightforward. But by your rules it might reverse to rapst or traps.
Perhaps one way to do this is to cycle through possibilities until you find a word that matches a word in English. You could make a word list:
with open("/usr/share/dict/words","r") as f:
wordlist = f.read().splitlines()
Then in toEnglish
, step back through each iteration of the mutation (add a if word in wordlist:
to test each of rapst, traps, etc). and settle on a word if it's in the list.
This method is guaranteed to fail on words like "strap". :-) YMMV.
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
Should to the trick :)
Well, for me, I just do this:
word = "python"
word = word[1:len(word)] + word[0] + "ay"