使用Python将Pig Latin翻译成英文3

时间:2014-03-31 14:56:30

标签: python language-translation

正如您将在下面的代码中看到的,我已经制作了一个程序,将英语翻译成Pig Latin。它遵循两个规则:

  • 如果单词以元音开头,则应附加“way”(例如:apple成为appleway)
  • 如果单词以辅音序列开头,则此序列应移至末尾,前缀为“a”,后跟“ay”(例如:请成为easeaplay)

我知道这是一种奇怪的方式,但只是幽默我。

问题:当翻译成英文时,我想不出一种方法让代码知道它应该在哪个点上将原始单词的根与后缀分开,因为有些单词以1辅音开头,其他单词用2个辅音等

任何帮助将不胜感激。请记住我是新手。

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

提前致谢!

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

虽然这对于每个猪拉丁语来说都不会成功,正如Kevin和ghoti所指出的那样,通过寻找你正在添加的特殊'a',你可以非常接近。

首先,在最后删除额外的'ay',因为它会抛弃对您的特殊'a'的搜索:

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

然后,使用findindex以获取该特殊'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'

有关切片的更多信息,请参阅此great answer

答案 2 :(得分: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

应该诀窍:))

答案 3 :(得分:0)

我确实看到了你的问题。

表带 - > apstray 似乎很简单。但根据您的规则,它可能会转向 rapst 陷阱

也许一种方法是循环查看可能性,直到找到与英语单词匹配的单词。你可以制作一个单词列表:

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

然后在toEnglish中,逐步回过每次变异的迭代(添加if word in wordlist:来测试每个 rapst 陷阱等)。并且如果它在列表中,则确定一个单词。

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

答案 4 :(得分:0)

嗯,对我来说,我只是这样做:

input = new DataInputStream(socket.getInputStream());
while(!formclosed) {
    try {
        String addtext = input.readUTF();
        addtext = formatText(addtext);
        logarea.setText(logarea.getText() + addtext);
    } catch (EOFException e) {
        System.out.println("Client has disconnected.");
        return;
    } 
}
// any other IOException should be treated as an error