我没有意识到 Python set 函数实际上将字符串分隔成单个字符。我为Jaccard编写了python函数并使用了python交集方法。我将两个集合传递到此方法中,在将这两个集合传递到我的 jaccard 函数之前,我在设置环上使用 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))
如果我不打电话set
我的字符串上的函数NEW Fujifilm 16MP 5x Optical Zoom Point and Shoot CAMERA 2 7 screen.jpg
我收到以下错误:
c = a.intersection(b)
AttributeError: 'str' object has no attribute 'intersection'
我不想进行字符与字符的交集,而是进行单词与单词的交集并获得杰卡德相似度。
首先尝试将字符串拆分为单词:
word_set = set(your_string.split())
Example:
>>> word_set = set("NEW Fujifilm 16MP 5x".split())
>>> character_set = set("NEW Fujifilm 16MP 5x")
>>> word_set
set(['NEW', '16MP', '5x', 'Fujifilm'])
>>> character_set
set([' ', 'f', 'E', 'F', 'i', 'M', 'j', 'm', 'l', 'N', '1', 'P', 'u', 'x', 'W', '6', '5'])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)