我有一个包含多行推文的数据框,我想使用 vader 情绪分析根据每行的内容创建 4 列分数“正”、“负”、“中性”和“复合”。
我查找了不同的帖子,但我无法弄清楚我的确切情况。先感谢您!
实际上,我找到了一个简单的解决方案,可以通过列表推导来解决遇到相同问题的任何人:
analyzer = SentimentIntensityAnalyzer()
df['compound'] = [analyzer.polarity_scores(x)['compound'] for x in df['tweet']]
df['neg'] = [analyzer.polarity_scores(x)['neg'] for x in df['tweet']]
df['neu'] = [analyzer.polarity_scores(x)['neu'] for x in df['tweet']]
df['pos'] = [analyzer.polarity_scores(x)['pos'] for x in df['tweet']]
像这样的东西应该工作:
analyzer = SentimentIntensityAnalyzer()
df['rating'] = df['tweets'].apply(analyzer.polarity_scores)
pd.concat([df.drop(['rating'], axis=1), df['rating'].apply(pd.Series)], axis=1)
我在 python 3 中使用 Vader 进行情感分析完成了相同类型的工作。看看你可能会找到一种方法来执行你需要的东西。
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import time
analyzer = SentimentIntensityAnalyzer()
pos_count = 0
pos_correct = 0
with open("D:/Corona_Vac/pythonprogramnet/Positive BOW.txt","r") as f:
for line in f.read().split('\n'):
vs = analyzer.polarity_scores(line)
if not vs['neg'] > 0.1:
if vs['pos']-vs['neg'] > 0:
pos_correct += 1
pos_count +=1
neg_count = 0
neg_correct = 0
with open("D:/Corona_Vac/pythonprogramnet/Positive BOW.txt","r") as f:
for line in f.read().split('\n'):
vs = analyzer.polarity_scores(line)
if not vs['pos'] > 0.1:
if vs['pos']-vs['neg'] <= 0:
neg_correct += 1
neg_count +=1
print("Positive accuracy = {}% via {} samples".format(pos_correct/pos_count*100.0, pos_count))
print("Negative accuracy = {}% via {} samples".format(neg_correct/neg_count*100.0, neg_count))
希望你能修复。谢谢