资源简介
python实现基于词典的文本情感分析,包含测试数据和实现代码
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Sun May 14 16:04:08 2017
wordcloud required C++ 14.0
running on python 3.5
@author: wangmin
“““
import jieba
import collections
import numpy as np
from PIL import Image
from wordcloud import WordCloud ImageColorGenerator
import matplotlib.pyplot as plt
# 读入评论数据,正负情感词典并合并
evaluation = []
stopwords = []
pos = []
neg = []
mydict = []
infile = open(“evaluation.csv“ ‘r‘)
for line in infile:
data = line.rstrip().split(‘‘)
evaluation.append(data[1])
del evaluation[0]
infile = open(“negative.csv“ ‘r‘)
for line in infile:
data = line.rstrip().split(‘‘)
neg.append(data[1])
infile = open(“positive.csv“ ‘r‘)
for line in infile:
data = line.rstrip().split(‘‘)
pos.append(data[1])
mydict = pos + neg
file = open(“stopwords.csv“ ‘r‘)
for s in file:
data = s.rstrip().split(‘‘)
stopwords.append(data[1])
# 对每条评论分词并保存分词结果
eva = []
for i in range(len(evaluation)):
seg_list = jieba.cut(evaluation[i] cut_all=False)
seg_list = list(seg_list)
eva.append(seg_list)
# 删除一个字的词
new_eva = eva
tmp = []
t = 0
for j in range(3321):
for k in range(len(eva[j])):
if len(eva[j][k]) >= 2:
tmp.append(eva[j][k])
new_eva[t] = tmp
tmp = []
t=t+1
# 删除停止词(对分析没有意义的词)
#for word in stopwords:
# 自定义情感类型得分函数
def GetScore(list):
neg_s = 0
pos_s = 0
for w in list:
if (w in neg) == True:
neg_s = neg_s + 1
elif (w in pos) == True:
pos_s = pos_s + 1
if (neg_s-pos_s) > 0:
score = ‘NEGATIVE‘
return score
elif (neg_s-pos_s) < 0:
score = ‘POSITIVE‘
return score
else:
score = ‘NEUTRAL‘
return score
# 计算每条评论的正负得分
Score = []
for l in range(len(new_eva)):
Score.append(GetScore(new_eva[l]))
‘‘‘
def find_all_index(arritem):
return [i for ia in enumerate(arr) if a==item]
NEG=find_all_index(Score‘NEGATIVE‘)
POS=find_all_index(Score‘POSITIVE‘)
NEU=find_all_index(Score‘NEUTRAL‘)
print(len(NEG))
print(len(POS))
print(len(NEU))
‘‘‘
# 统计词频
wf = {}
for p in range(len(new_eva)):
for word in new_eva[p]:
if word not in wf:
wf[word]=0
wf[word]+=1
def Sort_by_count(d):
d = collections.OrderedDict(sorted(d.items()key = lambda t: -t[1]))
return d
wf = Sort_by_count(wf)
top_key = []
top_word = []
for key in wf.items():
top_key.append(key)
top_word = top_key[1:51]
print(top_key[0:49])
#for keyvalues in wf.items():
# print(key + “%d“ % values)
# 绘制词云
word_space_split = ‘a‘
for i in range(3322):
new_eva[i] = “ “.join(new_eva[i])
word_space_split += new_eva[i]
word_space_split = word_space_split.replace(‘word‘‘‘)
abel_mask = np.array(Image.open(‘C:/Users/wangmin/Pictures/aaa/ab
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-11-04 13:47 python实现基于词典的文本情感分析\
文件 3902 2017-05-16 01:37 python实现基于词典的文本情感分析\python\python实现.py
文件 124695 2017-05-14 16:35 python实现基于词典的文本情感分析\python\positive.csv
文件 110411 2017-05-14 16:33 python实现基于词典的文本情感分析\python\negative.csv
文件 254017 2017-05-14 16:10 python实现基于词典的文本情感分析\python\evaluation.csv
文件 33619 2017-05-15 17:23 python实现基于词典的文本情感分析\python\Rplot.png
目录 0 2019-11-04 13:47 python实现基于词典的文本情感分析\python\
文件 124468 2017-05-14 22:33 python实现基于词典的文本情感分析\python\stopwords.csv
目录 0 2017-05-16 01:37 python\
文件 33619 2017-05-15 17:23 python\Rplot.png
文件 254017 2017-05-14 16:10 python\evaluation.csv
文件 110411 2017-05-14 16:33 python\negative.csv
文件 124695 2017-05-14 16:35 python\positive.csv
文件 3902 2017-05-16 01:37 python\python实现.py
文件 124468 2017-05-14 22:33 python\stopwords.csv
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论