Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 把句子译成拉丁语_Python - Fatal编程技术网

Python 把句子译成拉丁语

Python 把句子译成拉丁语,python,Python,所以我的任务是把多个单词翻译成拉丁语。假设用户总是输入小写字母,并且只输入字母和空格 #----------------global variables sentence = input("What do you want to translate into piglattin? ") sentence = list(sentence) sentence.insert(0, ' ') length = len(sentence) sentence.append(' ') pigLattin =

所以我的任务是把多个单词翻译成拉丁语。假设用户总是输入小写字母,并且只输入字母和空格

#----------------global variables
sentence = input("What do you want to translate into piglattin? ")
sentence = list(sentence)
sentence.insert(0, ' ')
length = len(sentence)
sentence.append(' ')
pigLattin = sentence
false = 0
true = 1
consonant = []

a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
j = 0
x = 0
y = 0


#----------------main functions
def testSpace(sentence, i):
    if sentence[i] == ' ':
        a = true
    else:
        a = false
    return a

def testVowel(sentence, i):
    if sentence[i] == 'a' or sentence[i] == 'e' or sentence[i] == 'i' or sentence[i] == 'o' or sentence[i] == 'u' or sentence[i] == 'y':
        b = true
    else:
        b = false
    return b

def testStartWord(sentence, i):
    x = 0
    if sentence[i].isalpha() and sentence[i-1] == ' ':
        c = true
        x = 1
    if x == 1 and sentence[i] != 'a' and sentence[i] != 'e' and sentence[i] != 'i' and sentence[i] != 'o' and sentence[i] != 'u' and sentence[i] != 'y':
        c = true
    else:
        c = false
    return c
def testWordEnd(sentence, i):
    if sentence[i].isalpha() and sentence[i+1] == ' ':
        d = true
    else:
        d = false
    return d


#----------------main loop
for i in range(1,length):
    x = 0
    space = testSpace(sentence, i)
    vowel = testVowel(sentence, i)
    word = testStartWord(sentence, i)
    end = testWordEnd(sentence, i)

    if vowel == false and space == false and word == true:
        e = i
        consonant.append(sentence[i])
        pigLattin.pop(e)
        f = f + 1

    if end == true:
        consonant.append('a')
        consonant.append('y')
        consLength = len(consonant)

        for x in range(consLength):
            y = i + j - f
            pigLattin.insert(y,consonant[x])
            j = j + 1

        del consonant[:]

pigLength = len(pigLattin)   
for b in range (pigLength):
    print(pigLattin[b], end='')   
这就是我目前所拥有的。当尝试删除项目时,会变得有点混乱。我有点困在这里,它不工作

好的,我现在开始工作了这是一个更新版本

sentence = input("Please enter a sentence: ")
vowels = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
words =  sentence.split()

count = 0

def find_vowel(word):
    for i in range(len(word)):
       if word[i] in vowels:
         return i
    return -1

for word in words:
  vowel = find_vowel(word)

  if(vowel == -1):
    print(word, ' ', end='')

  elif(vowel == 0):
    print(word + "ay", ' ', end='')

  else:
    print(word[vowel:] + word[:vowel] + "ay", ' ', end='')

不要使用
testSpace
而是使用
语句=句子.split()
来消除空格。这将把您的所有单词拆分为列表中的字符串。然后反复浏览列表中的单词


不要使用
testStartWord
,而是使用if语句:

for word in sentence:
  if word[0] in ["a","e","i","o","u"]:
    word[:(len(word)-1)] = word[0]
  #More Code...

最后,在打印输出的地方,使用
打印句子.join()。我使用正则表达式在输入字符串中查找单词,将它们传递给回调函数,然后将它们替换回原始字符串。这允许我保留数字、间距和标点符号:

import re
import sys

# Python 2/3 compatibility shim
inp = input if sys.hexversion >= 0x3000000 else raw_input

VOWELS = set('aeiouyAEIOUY')
YS = set('yY')

def pig_word(word):
    """
    Given a word, convert it to Pig Latin
    """
    if hasattr(word, 'group'):
        # pull the text out of a regex match object
        word = word.group()

    # find the first vowel and what it is
    vowel, where = None, None
    for i,ch in enumerate(word):
        if ch in VOWELS:
            vowel, where = ch, i
            break

    if vowel is None:
        # No vowels found
        return word
    elif where == 0 and vowel not in YS:
        # Starts with a vowel - end in 'way'
        #   (leading y is treated as a consonant)
        return word + 'way'
    else:
        # Starts with consonants - move to end and follow with 'ay'

        # check capitalization
        uppercase = word.isupper() and len(word) > 1
        titlecase = word[:1].isupper() and not uppercase

        # rearrange word
        word = word[where:] + word[:where] + 'ay'

        # repair capitalization
        if uppercase:
            word = word.upper()
        elif titlecase:
            # don't use str.title() because it screws up words with apostrophes
            word = word[:1].upper() + word[1:].lower()

        return word

def pig_latin(s, reg=re.compile('[a-z\']+', re.IGNORECASE)):
    """
    Translate a sentence into Pig Latin
    """
    # find each word in the sentence, pass it to pig_word, and insert the result back into the string
    return reg.sub(pig_word, s)

def main():
    while True:
        s = inp('Enter a sentence to translate (or Enter to quit): ')
        if s.strip():
            print(pig_latin(s))
            print('')
        else:
            break

if __name__=="__main__":
    main()
然后


x=“清管器”;打印“%s%say”%(x[1:],x[0])
以下是您应该执行的操作列表。首先忽略大小写和标点符号开始。然后编写一个在单个单词上工作的函数。对'aeiouy'中的
if字母使用紧凑表示法
。现在编写一个函数,它接受一个句子并使用
句子。拆分(“”)
“”。连接(新句子)
要拆分为单词,请使用第一个函数,然后重新组合为一个字符串。然后,如果你想的话,再加上大小写和标点符号。提示:在第一个版本中,每个函数应该像5行一样。如果您想要输入的灵活性,您可以始终使用
str.lower()
转换字符串“str”对于小写字符。堆栈溢出不应该是你完成家庭作业的方式。谢谢你的帮助,我现在使用你的建议使它工作起来,并且运行平稳且更简单。这些是一些非常小的选项卡!你会希望这个句子用空格连接起来,句子。连接(“”)可以做到这一点
Enter a sentence to translate (or Enter to quit):
>>> Hey, this is really COOL!  Let's try it 3 or 4 times...
Eyhay, isthay isway eallyray OOLCAY!  Et'slay ytray itway 3 orway 4 imestay...