日韩性视频-久久久蜜桃-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

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

總結

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

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

主站蜘蛛池模板: 中文字幕有码在线 | 国产精品亚洲欧美在线播放 | 日韩三级久久 | 成年人拍拍视频 | 日本后进式猛烈xx00动态图 | 一区二区导航 | 久久99精品国产麻豆91樱花 | 日韩精品视频一区二区 | 在线h网站 | 白俄罗斯毛片 | 国产99在线播放 | 国产在线不卡视频 | 亚洲国产欧美自拍 | 吃奶av | 97超碰人人在线 | 天堂av电影在线观看 | 老司机成人免费视频 | 原来神马电影免费高清完整版动漫 | 99久久久久久 | 牛人盗摄一区二区三区视频 | 免费在线观看成人 | 国产中文字幕第一页 | 日韩精品1区2区3区 欧美一本 | 亚洲三级视频在线观看 | 国产精品5区 | 美女露隐私网站 | 韩国日本中文字幕 | 久久五十路| 一边摸一边做爽的视频17国产 | 色乱码一区二区三区在线男奴 | 国产精品igao视频 | 日本a级片免费 | 91网站在线看 | 91久久爽久久爽爽久久片 | 美日韩一级 | 国产精品suv一区二区三区 | 日韩精品一二三四区 | 亚洲少妇毛片 | 免费午夜网站 | 国产自产在线视频 | 9191av| 国产正在播放 | 日本一本久久 | 欧美字幕| 成人污网站 | 欧美日韩午夜 | 中文字幕素人 | 亚洲九区| 精品人妻人人做人人爽夜夜爽 | 久久精品色欲国产AV一区二区 | 久草视频在 | 国产精品日韩精品欧美精品 | 绝顶高潮videos合集 | 久久久久噜噜噜亚洲熟女综合 | 91av视频网 | www.久久av.com| 日韩欧美网址 | 国产做爰视频免费播放 | 亚洲aav | 亚洲天堂av一区二区三区 | 蜜桃精品视频在线观看 | 欧美一级久久久 | ass亚洲熟妇毛耸耸pics | 91精品一区二区三区综合在线爱 | 神马久久影院 | 欧美激情网| 国产人伦精品一区二区三区 | 亚洲免费黄色片 | 欧美黑人猛交 | 一级成人毛片 | 国产成人自拍在线 | 久久欧美视频 | 国产熟女一区二区丰满 | 中文在线亚洲 | 欧美精品色婷婷五月综合 | 三级视频在线 | 亚洲一级二级三级 | 青草草在线 | 国产操女人 | 最新国产视频 | 欧美乱大交xxxxx潮喷 | 视频一区二区在线 | 成人做受黄大片 | www.精品视频| 欧美xxxxxhd| 91丨porny丨海角社区 | 十八禁一区二区三区 | 韩日av一区二区 | 国产精品久久久久三级 | 亚洲毛片儿 | 日韩特级毛片 | 国产又色又爽又黄又免费 | h视频在线免费观看 | 免费古装一级淫片潘金莲 | 国产成人无码精品 | 日韩中文字幕国产 | 少妇2做爰bd在线意大利堕落 | 人妻一区二区三区在线 | 精品一区二区三区成人免费视频 |