背景:
通过爬虫获取了大量的商品信息,由于爬到的商品类别是商家自定义的,现在要统一管理,把商家类别映射成自定义类别
思路:
匹配商家类别和自定义类别中每个名称的相似度,相似度大于某个阀值的,最大的自定义类别字符串则为映射目标
一、Levenshtein
使用python的Levenshtein来处理相似度
1、安装
pip install python-Levenshtein
sourceCode:https://github.com/ztane/python-Levenshtein/
2、使用
使用方法很简单,用 Levenshtein.distance 就行,直接上代码:
#!/usr/bin/python
import Levenshtein
baseCat = ['Womens Clothing & Accessories','Mens Clothing & Accessories'...]
needCampareStr = ['Masturbators','Palette','Mens Watch']
for cS in needCampareStr:
dStr = ''
distance = 999
for bS in baseCat:
cbDis = Levenshtein.distance(cS,bS)
if distance > cbDis:
distance = cbDis