【发布时间】:2012-08-11 01:56:51
【问题描述】:
我没有意识到 Python 的 set 函数实际上将字符串分隔为单个字符。我为 Jaccard 编写了 python 函数并使用了 python 交集方法。我将两个集合传递给这个方法,在将这两个集合传递给我的 jaccard 函数之前,我在 setring 上使用了 set 函数。
示例:假设我有字符串NEW Fujifilm 16MP 5x Optical Zoom Point and Shoot CAMERA 2 7 screen.jpg,我会调用set(NEW Fujifilm 16MP 5x Optical Zoom Point and Shoot CAMERA 2 7 screen.jpg),它将字符串分成字符。因此,当我将它发送到 jaccard 函数交集时,实际上是看字符交集而不是单词到单词的交集。我怎样才能做到字对字交集。
#implementing jaccard
def jaccard(a, b):
c = a.intersection(b)
return float(len(c)) / (len(a) + len(b) - len(c))
如果我不对我的字符串 NEW Fujifilm 16MP 5x Optical Zoom Point and Shoot CAMERA 2 7 screen.jpg 调用 set 函数,我会收到以下错误:
c = a.intersection(b)
AttributeError: 'str' object has no attribute 'intersection'
而不是字符到字符的交集,我想做单词到单词的交集并获得 jaccard 相似度。
【问题讨论】:
标签: python set intersection