0

I have created a simple chatbot and so far it is working good. The problem is, if I type for example "hibernate", the script will recognize this as a greetings. I am German and we have a lot of words containing "hi".

This is my Python code:

import json
import random

eingabe = input('DU: ')
eingabe = eingabe.lower()

with open('ebaykleinanzeigen\intents.json') as file:
    data = json.load(file)

for intent in data['intents']:
    for pattern in intent['patterns']:
        if pattern in eingabe:
            antwort = random.choice(intent['responses'])
            print(antwort)

    def getReply(eingabe):
        for intent in data['intents']:
            for pattern in intent['patterns']:
                if pattern in eingabe:
                    antwort = random.choice(intent['responses'])
                    print(antwort)

while 'beenden' not in eingabe:
    eingabe = input('DU: ')
    eingabe = eingabe.lower()
    getReply(eingabe)

And this is my JSON file:

{"intents": [
    {"tag": "gruessen",
     "patterns": ["hi"],
     "responses": ["Hi!"]
    }
]
}

Thanks in advance.

  • 1
    Use a regular expression that looks for word bounaries. Or split the input into words, and match whole words. – Barmar Jul 25 '21 at 09:36
  • `in` operator is just check does sequence exist in another sequence, your task is quite more complex. Consider using regular expressions. – Olvin Roght Jul 25 '21 at 09:36
  • So your real question is: How can I tell if a sentence contains the word "hi", right? See [this](https://stackoverflow.com/questions/1059559/split-strings-into-words-with-multiple-word-boundary-delimiters) thread. – at54321 Jul 25 '21 at 09:52

0 Answers0