I have a problem. I would like to use regex to recognise certain text modules in a text. For example, beach vibe some. The problem is that some text modules are three words long (or even longer). However, most people only use the first two and maybe an abbreviation of the second word.
Is there an option to say that the regex should hit if it only recognises the first two words? And that it should only look at the first three letters of the second word?
customerId text element code
0 1 please use beach vibe some beach vibe some 0
1 1 you should use beach vibe beach vibe some 0
2 1 right use beach vib beach vibe some 0
3 3 use floating pow floating power 1
4 3 use floating stuff right now floating stuff 2
import pandas as pd
import copy
import re
d = {
"customerId": [1, 1, 1, 3, 3],
"text": ["please use beach vibe some",
"you should use beach vibe",
"right use beach vib",
'use floating pow',
'use floating stuff right now'],
"element": ['beach vibe some', 'beach vibe some', 'beach vibe some', 'floating power', 'floating stuff']
}
df = pd.DataFrame(data=d)
df['code'] = df['element'].astype('category').cat.codes
print(df)
def f(x):
match = 999
for element in df['element'].unique():
check = bool(re.search(element, x['text'], re.IGNORECASE))
if(check):
#print(forwarder)
match = df['code'].loc[df['element']== element].iloc[0]
break
elif(re.search(' '.join(element.split()[:2]), x['text'], re.IGNORECASE)):
match = df['code'].loc[df['element']== element].iloc[0]
break
else:
s = element.split()
s[1] = s[1][:3]
string = ' '.join(s[:2])
if(bool(re.search(string, x['text'], re.IGNORECASE))):
match = df['code'].loc[df['element']== element].iloc[0]
break
x['test'] = match
return x
#print(match)
df['test'] = None
df = df.apply(lambda x: f(x), axis = 1)
print(df)
customerId text element code test
0 1 please use beach vibe some beach vibe some 0 0
1 1 you should use beach vibe beach vibe some 0 0
2 1 right use beach vib beach vibe some 0 0
3 3 use floating pow floating power 1 1
4 3 use floating stuff right now floating stuff 2 2