# part 1:情感詞典錄入positive_emotion =[]#積極詞匯數據庫negative_emotion =[]#消極詞匯數據庫extreme =[]#程度副詞1very =[]#程度副詞2more =[]#程度副詞3alittlebit =[]#程度副詞4insufficiently =[]#程度副詞5over =[]#程度副詞6no =[]#否定詞d =open("positive-emotion.txt", encoding='utf-8')#積極詞匯d2 =open("positive_evaluate.txt", encoding='utf-8')#積極詞匯n =open("negative-emotion.txt", encoding='utf-8')#否定詞匯n22 =open("negative_evaluate.txt", encoding='utf-8')#否定詞匯e =open("extreme-6.txt", encoding='utf-8')#程度副詞1v =open("very-5.txt", encoding='utf-8')#程度副詞2m =open("more-4.txt", encoding='utf-8')#程度副詞3a =open("alittlebit-3.txt", encoding='utf-8')#程度副詞4i =open("insufficiently-2.txt", encoding='utf-8')#程度副詞5o =open("over-1.txt", encoding='utf-8')#程度副詞6n2 =open("no.txt", encoding='utf-8')#否定詞for line in d.readlines():positive_emotion.append(line.strip())#添加進積極詞匯數據庫for line in d2.readlines():positive_emotion.append(line.strip())#添加進積極詞匯數據庫for line in n.readlines():negative_emotion.append(line.strip())#添加進消極詞匯數據庫for line in n22.readlines():negative_emotion.append(line.strip())#添加進消極詞匯數據庫for line in e.readlines():extreme.append(line.strip())#添加進程度副詞1for line in v.readlines():very.append(line.strip())#添加進程度副詞2for line in m.readlines():more.append(line.strip())#添加進程度副詞3for line in a.readlines():alittlebit.append(line.strip())#添加進程度副詞4for line in i.readlines():insufficiently.append(line.strip())#添加進程度副詞5for line in o.readlines():over.append(line.strip())#添加進程度副詞6for line in n2.readlines():no.append(line.strip().encode('utf-8'))#添加進否定詞
句子的情感分析與識別
# 句子情感的識別與分析line = self.textbox.toPlainText()#讀取用戶輸入aline = jieba.cut(line, cut_all=False)#對輸入進行分詞emotions =[]#情感詞匯數組emotion_value =0#初始情感值not_num =0#初始否定值為0emotion_times =1#初始程度副詞權重for word in aline:# print(word)if word in positive_emotion:emotion_value =1*((-1)** not_num)* emotion_timesemotions.append(emotion_value)not_num =0emotion_times =1# positiveelif word in negative_emotion:not_num = not_num +1emotion_value =1*((-1)** not_num)* emotion_timesemotions.append(emotion_value)not_num =0emotion_times =1# negativeelif word in extreme:emotion_times = emotion_times +2elif word in very:emotion_times = emotion_times +1.4elif word in more:emotion_times = emotion_times +1elif word in alittlebit:emotion_times = emotion_times +0.4elif word in insufficiently:emotion_times = emotion_times -0.2elif word in over:emotion_times = emotion_times +1.2elif word in no:not_num +=1elif word =="!":#如果是標點!,程度加1if emotions[len(emotions)-1]>0:emotions[len(emotions)-1]+=1else:emotions[len(emotions)-1]-=1mean_zhi=str(sum(emotions)/len(emotions))
import matplotlib.pyplot as plt
import jieba
import sys
import numpy as nmimport matplotlib
matplotlib.use('Qt5Agg')# 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends中 FigureCanvas繼承自QtWidgets.QWidget)from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import*import matplotlib.pyplot as plt
import sys
import numpy as npclassApp(QtWidgets.QDialog):def__init__(self, parent=None):# 父類初始化方法super(App, self).__init__(parent)self.initUI()definitUI(self):self.setWindowTitle('情感分析系統by(yudengwu)')# 幾個QWidgetsself.lb1 = QLabel("情感分析")self.lb2 = QLabel("情感分析均值(積極為正值,消極為負值):")self.lb3 = QLabel()self.lb4=QLabel("情緒波動方差:")self.lb5=QLabel()self.lb6 = QLabel("情緒波動曲線")self.textbox = QTextEdit()self.figure = plt.figure()self.canvas = FigureCanvas(self.figure)self.button_plot = QtWidgets.QPushButton("點擊情感分析")# 連接事件self.button_plot.clicked.connect(self.plot_)# 設置布局layout = QtWidgets.QVBoxLayout()layout.addWidget(self.lb1)layout.addWidget(self.textbox )layout.addWidget(self.lb2)layout.addWidget(self.lb3)layout.addWidget(self.lb4)layout.addWidget(self.lb5)layout.addWidget(self.lb6)layout.addWidget(self.canvas)layout.addWidget(self.button_plot)self.setLayout(layout)defplot_(self):# part 1:情感詞典錄入positive_emotion =[]#積極詞匯數據庫negative_emotion =[]#消極詞匯數據庫extreme =[]#程度副詞1very =[]#程度副詞2more =[]#程度副詞3alittlebit =[]#程度副詞4insufficiently =[]#程度副詞5over =[]#程度副詞6no =[]#否定詞d =open("positive-emotion.txt", encoding='utf-8')d2 =open("positive_evaluate.txt", encoding='utf-8')n =open("negative-emotion.txt", encoding='utf-8')n22 =open("negative_evaluate.txt", encoding='utf-8')e =open("extreme-6.txt", encoding='utf-8')v =open("very-5.txt", encoding='utf-8')m =open("more-4.txt", encoding='utf-8')a =open("alittlebit-3.txt", encoding='utf-8')i =open("insufficiently-2.txt", encoding='utf-8')o =open("over-1.txt", encoding='utf-8')n2 =open("no.txt", encoding='utf-8')for line in d.readlines():positive_emotion.append(line.strip())#添加進積極詞匯數據庫for line in d2.readlines():positive_emotion.append(line.strip())#添加進積極詞匯數據庫for line in n.readlines():negative_emotion.append(line.strip())#添加進消極詞匯數據庫for line in n22.readlines():negative_emotion.append(line.strip())#添加進消極詞匯數據庫for line in e.readlines():extreme.append(line.strip())#添加進程度副詞1for line in v.readlines():very.append(line.strip())#添加進程度副詞2for line in m.readlines():more.append(line.strip())#添加進程度副詞3for line in a.readlines():alittlebit.append(line.strip())#添加進程度副詞4for line in i.readlines():insufficiently.append(line.strip())#添加進程度副詞5for line in o.readlines():over.append(line.strip())#添加進程度副詞6for line in n2.readlines():no.append(line.strip().encode('utf-8'))#添加進否定詞# 句子情感的識別與分析# input =open(input.txt)# for line in open("out.txt").readlines():line = self.textbox.toPlainText()#讀取用戶輸入aline = jieba.cut(line, cut_all=False)#對輸入進行分詞emotions =[]#情感詞匯數組emotion_value =0#初始情感值not_num =0#初始否定值為0emotion_times =1#初始程度副詞權重for word in aline:# print(word)if word in positive_emotion:emotion_value =1*((-1)** not_num)* emotion_timesemotions.append(emotion_value)not_num =0emotion_times =1# positiveelif word in negative_emotion:not_num = not_num +1emotion_value =1*((-1)** not_num)* emotion_timesemotions.append(emotion_value)not_num =0emotion_times =1# negativeelif word in extreme:emotion_times = emotion_times +2elif word in very:emotion_times = emotion_times +1.4elif word in more:emotion_times = emotion_times +1elif word in alittlebit:emotion_times = emotion_times +0.4elif word in insufficiently:emotion_times = emotion_times -0.2elif word in over:emotion_times = emotion_times +1.2elif word in no:not_num +=1elif word =="!":if emotions[len(emotions)-1]>0:emotions[len(emotions)-1]+=1else:emotions[len(emotions)-1]-=1mean_zhi=str(sum(emotions)/len(emotions))self.lb3.setText(mean_zhi)qingxustd=str(nm.cov(emotions))self.lb5.setText(qingxustd)x1 =range(0,len(emotions))ax = self.figure.add_axes([0.1,0.1,0.8,0.8])ax.clear()# 每次繪制一個函數時清空繪圖ax.plot(x1, emotions, label='emotion values', marker='.', markerfacecolor='red', markersize=12)ax.set_xlabel('emotion_words_apper_times')ax.set_ylabel('emotion_value')#ax.legend()#ax.ylim(-10, 10)self.canvas.draw()# 解析上傳文件# 運行程序if __name__ =='__main__':app = QtWidgets.QApplication(sys.argv)main_window = App()main_window.show()app.exec()