-2

I have this data coming via an api and I want to pull all the "confidence" scores that are in there. How would I go about doing that:

{
  "time": 765,
  "annotations": [
    {
      "start": 106,
      "end": 115,
      "spot": "customers",
      "confidence": 0.7198,
      "id": 234206,
      "title": "Customer",
      "uri": "h''ttp://en.wikipedia.org/wiki/Customer",
      "label": "Customer"
    },
    {
      "start": 116,
      "end": 122,
      "spot": "online",
      "confidence": 0.6313,
      "id": 41440,
      ''"title": "Online and offline",
      "uri": "http://en.wikipedia.org/wiki/Online_and_offline",
      "label": "Online"
    },
    {
      "start": 138,
      "end": 143,
      "s''pot": "small",
      "confidence": 0.7233,
      "id": 276495,
      "title": "Small business",
      "uri": "http://en.wikipedia.org/wiki/Small_business",
      "label''": "Small business"
    }
  ]
}

I tried doing things like data["confidence"], but it didn't seem to pull the confidence figures. How do I pull the confidence figures in Python? Thank you in advance.

This is the code that retrieves the data:

import requests

api_key = INSERT API KEY

content_urls = "http://www.startupdonut.co.uk/sales-and-marketing/promote-your-business/using-social-media-to-promote-your-business"

url = "https://api.dandelion.eu/datatxt/nex/v1/?lang=en &url="+content_urls+"&token=" + api_key

request = requests.get(url)

for data in request:
    print (data)
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70
semiflex
  • 1,176
  • 3
  • 25
  • 44

1 Answers1

1

Your API response data looks like this:

{
  "time": 765,
  "annotations": [
    {
      "start": 106,
      "end": 115,
      "spot": "customers",
      "confidence": 0.7198,
      "id": 234206,
      "title": "Customer",
      "uri": "h''ttp://en.wikipedia.org/wiki/Customer",
      "label": "Customer"
    },
    {
      "start": 116,
      "end": 122,
      "spot": "online",
      "confidence": 0.6313,
      "id": 41440,
      ''"title": "Online and offline",
      "uri": "http://en.wikipedia.org/wiki/Online_and_offline",
      "label": "Online"
    }
  ]
}

To extract condidence you can use this code:

import json
# ... request formation code goes here
response = requests.get(url)
data = response.json() # OR data = json.loads(response.content)
confidence_values = []
for annotation in data['annotations']:
    confidence_values.append(annotation.get('confidence'))

print(confidence_values)
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70