Reputation: 163
import random
import re
import pandas as pd
df1 = pd.DataFrame({"Greetings": ["Greetings to you too", "hi", "hello", "hey", "greetings", "sup", "what's up", "yo"]})
df2 = pd.DataFrame({"Farewell": ["GoodBye", "see you", "bye", "laters"]})
frames = [df1, df2]
df3=pd.concat(frames, axis=1, join='outer', copy=True)
def check_for_greet():
while True:
try:
sentence = input("Start chatting: ")
wordList = re.sub("[^\w]", " ", sentence).split()
if sentence == "$$$":
return "END"
for word in wordList:
for col in df3.columns:
if word.lower() in df3[col].values:
print (df3[col][0])
break
The above works perfectly now between columns (thank you @R.yan), only issue however is when I type "hi hi', it prints it twice:
Start chatting: hi hi
Greetings to you too
Greetings to you too
Why is it doing that, I am breaking the for loop with continue to return to the while loop?!
Upvotes: 2
Views: 72
Reputation: 2382
Replace your function with following:
def check_for_greet():
while True:
try:
sentence = input("Start chatting: ")
wordList = re.sub("[^\w]", " ", sentence).split()
if sentence == "$$$":
return "END"
for col in df3.columns:
if sentence.lower() in df3[col].values:
print df3[col][0]
continue
Output:
Start chatting: hi
Greetings to you too
Start chatting: bye
GoodBye
answer for your duplicated output
word_found = False
for word in wordList:
for col in df3.columns:
if word.lower() in df3[col].values:
print (df3[col][0])
word_found = True
if word_found:
break
Upvotes: 2