0

I am currently working on a chatbot for buying/selling items and I need this project for my resume. Looking forward into some help.

I have a JSON file that includes patterns and responses:

{"intents": [
    {"tag": "greetings",
    "patterns": ["hallo"],
    "responses": ["Hallo!", "Guten Tag!"]
    }
]
}

My Python script so far looks like this:

import json
import random

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

What I want is:

If I give an input, it should search in all tags (I want to expand the intents data later) and if input is found in patterns of one of those tags, it should print a random line of its responses. For example, if input is "hallo" which is a greeting, the script should know it is a greeting and if its found in the tag, it should print its responses. In this case "Hallo" or "Guten Tag"

This method works, but like I mentioned I want the script to know what kind of input it is whether it is a greeting, a question, etc.

for i in data['intents']:
    if i['tag'] == 'greetings':
        if inp in i['patterns']:
            print(i['responses'][0])
        break

Thanks in advance.

  • `if i['tag'] == 'greetings'` this check is not needed as per your output – deadshot Jun 02 '21 at 08:42
  • try `if input in i['patterns']` or you need define what kind of word is "greetings" – leaf_yakitori Jun 02 '21 at 08:43
  • i'm not really getting what exactly do you want to achieve and whats wrong with the last block of code? (as @deadshot mentioned the first if is not needed) – Kevin Jun 02 '21 at 08:44
  • Does this answer your question? [Parsing json and searching through it](https://stackoverflow.com/questions/8383136/parsing-json-and-searching-through-it) – Rushikesh Talokar Jun 02 '21 at 08:44
  • for optimal solution you can create new dict where `'patterns'` is key and `'responses'` is values – deadshot Jun 02 '21 at 08:45

0 Answers0