当然可以!以下是一个针对投诉内容进行情感分析的完整案例,包含数据准备、模型训练、情感分析以及结果展示的过程。
案例:投诉内容情感分析
步骤 1:数据准备
首先,我们准备一份包含用户投诉内容的数据集。假设数据集是一个CSV文件,包含两列:id
和 complaint
。
import pandas as pd
# 读取数据
data = pd.read_csv('complaints.csv')
# 查看数据
data.head()
步骤 2:数据预处理
对文本数据进行预处理,包括分词、去停用词、词干提取等。
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
# 下载必要的nltk资源
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
# 初始化
stop_words = set(stopwords.words('english'))
lemmatizer = WordNetLemmatizer()
# 预处理函数
def preprocess(text):
# 分词
words = word_tokenize(text.lower())
# 去停用词和词干提取
words = [lemmatizer.lemmatize(word) for word in words if word.isalpha() and word not in stop_words]
return ' '.join(words)
# 预处理数据
data['clean_complaint'] = data['complaint'].apply(preprocess)
# 查看预处理后的数据
data.head()
步骤 3:情感分析
使用预训练的情感分析模型对投诉内容进行情感分类。这里使用 TextBlob
库进行情感分析。
from textblob import TextBlob
# 情感分析函数
def analyze_sentiment(text):
analysis = TextBlob(text)
if analysis.sentiment.polarity > 0:
return 'positive'
elif analysis.sentiment.polarity == 0:
return 'neutral'
else:
return 'negative'
# 对预处理后的投诉内容进行情感分析
data['sentiment'] = data['clean_complaint'].apply(analyze_sentiment)
# 查看分析结果
data.head()
步骤 4:结果展示
展示情感分析的结果,包括情感分类的分布情况。
import matplotlib.pyplot as plt
import seaborn as sns
# 情感分类分布情况
plt.figure(figsize=(8, 6))
sns.countplot(x='sentiment', data=data)
plt.title('Sentiment Distribution of Complaints')
plt.xlabel('Sentiment')
plt.ylabel('Count')
plt.show()
步骤 5:保存结果
将分析结果保存到新的CSV文件中。
# 保存结果
data.to_csv('complaints_with_sentiment.csv', index=False)
代码总结
通过以上步骤,我们完成了对投诉内容的情感分析。从数据读取、预处理,到情感分析、结果展示,完整地实现了一个情感分析流程。该流程可以根据具体需求进行调整和扩展,例如使用更高级的情感分析模型(如BERT)来提高分析的准确性。