我在计算一个多达6个变量的序列有多相似。目前我正在使用集合计数器返回不同变量的频率作为编辑距离。在
默认情况下,编辑变量(add/sub/change)时的距离为1或0。我想根据变量和为变量设置的值更改距离。
所以我可以说某些变量与其他变量相似,并提供一个值来说明它们的相似程度。
我还想说,某些变量的价值比通常少或多。在
示例:# 'c' and 'k' are quite similar, so their distance from eachother is 0.5 instead of 1
>>> groups = {['c','k'] : 0.5}
# the letter 'e' is less significant, and 'x' is very significant
>>> exceptions = {'e': 0.3, 'x': 1.5}
>>> distance('woke', 'woc')
0.8
说明:
^{pr2}$
我怎样才能做到这一点?这能用这个计数器算法实现吗?
当前代码(感谢David Eisenstat)def distance(s1, s2):
cnt = collections.Counter()
for c in s1:
cnt[c] += 1
for c in s2:
cnt[c] -= 1
return sum(abs(diff) for diff in cnt.values()) // 2 + \
(abs(sum(cnt.values())) + 1) // 2