In: Computer Science
Can you please check for the error in my code?:
#data object is created
text_data = {"the_final_output": [{"abs": ""}, {"abs": ""}, ...,
{"abs": ""}]}
#a list is created
main_terms = []
#iterating every data in the_final_output
for i in range(len(text_data["the_final_output"]):
blob = TextBlob(text_data["the_final_output"][i]["abs"])
main_terms.append(blob.polarity)
#sentiment analysis sorts the text into positive, neutral, and
negative.
tp = sum(main_terms)
#if tp is less than 0, it's negative
if tp < 0:
print("Negative")
#if tp is greater than 0, it's positive
elif tp > 0:
print("Positive")
#if neither positive nor negative, it's neutral
else:
print("Neutral")
Code:
from textblob import TextBlob
#data object is created
text_data = {"the_final_output": [{"abs": ""}, {"abs": ""}, {"abs": ""}]}
#a list is created
main_terms = []
#iterating every data in the_final_output
for i in range(len(text_data["the_final_output"])):
blob = TextBlob(text_data["the_final_output"][i]["abs"])
main_terms.append(blob.polarity)
#sentiment analysis sorts the text into positive, neutral, and negative.
tp = sum(main_terms)
#if tp is less than 0, it's negative
if tp < 0:
print("Negative")
#if tp is greater than 0, it's positive
elif tp > 0:
print("Positive")
#if neither positive nor negative, it's neutral
else:
print("Neutral")
Code screenshot:
Code output:
===========================
Following errors were in your code:
1) for i in range(len(text_data["the_final_output"])
Here the ending parentheses were not present. So updated code:
for i in range(len(text_data["the_final_output"])):
2) The data you mentioned
text_data = {"the_final_output": [{"abs": ""}, {"abs": ""}, ..., {"abs": ""}]}
Here ... in between is not valid. So I removed them.
On Making above changes the code runs as shown above.
===================
For any query comment.