Python 译者:我想翻译句子中的多个单词
Python translator: I want to translate multiple words in sentence
所以我开始研究这个小的翻译程序,它可以通过输入将英语翻译成德语。但是,当我输入多个单词时,我会得到我输入的单词,然后是正确的翻译。
这是我目前拥有的:
data = [input()]
dictionary = {'i':'ich', 'am':'bin', 'a':'ein', 'student':'schueler', 'of
the':'der', 'german':'deutschen', 'language': 'sprache'}
from itertools import takewhile
def find_suffix(s):
return ''.join(takewhile(str.isalpha, s[::-1]))[::-1]
for d in data:
sfx = find_suffix(d)
print (d.replace(sfx, dictionary.get(sfx, sfx)))
我正在尝试获得以下输出:
"i am a student of the german sprache"
相对于:
"ich bin ein schueler der deutschen spracher"
我是 python 的新手,非常感谢任何帮助
data = [input()]
dictionary = {'i':'ich', 'am':'bin', 'a':'ein', 'student':'schueler', 'of the':'der', 'german':'deutschen', 'language': 'sprache'}
for word in data:
if word in dictionary:
print dictionary[word],
说明:
对于您输入的每个词,如果该词存在于您的词典中
它将打印与该单词关联的值,逗号 (,) 用于跳过换行符。
将您的代码更改为此应该为您正在寻找的内容提供第一步。
data = raw_input()
dictionary = {'i':'ich', 'am':'bin', 'a':'ein', 'student':'schueler', 'of':'der', 'german':'deutschen', 'language': 'sprache'}
from itertools import takewhile
def find_suffix(s):
return ''.join(takewhile(str.isalpha, s[::-1]))[::-1]
for d in data.split():
sfx = find_suffix(d)
print (d.replace(sfx, dictionary.get(sfx,''))),
您现在所拥有的并没有考虑到每个单独的单词,因为数据不是您想要的单词列表,而是包含一个字符串的列表,即您提供的输入。尝试打印调试您的代码片段以了解我在说什么。
请注意,您的项目中会出现此类逻辑极端情况。提取每个单词并将其翻译成对应的德语单词,禁止字典条目超过 1 个单词,例如 'of the':'der'
。出于演示目的,我选择保留一个键长度为 1 的字典,所以上面的 key:value 对变成 'of':'der'
这是不正确的,因为德语语法比这复杂一点。
你现在遇到的问题比你开始的时候多,这就是玩具项目的目的。如果我是你,我会研究开源项目如何处理此类情况,并尝试找出合适的方法。祝你的项目好运。
我在您的 input
中注意到两件事。第一件事是你可以将两个单词翻译成一个(dictionary
中的两个单词 key
),另一件事是 input
可以有不应该翻译的德语单词.有了这两个条件,我认为最好的方法是通过 split()
和 input
和 loop
来检查单词。按照下面代码中的注释进行操作:
dictionary = {'i': 'ich', 'am': 'bin', 'a': 'ein', 'student': 'schueler', 'of the': 'der', 'german': 'deutschen', 'language': 'sprache'}
data = "i am a student of the german sprache"
lst = data.split()
result = ''
i = 0
while i < len(lst):
# try/except to see if the key is one word or two words
try:
if lst[i] in dictionary.values(): # Check if the word is german
result += lst[i] + ' '
i += 1
else:
result += dictionary[lst[i]] + ' ' # get the word from the dictionary
i += 1
except KeyError:
result += dictionary[lst[i] + ' ' + lst[i+1]] + ' ' # if the word is not german and not in dictionary, add the 2nd word and get from dictionary
i += 2
print result
输出:
ich bin ein schueler der deutschen sprache
例如,如果您有 3 个单词,这也会失败 key
,但如果您最多只有两个单词,那么应该没问题。
所以我开始研究这个小的翻译程序,它可以通过输入将英语翻译成德语。但是,当我输入多个单词时,我会得到我输入的单词,然后是正确的翻译。
这是我目前拥有的:
data = [input()]
dictionary = {'i':'ich', 'am':'bin', 'a':'ein', 'student':'schueler', 'of
the':'der', 'german':'deutschen', 'language': 'sprache'}
from itertools import takewhile
def find_suffix(s):
return ''.join(takewhile(str.isalpha, s[::-1]))[::-1]
for d in data:
sfx = find_suffix(d)
print (d.replace(sfx, dictionary.get(sfx, sfx)))
我正在尝试获得以下输出:
"i am a student of the german sprache"
相对于:
"ich bin ein schueler der deutschen spracher"
我是 python 的新手,非常感谢任何帮助
data = [input()]
dictionary = {'i':'ich', 'am':'bin', 'a':'ein', 'student':'schueler', 'of the':'der', 'german':'deutschen', 'language': 'sprache'}
for word in data:
if word in dictionary:
print dictionary[word],
说明:
对于您输入的每个词,如果该词存在于您的词典中 它将打印与该单词关联的值,逗号 (,) 用于跳过换行符。
将您的代码更改为此应该为您正在寻找的内容提供第一步。
data = raw_input()
dictionary = {'i':'ich', 'am':'bin', 'a':'ein', 'student':'schueler', 'of':'der', 'german':'deutschen', 'language': 'sprache'}
from itertools import takewhile
def find_suffix(s):
return ''.join(takewhile(str.isalpha, s[::-1]))[::-1]
for d in data.split():
sfx = find_suffix(d)
print (d.replace(sfx, dictionary.get(sfx,''))),
您现在所拥有的并没有考虑到每个单独的单词,因为数据不是您想要的单词列表,而是包含一个字符串的列表,即您提供的输入。尝试打印调试您的代码片段以了解我在说什么。
请注意,您的项目中会出现此类逻辑极端情况。提取每个单词并将其翻译成对应的德语单词,禁止字典条目超过 1 个单词,例如 'of the':'der'
。出于演示目的,我选择保留一个键长度为 1 的字典,所以上面的 key:value 对变成 'of':'der'
这是不正确的,因为德语语法比这复杂一点。
你现在遇到的问题比你开始的时候多,这就是玩具项目的目的。如果我是你,我会研究开源项目如何处理此类情况,并尝试找出合适的方法。祝你的项目好运。
我在您的 input
中注意到两件事。第一件事是你可以将两个单词翻译成一个(dictionary
中的两个单词 key
),另一件事是 input
可以有不应该翻译的德语单词.有了这两个条件,我认为最好的方法是通过 split()
和 input
和 loop
来检查单词。按照下面代码中的注释进行操作:
dictionary = {'i': 'ich', 'am': 'bin', 'a': 'ein', 'student': 'schueler', 'of the': 'der', 'german': 'deutschen', 'language': 'sprache'}
data = "i am a student of the german sprache"
lst = data.split()
result = ''
i = 0
while i < len(lst):
# try/except to see if the key is one word or two words
try:
if lst[i] in dictionary.values(): # Check if the word is german
result += lst[i] + ' '
i += 1
else:
result += dictionary[lst[i]] + ' ' # get the word from the dictionary
i += 1
except KeyError:
result += dictionary[lst[i] + ' ' + lst[i+1]] + ' ' # if the word is not german and not in dictionary, add the 2nd word and get from dictionary
i += 2
print result
输出:
ich bin ein schueler der deutschen sprache
例如,如果您有 3 个单词,这也会失败 key
,但如果您最多只有两个单词,那么应该没问题。