文本对齐:使用Python提取匹配序列

5 投票
2 回答
3225 浏览
提问于 2025-04-17 20:44

我的目标是从两个文本段落中提取出对齐的匹配序列。接下来是我的文本:

txt1='the heavy lorry crashed into the building at midnight'
txt2='what a heavy lorry it is that crashed into the building'

期望的输出结果:

'heavy lorry'
'crashed into the building'

我尝试过的方法:

def sequ(s1,s2):
    _split1=s1.split()
    _split2=s2.split()
    _match=''.join(list(set(_split1) & set(_split2)))
    return _match

print sequ(txt1, txt2)

Result: heavybuildingintocrashedthelorry

......结果有点扭曲。

有没有什么建议可以让我得到期望的结果?谢谢。

2 个回答

3

在编程中,有时候我们会遇到一些问题,比如代码运行不正常或者出现错误。这时候,我们可以去一些技术论坛,比如StackOverflow,寻找解决方案。这里有很多经验丰富的程序员会分享他们的知识和经验,帮助我们解决问题。

在这些讨论中,大家会提出问题,描述遇到的情况,然后其他人会给出建议或者解决办法。通常,问题的描述会包含一些代码示例,帮助别人更好地理解问题所在。

总之,StackOverflow是一个很好的地方,可以让我们学习到很多编程知识,找到解决问题的方法,也能让我们与其他程序员交流经验。

txt1='the heavy lorry crashed into the building at midnight'
txt2='what a heavy lorry it is that crashed into the building'

s1, s2 = txt1.split(), txt2.split()
l1, l2 = len(s1), len(s2)
set1 = {" ".join(s1[j:j+i]) for i in range(1,l1+1) for j in range(l1+1-i)}
set2 = {" ".join(s2[j:j+i]) for i in range(1,l2+1) for j in range(l2+1-i)}
r = sorted(set1.intersection(set2), key = lambda x: len(x.split()))
rs = [j for k,j in enumerate(r) if all(j not in r[i] for i in range(k+1,len(r)))]
print rs
# ['heavy lorry', 'crashed into the building']
7

difflib.SequenceMatcher.get_matching_blocks 这个功能正好满足你的需求。

import difflib

def sequ(s1, s2):
    words1 = s1.split()
    words2 = s2.split()
    matcher = difflib.SequenceMatcher(a=words1, b=words2)
    for block in matcher.get_matching_blocks():
        if block.size == 0:
            continue
        yield ' '.join(words1[block.a:block.a+block.size])

txt1 = 'the heavy lorry crashed into the building at midnight'
txt2 = 'what a heavy lorry it is that crashed into the building'
print list(sequ(txt1, txt2))

输出结果:

['heavy lorry', 'crashed into the building']

撰写回答