首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Textblob遍历情感分析

如何使用Textblob遍历情感分析
EN

Stack Overflow用户
提问于 2020-07-15 07:04:06
回答 1查看 323关注 0票数 1

我正在尝试使用Textblob对从纽约时报API检索到的摘要执行情感分析。最后,我想使用Pandas将这些数据提取到一个excel文件中。我如何一次对所有20个摘要进行情感分析?

这就是我到目前为止所知道的:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-07-15 07:43:42

我不知道我是否明白你说的“一下子”是什么意思。也许你可以对每个标题进行情绪分析,找出每个标题的极性。如果你想查看今天所有的标题极性,那么你可以把它们加起来,看看它是不是负面的。

这只是一个例子:

代码语言:javascript
复制
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

现在,如果你有多个文章标题,想要查看它的极性,你只需将它们添加到一个列表中,然后查看它们的总和。

代码语言:javascript
复制
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")

我只是注意到你不是在谈论标题,我不知道抽象是什么,但理论上这应该会让你对集合的极性有一个概念。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62905252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档