我正在尝试使用Textblob对从纽约时报API检索到的摘要执行情感分析。最后,我想使用Pandas将这些数据提取到一个excel文件中。我如何一次对所有20个摘要进行情感分析?
这就是我到目前为止所知道的:
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发布于 2020-07-15 07:43:42
我不知道我是否明白你说的“一下子”是什么意思。也许你可以对每个标题进行情绪分析,找出每个标题的极性。如果你想查看今天所有的标题极性,那么你可以把它们加起来,看看它是不是负面的。
这只是一个例子:
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现在,如果你有多个文章标题,想要查看它的极性,你只需将它们添加到一个列表中,然后查看它们的总和。
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")我只是注意到你不是在谈论标题,我不知道抽象是什么,但理论上这应该会让你对集合的极性有一个概念。
https://stackoverflow.com/questions/62905252
复制相似问题