Hi I'm trying to build a chatbot with Deeplearning in Python. (I'm quite new) And I want to have commands like if I put "play something" in the as an input, the bot should play a YouTube video. And if "play" is not in the input it should give an answer from it's deeplearning process. But in my case it does both. Does anyone know how to solve it? So if a word is in the input it should do a command, but if it's not, it should answer me with a response from the deeplearning. So my actual problem is that i get two outputs instead of one.
Code
def chat():
print("Start talking with the bot! (type quit to stop)")
while True:
inp = input("You: ")
# Commands
if inp.lower() == "quit":
break
elif "play" in inp:
song = inp.replace("play", "")
talk("Playing" song)
print("Playing" song)
pywhatkit.playonyt(song)
results = model.predict([bag_of_words(inp, words)])[0]
results_index = numpy.argmax(results)
tag = labels[results_index]
if ['play', 'quit'] not in inp:
# If accuracy is over 75% respond with it.
if results[results_index] > 0.75:
for tg in data["intents"]:
if tg['tag'] == tag:
responses = tg['responses']
back = ' ' random.choice(responses)
talk(back)
print(back)
else:
talk("What are you trying to say?")
print("What are you trying to say?")
chat()
That is not the whole code, I left out the Deeplearning and the TTS part. Thanks for the help!
CodePudding user response:
if "play" or "quit" not in inp, Should be:
if "play" and "quit" not in inp.
You want to say that they neither of them is in inp.
CodePudding user response:
Try something like this:
# above: lowercase it, AND remove leading/trailing spaces for consistency
inp = inp.lower().strip()
if inp not in ['play', 'quit']:
# code ...
This is more obvious and extensible. As other user mentioned above, if you use this, but it before you test for exact match with 'quit' and exact match with 'play' as this is basically the else up front.
