NLTK情感维达:极性得分(文本)不起作用

2024-06-02 07:23:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用NLTK中的Vader情绪分析中的polarity_scores(),但它给了我错误:

polarity_scores() missing 1 required positional argument: 'text'

我完全是个Python初学者。谢谢你的帮助!

from nltk.sentiment.vader import SentimentIntensityAnalyzer as sid
sentences=["hello","why is it not working?!"]
for sentence in sentences:
    ss = sid.polarity_scores(sentence) 

Tags: text错误requiredsentencesargumentsentence情绪missing
2条回答

你需要首先初始化一个感伤强度分析器的对象,然后调用该对象上的方法polarity_scores。

请尝试以下代码:

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()

sentences=["hello","why is it not working?!"]
for sentence in sentences:
    ss = sid.polarity_scores(sentence) 
print (ss)

SentimentIntensityAnalyzer是一个类。您需要初始化SentimentIntensityAnalyzer的对象,并在该对象上调用polarity_scores()方法。

from nltk.sentiment.vader import SentimentIntensityAnalyzer as SIA
sentences=["hello","why is it not working?!"]
sid = SIA()
for sentence in sentences:
    ss = sid.polarity_scores(sentence) 

如果你还没有下载词典文件

>>> import nltk
>>> nltk.download()
---------------------------------------------------------------------------
d) Download   l) List    u) Update   c) Config   h) Help   q) Quit
---------------------------------------------------------------------------
Downloader> d vader_lexicon
Downloader> q

相关问题 更多 >