NLP之情感分析:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)之全部代码

目录

全部代码


相关文章
NLP之情感分析:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)
NLP之情感分析:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)之全部代码

全部代码

# coding:utf-8
import jieba
import numpy as np#打开词典文件,返回列表
def open_dict(Dict = 'hahah', path=r'data/Textming'):path = path + '%s.txt' % Dictdictionary = open(path, 'r', encoding='utf-8')dict = []for word in dictionary:word = word.strip('\n')dict.append(word)return dictdef judgeodd(num):if (num % 2) == 0:return 'even'else:return 'odd'#注意,这里你要修改path路径。
deny_word = open_dict(Dict = '否定词', path= r'F:/File_Python/Resources/data/Textming/')
posdict = open_dict(Dict = 'positive', path= r'F:/File_Python/Resources/data/Textming/')
negdict = open_dict(Dict = 'negative', path= r'F:/File_Python/Resources/data/Textming/')degree_word = open_dict(Dict = '程度级别词语', path= r'F:/File_Python/Resources/data/Textming/')
mostdict = degree_word[degree_word.index('extreme')+1 : degree_word.index('very')]#权重4,即在情感词前乘以4
verydict = degree_word[degree_word.index('very')+1 : degree_word.index('more')]#权重3
moredict = degree_word[degree_word.index('more')+1 : degree_word.index('ish')]#权重2
ishdict = degree_word[degree_word.index('ish')+1 : degree_word.index('last')]#权重0.5def sentiment_score_list(dataset):seg_sentence = dataset.split('。')count1 = []count2 = []for sen in seg_sentence: #循环遍历每一个评论segtmp = jieba.lcut(sen, cut_all=False)  #把句子进行分词,以列表的形式返回i = 0 #记录扫描到的词的位置a = 0 #记录情感词的位置poscount = 0 #积极词的第一次分值poscount2 = 0 #积极词反转后的分值poscount3 = 0 #积极词的最后分值(包括叹号的分值)negcount = 0negcount2 = 0negcount3 = 0for word in segtmp:if word in posdict:  # 判断词语是否是情感词poscount += 1c = 0for w in segtmp[a:i]:  # 扫描情感词前的程度词if w in mostdict:poscount *= 4.0elif w in verydict:poscount *= 3.0elif w in moredict:poscount *= 2.0elif w in ishdict:poscount *= 0.5elif w in deny_word:c += 1if judgeodd(c) == 'odd':  # 扫描情感词前的否定词数poscount *= -1.0poscount2 += poscountposcount = 0poscount3 = poscount + poscount2 + poscount3poscount2 = 0else:poscount3 = poscount + poscount2 + poscount3poscount = 0a = i + 1  # 情感词的位置变化elif word in negdict:  # 消极情感的分析,与上面一致negcount += 1d = 0for w in segtmp[a:i]:if w in mostdict:negcount *= 4.0elif w in verydict:negcount *= 3.0elif w in moredict:negcount *= 2.0elif w in ishdict:negcount *= 0.5elif w in degree_word:d += 1if judgeodd(d) == 'odd':negcount *= -1.0negcount2 += negcountnegcount = 0negcount3 = negcount + negcount2 + negcount3negcount2 = 0else:negcount3 = negcount + negcount2 + negcount3negcount = 0a = i + 1elif word == '!' or word == '!':  ##判断句子是否有感叹号for w2 in segtmp[::-1]:  # 扫描感叹号前的情感词,发现后权值+2,然后退出循环if w2 in posdict or negdict:poscount3 += 2negcount3 += 2breaki += 1 # 扫描词位置前移# 以下是防止出现负数的情况pos_count = 0neg_count = 0if poscount3 < 0 and negcount3 > 0:neg_count += negcount3 - poscount3pos_count = 0elif negcount3 < 0 and poscount3 > 0:pos_count = poscount3 - negcount3neg_count = 0elif poscount3 < 0 and negcount3 < 0:neg_count = -poscount3pos_count = -negcount3else:pos_count = poscount3neg_count = negcount3count1.append([pos_count, neg_count])count2.append(count1)count1 = []return count2def 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])AvgPos = float('%.1f'%AvgPos)AvgNeg = np.mean(score_array[:, 1])AvgNeg = float('%.1f'%AvgNeg)StdPos = np.std(score_array[:, 0])StdPos = float('%.1f'%StdPos)StdNeg = np.std(score_array[:, 1])StdNeg = float('%.1f'%StdNeg)score.append([Pos, Neg, AvgPos, AvgNeg, StdPos, StdNeg]) #积极、消极情感值总和(最重要),积极、消极情感均值,积极、消极情感方差。return scoredef EmotionByScore(data):result_list=sentiment_score(sentiment_score_list(data))return result_list[0][0],result_list[0][1]def JudgingEmotionByScore(Pos, Neg):if Pos > Neg:str='1'elif Pos < Neg:str='-1'elif Pos == Neg:str='0'return strdata1= '今天上海的天气真好!我的心情非常高兴!如果去旅游的话我会非常兴奋!和你一起去旅游我会更加幸福!'
data2= '救命,你是个坏人,救命,你不要碰我,救命,你个大坏蛋!'
data3= '美国华裔科学家,祖籍江苏扬州市高邮县,生于上海,斯坦福大学物理系,电子工程系和应用物理系终身教授!'print(sentiment_score(sentiment_score_list(data1)))
print(sentiment_score(sentiment_score_list(data2)))
print(sentiment_score(sentiment_score_list(data3)))a,b=EmotionByScore(data1)emotion=JudgingEmotionByScore(a,b)
print(emotion)

