获得所有可能的翻译产品

getting all possible products of translation

我在 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)

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

有人可以帮我解决这里的问题吗?

您可以使用 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
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']