资源简介
用python实现基于情感词典的情感分析
大数据分析

代码片段和文件信息
# -*- coding: utf-8 -*-i
#!/usr/bin/python
import sys
import json
from smallseg import SEG
seg = SEG()
reload(sys)
sys.setdefaultencoding(“utf-8“)
def loadDict(fileName score):
wordDict = {}
with open(fileName) as fin:
for line in fin:
word = line.strip()
wordDict[word] = score
return wordDict
def appendDict(wordDict fileName score):
with open(fileName) as fin:
for line in fin:
word = line.strip()
wordDict[word] = score
def loadExtentDict(fileName):
extentDict = {}
for i in range(6):
with open(fileName + str(i + 1) + “.txt“) as fin :
for line in fin:
word = line.strip()
extentDict[word] = i + 1
return extentDict
postDict = loadDict(u“sentimentDict/正面情感词语(中文).txt“ 1)
appendDict(postDict u“sentimentDict/正面评价词语(中文).txt“ 1)
appendDict(postDict u“sentimentDict/正面评价词语(中文)1.txt“ 1)
appendDict(postDict u“sentimentDict/正面评价词语(中文)2.txt“ 1)
negDict = loadDict(u“sentimentDict/负面情感词语(中文).txt“ -1)
appendDict(negDict u“sentimentDict/负面评价词语(中文).txt“ -1)
extentDict = loadExtentDict(u“sentimentDict/程度级别词语(中文)“)
inverseDict = loadDict(u“sentimentDict/否定词语.txt“ -1)
punc = loadDict(u“sentimentDict/标点符号.txt“ 1)
exclamation = {“!“:2 “!“:2}
for line in sys.stdin:
line = line.strip()
record = json.loads(line[1:-1])
key = record[“weiboId“]
content = record[“content“]
wordList = seg.cut(content)
wordList.reverse()
lastWordPos = 0
lastPuncPos = 0
i = 0
for word in wordList:
word = word.encode(“utf-8“)
if word in punc:
lastPuncPos = i
if word in postDict:
if lastWordPos > lastPuncPos:
start = lastWordPos
else:
start = lastPuncPos
score = 1
#print “start: “ + str(start)
#print “end: “ + str(i)
for word_before in wordList[start:i]:
word_before = word_before.encode(“utf-8“)
if word_before in extentDict:
score = score * extentDict[word_before]
if word_before in inverseDict:
score = score * -1
for word_after in wordList[i+1:]:
word_after = word_after.encode(“utf-8“)
if word_after in punc:
if word_after in exclamation:
score = score + 2
else:
break;
#print ‘%s\t%s\t%s‘ % (key word score)
print ‘%s\t%s‘ % (key score)
lastWordPos = i
elif word in negDict:
if lastWordPos > lastPuncPos:
start = lastWordPos
else:
start = lastPuncPos
score = -1
#print “start: “ + str(start)
#print “end: “ + str(i)
for word_before in wordList[start:i]:
word_before = word_before.encode(“utf-8“)
if word_before in extentDict:
score = score * extentDict[word_before]
if word_before in inverseDict:
score = score * -1
for word_after in wordList[i+1:]:
word_after = word_after.encode(“utf-8“)
if word_after in punc:
if word_after in exclamation:
score = score - 2
else:
break;
#print ‘%s\t%s\t%s‘ % (key word score)
print ‘%s\t%s‘ % (key score)
lastWordPo
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 207606 2015-12-24 22:37 基于情感词典进行情感分析\10000+运行结果.txt
文件 3184 2015-12-24 00:01 基于情感词典进行情感分析\mapper.py
文件 391 2015-12-24 00:01 基于情感词典进行情感分析\reducer.py
文件 289 2015-12-19 21:52 基于情感词典进行情感分析\情感词典\主张词语(中文).txt
文件 140 2015-12-19 22:14 基于情感词典进行情感分析\情感词典\否定词语.txt
文件 5 2015-12-22 19:24 基于情感词典进行情感分析\情感词典\感叹号.txt
文件 6252 2015-12-19 21:51 基于情感词典进行情感分析\情感词典\正面情感词语(中文).txt
文件 30384 2015-12-19 21:51 基于情感词典进行情感分析\情感词典\正面评价词语(中文).txt
文件 15905 2015-12-19 21:51 基于情感词典进行情感分析\情感词典\正面评价词语(中文)1.txt
文件 14973 2015-12-19 21:51 基于情感词典进行情感分析\情感词典\正面评价词语(中文)2.txt
文件 72 2015-12-19 21:49 基于情感词典进行情感分析\情感词典\程度级别1词语(中文).txt
文件 174 2015-12-19 21:49 基于情感词典进行情感分析\情感词典\程度级别2词语(中文).txt
文件 224 2015-12-19 21:50 基于情感词典进行情感分析\情感词典\程度级别3词语(中文).txt
文件 218 2015-12-19 21:50 基于情感词典进行情感分析\情感词典\程度级别4词语(中文).txt
文件 170 2015-12-19 21:50 基于情感词典进行情感分析\情感词典\程度级别5词语(中文).txt
文件 430 2015-12-19 21:50 基于情感词典进行情感分析\情感词典\程度级别6词语(中文).txt
文件 9927 2015-12-19 21:51 基于情感词典进行情感分析\情感词典\负面情感词语(中文).txt
文件 26076 2015-12-19 21:51 基于情感词典进行情感分析\情感词典\负面评价词语(中文).txt
文件 88 2015-12-24 23:10 基于情感词典进行情感分析\结果汇总.txt
目录 0 2016-01-11 23:38 基于情感词典进行情感分析\情感词典
目录 0 2016-01-11 23:39 基于情感词典进行情感分析
----------- --------- ---------- ----- ----
316508 21
- 上一篇:逐像元趋势分析+F值检验
- 下一篇:python音乐源代码
相关资源
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
- python 名字用字排行
评论
共有 条评论