"""Free fuzzy name match. Usage: python3 fuzzy_match.py names.txt list.txt > matches.csv"""
import csv, difflib, sys

names = [l.strip() for l in open(sys.argv[1], encoding="utf8") if l.strip()]
targets = [l.strip() for l in open(sys.argv[2], encoding="utf8") if l.strip()]
lower = {t.lower(): t for t in targets}
out = csv.writer(sys.stdout)
out.writerow(["Name", "Best match", "Score"])
for name in names:
    close = difflib.get_close_matches(name.lower(), list(lower), n=1, cutoff=0)
    best = close[0] if close else ""
    ratio = difflib.SequenceMatcher(None, name.lower(), best).ratio()
    out.writerow([name, lower.get(best, ""), round(ratio * 100)])
