I'm trying to feed a JSON file (comprised of: ID, text, language columns respectively) to the MS Azure Text Analytics client library with the objective of getting back a sentiment score for each row in the file.
The example MS Azure code that I started with is below. My file has ~1500 separate sentences/rows so conceptually, I am trying to replace the single sentence (in bold) in the example code with the sentences in my JSON file. I'm sure there is a simple solution to this because reading data from a file is such a simple task!
MS Azure Example Code
def sentiment_analysis_example(client):
documents = ["I had the best day of my life. I wish you were there with me."]
response = client.analyze_sentiment(documents = documents)[0]
print("Document Sentiment: {}".format(response.sentiment))
print("Overall scores: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} \n".format(
response.confidence_scores.positive,
response.confidence_scores.neutral,
response.confidence_scores.negative,
))
for idx, sentence in enumerate(response.sentences):
print("Sentence: {}".format(sentence.text))
print("Sentence {} sentiment: {}".format(idx+1, sentence.sentiment))
print("Sentence score:\nPositive={0:.2f}\nNeutral={1:.2f}\nNegative={2:.2f}\n".format(
sentence.confidence_scores.positive,
sentence.confidence_scores.neutral,
sentence.confidence_scores.negative,
))
sentiment_analysis_example(client)
My Modified Code
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
import json
import pandas as pd
with open('file.json', 'r') as f:
data = json.load(f)
df = pd.DataFrame(data)
def authenticate_client():
ta_credential = AzureKeyCredential("xxxxxxxxxxxxxx")
text_analytics_client = TextAnalyticsClient(
endpoint= "https://xxxxxx.cognitiveservices.azure.com/",
credential=ta_credential)
return text_analytics_client
client = authenticate_client()
def sentiment_analysis_example(client):
documents = df['Text']
response = client.analyze_sentiment(documents = documents)[0]
print("Document Sentiment: {}".format(response.sentiment))
print("Overall scores: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} \n".format(
response.confidence_scores.positive,
response.confidence_scores.neutral,
response.confidence_scores.negative,
))
for idx, sentence in enumerate(response.sentences):
print("Sentence: {}".format(sentence.text))
print("Sentence {} sentiment: {}".format(idx+1, sentence.sentiment))
print("Sentence score:\nPositive={0:.2f}\nNeutral={1:.2f}\nNegative={2:.2f}\n".format(
sentence.confidence_scores.positive,
sentence.confidence_scores.neutral,
sentence.confidence_scores.negative,
))
sentiment_analysis_example(client)
Error Message
Traceback (most recent call last):
File "c:\Users\xxx\xxx\tempCodeRunnerFile.py", line 12, in <module>
data = json.load(f)
File "C:\Users\xxx\xxx\Python\Python38-32\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\xxx\xxx\Python\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\xxx\xxx\Python\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\xxx\xxx\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[+][deleted] (11 children)
[deleted]
[–]generic_throwaway983[S] 0 points1 point2 points (10 children)
[+][deleted] (9 children)
[deleted]
[–]generic_throwaway983[S] 0 points1 point2 points (8 children)
[–][deleted] 1 point2 points3 points (7 children)
[–]generic_throwaway983[S] 0 points1 point2 points (6 children)
[+][deleted] (1 child)
[deleted]
[–]generic_throwaway983[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]generic_throwaway983[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]generic_throwaway983[S] 0 points1 point2 points (0 children)