def overlap_score(q1, q2):
"""
>>> overlap_score("fun", "real fun")
0.6666666666666666
>>> overlap_score(" ", " ")
0
"""
q1count = Counter(q1.split())
q2count = Counter(q2.split())
both = set(q1count.keys())
both = both.intersection(q2count.keys())
combined = q1count + q2count
mplusn = float(sum(combined.values()))
overlap = float(sum(combined[x] for x in both))
try:
return overlap / mplusn
except ZeroDivisionError:
return 0
评论列表
文章目录