David542
David542

Reputation: 110382

Programmatically figuring out if translated names are equivalent

I'm trying to see if two translated names are equivalent. Sometimes the translation will have the names ordered differently. For example:

>>> import difflib
>>> a = 'Yuk-shing Au'
>>> b = 'Au Yuk Sing'
>>> seq=difflib.SequenceMatcher(a=a.lower(), b=b.lower())
>>> seq.ratio()
0.6086956521739131

'Yuk-Shing Au' and 'Au Yuk Sing' are the same person. Is there a way to detect something like this, such that the ratio for names like this will be much higher? Similar to the result for:

>>> a = 'Yuk-shing Au'
>>> b = 'Yuk Sing Au'
>>> seq=difflib.SequenceMatcher(a=a.lower(), b=b.lower())
>>> seq.ratio()
0.8181818181818182

Upvotes: 0

Views: 53

Answers (1)

orlp
orlp

Reputation: 117771

You can normalize the ordering of names before comparing:

def normalize(name):
    name_parts = name.replace("-", " ").split()
    return " ".join(sorted(name_parts)).lower()

Upvotes: 2

Related Questions