0

I'm making a chatbot to detect toxicity, using the google perspective API, It responds with a dictionary shown below.

{
  "attributeScores": {
    "TOXICITY": {
      "spanScores": [
        {
          "begin": 0,
          "end": 11,
          "score": {
            "value": 0.05588363,
            "type": "PROBABILITY"
          }
        }
      ],
      "summaryScore": {
        "value": 0.05588363,
        "type": "PROBABILITY"
      }
    }
  },
  "languages": [
    "en"
  ],
  "detectedLanguages": [
    "en"
  ]
}

How can I format the above json to get the first "value": 0.05588363 as either a string or int? Help would be much appreciated!

This is my code:

from googleapiclient import discovery
import json
import os

API_KEY= os.getenv('API_KEY')


service = discovery.build('commentanalyzer', 'v1alpha1', developerKey=API_KEY)

analyze_request = {
  'comment': { 'text': 'sample text' },
  'requestedAttributes': {'TOXICITY': {}}
}

response = service.comments().analyze(body=analyze_request).execute()

val = (json.dumps(response, indent=2))


print(val)
final = val["attributeScores"]["TOXICITY"]["spanScores"][0]["score"]["value"]

print(final)
  • Did you try converting like so : `int(my_dict["attributeScores"]["TOXICITY"]["spanScores"][0]["score"]["value"])`? You should be able to replace `int()` with `str()`. – SpaceBurger Mar 01 '21 at 09:07

3 Answers3

1

Maybe a more general solution to your problem, instead of using a script with "hardcoded" values & enumerations:

It converts all int/float values to strings, however it's easy to modify it to convert only the values of specific keys as well:

def handler(data):
    if data is not None:
        res = {}
        for k,v in data.items():
            if type(v) not in (dict, list):
                res[k] = str(v) if type(v) in (int, float) else v
            elif type(v) == list:
                t_list = []
                for rec in v:
                    if type(rec) in (dict, list):
                        tmp_l = [{k2:v2} for k2, v2 in handler(rec).items()]
                        t_list.append(tmp_l)
                    else: t_list.append(rec)
                res[k] = t_list[0]
            else: res[k] = handler(v)
        return res
    else: return None

results = handler(data)
print(results)
Lopofsky
  • 518
  • 6
  • 15
0

That would be something along the lines of dict["attributeScores"]["TOXICITY"]["spanScores"][0]["score"]["value"].

Yolwoocle
  • 42
  • 1
  • 3
  • I tried exactly that, I will alter my question to show my code, but it pulled an error. – Romulus Hill Mar 01 '21 at 09:10
  • I get this error: `Traceback (most recent call last): File "main.py", line 21, in final = int(val["attributeScores"]["TOXICITY"]["spanScores"][0]["score"]["value"]) TypeError: string indices must be integers` – Romulus Hill Mar 01 '21 at 09:11
  • Oh wait, I just need to handle the result better – Romulus Hill Mar 01 '21 at 09:12
  • Am I correct in saying, I need to put the resultant value into a format which accepts decimals? – Romulus Hill Mar 01 '21 at 09:13
  • Your result is a string because you called `json.dumps()`. To read a JSON string as a dictionnary you can use `json.loads` (https://stackoverflow.com/questions/45148704/how-to-hide-axes-and-gridlines-in-matplotlib-python) – SpaceBurger Mar 01 '21 at 09:14
  • @SpaceBurger ive just switched it to loads, and it now says `TypeError: the JSON object must be str, bytes or bytearray, not dict` – Romulus Hill Mar 01 '21 at 09:15
  • Ive resolved the problem! Thank you so much everyone! Ive just stopped converting it into json! – Romulus Hill Mar 01 '21 at 09:17
0

i define your dictionary as a dict:

d = dict({
  "attributeScores": {
    "TOXICITY": {
      "spanScores": [
        {
          "begin": 0,
          "end": 11,
          "score": {
            "value": 0.05588363,
            "type": "PROBABILITY"
          }
        }
      ],
      "summaryScore": {
        "value": 0.05588363,
        "type": "PROBABILITY"
      }
    }
  },
  "languages": [
    "en"
  ],
  "detectedLanguages": [
    "en"
  ]
})

and then access to the value by keys:

d['attributeScores']['TOXICITY']['summaryScore']['value']
>>0.05588363