I have a dictionary with the following structure:
{
1: {"names": ["name1_A", "name1_B", ...]},
2: {"names": ["name2_A", "name2_B", ...]},
...
}
where name1_A
and name1_B
are synonyms/aliases/different ways to write the same name, whose ID is 1. name2_A
and name2_B
are aliases of the same name, whose ID is 2, and so on.
I need to write a function which takes a user input and returns the ID of the name whose alias is most similar to the user input.
I know it's not very intuitive to understand what I mean, so here's an example. Let's say this is my dictionary:
{
1: {"names": ["James", "Jamie"]},
2: {"names": ["Karen", "Karyn"]}
}
The user types in the word Jimmy
. Since the closest match to Jimmy
from the dictionary is Jamie
, the function has to return the ID 1.
If the user types in the world Karena
, since the closest match is Karen
, the function has to return the ID 2.
I think the best way to get the closest math is to use difflib's get_close_matches()
. However, that function takes a list of possibilities as argument, and I cannot think of a way to correctly use it in my function. Any help would be appreciated.