【问题标题】:Accessing nested dictionary in json from python file从python文件访问json中的嵌套字典
【发布时间】:2020-03-12 06:08:18
【问题描述】:

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

现在我想获取与标签问候语相对应的响应。 我该怎么做?

【问题讨论】:

  • 检查classify_local函数,它必须使用“tag”键返回值,您也可以更改/添加“responses”键。
  • 这能回答你的问题吗? Parsing Through Nested JSON Python
  • 你期待什么输出?
  • patterns 中的第三个元素是标签greeting、"Hello", "Good day" 还是"Hello, good day!"?
  • 所以你想要响应中的第一个元素?

标签: python json python-3.x dictionary


【解决方案1】:

这里有很多可能的漏洞,但考虑到预期的输出,这应该可以确定句子在提供有限 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,但这在更广泛的数据集中不太可能是正确的。

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

【讨论】:

    【解决方案2】:

    你可以使用正则表达式:

    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)
    

    【讨论】:

      【解决方案3】:

      如果您只是想要标签 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)
      

      【讨论】:

      • 无论你的函数输入如何classify_local,你都会给出响应
      猜你喜欢
      • 2018-12-06
      • 2018-05-31
      • 2018-02-09
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 2018-02-07
      相关资源
      最近更新 更多