【问题标题】:Python translator: I want to translate multiple words in sentencePython 翻译器:我想翻译句子中的多个单词
【发布时间】:2017-11-13 03:57:51
【问题描述】:

所以我开始研究这个小翻译程序,它可以通过输入将英语翻译成德语。但是,当我输入多个单词时,我会得到我输入的单词,然后是正确的翻译。

这是我目前所拥有的:

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 很陌生,所以任何帮助都将不胜感激

【问题讨论】:

    标签: python translate


    【解决方案1】:
    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],
    

    解释:

    对于输入中的每个单词,如果该单词出现在您的字典中 它将打印与该单词关联的值,逗号 (,) 用于跳过换行符。

    【讨论】:

      【解决方案2】:

      将您的代码更改为此应该是您寻找的第一步。

      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,''))),
      

      您现在所拥有的并没有考虑每个单独的单词,因为数据不是您想要的单词列表,而是包含一个字符串的列表,即您提供的输入。尝试打印调试您的 sn-p 以查看我在说什么。

      请注意,您的项目中会出现这种逻辑极端情况。提取每个单词并将其与德语对应项进行翻译禁止字典条目长度超过 1 个单词,例如 'of the':'der'。出于演示目的,我选择保留长度为 1 的键的字典,因此上面的键:值对变为 'of':'der' 这是不正确的,因为德语语法比这要复杂一些。

      您现在遇到的问题比您开始时要多,这就是玩具项目的用途。如果我是你,我会研究开源项目如何处理这种情况,并尝试找出适合的方法。祝你的项目好运。

      【讨论】:

        【解决方案3】:

        我注意到您的input 中有两件事。第一件事是您可以将两个单词翻译成一个单词(dictionary 中的两个单词 key),另一件事是 input 可以包含不应翻译的德语单词。拥有这两个条件我认为最好的方法是通过split()inputloop 来检查单词。按照下面代码中的cmets:

        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,这也会失败,但如果您最多只有两个字,那么应该没问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多