开发者社区> 问答> 正文

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

我有一个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']

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

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 12:09:18 440 0
1 条回答
写回答
取消 提交回答
  • 如果您只是想从所有问候中获得标签“ 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)
    

    回答来源:stackoverflow

    2020-03-24 12:09:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载