日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

情感分析基于词典(算例代码)

發布時間:2024/9/30 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 情感分析基于词典(算例代码) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

基于詞典的情感分析

情感分析是指挖掘文本表達的觀點,識別主體對某客體的評價是褒還是貶,褒貶根據進態度行傾向性研究。文本情感分析可以分為基于機器學習的情感分類方法和基于語義理解的情感分析。基于機器學習進行語義分析的話需要大量的訓練集,同時需要人工對其進行分類標注。本文采用基于詞典的方法的進行情感分析。
詞典情感分析流程圖如下:大致意思就是將輸入的文本進行分詞,將分的詞和詞典數據庫的的詞進行匹配。看是屬于積極還是消極,否定,還是程度詞。然后按照人為定義的打分公式對每個詞進行情感打分。每個詞匯的情感平均值作為整個句子的情感得分。本文定義的打分公式為:
emotion_value = 1 * ((-1) ** not_num) * emotion_times
式子中not_num為否定詞,如果一個詞為積極詞,則not_num=0.否定詞則not_num=1.。emotion_times為程度初始值,初始值為1,如果一個詞匯前面出現一個程度副詞,emotion_times應加上這個程度副詞得數值。


由于是基于詞典的情感分析方法。首先準備好幾個本地詞匯文件。
積極詞匯.txt, 消極詞匯txt, 否定詞匯.txt ,程度副詞1.txt,,程度副詞2.txt,,程度副詞3.txt,,程度副詞4.txt,,程度副詞5.txt,,程度副詞6.txt,。程度副詞由于有多種程度不一的程度副詞如好,非常好。所以準備多個文件。
讀取詞匯文件并添加進各自數組:

# 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))

建立pyqt5的簡單頁面
頁面

import 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 np class App(QtWidgets.QDialog):def __init__(self, parent=None):# 父類初始化方法super(App, self).__init__(parent)self.initUI()def initUI(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)# 運行程序 if __name__ == '__main__':app = QtWidgets.QApplication(sys.argv)main_window = App()main_window.show()app.exec()

將情感分析部分添加進去作為事件:
總代碼如下

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 npclass App(QtWidgets.QDialog):def __init__(self, parent=None):# 父類初始化方法super(App, self).__init__(parent)self.initUI()def initUI(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)def plot_(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()

運行結果示范:

詞典的優劣決定著模型的好壞。
數據集鏈接:
中文情感分析詞典數據集(基于詞典).zip

電氣專業的計算機小白: 余登武,寫博文不容易,如果你覺得本文對你有用,請點個贊支持下,謝謝。

總結

以上是生活随笔為你收集整理的情感分析基于词典(算例代码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美久久影院 | 黄色一级欧美 | 日韩区在线| 午夜少妇久久久久久久久 | 在线观看亚洲精品 | 国产精品19p | 亚洲精选免费 | 日韩人妻无码精品久久久不卡 | 双性高h1v1 | 亚洲午夜久久久久久久国产 | 欧美日韩在线影院 | 欧美大片在线免费观看 | 麻豆av网| av国产片 | 一级色网站 | 国产美女又黄又爽又色视频免费 | 一级黄色片免费观看 | 美国福利片 | 爽爽影院在线免费观看 | 一区二区三区视频播放 | 女上男下动态图 | 亚洲人成亚洲人成在线观看 | 日本男男激情gay办公室 | 五十路妻| 干一夜综合 | 一区二区三区四区高清视频 | 国产在线久 | 亚洲国产情侣 | 久久久成人精品一区二区三区 | 日本一二三视频 | 欧洲色视频| 俺来也av | 免费在线观看a视频 | 强乱中文字幕 | 免费av观看 | 91亚洲精品在线 | 久久久嫩草 | 一本一道波多野结衣一区二区 | 中文字幕在线播放一区二区 | 97超碰在线资源 | 在线一区av | 人妻少妇精品久久 | av电影网站在线观看 | av一区在线播放 | 国产精品人妻一区二区三区 | 青青超碰 | 永久免费av在线 | 欧美爱爱一区二区 | av鲁丝一区二区鲁丝 | 日韩不卡一二区 | 中文日韩字幕 | 少妇一级淫片 | 国产一区综合 | 女女百合高h喷汁呻吟玩具 www.亚洲一区 | 日韩18p| 成人特级毛片69免费观看 | 亚洲日本在线观看 | 欧美黄色大片免费看 | 欧美 日韩 国产 成人 在线 91 | 色欲av无码一区二区三区 | beeg日本高清xxxx18| 免费看毛片网站 | 日本网站在线免费观看 | 日韩在线观看视频一区二区三区 | 性欧美大战久久久久久久久 | 久久久av一区二区三区 | 亚洲小说在线 | 高清一区二区三区 | 九九在线观看高清免费 | 国产福利视频在线 | 少妇被躁爽到高潮无码文 | 久久婷婷国产麻豆91 | 国产同性人妖ts口直男 | 中国一级特黄真人毛片免费观看 | 精品视频在线免费看 | 91鲁| 欧美交受高潮1 | 久久涩| 蜜乳av中文字幕 | 中文一区二区在线观看 | 日韩操操操 | www.三区 | 日韩免费福利 | 久久精品黄 | 国产精品毛片一区视频播 | 午夜免费网站 | 五月六月丁香 | 国产香蕉视频在线观看 | 黄色成人小视频 | 91超碰人人| 精品一区二区精品 | 葵司有码中文字幕二三区 | 亚洲成人无码久久 | 狠狠操2019 | 久久精品三级 | 茄子av| 亚洲aⅴ在线观看 | 久久性爱视频网站 | 谁有免费的黄色网址 |