Search code examples
pythonpython-2.7artificial-intelligencewit-ai

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.


Solution

  • 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