import numpy as np
import pandas as pd

df=pd.read_csv("chatbot.csv")
df["clean"]=df["query"].str.lower().str.replace(r"[^a-z0-9\s]","",regex=True)

print("Chatbot: Type 'bye' to exit.")

while True:
    q=input("You: ").lower().strip()
    q=pd.Series([q]).str.replace(r"[^a-z0-9\s]","",regex=True).iloc[0]
    if not q:
        print("Bot: Please enter a question.")
        continue
    match=np.where(df["clean"].eq(q))[0]
    if len(match):
        print("Bot:",df["response"].iloc[match[0]])
        if q=="bye": break
        continue
    scores=df["clean"].apply(lambda x:len(set(q.split())&set(x.split()))/len(set(q.split())|set(x.split())))
    i=np.argmax(scores)
    print("Bot:",df["response"].iloc[i] if scores.iloc[i]>=0.5 else "Sorry, I don't understand that question.")