使用词典翻译(Python)

时间:2015-02-04 01:33:50

标签: python python-3.x dictionary

好吧,所以我的老师给了我下面的作业,我们只用了大约一天的词典:

1)创建一个字典,将句子从一种语言翻译成另一种语言(如西班牙语译成英语)。 2)程序应该用第一语言写一个句子,然后用第二语言。

到目前为止,我的代码是:

def userinput():
    inputsentence = input("What sentence would you like to translate?(Hint! Make the sentence I speak _ more than my friends")
    spanoreng = input("Is this sentence Spanish or English?(Please enter span or eng")
    return(inputsentence,spanoreng)

def spantoeng(spantoengtras,inputsentence):
    inputsentence.lower()
    inputsentence.split()
    print(inputsentence)
def engtospan(engtospantrans,inputsentence):
    inputsentence.lower()
    inputsentence.split()
    print(inputsentence)
def main():
    spantoengtrans = {'yo' : 'I', 'hablo' : 'speak', 'espanol' : 'spanish', 'ingles' : 'english', 'mas' : 'more', 'de' : 'than','mis' : 'my', 'amigos' : 'friends'}
    engtospantrans = {'I' : 'yo', 'speak' : 'hablo', 'spanish' : 'espanol', 'english' : 'ingles', 'more' : 'mas', 'than' : 'de','my' : 'mis', 'friends' : 'amigos'}
    (inputsentence,spanoreng) = userinput()
    if spanoreng == 'span':
        spantoeng(spantoengtrans,inputsentence)
    elif spanoreng == 'eng':
        engtospan(engtospantrans,inputsentence)
    else:
        print("please type span or eng")
        (inputsentence,spanoreng) = userinput()


main()

我遇到的麻烦就是使用字典更改列表。另外,我的.lower()似乎没有做任何事情。帮助

编辑:意识到我的错误。感谢。

4 个答案:

答案 0 :(得分:2)

我没有看到任何列表 - 我只看到字符串,它们是不可变的,并且没有使用字典。

所以核心功能可能是:

def translate(sentence, transdict):
    words = sentence.split()
    trans = [transdict.get(w.lower(),w) for w in words]
    print(' '.join(trans))

split将句子放入以空格分隔的单词列表中;然后列表理解使其成为翻译单词列表(如果单词不在字典中则单独留下单词);最后,我们将后一个列表加入到一个以空格分隔的句子中。

当然,这还有很多不足之处,但如果没有正则表达式,很难做得更好 - 如果你一直使用词典只有一天,正则表达式可能远远超出你的研究范围。万一他们不是:

import re

def maketrans(somedict):
    def trans(mo):
        word = mo.group()
        return somedict.get(word.lower(), word)
    return trans

然后

translated = re.sub(r'\w+', maketrans(right_dict), sentence)
print (translated)

将保留标点符号和间距。但是在RE和高阶函数之间,我怀疑你最好在Python研究的后期忽略这个函数: - )。

答案 1 :(得分:0)

首先,您只需要一个translate功能。这个函数的目标应该是取一些句子和一些字典,并用另一种语言输出一个句子。要做到这一点,我会做这样的事情:

def translate(trans,inputsentence):
    inputsentence.lower()
    words = inputsentence.split(' ')
    new_sentence = [trans[w] for w in words]
    print(' '.join(new_sentence))

接下来,对于输入函数,您需要调用raw_input()而不是input()。这将清除语法错误。

def userinput():
    inputsentence = raw_input("What sentence would you like to translate?(Hint! ... > ")
    spanoreng = raw_input("Is this sentence Spanish or English? > ")
    return(inputsentence,spanoreng)

如果你在main()内适当地重命名函数调用,那么它应该可以做到这一点。

答案 2 :(得分:0)

这不能回答你的主要问题,但是你的lower()调用不起作用的原因是你在调用inputsentence.lower()而没有将返回值赋给变量,所以它所做的就是以小写形式返回字符串,但没有任何指向它。原始字符串保持不变。当你执行inputsentence.split(),创建并返回列表时会发生同样的事情,但它无处可去。最终当你return inputsentence返回你给它的完全相同的值时,使lower()和split()不做任何事。

如果你想在每次使用字符串方法时替换变量inputsentence的值,你应该这样做

inputsentence = inputsentence.lower().split()

答案 3 :(得分:0)

您应该将变量分配给.lower()的输出,然后将.split()应用于该变量。 (保存为变量)的输出是您想要返回的(或在当前代码中打印)。

相关问题