1

I built a chatbot using JSON data for input and responses. The Python accepts an input for example "hi". If "hi" is listed in JSON array, it should output its response. In this case "Hi!". But problem is, if I type "hire", it will also print out "Hi!".

This is my Python script:

import json
import random

f = open('/home/anilo/Dokumente/Anilo/Projekte/Chatbot/chatdata.json', encoding='utf-8')
data = json.load(f)

wörter = []

nutzereingabe = input().lower()

for data in data["data"]:
    for input in data["input"]:
        if input in nutzereingabe:
            print(random.choice(data["antwort"]))

And this is my JSON data:

{"data": [
    {"tag": "grüßen",
    "input": ["hi"],
    "antwort": ["Hi!"]
    }]}

I am German, so if you are confused with the words:

nutzereingabe = user input

wörter = words

antwort = response

grüßen = greeting

  • 1
    Does this answer your question? [Find substring in string but only if whole words?](https://stackoverflow.com/questions/4154961/find-substring-in-string-but-only-if-whole-words) – Libra Nov 14 '21 at 22:05
  • This begins to get more complicated than you think, because you need to be able to parse things like "Hi!" and "Hi, [other text]", I recommend regex – Libra Nov 14 '21 at 22:06
  • Hey, Laif. I think it worked by adding: if input in nutzereingabe and input == nutzereingabe: I tried hi and hire and it won't print out anything when "hire". Thank you very much –  Nov 14 '21 at 22:11
  • Either delete this question or answer it yourself, your solution also doesn't acknowledge the things I pointed out, they will still be errors. – Libra Nov 14 '21 at 22:12
  • first compare with longer inputs - like `hire`, later compare with shorter inputs - like `hi` – furas Nov 14 '21 at 22:47

0 Answers0