如何解决python中的字符串匹配分数
我有两个文件 - 一个包含两列:“字段名”和“关键字”,第二个包含单列“标题”。
[![Dataset-1][1]][1] [1]:https://i.stack.imgur.com/BdwmR.png
[![数据集 -2][2]][2] [2]:https://i.stack.imgur.com/apRzs.png
我正在尝试根据标题是否在关键字中将标题映射到字段名。如果标题在关键字中,则它是直接映射。但是,如果不是,那么我正在做模糊匹配。
def fuzzy_match(outputdf2):
d1={}
for i in outputdf2.index:#get row index
if(outputdf2.iloc[i,1]=="NoMatch"):
for key,value in d.items():#search in the dictionary
score=0
for Str2 in value:
Distance = levenshtein_ratio_and_distance(str(outputdf2.iloc[i,0]),Str2)
# print(Str2+ " : "+ Distance)
Ratio = levenshtein_ratio_and_distance(str(outputdf2.iloc[i,Str2,ratio_calc = True)
score = score + Ratio
# print(Ratio)
avg_score = round((score/len(value))*100,2)
if avg_score>=30: #Threshold score. Currently set to 30
d1[key] = avg_score
# print(outputdf2.iloc[i,0])
# print(sorted(d1.items(),key=lambda x: x[1],reverse=True)[:5])
# print("\n")
outputdf2.iloc[i,1] = sorted(d1.items(),reverse=True)[:5]
return(outputdf2)
df_fuzzy_match = fuzzy_match(df_no_match)
哪里, df_no_match 包含两列:columns1 具有来自 Headers 文件的标头名称,没有直接映射。第 2 列包含所有观察结果的“NoMatch”。 'levenshtein_ratio_and_distance' 函数的来源 - https://www.datacamp.com/community/tutorials/fuzzy-string-python
我的匹配分数很低。是不是因为关键词数量有限?还是因为我利用模糊匹配的功能。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。