1

I have a intent.json file

{"intents": [
    {"tag": "greeting",
     "patterns": ["Hi there", "How are you", "Is anyone there?", "Hello", "Good day"],
     "responses": ["Hello, thanks for asking", "Good to see you again", "Hi there, how can I help?"],
     "context": [""]
    },
    {"tag": "goodbye",
     "patterns": ["Bye", "See you later", "Goodbye", "Nice chatting to you, bye", "Till next time"],
     "responses": ["See you!", "Have a nice day", "Bye! Come back again soon."],
     "context": [""]
    }
    ]
}

I have python file in which following function fetches the intent of the sentence and output is as follows

classify_local('Hello, good day!')

and output comes as

['greeting']

Now I want to fetch a response corresponding to tag greeting. How can I do that?

10
  • check classify_local function it must be using "tag" key to return values, there you can change/add "responses" key as well. Commented Mar 12, 2020 at 6:14
  • Does this answer your question? Parsing Through Nested JSON Python Commented Mar 12, 2020 at 6:17
  • What output are you expecting ? Commented Mar 12, 2020 at 6:31
  • Is the third element in your patterns for the tag greeting, "Hello", "Good day" or "Hello, good day!"? Commented Mar 12, 2020 at 6:55
  • 1
    so you want the first element from the response ? Commented Mar 12, 2020 at 6:59

3 Answers 3

2

There's a ton of possible holes here, but given the expected output, this should work to figure out what type of intent the sentence has given the limited I/O provided.

import string

def classify_local(text):
    # Separate out the phrases of the input text
    # "Hello, good day!" --> ['hello', 'good day']
    phrases = [
        s.translate(s.maketrans("", "", string.punctuation)).strip().lower()
        for s in text.split(",")
    ]

    # Find tags where all phrases are in patterns
    return [
        i["tag"]
        for i in intents_json["intents"]
        if all(j in [k.lower() for k in i["patterns"]] for j in phrases)
    ]


classify_local("Hello, good day!")

If you want a softer match, replace all in the return with any, but that's much less likely to be correct in a broader data set.

Keep in mind that this all falls apart very quickly when parsing written sentences against larger data sets.

Sign up to request clarification or add additional context in comments.

Comments

2

you could use regular expresions:

import re


my_json = {"intents": [
    {"tag": "greeting",
     "patterns": ["Hi there", "How are you", "Is anyone there?", "Hello", "Good day"],
     "responses": ["Hello, thanks for asking", "Good to see you again", "Hi there, how can I help?"],
     "context": [""]
    },
    {"tag": "goodbye",
     "patterns": ["Bye", "See you later", "Goodbye", "Nice chatting to you, bye", "Till next time"],
     "responses": ["See you!", "Have a nice day", "Bye! Come back again soon."],
     "context": [""]}]}


pattern_tag = {'|'.join(d["patterns"]): d['tag'] for d in my_json["intents"]}

def classify_local(my_strig):
    for p, t in pattern_tag.items():
        if re.search(p, my_strig):
            return t

classify_local('Hello, good day!')

output:

'greeting'

to read from your json file:

import json

with open('intent.json') as f:
    my_json = json.load(f)

Comments

1

If you simply want the first response from all the responses for the tag greeting then it is,

json_value = {
    "intents": [
        {
            "tag": "greeting",
            "patterns": ["Hi there", "How are you", "Is anyone there?", "Hello", "Good day"],
            "responses": ["Hello, thanks for asking", "Good to see you again", "Hi there, how can I help?"],
            "context": [""]
        },
        {
            "tag": "goodbye",
            "patterns": ["Bye", "See you later", "Goodbye", "Nice chatting to you, bye", "Till next time"],
            "responses": ["See you!", "Have a nice day", "Bye! Come back again soon."],
            "context": [""]
        }
    ]
}


for intent in json_value['intents']:
    if intent['tag'] == 'greeting':
        response = intent['responses'][0]
print(response)

1 Comment

you give a response no matter the input of your function classify_local

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.