我在下面的数据集中查找文本匹配时遇到一些困难(请注意Sim
是我当前的输出,它是通过运行下面的代码生成的。它显示了错误的匹配)
ID Text Sim
13 fsad amazing ... fsd
14 fdsdf best sport everand the gane of the year❤️❤️❤️❤️... fdsfdgte3e
18 gsd wonderful fast
21 dfsfs i love this its incredible ... reds
23 gwe wonderful end ever seen you ... add
... ... ... ...
261 add wonderful gwe
261 add wonderful gsd
261 add wonderful fdsdf
267 fdsfdgte3e best match ever its a masterpiece fdsdf
277 hgdfgre terrible destroys everything ... tm28
如上所示,Sim
没有给出编写匹配文本的ID
。
例如,add
应该与gsd
匹配,反之亦然。但是我的输出说add
与gwe
匹配,这不是真的
我使用的代码如下所示:
from fuzzywuzzy import fuzz
def sim (nm, df): # this function finds matches between texts based on a threshold, which is 100. The logic is fuzzywuzzy, specifically partial ratio. The output should be IDs whether texts match, based on the threshold.
matches = dataset.apply(lambda row: ((fuzz.partial_ratio(row['Text'], nm)) = 100), axis=1)
return [df.ID[i] for i, x in enumerate(matches) if x]
df['L_Text']=df['Text'].str.lower()
df['Sim']=df.apply(lambda row: sim(row['L_Text'], df), axis=1)
df=df.assign(
Sim = df.apply(lambda x: [s for s in x['Sim'] if s != x['ID']], axis=1)
)
def tr (row): # this function assign a similarity score for each text applying partial_ratio similarity
return (df.loc[:row.name-1, 'L_Text']
.apply(lambda name: fuzz.partial_ratio(name, row['L_Text'])))
t = (df.loc[1:].apply(tr, axis=1)
.reindex(index=df.index,
columns=df.index)
.fillna(0)
.add_prefix('txt')
)
t += t.to_numpy().T + np.diag(np.ones(t.shape[0]))
你能帮我理解代码中的错误吗?不幸的是,我看不见它
我的预期产出如下:
ID Text Sim
13 fsad amazing ...
14 fdsdf best sport everand the gane of the year❤️❤️❤️❤️...
18 gsd wonderful add
21 dfsfs i love this its incredible ...
23 gwe wonderful end ever seen you ...
... ... ... ...
261 add wonderful gsd
261 add wonderful gsd
261 add wonderful gsd
267 fdsfdgte3e best match ever its a masterpiece
277 hgdfgre terrible destroys everything ...
由于在sim
函数中设置了完美匹配(=1)
初始假设
首先,由于我对您的问题不是百分之百的清楚,我假设您希望对所有行进行两两比较,并且如果比赛分数为>;100您想添加匹配行的键。如果不是这样,请纠正我
句法问题
因此,上面的代码存在多个问题。首先,如果只是复制并粘贴它,那么从语法上讲是不可能运行它的。
sim()
函数应如下所示:请注意
df
而不是dataset
,以及==
而不是=
。为了更好的可读性,我还删除了多余的括号语义问题
如果我随后运行您的代码并打印
t
(这似乎不是最终结果),这将为我提供以下信息:这对我来说似乎是正确的,因为
fuzz.partial_ratio("wonderful end ever seen you", "wonderful")
返回100
(因为部分匹配已经被认为是100分)。 出于一致性原因,您可以进行更改到
因为所有元素都应该完全匹配。所以当你说
这将是真的,因为{{CD9}},您可能需要考虑使用^ {CD10>}。另外,在将
t
转换为新的Sim
列时可能会出错,但在提供的示例中似乎没有代码替代实施
此外,正如一些评论所建议的那样,有时重构代码是有帮助的,这样人们就更容易帮助您。下面是一个示例,说明了这可能是什么样子:
给出:
相关问题 更多 >
编程相关推荐