6

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!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
aidandeno
  • 307
  • 1
  • 2
  • 16
  • 6
    Completely unambiguous translation from Pig Latin into English is impossible. For instance, "oat" and "two" both translate to "oatway". – Kevin Mar 31 '14 at 15:03
  • Is this for homework? – 2rs2ts Mar 31 '14 at 15:14
  • According to the rules shown in http://en.wikipedia.org/wiki/Pig_Latin, the end consonent structure should not be prefixed with an 'a'. Thus, please should become easeplay not easeaplay. If you do not prefix the a, then win and in both become inway. No matter which rule you follow there is no unique reverse translation. Since there is no unique reverse translation, then you cannot set up an algorithm to go from Pig Latin to English with a definite result. – sabbahillel Mar 31 '14 at 15:19
  • 1
    @sabbahillel OP says "I know this is an odd way to do it, but just humour me." Obviously he is not working with standard pig latin rules! – 2rs2ts Mar 31 '14 at 15:21
  • @2rs2ts The point that I was trying to make was that no matter which set of rules he is using there is no unique translation from English to Pig Latin. As a result there cannot be a reverse translation algorithm. I could not edit the comment to make the point clearer as the time limit expired while I was typing. – sabbahillel Mar 31 '14 at 15:27
  • @sabbahillel This is true, of course, but I thought for a moment you had missed his comment about atypical rules. – 2rs2ts Mar 31 '14 at 15:36
  • @2rs2ts Actually, the modified rules are more nearly useable as the consonant cluster **must** be bound by an a so that perfect becomes erfectapay rather than erfectpay and the backward search is correct. I see that answers have already been given so no point in saying more. – sabbahillel Mar 31 '14 at 15:42
  • This is for homework. I'm not following the standard rules. – aidandeno Mar 31 '14 at 16:10

5 Answers5

2

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
austin-schick
  • 1,225
  • 7
  • 11
1

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.

Community
  • 1
  • 1
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
0
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 :)

floppy12
  • 1,045
  • 6
  • 12
  • *Yellow* becomes *ellowyay*, does it not? *Yarn* becomes *arnyay*? Y is an intermittent/irregular vowel, and as such will require more complex analysis than this. – ghoti Mar 31 '14 at 15:18
  • Fails on the example in the OP. `>>> word = "easeaplay"` `>>> word[-3:-2] + word[:-4]` `'leasea'` – 2rs2ts Mar 31 '14 at 15:20
  • depends if y is considered as vowels – floppy12 Mar 31 '14 at 15:20
  • 1
    Y is a vowel in "hyper" (yperhay), and a consonent in "yak" (akyay). – ghoti Mar 31 '14 at 15:22
  • I took great care in the wording of my question. For my example, I chose that Y not be a vowel because there is ABSOLUTELY no way for my code to know how something is pronounced. But thanks for the interest and reply. – aidandeno Mar 31 '14 at 16:23
0

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.

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • In fact strap -> apAstray (capital A for emphasis) – Steve Jessop Mar 31 '14 at 15:29
  • I should have stated that I'd prefer not to use lists, dictionaries, etc. Original code. I even compute pi, e, and trig functions using Taylor Series formulas. It speeds up my learning :) – aidandeno Mar 31 '14 at 16:25
0

Well, for me, I just do this:

word = "python"

word = word[1:len(word)] + word[0] + "ay"
C. Leung
  • 6,218
  • 3
  • 20
  • 32