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

下载必要的情感分析语料库(如果尚未下载)

nltk.download('vader_lexicon')

初始化情感分析器

sid = SentimentIntensityAnalyzer()

假设的社交媒体帖子数据(可替换为真实数据)

posts = [

"I love this new movie! The plot is amazing and the actors did a great job.",
"This product is a total waste of money. I'm so disappointed.",
"The weather today is just okay. Nothing special.",
"I had a wonderful dinner at that new restaurant. Highly recommended!"

]

存储分析结果的列表

results = []

对每个帖子进行情感分析

for post in posts:

sentiment_scores = sid.polarity_scores(post)
compound_score = sentiment_scores['compound']
if compound_score >= 0.05:
    sentiment = 'Positive'
elif compound_score <= -0.05:
    sentiment = 'Negative'
else:
    sentiment = 'Neutral'
results.append({
    'Post': post,
    'Sentiment': sentiment,
    'Compound Score': compound_score
})

将结果转换为 DataFrame 以便展示

df = pd.DataFrame(results)
print(df)


莲藕
1 声望0 粉丝

啦啦啦