模糊模糊偏微分的错误评分

2024-06-28 10:52:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python相当陌生,我尝试使用fuzzywuzzy进行模糊匹配。我相信我用部分比率函数得到的分数不正确。下面是我的探索代码:

>>>from fuzzywuzzy import fuzz
>>>fuzz.partial_ratio('Subject: Dalki Manganese Ore Mine of M/S Bharat Process and Mechanical Engineers Ltd., Villages Dalki, Soyabahal, Sading and Thakurani R.F., Tehsil Barbil, Distt, Keonjhar, Orissa environmental clearance','Barbil')
50

我认为这应该返回100分,因为第二个字符串“Barbil”包含在第一个字符串中。当我尝试在第一个字符串的结尾或开头去掉几个字符时,我得到的匹配分数是100。在

^{pr2}$

当第一根弦的长度变为199时,它似乎从50分变为100分。有人对可能发生的事情有什么见解吗?在


Tags: and函数字符串代码fromimportpartial分数
1条回答
网友
1楼 · 发布于 2024-06-28 10:52:04

这是因为当其中一个字符串是200 characters or longer, an automatic junk heuristic gets turned on in python's SequenceMatcher。 此代码应适用于您:

from difflib import SequenceMatcher

def partial_ratio(s1, s2):
    """"Return the ratio of the most similar substring
    as a number between 0 and 100."""

    if len(s1) <= len(s2):
        shorter = s1
        longer = s2
    else:
        shorter = s2
        longer = s1

    m = SequenceMatcher(None, shorter, longer, autojunk=False)
    blocks = m.get_matching_blocks()

    # each block represents a sequence of matching characters in a string
    # of the form (idx_1, idx_2, len)
    # the best partial match will block align with at least one of those blocks
    #   e.g. shorter = "abcd", longer = XXXbcdeEEE
    #   block = (1,3,3)
    #   best score === ratio("abcd", "Xbcd")
    scores = []
    for (short_start, long_start, _) in blocks:
        long_end = long_start + len(shorter)
        long_substr = longer[long_start:long_end]

        m2 = SequenceMatcher(None, shorter, long_substr, autojunk=False)
        r = m2.ratio()
        if r > .995:
            return 100
        else:
            scores.append(r)

    return max(scores) * 100.0

相关问题 更多 >