import nltk
from textblob import TextBlob
import pandas as pd
import os
os.chdir("F:\\")
# 读取csv文件
df = pd.read_csv('Twitter.csv')
# 获取第3行和第2列的数据
row_num = 2
col_num = 3
print(df.iloc[row_num])
print(df.iloc[:, col_num])
# 输入英文文本
text = df.iloc[2, 2]
# 分句
sentences = nltk.sent_tokenize(text)
# 分词
words = []
for sentence in sentences:
words += nltk.word_tokenize(sentence)
# 词性标注
pos_tags = nltk.pos_tag(words)
# 情感分析
sentiment_scores = []
for sentence in sentences:
blob = TextBlob(sentence)
sentiment_score = blob.sentiment.polarity
sentiment_scores.append(sentiment_score)
# 判断情感态度
if sentiment_score > 0.5:
attitude = '非常看好'
elif sentiment_score <= 0.5 and sentiment_score >= 0.1:
attitude = '看好'
elif sentiment_score < 0.1 and sentiment_score > -0.1:
attitude = '中性'
elif sentiment_score < -0.1 and sentiment_score >= -0.5:
attitude = '不太看好'
else:
attitude = '非常不看好'
# 打印结果
print("Sentence:", sentence)
print("Sentiment score:", sentiment_score)
print("Attitude:", attitude)
# 汇总情感得分
total_sentiment_score = sum(sentiment_scores)
# 判断整体情感态度
if total_sentiment_score > 0.5:
overall_attitude = '非常看好'
elif total_sentiment_score <= 0.5 and total_sentiment_score >= 0.1:
overall_attitude = '看好'
elif total_sentiment_score < 0.1 and total_sentiment_score > -0.1:
overall_attitude = '中性'
elif total_sentiment_score < -0.1 and total_sentiment_score >= -0.5:
overall_attitude = '不太看好'
else:
overall_attitude = '非常不看好'
# 打印整体情感得分和态度
print("Total sentiment score:", total_sentiment_score)
print("Overall attitude:", overall_attitude)