1

I am trying to use Textblob to perform sentiment analysis on abstracts retrieved from the New York Times APIs. Eventually, I want to extract this data into an excel file using Pandas. How would I go about performing sentiment analysis on all 20 abstracts at once?

This is what I have so far:

import requests

url = f'https://api.nytimes.com/svc/mostpopular/v2/viewed/1.json?api-key={topkey}'
data = requests.get(url)
import json
data = data.json()

# Prints all abstracts for top 20 articles on the NYT
for i in range(0,19):
    print(data['results'][i]['abstract'])

import textblob
4
  • 1
    Please provide more information on what you are trying to do. What output or result do you want to get? Also, attempt to use textblob first, and if you can't get the result you want, post what you expected to get, what your code actually returned, and any error codes or output that resulted. Commented Jul 14, 2020 at 23:48
  • Ive used textblobs in other projects, just lost on how to tackle this in particular. What I am trying to do is perform sentiment analysis on all the articles abstracts for the top 20 articles, I could easily do this one by one, but need to present this information in a concise matter, so I wanted a way where I can get the sentiment and polarity of all the abstracts at once. Commented Jul 14, 2020 at 23:56
  • Mainly, I want to perform a sentiment and polarity analysis on the range of abstracts 0-19 Commented Jul 15, 2020 at 0:07
  • It would improve your answer if you edited it to add the information from your comments. Also, editing it to add a few lines of code for how you want to analyze a single instance would be helpful as well. Note that this improves the question not just now to get an answer, but for anyone that has the same issue and finds this in a search. :) Commented Jul 15, 2020 at 0:36

1 Answer 1

1

I don't know if I understood what you mean "all at once". Maybe you could perform sentiment analisys on each one and find each headline's polarity. If you wanted to view all of todays headlines polarity, then you can just add them up and see if it is negative or not.

This is a just an example:

from textblob import TextBlob

swear_words =  # Idk if you are allowed to swear in your answers, even if it is educational :P

loving_words = "I love you very much. You are wonderful."

blob = TextBlob(swear_words)

# If you have negative words in this string then the polarity will be negative.
# I tested some of mine and it printed out -0.46666666666666673
# I included 3 swear words, however I don't know exactly how the polarity is calculated
# but I assume some of it has to do with swears.
print(blob.polarity)
blob2 = TextBlob(loving_words)
print(blob2.polarity)  # Prints 0.5866666666666667

Now if you had multiple article headlines and wanted to view it's polarity, you could just append them to a list and view the sum of it.

data = {"results": [{"abstract": ""}, {"abstract": ""}, ..., {"abstract": ""}]}

head_polarities = []
for i in range(len(data["results"]):
    blob = TextBlob(data["results"][i]["abstract"])
    head_polarities.append(blob.polarity)


total_polarity = sum(head_polarities)
if total_polarity < 0:
    print("Negative headlines")
elif total_polarity > 0:
    print("Positive headlines")
else:
    print("Neutral headlines")

I just noticed you weren't talking about headlines and I don't know what abstracts are, but in theory this should give you an idea of the set's polarity.

Sign up to request clarification or add additional context in comments.

3 Comments

Recommend just: for result in data['results']: then result will represent data['results'][i] above. So you can shorten to blob = TextBlob(result['abstract']). Avoid creating an index when the for loop can just loop over every item for you. :)
Thank you for the help! I in fact wanted a separate polarity and sentiment for each of the abstracts (summaries of the article). This way I would be able to make an excel sheet with each value.
@humzv Glad you found the answer useful :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.