【问题标题】:getting all possible products of translation获取所有可能的翻译产品
【发布时间】:2022-01-18 16:17:49
【问题描述】:

我在 Python 中有一个元组,它存储了一些德语单词的翻译,如下所示:

[("mother", ["Mutter"]), ("and", ["und"]), ("father", ["Vater"]), ("I", ["ich", "mich"]),("not", ["nicht"]), ("at", ["dort", "da"]), ("home", ["Haus", "Zuhause"]), ("now", ["jetzt"])]

如您所见,有些英语单词在德语中有 2 种可能的翻译

我需要创建一个输出,它会自动给出一个句子的所有可能翻译。例如

[’ Vater ich nicht dort Haus jetzt ’,
’Vater ich nicht dort Zuhause jetzt ’,
’Vater ich nicht da Haus jetzt ’,
’Vater ich nicht da Zuhause jetzt ’,
’Vater mich nicht dort Haus jetzt ’,
’Vater mich nicht dort Zuhause jetzt ’,
’Vater mich nicht da Haus jetzt ’,
’Vater mich nicht da Zuhause jetzt ’] 

我的第一个想法是将元组存储在两个不同的列表中,如下所示:

english = []
german = []

for pair in wordlist:
  english.append(pair[0])
  for item in pair[1]: german.append(item)

但我不确定如何将第二个德语翻译转换为另一个列表,以及如何制作这些列表的笛卡尔积,以便它们出现在正确的位置

有人可以帮我在这里做什么吗?

【问题讨论】:

    标签: python list tuples cartesian-product


    【解决方案1】:

    你可以使用itertools.product:

    from itertools import product
    
    
    lst = [("mother", ["Mutter"]), ("and", ["und"]), ("father", ["Vater"]), ("I", ["ich", "mich"]),("not", ["nicht"]), ("at", ["dort", "da"]), ("home", ["Haus", "Zuhause"]), ("now", ["jetzt"])]
    
    ger = [i[1] for i in lst] # get only german words
    
    out = [' '.join(i) for i in list(product(*ger))] # combine words into string
    
    Mutter und Vater ich nicht dort Haus jetzt
    Mutter und Vater ich nicht dort Zuhause jetzt
    Mutter und Vater ich nicht da Haus jetzt
    Mutter und Vater ich nicht da Zuhause jetzt
    Mutter und Vater mich nicht dort Haus jetzt
    Mutter und Vater mich nicht dort Zuhause jetzt
    Mutter und Vater mich nicht da Haus jetzt
    Mutter und Vater mich nicht da Zuhause jetzt
    

    【讨论】:

      【解决方案2】:
      dataset = [("Mother", ["Mutter"]), ("and", ["und"]), ("father", ["Vater"]), ("I", ["ich", "mich"]),("not", ["nicht"]), ("at", ["dort", "da"]), ("home", ["Haus", "Zuhause"]), ("now", ["jetzt"])]
      
      sent = "Mother I at home"
      def translate(database, sampleSentence):
          dataDict = {k[0].lower() : k[1] for k in database} # convert to lowercase ; optional
          sentLst = sampleSentence.lower().strip().split() # convert sentence to list
          allTranslatons = [] # we'll hold translations here
          for wrd in sentLst:
              allCombs = dataDict[wrd] # get all combinatons of this word in German
              if len(allTranslatons) == 0: # if the list is empty, it means we haven't done anything
                  allTranslatons.extend(allCombs) # add the first combination then
              else:
                  holdout = [] # if not empty, then we need to add words to all the elements there
                  for i in range(len(allTranslatons)): #loopng through them
                      temp = allTranslatons[i] # get the word
                      for allC in allCombs: #loop through the combinations
                          holdout.append(temp + " " + allC) # add this added combo to a temp list
      
                  allTranslatons.clear() # once done, clean the allTranslations list to add fresh values
                  allTranslatons.extend(holdout) # add the temp combos
          return allTranslatons
      
      print(translate(dataset,sent ))
      >>> ['Mutter ich dort Haus', 'Mutter ich dort Zuhause', 'Mutter ich da Haus', 'Mutter ich da Zuhause', 'Mutter mich dort Haus', 'Mutter mich dort Zuhause', 'Mutter mich da Haus', 'Mutter mich da Zuhause']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-28
        • 2012-02-11
        • 1970-01-01
        • 1970-01-01
        • 2012-08-30
        • 1970-01-01
        相关资源
        最近更新 更多