-2

my code is

import requests

url = "https://api.aiforthai.in.th/ssense"

text = 'ต้องติดอยู่ในฤดู อกหัก ตกอยู่ในห้วงความรักที่มันเลวร้าย'

data = {'text':text}

headers = {
    'Apikey': "BghsplRaCk6QRBZR5fX7krSjOdn1RS0w"
    }

response = requests.post(url, data=data, headers=headers)
response_print = response.json() 

The result of this code is

{'sentiment': {'score': '85.71', 'polarity-neg': True, 'polarity-pos': False, 'polarity': 'negative'}, 'preprocess': {'input': 'ต้องติดอยู่ในฤดู
 อกหัก ตกอยู่ในห้วงความรักที่มันเลวร้าย', 'neg': ['อกหัก', 'เลวร้าย'], 'pos': [], 'segmented': ['ต้อง', 'ติด', 'อยู่', 'ใน', 'ฤดู', ' ', 'อกหัก'
, ' ', 'ตก', 'อยู่', 'ใน', 'ห้วง', 'ความรัก', 'ที่', 'มัน', 'เลวร้าย'], 'keyword': ['ติด', 'ฤดู', 'ตก', 'ห้วง', 'ความรัก']}, 'alert': [], 'compa
rative': [], 'associative': [], 'intention': {'request': '0', 'sentiment': '85.71', 'question': '0', 'announcement': '0'}}

but I want result after the word "polarity" (that is the word "negative") How Can I fix it? PS. My English skill is bad sorry if you don't understand

2 Answers2

0

Try this below :

import requests

url = "https://api.aiforthai.in.th/ssense"

text = 'ต้องติดอยู่ในฤดู อกหัก ตกอยู่ในห้วงความรักที่มันเลวร้าย'

data = {'text':text}

headers = {
    'Apikey': "BghsplRaCk6QRBZR5fX7krSjOdn1RS0w"
    }

response = requests.post(url, data=data, headers=headers)
response_print = response.json()['sentiment']['polarity'] 
Abhishek Kulkarni
  • 1,747
  • 1
  • 6
  • 8
0

what you are looking for is

response_print['sentiment']['polarity']

It is a dictionary object with dictionaries inside of it, and has keys and values.

so to get close to the first value, you need to go to the sentiment part of the dictionary. after that, you must go to the polarity key in the dictionary.

ie response_print['sentiment']['polarity'] where it goes dictionary[first key][second key]

mark pedersen
  • 245
  • 1
  • 9