Python 文本挖掘:使用情感词典进行情感分析(算法及程序设计)
这篇文章讲到了使用情感词典进行英文情感分析的方法和代码讲解,非常详细。
#! /usr/bin/env python2.7
#coding=utf-8
import pickle
import textprocessing as tp
import numpy as np
posdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/posdict.pkl', 'r'))
negdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/negdict.pkl', 'r'))
mostdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/mostdict.pkl', 'r'))
verydict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/verydict.pkl', 'r'))
moredict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/moredict.pkl', 'r'))
ishdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/ishdict.pkl', 'r'))
insufficientdict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/insufficentdict.pkl', 'r'))
inversedict = pickle.load(open('D:/code/sentiment_test/sentiment_dictionary/inversedict.pkl', 'r'))
review = pickle.load(open('D:/code/review_set/review_pkl/Motorala.pkl', 'r'))
def judgeodd(num):
if (num/2)*2 == num:
return 'even'
else:
return 'odd'
def sentiment_score_list(dataset):
cuted_data = []
for cell in dataset:
cuted_data.append(tp.cut_sentence(cell))
count1 = []
count2 = []
for sents in cuted_data: #循环遍历每一个评论
for sent in sents: #循环遍历评论中的每一个分句
segtmp = tp.segmentation(sent, 'list') #把句子进行分词,以列表的形式返回
i = 0 #记录扫描到的词的位置
a = 0 #记录情感词的位置
poscount = 0 #积极词的第一次分值
poscount2 = 0 #积极词反转后的分值
poscount3 = 0 #积极词的最后分值(包括叹号的分值)
negcount = 0
negcount2 = 0
negcount3 = 0
for word in segtmp:
if word in posdict: #判断词语是否是情感词
poscount += 1
c = 0
for w in segtmp[a:i]: #扫描情感词前的程度词
if w in mostdict:
poscount *= 4.0
elif w in verydict:
poscount *= 3.0
elif w in moredict:
poscount *= 2.0
elif w in ishdict:
poscount /= 2.0
elif w in insufficientdict:
poscount /= 4.0
elif w in inversedict:
c += 1
if judgeodd(c) == 'odd': #扫描情感词前的否定词数
poscount *= -1.0
poscount2 += poscount
poscount = 0
poscount3 = poscount + poscount2 + poscount3
poscount2 = 0
else:
poscount3 = poscount + poscount2 + poscount3
poscount = 0
a = i + 1 #情感词的位置变化
elif word in negdict: #消极情感的分析,与上面一致
negcount += 1
d = 0
for w in segtmp[a:i]:
if w in mostdict:
negcount *= 4.0
elif w in verydict:
negcount *= 3.0
elif w in moredict:
negcount *= 2.0
elif w in ishdict:
negcount /= 2.0
elif w in insufficientdict:
negcount /= 4.0
elif w in inversedict:
d += 1
if judgeodd(d) == 'odd':
negcount *= -1.0
negcount2 += negcount
negcount = 0
negcount3 = negcount + negcount2 + negcount3
negcount2 = 0
else:
negcount3 = negcount + negcount2 + negcount3
negcount = 0
a = i + 1
elif word == '!'.decode('utf8') or word == '!'.decode('utf8'): ##判断句子是否有感叹号
for w2 in segtmp[::-1]: #扫描感叹号前的情感词,发现后权值+2,然后退出循环
if w2 in posdict or negdict:
poscount3 += 2
negcount3 += 2
break
i += 1 #扫描词位置前移
#以下是防止出现负数的情况
pos_count = 0
neg_count = 0
if poscount3 < 0 and negcount3 > 0:
neg_count += negcount3 - poscount3
pos_count = 0
elif negcount3 < 0 and poscount3 > 0:
pos_count = poscount3 - negcount3
neg_count = 0
elif poscount3 < 0 and negcount3 < 0:
neg_count = -poscount3
pos_count = -negcount3
else:
pos_count = poscount3
neg_count = negcount3
count1.append([pos_count, neg_count])
count2.append(count1)
count1 = []
return count2
def sentiment_score(senti_score_list):
score = []
for review in senti_score_list:
score_array = np.array(review)
Pos = np.sum(score_array[:,0])
Neg = np.sum(score_array[:,1])
AvgPos = np.mean(score_array[:,0])
AvgNeg = np.mean(score_array[:,1])
StdPos = np.std(score_array[:,0])
StdNeg = np.std(score_array[:,1])
score.append([Pos, Neg, AvgPos, AvgNeg, StdPos, StdNeg])
return score
Python 文本挖掘:使用情感词典进行情感分析(算法及程序设计)的更多相关文章
- 基于情感词典的python情感分析
近期老师给我们安排了一个大作业,要求根据情感词典对微博语料进行情感分析.于是在网上狂找资料,看相关书籍,终于搞出了这个任务.现在做做笔记,总结一下本次的任务,同时也给遇到有同样需求的人,提供一点帮助. ...
- Python 爬取淘宝商品数据挖掘分析实战
Python 爬取淘宝商品数据挖掘分析实战 项目内容 本案例选择>> 商品类目:沙发: 数量:共100页 4400个商品: 筛选条件:天猫.销量从高到低.价格500元以上. 爬取淘宝商品 ...
- 理解 python metaclass使用技巧与应用场景分析
理解python metaclass使用技巧与应用场景分析 参考: decorator与metaclass:http://jfine-python-classes.readthedocs. ...
- Python学习二:词典基础详解
作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...
- HanLP用户自定义词典源码分析
HanLP用户自定义词典源码分析 1. 官方文档及参考链接 关于词典问题Issue,首先参考:FAQ 自定义词典其实是基于规则的分词,它的用法参考这个issue 如果有些数量词.字母词需要分词,可参考 ...
- python实现归并排序,归并排序的详细分析
python实现归并排序,归并排序的详细分析. 学习归并排序的过程是十分痛苦的.它并不常用,看起来时间复杂度好像是几种排序中最低的,比快排的时间复杂度还要低,但是它的执行速度不是最快的.很多朋友不 ...
- Python --深入浅出Apriori关联分析算法(二) Apriori关联规则实战
上一篇我们讲了关联分析的几个概念,支持度,置信度,提升度.以及如何利用Apriori算法高效地根据物品的支持度找出所有物品的频繁项集. Python --深入浅出Apriori关联分析算法(一) 这次 ...
- python导入csv文件出现SyntaxError问题分析
python导入csv文件出现SyntaxError问题分析 先简单描述下碰到的题目,要求是写出2个print的结果 可以看到,a指向了一个列表list对象,在Python中,这样的赋值语句,其实内部 ...
- Python中的浮点数原理与运算分析
Python中的浮点数原理与运算分析 本文实例讲述了Python中的浮点数原理与运算.分享给大家供大家参考,具体如下: 先看一个违反直觉的例子: >>> s = 0. > ...
随机推荐
- GitHub webstorm 及 README.md 姿势
README.md 语法格式: 规范的README文件开头都写上一个标题,这被称为大标题. 标题: #一级标题 ##二级标题 ###三级标题 ####四级标题 #####五级标题 ######六级标题 ...
- 新版mysql 5.7的group_by非常不和谐
sqlalchemy.exc.OperationalError OperationalError: (_mysql_exceptions.OperationalError) (1055, " ...
- Ubuntu 如何将桌面上的Home中的文件夹除去
安装Ubuntu后, 由于无法用Terminal(终端)进入带中文的文件夹,会引起很多操作不便.很多朋友想到了将它们都改成中文,但是当再次开机重启使却会发现,原本光洁的桌面现在竟然出现了一堆文件夹?? ...
- nutzwk运行后wk-web中生成ehcache.disk.store.dir有什么用,怎么去掉
nutzwk运行后wk-web中生成ehcache.disk.store.dir有什么用,怎么去掉 发布于 29天前 作者 qq_96c46988 64 次浏览 复制 上一个帖子 下一个帖 ...
- 广搜,智能拼图(ZOJ1079)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=79 解题报告: 思路简单,写法太难. #include <std ...
- Buffer实例
互联网的基础是数据的传送,一切都围绕着数据展开,比如发送啊,接收啊,这一切都离不开网络,通过之前,学会了通过http模块来搭建一个服务器,也实现了网络爬虫,nodejs中网络的部分,Net这个模块,对 ...
- node执行环境
nodejs本质上是一个javascript的执行环境,只是由于他的封装,加上更多web底层的一个处理,赋予了更多的能力,那么执行环境到底是什么呢,我们到浏览器里面体验看看,在chrome里面控制台, ...
- 2295: KMP模式匹配 一(串)
2295: KMP模式匹配 一(串) 时间限制: 1 Sec 内存限制: 128 MB提交: 210 解决: 97[提交][状态][讨论版][命题人:外部导入] 题目描述 求子串的next值,用n ...
- lasagne保存网络参数
# Optionally, you could now dump the network weights to a file like this: # np.savez('model.npz', *l ...
- mysql如何查看错误代码具体释义?(基于perror)
mysql如何查看错误代码具体释义? 关键词:mysql错误代码,mysql错误号 perror 错误号