【发布时间】:2020-08-17 11:40:00
【问题描述】:
我正在尝试比较从不同网站抓取的文本。 我有一个从数据框中的列中获取的文本列表。为了比较这个列表中的文本,我尝试使用相似度(我不知道是否有其他方法可以做到这一点)。 这是代码:
from difflib import SequenceMatcher
titles = filtered_dataset['Titles'].tolist()
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
def get_jaccard_sim(str1, str2):
a = set(str1.split())
b = set(str2.split())
c = a.intersection(b)
return float(len(c)) / (len(a) + len(b) - len(c))
similarities=[]
j_similarities=[]
for title in titles:
similarity=similar(title, title+1)
jacc_similarity=get_jaccard_sim(title, title+1) # I would like to compare the first text to the others; then the second one, and so on...
我收到以下错误:
TypeError: can only concatenate str (not "int") to str
因为
similarity=similar(title, title+1)
jacc_similarity=get_jaccard_sim(title, title+1)
您能帮我修复错误以比较文本吗?
【问题讨论】:
-
对不起,很遗憾我认为比较可能有问题。我想比较的是“标题”列表中的字符串“标题”与该列表中的每个项目。我认为 title +str(1) 不能做同样的事情,但也许我说错了。你能确认这没问题吗?毫无疑问,使用 str(1) 修复错误,但不允许我比较文本。我不知道这是否是另一个问题。如果我需要开一个新的,请告诉我。谢谢@Nuwan Madushanka
标签: python scikit-learn nlp similarity