def similarity2(a, b): """Calculates similarity of a multi-word strings.""" alist = filter(bool, _split_words_re.split(a.lower())) blist = filter(bool, _split_words_re.split(b.lower())) total = 0 score = 0.0 if len(alist) > len(blist): alist, blist = blist, alist for a in alist: ms = 0.0 mp = None for position, b in enumerate(blist): s = astrcmp(a, b) if s > ms: ms = s mp = position if mp is not None: score += ms if ms > 0.6: del blist[mp] total += 1 total += len(blist) * 0.4 if total: return score / total else: return 0
def similarity(a1, b1): """Calculates similarity of single words.""" # return astrcmp(a1, b1) """Calculates "smart" similarity of strings ``a`` and ``b``.""" a2 = normalize(a1) if a2: b2 = normalize(b1) else: b2 = "" return astrcmp(a2, b2)