Skip to main content
 首页 » 编程设计

python之搜索词的字符串相似度分数

2025年04月02日33java哥

下面是我的示例数据及其产品 ID。对于产品,如果用户从库中输入任何相关关键字,那么最相关的产品应该返回顶部链接。应该使用什么来查找字符串相似性分数。如果相似度为 80%,则得分应为 3,如果相似度为 50%,则得分应为 2,如果不匹配 1。

搜索库: {'100001': ['Bullet01', '用于各种 90° 连接和家庭维修项目的多功能连接器', 'Bullet02', '比单独使用角钉或螺钉紧固更坚固', ['Not', 'angles', 'make ','关节','更强','也','提供','一致','直','角','辛普森','强','领带','提供','宽', 'variety', 'angles', 'various', 'sizes', 'thicknesses', 'handle', 'light', 'duty', 'jobs', 'projects', 'structural', 'connection', '需要', 'Some', 'bent', 'skewed', 'match', 'project', 'For', 'outdoor', 'projects', 'moisture', 'present', 'use', 'ZMAX', 'zinc', 'coated', 'connectors', 'provide', 'extra', 'resistance', 'corrosion', 'look', 'Z', 'end', 'model', 'number', 'Versatile ', 'connector', 'various', 'connections', 'home', 'repair', 'projectsStronger', 'angled', 'nailing', 'screw', 'fastening', 'aloneHelp', 'ensure', 'joints', 'consistently', 'straight', 'strongDimensions', 'x', 'x', 'Made', 'Gauge', 'steelGalvanized', 'extra', 'corrosion', 'resistanceInstall', 'd ', 'common', 'nails', 'x', 'Strong', 'Drive', 'SD', 'screws'], 'Simpson Strong-Tie 12-Gauge Angle', 'Simpson Strong-Tie 12-Gauge角度']}

搜索词:{'100001': ['尖括号', 'l 括号']}

请您参考如下方法:

下面是如何在 python 中获得两个字符串之间的相似度

from difflib import SequenceMatcher 
 
string1 = "hello" 
string2 = "hellx" 
 
print (SequenceMatcher(None, string1, string2).ratio()) 
 
#prints 0.8