【发布时间】:2023-03-27 06:52:01
【问题描述】:
我搜索了一些解决方案来进行情绪分析,并将结果写入正在分析的文本列旁边的列中。这就是我想出的。
import nltk
nltk.download('vader_lexicon')
nltk.download('punkt')
# first, we import the relevant modules from the NLTK library
from nltk.sentiment.vader import SentimentIntensityAnalyzer
# next, we initialize VADER so we can use it within our Python script
sid = SentimentIntensityAnalyzer()
# the variable 'message_text' now contains the text we will analyze.
message_text = '''Like you, I am getting very frustrated with this process. I am genuinely trying to be as reasonable as possible. I am not trying to "hold up" the deal at the last minute. I'm afraid that I am being asked to take a fairly large leap of faith after this company (I don't mean the two of you -- I mean Enron) has screwed me and the people who work for me.'''
print(message_text)
# Calling the polarity_scores method on sid and passing in the message_text outputs a dictionary with negative, neutral, positive, and compound scores for the input text
scores = sid.polarity_scores(message_text)
# Here we loop through the keys contained in scores (pos, neu, neg, and compound scores) and print the key-value pairs on the screen
for key in sorted(scores):
print('{0}: {1}, '.format(key, scores[key]), end='')
这给了我:
compound: -0.3804, neg: 0.093, neu: 0.836, pos: 0.071,
现在,我正在尝试从数据框中输入我自己的文本列。
示例代码来自此站点。
https://programminghistorian.org/en/lessons/sentiment-analysis
我在数据框中有一个由文本组成的字段,如下所示。
These brush heads are okay! Wish they came in a larger diameter, would cover more facial surface area and require less time to do the job! However, I think they do a better job than just a face cloth in cleansing the pores. I would recommend this product!
No opening to pee with. weird. And really tight. not very comfortable.
I choose it as spare parts always available and I will buy it again for sure!I will recommend it, without doubt!
love this cleanser!!
Best facial wipes invented!!!!!!(:
这些是我的数据框中的 5 条单独记录。我正在想办法将每条记录评估为“正面”、“负面”或“中性”,并将每条情绪放在同一行的新字段中。
在本例中,我认为这 5 条记录具有以下 5 种情绪(在每条记录旁边的字段中):
neutral
negative
positive
positive
positive
我该怎么做?
我想出了一个替代代码示例,如下所示。
event_dictionary ={scores["compound"] >= 0.05 : 'positive', scores["compound"] <= -0.05 : 'negative', scores["compound"] >= -0.05 and scores["compound"] <= 0.05 : 'neutral'}
#message_text = str(message_text)
for message in message_text:
scores = sid.polarity_scores(str(message))
for key in sorted(scores):
df['sentiment'] = df['body'].map(event_dictionary)
这运行了大约 15 分钟,然后我取消了它,我发现它实际上什么也没做。我想添加一个名为 'sentiment' 的字段,如果 score["compound"] >= 0.05 则使用 'positive' 填充它,如果 score["compound"] = -0.05 and scores["compound"]
【问题讨论】:
-
我的问题与前一个(相同)问题的问题相同:具体来说,您在哪个部分苦苦挣扎?接受的答案只是一个循环和三行代码,其功能由文档给出。
标签: python python-3.x nltk sentiment-analysis