NLP之情感分析:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)之全部代码相关推荐

  1. NLP之TEA:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)之全部代码

    NLP之TEA:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)之全部代码 目录 全部代码 相关文章 NLP之TEA:基于python编程(jieba库)实现中文文本情感分 ...

  2. ​​​​​​​NLP之TEA:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)

    NLP之TEA:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分) 目录 输出结果 设计思路 相关资料 1.关于代码 2.关于数据集 关于留言 1.留言内容的注意事项 2.如 ...

  3. 基于python中jieba包的中文分词中详细使用

    基于python中jieba包的中文分词中详细使用(一) 01.前言 之前的文章中也是用过一些jieba分词但是基本上都是处于皮毛,现在就现有的python环境中对其官方文档做一些自己的理解以及具体的 ...

  4. 基于python中jieba包的中文分词中详细使用(一)

    文章目录 基于python中jieba包的中文分词中详细使用(一) 01.前言 02.jieba的介绍 02.1 What 02.2特点 02.3安装与使用 02.4涉及到的算法 03.主要功能 03 ...

  5. 基于python中jieba包的中文分词中详细使用(二)

    文章目录 基于python中jieba包的中文分词中详细使用(二) 01.前言 02.关键词提取 02.01基于TF-IDF算法的关键词提取 02.02词性标注 02.03并行分词 02.04Toke ...

  6. python的jieba库第一次中文分词记录

    python的jieba库第一次中文分词记录 记录一下最基本的jieba分词程序 1.通过cut import jiebaseg = jieba.cut("这是一段中文字符", c ...

  7. 词云分析——基于Python对天猫商品评论进行词云分析

    文章目录 0 引言 1 准备工作 2 主程序 3 分析与改进 4 可能出现的报错及解决方案 0 引言 什么是词云分析? 词云图,也叫文字云,是对文本中出现频率较高的"关键词"予以视 ...

  8. kettle大于0的转换成1_第一期实训周:基于Python+MySQL+Kettle+R的某网站数据采集分析...

    ↓ 基于Python+MySQL+Kettle+R的 某网站数据采集分析 哈喽!各位学员们 咱们第一期课程就要开始了 下面划重点! 一 高校院系 齐鲁工业大学数学与统计学院应用统计系 二 实训日期 2 ...

  9. 基于 Python 的全国空气质量监测与可视化分析平台

    温馨提示:文末有 CSDN 平台官方提供的学长 Wechat / QQ 名片 :) 1. 项目背景 空气质量优劣程度与一个城市的综合竞争力密切相关,它直接影响到投资环境和居民健康,因此越来越受到政府和 ...

最新文章

  1. Revit: Twinmotion工作流程学习
  2. 一个丧心病狂的Github项目:东北话编程,大写的服!
  3. 如何快速学会别人的代码和思维
  4. 解决: Error: Program type already present: android.support.v4.os.ResultReceiver$MyResultReceiver
  5. 计算机网络期中考试总结反思,期中考试总结反思作文(通用6篇)
  6. PHP字符串替换函数选择
  7. 计算机网络-基本概念(10)【传输层】TCP运输连接管理
  8. 从Wireshark看TCP连接的建立与关闭
  9. Yii2 理解Validator
  10. 编程语言python怎么读-Python和Go都很火,我要怎么选?
  11. [转载] python--isalnum()函数
  12. mybatis中的SqlMapConfig.xml配置文件基本使用
  13. linux steam安装路径,Ubuntu Kylin 18.04 steam安装及解决方法
  14. jsp允许跨域访问_如何解决js跨域问题
  15. 笔记本连接无线网络后通过有线网口共享网络
  16. w ndows10更改浏览器,Win10系统默认浏览器怎么修改
  17. 中国软件企业排名(不是绝对的)
  18. 电路交换、报文交换、分组交换三种数据交换方式的特点、优点、应用场景以及技术对比分析
  19. 数据分析方法,寻找规律的第一步,聚类分析法!第3辑
  20. C++ Reference: Standard C++ Library reference: C Library: cmath: erfc

热门文章

  1. 用html做一个发送邮件验证,邮件发送还有问题吗?送大家一个写好的类吧,支持stmp认证、HTML格式邮件-PHP教程,PHP应用...
  2. 主从mysql replication 集群的sharding memcache集群使用consistent hash
  3. 安装和规划邮件服务器
  4. 一步步实施 DevOps (三)
  5. 【mongodb用户和身份认证管理】
  6. python_day10_并发编程
  7. dos下打包整个java工程
  8. 阿里巴巴是如何管理测试环境的?
  9. 重磅!6.7亿美元!F5喜提开源服务器Nginx
  10. Python2爬虫学习系列教程