-2

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!

desertnaut
  • 57,590
  • 26
  • 140
  • 166
JEA
  • 65
  • 7

2 Answers2

1

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.

TomServo
  • 7,248
  • 5
  • 30
  • 47
0

If you have 3 cases 1) contains quit, 2) contains play, 3) Everything else, you should put that section 3 in an else statement, no need to write another if condition for things you already checked.

if "play" or "quit" not in inp is also not the condition you’re seeking.

As tomservo mentioned it’s: if inp not in ['play', 'quit']:

Yassin Sameh
  • 361
  • 2
  • 13
  • Ahh yes that's true, but it is still giving me 2 outputs: the Command one and the Deeplearning one that's my acctual problem. – JEA Jan 09 '22 at 21:08
  • 1
    If you have 3 cases 1) contains quit, 2) contains play, 3) Everything else, you should put that section 3 in an else statement, no need to write another if condition for things you already checked, hopefully that solves it. – Yassin Sameh Jan 09 '22 at 21:16
  • Oh god I see now thanks I put it in a else statement before but i couldn't do that because i was assigning variables in between. ^^" Thanks! – JEA Jan 09 '22 at 21:32
  • You never tested this, didn't you? `if "play" and "quit" not in inp` is interpreted as `if ("play") and ("quit" not in inp:)`. Since `bool("play")` evaluates to `True` you can reduce the full condition to `if "quit" not in inp:` and this is obviously not what is wanted. – Matthias Jan 09 '22 at 22:08
  • Fixed the answer, thanks mathias. I had checked an example that was using the same maybe i missed something anyways checkout the answer above. – Yassin Sameh Jan 12 '22 at 08:01