1

我有两个字符串列表,一个叫l1另一个l2。我有兴趣为 中的每个字符串找到l1最匹配的字符串l2(但不是相反,即我只关心 中的字符串l1)。我知道没有完美的匹配。我使用 jaro-winkler 分数来计算每个字符串的相似度,使用 jellyfish 模块。

为了做到这一点,我创建了一个包含所有 jaro-winkler 分数的矩阵,然后找到 maxtrix 中每个点的最大值。但是,问题是有时字符串 froml2可能是多个字符串 from 的最佳匹配,l1我想防止这种情况发生。

有没有办法优化 argmax 方法,使得最大索引位置只能出现在结果矩阵中一次?

举个例子,拖车列表和后续代码如下:

l1 = ['skinnycorebrokenblack184567', 'promtex2365h6', 'lovelinen940770', 'promtex2365h1', 'lovetrs844705', 
      'lovetrs844704', 'bennttrs49655', 'stella55900', 'kaxsprassel55250', 'smurfbs185573', 'kaxsprassel55880', 
      'victoriacort182062', 'juliatreggings916531', 'juliatreggings916530', 'milo63624505', 'promtex2365s2', 
      'promtex2365s1', 'promtex2365s6', 'promtex2365s4', 'stantwill160810', 'topazchini51081', 'topazchini51087',
      'juliatreggings187109', 'hansentrs50924', '2454s1ladiesjeanscolure', 'promtex2365h2']
l2 = ['stannewtwill160810', 'stellatrs55900', 'jennyhigh352300', 'victoriacort180565', 
      'mistylowribsatins818820202031', 'lovelinen940771', 'kaxsprasseltrs55250', 'milo63626624', 'lovetrs844702',
      'sarabootcuts842887019398270', 'sarabootcuts84288701939', 'victoriacords81805848817', 
      'ladiesjeanscolouredxxl2454s340999', 'julliatregging1871168817', 'logandrawstringpants92686705656', 
      '72480', 'victoriacords85203408817', 'julliatregging9673907817', 'lilypoplin9418412031', 'stellatrs56023',
      'tysontrs50626', 'bolttrousers51370', 'bellamystripe184539', 'tenrhino63602214', 'kidsthermotrousers2365h1',
      'bennytrouser53648', 'bluerinse070201072', 'topazchino51077', 'slimclassicblack674220203128999', 
      'milo63603812', 'milo63603813', 'milo63603814', 'slimclassicblack6742202031', 'lilypoplin9418402031', 
      'julliatregging9673917817', 'smurfjr185606', 'sarabootcuts81884571939', 'julliatregging9165318817']

#create the matrix
mat = np.matrix([[jf.jaro_distance(str(st1), str(st2)) 
              if jf.jaro_distance(str(st1), str(st2)) > 0.85 else 0 
              for st2 in l2] for st1 in l1])

#get max values
mat_max = (mat.argmax(1))

#create match dictionary
match_dict = {}
for x in xrange(len(mat_max)):
    if int(mat_max[x]):
        match_dict[styles[x]] = s2[int(mat_max[x])]

在上面的示例中,请注意'topazchino51077'from与 froml2的字符串匹配两次l1。这正是我希望防止的。来自 l2 的字符串应该与最佳匹配进行匹配。

4

1 回答 1

1

您可以根据经典的稳定婚姻问题来建模您的问题。在您的问题中,成对匹配首选项由jaro_distance两个字符串给出;并且我们希望将每个字符串匹配l1到最接近的字符串,l2 除非该字符串已经与另一个l1更相似的字符串配对。

这里提供了算法的核心。这是一个可能的实现:

xs = np.array([[jf.jaro_distance(x, y) for y in l2] for x in l1])
order = np.argsort(xs, axis=1)

FREE = -1 # special value to indicate no match yet
match = FREE * np.ones(len(l1), dtype=np.int_)
jnext = len(l2) * np.ones(len(l1), dtype=np.int_)

# reverse match: if string in l2 is matched to a string in  l1
rev_match = FREE * np.ones(len(l2), dtype=np.int_)

while(np.any(match == FREE)): # while there is an un-matched string
    i = np.where(match == FREE)[0][0] # take the first un-matched index
    jnext[i] -= 1
    j = order[i, jnext[i]] # next l2 string that l1[i] will be matched against
    if rev_match[j] == FREE:  # l2[j] is free, pair i & j together
        rev_match[j], match[i] = i, j
        print('{:30} --> {}'.format(l1[i], l2[j]))
    else: # l2[j] is already paired
        l = rev_match[j] # current l1 string that l2[j] is paired with
        if xs[l, j] < xs[i, j]:  # l2[j] is more similar to l1[i] than l1[l]
            match[l] = FREE      # unpair l & j, and pair i & j
            rev_match[j], match[i] = i, j
            print('{:30} -/- {}'.format(l1[l], l2[j]))
            print('{:30} --> {}'.format(l1[i], l2[j]))

要查看最终匹配:

for i, w in enumerate(l1):
    print('{:30} {}'.format(w, l2[match[i]]))

如您所见,在此解决方案'topazchino51077'与 配对'topazchini51087',因为这两个更相似:

>>> jf.jaro_distance('topazchini51087', 'topazchino51077')
0.9111
>>> jf.jaro_distance('topazchini51081', 'topazchino51077')
0.8667
于 2014-08-03T15:11:38.523 回答