资源简介
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+ 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 名字用字排行
评论
共有 条评论