I have a two dataframes - One contains free-flowing text description and the other is the master dictionary. I am trying to check if the words in master dictionary occurs in the text description in any format - for example if master keyword is 123456789, it can be present in user text as 12345 6789 or 123 456 789. The keyword can be both numeric as well alphanumeric.
I have tried to remove spaces in the text description and check using in function but this approach also matches noises. Ex, it will also match b123 4567 89klx. I want to match only if the whole keyword is split and given as multiple words and not in-between different words.
Code I have now:
def matcher(x,word_dict):
match=""
for i in list(dict.fromkeys(word_dict)):
if i.replace(" ", "").lower() in x.replace(" ", "").lower():
if(match==""):
match=i
else:
match=match+"_"+i
return match
import pandas as pd
df = pd.DataFrame({'ID' : ['1', '2', '3', '4','5'],
'Text' : ['sample 123 45 678 text','sample as123456 text','sample As123 456','sample bas123456 text','sample bas123 456ts text']},
columns = ['ID','Text'])
master_dict= pd.DataFrame({'Keyword' : ['12345678','as123456']},
columns = ['Keyword'])
df['Match']=df['Text'].apply(lambda x: matcher(x,master_dict.Keyword))
Expected Output
ID Text Match
0 1 sample 123 45 678 text 12345678
1 2 sample as123456 text as123456
2 3 sample As123 456 as123456
3 4 sample bas123456 text NA
4 5 sample bas123 456ts text NA
Any leads will be helpful. Thanks in advance.