I have a use-case where I have a:
search_text = '12 Jim Smith'
I have a list of tuples from which I need to search the above search_text and return matching tuples:
In [1742]: l
Out[1742]:
[(1234, 'Jim Beam'),
(13, 'Mark Smith'),
(12, 'Jim Jones'),
(23, 'Adam Smith'),
(15, 'Mark Taylor'),
(123, 'Mark Adam')]
I need to return the matching tuples from l with a score based on the most relevant search, something like below:
Expected Output:
In [1698]: ans
Out[1698]:
{
(12, 'Jim Jones'): 66, # matches ~66% of the search_text. `12` and `Jim` both match
(1234, 'Jim Beam'): 55, # matches ~55% of the search_text. `12` from `1234` and `Jim` both match
(13, 'Mark Smith'): 33, # matches ~33% of the search_text. `Smith`matches
(23, 'Adam Smith'): 33, # matches ~33% of the search_text. `Smith`matches
(123, 'Mark Adam'): 20 # matches ~20 %. Just `12` matches from `123`
}
Note: The values in above dict are random. They are just showing the percent of match.
Below is my attempt:
In [1745]: l1 = [(str(i[0]), i[1]) for i in l]
In [1746]: m = set()
In [1747]: for i in search_text.split():
...: if i.isnumeric():
...: for item in l1:
...: #print(item[0])
...: if i in item[0]:
...: print("i is = ", i)
...: print("numeric part matches", item)
...: m.add(item)
...: else:
...: for item in l1:
...: if i in item[1]:
...: print("text matches", item)
...: m.add(item)
...:
In [1748]: m
Out[1748]:
{('12', 'Jim Jones'),
('123', 'Mark Adam'),
('1234', 'Jim Beam'),
('13', 'Mark Smith'),
('23', 'Adam Smith')}
This returns the tuples that match, but I am not sure how to get percentage of relevance of match.