简体   繁体   中英

Wit.ai Python - Extract confidence level from API output

I am new to Wit.ai and have started to implement it in my code. I was pondering an easier way than hardcoding to extract all the confidence levels from a given wit.ai API output.

For example(API output):

{
  "_text": "I believe I am a human",
  "entities": {
    "statement": [
      {
        "confidence": 0.97691847787856,
        "value": "I",
        "type": "value"
      },
      {
        "confidence": 0.91728476663947,
        "value": "I",
        "type": "value"
      }
    ],
     "query": [
      {
        "confidence": 1,
        "value": "am",
        "type": "value"
      }
    ]
  },
  "msg_id": "0YKCUvDvHC2gyydiU"
}

Thank You in advance.

You can iterate over entities to get confidence .

Something like :

data = {
"_text": "I believe I am a human",
"entities": {
    "statement": [
    {
        "confidence": 0.97691847787856,
        "value": "I",
        "type": "value"
    },
    {
        "confidence": 0.91728476663947,
        "value": "I",
        "type": "value"
    }
    ],
    "query": [
    {
        "confidence": 1,
        "value": "am",
        "type": "value"
    }
    ]
},
"msg_id": "0YKCUvDvHC2gyydiU"
}
confidence = list()
for k , v in data['entities'].iteritems():
    for item in v:
        confidence.append( (item['value'], item['confidence']))

print confidence

Which gives us:

[('I', 0.97691847787856), ('I', 0.91728476663947), ('am', 1)]

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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