首页 > 解决方案 > Python中的高精度词对齐算法

问题描述

我正在做一个项目,在句子和其他语言的翻译之间建立高精度的词对齐,以衡量翻译质量。我知道 Giza++ 和其他单词对齐工具被用作统计机器翻译管道的一部分,但这不是我想要的。我正在寻找一种算法,该算法可以将源句子中的单词映射到目标句子中的相应单词,并且在给定这些限制的情况下透明且准确地:

这是我所做的:

这是英语和德语句子之间的相关矩阵的示例。我们可以看到上面讨论的挑战。

英语和德语句子对齐示例,显示单词之间的相关性,绿色单元格是应由单词对齐算法识别的正确对齐点

在图像中,有一个英语和德语句子之间的对齐示例,显示了单词之间的相关性,绿色单元格是应该由单词对齐算法识别的正确对齐点。

这是我尝试的一些方法:

这是我正在使用的代码:

import random
src_words=["I","know","this"]
trg_words=["Ich","kenne","das"]
def match_indexes(word1,word2):
    return random.random() #adjust this to get the actual correlation value

all_pairs_vals=[] #list for all the source (src) and taget (trg) indexes and the corresponding correlation values
for i in range(len(src_words)): #iterate over src  indexes
    src_word=src_words[i] #identify the correponding src word
    for j in range(len(trg_words)): #iterate over trg indexes
        trg_word=trg_words[j] #identify the correponding trg word
        val=match_indexes(src_word,trg_word) #get the matching value from the inverted indexes of     each word (or from the data provided in the speadsheet)
        all_pairs_vals.append((i,j,val)) #add the sentence indexes for scr and trg, and the corresponding val

all_pairs_vals.sort(key=lambda x:-x[-1])  #sort the list in descending order, to get the pairs with the highest correlation first
selected_alignments=[]
used_i,used_j=[],[] #exclude the used rows and column indexes
for i0,j0,val0 in all_pairs_vals:
    if i0 in used_i: continue #if the current column index i0 has been used before, exclude current pair-value
    if j0 in used_j: continue #same if the current row was used before
    selected_alignments.append((i0,j0)) #otherwise, add the current pair to the final alignment point selection
    used_i.append(i0) #and include it in the used row and column indexes so that it will not be used again
    used_j.append(j0)

for a in all_pairs_vals: #list all pairs and indicate which ones were selected
    i0,j0,val0=a
    if (i0,j0) in selected_alignments: print(a, "<<<<")
    else: print(a)

这是有问题的,因为它不支持多对多,甚至是一对多的对齐方式,并且在开始时很容易出错,因为选择了具有最高相关性的错误对,将其行和列排除在未来的选择之外。一个好的算法会考虑到某个对在其各自的行/列中具有最高的相关性,但也会考虑与具有高相关性的其他对的接近度。

如果您愿意,可以尝试以下数据,它位于 Google 表格中: https ://docs.google.com/spreadsheets/d/1-eO47RH6SLwtYxnYygow1mvbqwMWVqSoAhW64aZrubo/edit?usp=sharing

标签: pythonmachine-translation

解决方案


词对齐在某种程度上仍然是一个开放的研究课题。Giza++ 背后的概率模型相当重要,参见:http ://www.ee.columbia.edu/~sfchang/course/svia/papers/brown-machine-translate-93.pdf

您可以采取许多现有的方法,例如:

这是一个非常困难的机器学习问题,虽然像您这样的简单方法可以工作并非不可能,但首先研究现有工作可能是一个好主意。话虽如此,我们已经看到该领域令人惊讶的简单技术取得了相当多的突破,所以谁知道:-)


推荐阅读