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. > ...
随机推荐
- 安装Android模拟器Genymotion【Android学习入门】
安装Android模拟器Genymotion 推荐教程:一个强大的Android模拟器Genymotion具体内容如下: 相信很多Android开发者一定受够了速度慢.体验差效率及其地下的官方模拟器了 ...
- SpringCloud的学习记录(3)
这一章节讲搭建config-server的项目. 在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Arti ...
- react-native 视频播放器(很不错哦)
第一步: npm i -S react-native-af-video-player(安装前:先安装: react-native-video.react-native-keep-awake.react ...
- vos设置禁止被叫特定号码段特定区域
问题: 为了防止卡线遭投诉被运营商停,给公司带来损失,对一些特定号段特定区域要进行限制,不让客户呼出 打开VOS3000 落地网关——补充设置——落地被叫前缀——禁止 添加禁止号段 具体案例: 如填写 ...
- phpstorm 2017.1 激活
打开网址 http://idea.lanyus.com/ 选择获取注册码,复制生成的验证码 安装完成后,打开软件,依次选择菜单栏 Help -> Register-> Activation ...
- 框架: namespace和use的区别以及使用注意项
我们在使用框架的时候,总会使用到namespace和Use这两个东西,我们先来看它们存在的意义 namespace:是指我们当前类中,所在的位置.使用namespace关键字的话,我们就可以达到效果: ...
- 用户表单事件(focus事件)
以前做用户系统的时候经常用到表单验证,正则表达式事件来处理和绑定事件和进行事件,这里说的其实只是一小部分,也不是很值得写,但是今天遇到了还是写一下,毕竟基础还是蛮重要的,就算懂的童鞋,巩固一下也是好的 ...
- Selenium入门系列5 默认不显示的下拉列表元素操作
本节课程的下拉框是那种默认隐藏,当鼠标移到菜单上下拉框才显示的.如果直接getelement会报错,提示元素不可见: so,得先让下拉列表显示出来再获取元素 用到的新知识: is_display() ...
- IOS 线程的总结(及cell的图片下载)
零.线程的注意点(掌握) 1.不要同时开太多的线程(1~3条线程即可,不要超过5条)2.线程概念1> 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件2> 子线程 : 后台线程 ...
- IOC、注入
转:https://blog.csdn.net/lutianfeiml/article/details/51731219 实际开发中使用XML还是注解 XML: bean管理 注解: 注入属性的时候比 ...