Suppose I have coffee shop menu list. I want to take text and return quantity and item name.
menu = ['Cappuccino','Café Latte','Expresso','Macchiato ','Irish coffee ']
Now I want to extract number and the ordered item name matching from my menu(Any 1st match from menu)
Example Text : Bring 1 Capputino
Output dataframe :
text Quantity match
Bring 1 Capputino 1 Cappuccino
Not necessary text entered spelling will be exact same as menu so it will just return the matching pattern from menu list in match column.
I have written below code but its returning Nan in match column. Appreciate any guidance.
Code:
import pandas as pd
import numpy as np
import re
def ccd():
global df
menu = ['Cappuccino','Café Latte','Expresso','Macchiato ','Irish coffee ']
for i in range(len(menu)):
menu[i] = menu[i].upper()
order = input('Enter a substring: ').upper()
args_dict = {'CAPUCINO':'CAPPUCCINO',
"COFFI":"COFFEE",
"COOKI":"COOKIE" }
#order=order.split()
for i,j in enumerate(order):
if j in args_dict:
order[i]=args_dict[j]
df = pd.DataFrame({'text':[order]})
df["Quantity"] = df.text.str.extract('(\d+)')
df['match'] = df.text.str.extract('(' + '|'.join(menu) + ')')