我在 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)
但我不確定如何將第二個德語翻譯轉換為另一個串列,以及如何制作這些串列的笛卡爾積,以便它們出現在正確的位置
有人可以幫我在這里做什么嗎?
uj5u.com熱心網友回復:
您可以使用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
uj5u.com熱心網友回復:
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']
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/382201.html
