VADER情感划分

import pandas as pd
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import nltk

# 下载NLTK的VADER情感分析器所需的资源
nltk.download('vader_lexicon')

# 加载VADER情感分析器
sia = SentimentIntensityAnalyzer()

# 读取包含清洗后文本的DataFrame,假设文本保存在 'cleaned_content' 列中
df = pd.read_excel('nltk分词处理结果第二次部分删除后.xlsx')

# 定义情感阈值
positive_threshold = 0.5
negative_threshold = -0.5

# 对每条文本进行情感分析和分类
def classify_sentiment(text):
    sentiment_score = sia.polarity_scores(text)['compound']
    if sentiment_score > positive_threshold:
        return '积极'
    elif sentiment_score < negative_threshold:
        return '消极'
    else:
        return '中性'

# 将分类结果添加到DataFrame中的新列 'sentiment_category'
df['sentiment_category'] = df['content'].apply(classify_sentiment)

# 输出带有情感分类的数据
print(df[['content', 'sentiment_category']])
# 保存带有情感分类的数据到Excel文件
df.to_excel('带有情感分类的文档.xlsx', index=False)

你可能感兴趣的:(大数据)