-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_analysis.py
18 lines (18 loc) · 1.02 KB
/
sentiment_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import requests
import json
def sentiment_analyzer(text_to_analyse):
# URL of the sentiment analysis service
url = 'https://sn-watson-sentiment-bert.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/SentimentPredict'
# Constructing the request payload in the expected format
myobj = { "raw_document": { "text": text_to_analyse } }
# Custom header specifying the model ID for the sentiment analysis service
header = {"grpc-metadata-mm-model-id": "sentiment_aggregated-bert-workflow_lang_multi_stock"}
# Sending a POST request to the sentiment analysis API
response = requests.post(url, json=myobj, headers=header)
# Parsing the JSON response from the API
formatted_response = json.loads(response.text)
# Extracting sentiment label and score from the response
label = formatted_response['documentSentiment']['label']
score = formatted_response['documentSentiment']['score']
# Returning a dictionary containing sentiment analysis results
return {'label': label, 'score': score}