我正在构建一个程序,用于将列表中的每个promocode(可能包含ocr错误)与另一个列表中的所有promocode(正确的promocode列表)进行比较
预期的输出是编辑距离和促销代码,与要比较的那个编辑距离最少。
我的代码
import csv
from nltk.metrics import distance
with open("all_correct_promo.csv","rb") as file1:
reader1 = csv.reader(file1)
correctPromoList = list(reader1)
#print correctPromoList
with open("all_extracted_promo.csv","rb") as file2:
reader2 = csv.reader(file2)
extractedPromoList = list(reader2)
#print extractedPromoList
def find_min_edit(str_,list_):
nearest_correct_promos = []
distances = {}
min_dist = 100 # arbitrary large assignment
for correct_promo in list_:
dist = distance.edit_distance(extracted,correct_promo,True) # compute Levenshtein distance
distances[correct_promo] = dist # store each score for real promo codes
if dist<min_dist:
min_dist = dist # store min distance
# extract all real promo codes with minimum Levenshtein distance
nearest_correct_promos.append(','.join([i[0] for i in distances.items() if i[1]==min_dist]))
return ','.join(nearest_correct_promos) # return a comma separated string of nearest real promo codes
incorrectPromo = {}
count = 0
for extracted in extractedPromoList:
print 'Computing %dth promo code...' % count
incorrectPromo[extracted] = find_min_edit(extracted,correctPromoList) # get comma separated str of real promo codes nearest to extracted
count+=1
print incorrectPromo
预期输出
Computing 0th promo code...
Computing 1th promo code...
Computing 2th promo code...
{'abc': 'abc', 'abd': 'abx,aba,abz,abc', 'acd': 'abx,aba,abz,abc'}
但是,我的代码显示以下错误
Computing 0th promo code...
Traceback (most recent call last):
File "correctpromo_test4.py", line 31, in <module>
incorrectPromo[extracted] = find_min_edit(extracted,correctPromoList) # get
comma separated str of real promo codes nearest to extracted
File "correctpromo_test4.py", line 20, in find_min_edit
distances[correct_promo] = dist # store each score for real promo codes
TypeError: unhashable type: 'list'
答案 0 :(得分:0)
您正在将CSV作为列表列表阅读 - 函数find_min_edit()期望一个字符串列表作为其第二个参数;你传递的是一系列字符串列表。
通过对这些东西进行排序来改变你阅读csv文件的方式 -
而不是
with open("all_correct_promo.csv","rb") as file1:
reader1 = csv.reader(file1)
correctPromoList = list(reader1)
只需使用此
即可with open("all_correct_promo.csv","rb") as file1:
reader1 = csv.reader(file1)
correctPromoList = [''.join(i) for i in reader1]
print correctPromoList
为这两个CSV执行此操作,将其排序......