aid*_*eno 6 python language-translation
正如您将在下面的代码中看到的那样,我已经制作了一个程序,将英语翻译成Pig Latin.它遵循两个规则:
我知道这是一个奇怪的方法,但只是幽默我.
问题:当翻译成英文时,我想不出一种方法让代码知道它应该将原始单词的根与后缀分开,因为有些单词以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
Run Code Online (Sandbox Code Playgroud)
提前致谢!
小智 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
Run Code Online (Sandbox Code Playgroud)