繁体   English   中英

从python文件访问json中的嵌套字典

[英]Accessing nested dictionary in json from python file

我有一个 intent.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": [""]
    }
    ]
}

我有 python 文件,其中以下函数获取句子的意图,输出如下

classify_local('Hello, good day!')

输出为

['greeting']

现在我想获取与标签问候相对应的响应。 我怎样才能做到这一点?

这里有很多可能的漏洞,但考虑到预期的输出,这应该可以找出句子给出的有限 I/O 的意图类型。

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!")

如果您想要更柔和的匹配,请将返回中的all替换为any ,但这在更广泛的数据集中不太可能正确。

请记住,当针对较大的数据集解析书面句子时,这一切都会很快崩溃。

你可以使用正则表达式:

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!')

输出:

'greeting'

从您的 json 文件中读取:

import json

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

如果您只想要标签greeting所有响应中的第一个响应,那么它是,

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM