#从excel读取数据,利用情感词典进行文本分类
数据集:从微博爬取的美妆博主的评论
情感词典:BosonNLP_情感词典
准确率:预测了3000条数据,准确率约53%。对中性评论不敏感。
import openpyxl
import codecs
from collections import defaultdict
import jieba
import xlrd
#分词,去除停用词
def seg_word (sentence):
#分词
seg_list = jieba.cut(sentence)
seg_result = []
for w in seg_list:
seg_result.append(w)
#读取停用词
stopwords = set()
fr = codecs.open('D:\个人资料\数据集\情感词典_微博专用\chineseStopWords.txt','r','utf-8')
for word in fr:
stopwords.add(word.strip())
fr.close()
#去除停用词
return list (filter(lambda x: x not in stopwords, seg_result))
#对分词结果分类:情感词、否定词、程度副词
#key未索引,value为权值
def classify_words(word_list):
#读取情感词典
sen_file = open('D:\个人资料\数据集\BosonNLP_情感词典\BosonNLP_sentiment_score\BosonNLP_sentiment_score.txt','r+',encoding='utf-8')
#获取字典内容
#去除‘\n’
sen_list = sen_file.read().splitlines()
#创建情感词典
sen_dict = defaultdict()
#读取字典文件每一行内容,将其转换为字典对象,key为情感词,value为对应的分值
for s in sen_list:
<