根据推文的数据帧行在列中输出 vader 情绪分数

Output vader sentiment scores in columns based on dataframe rows of tweets

我有一个包含多行推文的数据框,我想根据 'positive'、'negative'、'neutral' 和 'compound' 创建 4 列分数每行的内容使用 vader 情感分析。

我查了不同的帖子,但我无法弄清楚我的具体情况。提前致谢!

像这样的东西应该可以工作:

analyzer = SentimentIntensityAnalyzer()
df['rating'] = df['tweets'].apply(analyzer.polarity_scores)
pd.concat([df.drop(['rating'], axis=1), df['rating'].apply(pd.Series)], axis=1)

我实际上找到了一个简单的解决方案,通过列表推导来解决面临同样问题的任何人:

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']]

我在 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))

希望你能解决。谢谢