写完毕业论文很久了,现在开始来写这篇博客
我的本科毕业论文是《融合图书评论情感分析、图书评分和用户评分的图书推荐系统》
其中一部分就运用到了自然语言处理中的情感分析,我用的是深度学习的方法解决,用的深度学习的Keras框架
语料数据来源于公开的ChineseNlpCorpus的数据集online_shopping_10_cats,截取其中的图书评论数据作为后面长短记忆神经网络的训练集。项目地址:https://github.com/liuhuanyong/ChineseNLPCorpus
1、情感分析的语料处理
在对图书评论的数据进行情感分析之前,去掉语料中的标点符号、特殊符号、英文字母、数字等没用的信息,这里使用的是正则表达式去除,运用jieba汉语分词库对评论文本进行分词。之后去掉一些停用词,使用的是哈工大的停用词库,将哈工大停用词库的数据读取成集合,每次对评论文本分词后的词语判断是否存在于停用词的集合当中,如果存在则去掉,否则进入下一步。最后形成一个常用词的词袋。
import collections
import pickle
import re
import jieba
# 数据过滤
def regex_filter(s_line):
# 剔除英文、数字,以及空格
special_regex = re.compile(r"[a-zA-Z0-9\s]+")
# 剔除英文标点符号和特殊符号
en_regex = re.compile(r"[.…{|}#$%&\'()*+,!-_./:~^;<=>?@★●,。]+")
# 剔除中文标点符号
zn_regex = re.compile(r"[《》、,“”;~?!:()【】]+")
s_line = special_regex.sub(r"", s_line)
s_line = en_regex.sub(r"", s_line)
s_line = zn_regex.sub(r"", s_line)
return s_line
# 加载停用词
def stopwords_list(file_path):
stopwords = [line.strip() for line in open(file_path, 'r', encoding='utf-8').readlines()]
return stopwords
#主函数开始
word_freqs = collections.Counter() # 词频
stopword = stopwords_list("stopWords.txt") #加载停用词
max_len = 0
with open("Corpus.txt", "r", encoding="utf-8",errors='ignore') as f:
for line in f:
comment , label = line.split(",")
sentence = comment.replace("\n","")
# 数据预处理
sentence = regex_filter(sentence) #去掉评论中的数字、字符、空格
words = jieba.cut(sentence) #使用jieba进行中文句子分词
x =