目录
1、情感分析介绍
情感分析是一种自然语言处理技术,旨在识别文本中的情感并将其分类为积极、消极或中性。它通过使用机器学习算法和自然语言处理技术来自动分析文本中的情感,从而帮助人们更好地理解文本的情感含义。
本文以某译本new_deepl_translated.txt为分析对象,通过对译文断章切句,进而对每一个句子情感词提取、情感值计算,以及词频统计,最后保存为excel文件。
new_deepl_translated.txt部分内容如下:
情感值计算结果:
词频统计结果:
2、基于大连理工情感词汇方法
2.1加载大连理工情感词典,程度副词典,否定词典,停用词典
各词典大家可以从网上进行下载,顶部我放了一个资源包可以直接拿来用,或者直接网盘提取:
链接: https://pan.baidu.com/s/1Cke-6qyB0dzgNxBwxgL_6Q 提取码: pi7
定义一个类sa,用于加载情感词典、程度副词典、否定词典、停用词典,以及分词和去除停用词。针对大连理工情感词典,我只需要加载词语、情感分类、强度、极性这4列内容。
class sa:
# 加载情感词典、程度副词典、否定词典、停用词典
def __init__(self, senti_dict_path, degree_dict_path, not_dict_path, stopword_path):
self.senti_dict = self.load_dict(senti_dict_path)
self.degree_dict = self.load_degree_dict(degree_dict_path)
self.not_dict = self.load_not_dict(not_dict_path)
self.stopwords = self.load_stopwords(stopword_path)
# 加载情感词典
def load_dict(self, path):
with open(path, 'r', encoding='utf-8') as f:
lines = f.readlines()
word_dict = {}
for line in lines:
items = line.strip().split('\t')
if len(items) >= 7:
word = items[0] #情词
emotion = items[4] #情词类别
strength = int(items[5]) #情词强度
polarity = int(items[6]) #情词极性
word_dict[word] = {'emotion': emotion, 'strength': strength, 'polarity': polarity}
return word_dict
# 加载程度副词词典
def load_degree_dict(self, path):
with open(path, 'r', encoding='utf-8') as f:
lines = f.readlines()
degree_dict = {}
for line in lines:
items = line.strip().split('\t')
if len(items) >= 2:
degree_word = items[0] #副词
degree_value = float(items[1]) #副词权值
degree_dict[degree_word] = degree_value
return degree_dict
# 加载否定词词典
def load_not_dict(self, path):
with open(path, 'r', encoding='utf-8') as f:
lines = f.readlines()
not_dict = {}
for line in lines:
items = line.strip().split('\t')
if len(items) >= 1:
not_word = items[0] #否定词