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!

4

5 回答 5

2

凯文认为完全明确的翻译是不可能的,但我认为这可能是您正在寻找的内容:

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
于 2014-03-31T15:30:25.380 回答
1

正如 Kevin 和 ghoti 所指出的那样,虽然这不会对每个猪拉丁词都成功,但您可以通过查找要'a'添加的特殊词来非常接近。

首先,最后去掉多余'ay'的,因为它会抛出你的 special 搜索'a'

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

然后,使用findorindex来获取那个特殊的位置'a'。请注意,它将从字符串的开头搜索,因此您应该暂时反转字符串:

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

现在,取出该索引左侧的部分和该索引右侧的部分,将它们中的每一个反转,然后重新组合。这些切片开始变得非常棘手,所以请耐心等待:

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

有关切片的更多信息,请参阅这个很好的答案

于 2014-03-31T15:33:25.963 回答
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

应该诀窍:)

于 2014-03-31T15:14:22.540 回答
0

我确实看到了你的问题。

strap -> apstray看起来很简单。但是根据您的规则,它可能会反转为rapsttraps

也许一种方法是循环遍历各种可能性,直到找到与英语单词匹配的单词。你可以做一个单词列表:

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

然后在 中toEnglish,通过突变的每次迭代退一步(添加 aif word in wordlist:以测试每个rapsttraps等)。如果它在列表中,请确定一个词。

这种方法保证在“strap”之类的词上失败。:-) YMMV。

于 2014-03-31T15:16:13.827 回答
0

好吧,对我来说,我只是这样做:

word = "python"

word = word[1:len(word)] + word[0] + "ay"
于 2017-03-07T01:57:42.297 回答