使用Python 3将Pig Latin转换为英语 [英] Translating Pig Latin into English Using Python 3
问题描述
正如您将在下面的代码中看到的那样,我已经制作了一个将英语翻译成Pig Latin的程序.它遵循两个规则:
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:
- 如果单词以元音开头,则应添加"way"(例如:apple成为appleway)
- 如果单词以辅音序列开头,则应将此序列移到末尾,以"a"开头,后跟"ay"(例如:请成为easyaplay)
我知道这是一种奇怪的方法,但是只是让我感到幽默.
I know this is an odd way to do it, but just humour me.
问题:翻译成英文时,我想不出一种方法让代码确切地知道应该在哪一点上将原始词的词根与后缀分开,因为有些词以1个辅音开头,而另一些则以2个辅音,等等.
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
提前谢谢!
推荐答案
Kevin认为完全明确的翻译是不可能的,但是我认为这可能是您正在寻找的东西:
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
这篇关于使用Python 3将Pig Latin转换为英语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!