赞
踩
对文本进行情感值计算的简单方法,使用python中的SnowNLP。但是,在此之前需要对原始模型进行训练。
训练代码如下:
- import sys
- #reload(sys) sys.setdefaultencoding('utf-8')
- import pandas as pd #加载pandas
- text=pd.read_csv(u'D:\文本.csv',header=0) #读取文本数据
- text0=text.iloc[:,0] #提取所有数据
- text1=[i.encode('utf-8') for i in text0]
- print(text1)
- from snownlp import sentiment #加载情感分析模块
- sentiment.train('E:/anaconda/Lib/site-packages/snownlp/sentiment/neg.txt', 'E:/anaconda/Lib/site-packages/snownlp/sentiment/pos.txt') #对语料库进行训练,把路径改成相应的位置。
- sentiment.save('D:/sentiment.marshal')
然后对训练好的模型进行调用,并绘制出饼状图。
- from snownlp import SnowNLP
- import matplotlib.pyplot as plt
- import re
- import csv
-
- comment = []
- pos_count = 0
- neg_count = 0
-
- for line_data in open("D:\文本.csv",encoding='utf-8'):
- comment = line_data
- s = SnowNLP(comment)
- rates = s.sentiments
- print(rates)
- if (rates >= 0.5):
- pos_count += 1
-
- elif (rates < 0.5):
- neg_count += 1
- else :
- pass
- with open("D:/文本处理.csv", "a", encoding="gbk") as fp:
- fp.write("%s" % rates+"\n")
-
- labels = 'Positive Side\n(eg. pray,eulogize and suggestion)', 'Negative Side\n(eg. abuse,sarcasm and indignation)'
- fracs = [pos_count,neg_count]
- explode = [0.1,0] # 0.1 凸出这部分,
- plt.axes(aspect=1)
- plt.pie(x=fracs, labels=labels, explode=explode,autopct='%3.1f %%',
- shadow=True, labeldistance=1.1, startangle = 90,pctdistance = 0.6)
- plt.savefig("emotions_pie_chart.jpg",dpi = 360)
- plt.show()
计算结果显示如下图:
绘制的饼状图如下所示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。