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


狗微笑
狗微笑 2022-09-19 13:49:27 51636
分类专栏: 资讯

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

目录

全部代码


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

全部代码

  1. coding:utf-8
  2. import jieba
  3. import numpy as np
  4. 打开词典文件,返回列表
  5. def open_dict(Dict = 'hahah', path=r'data/Textming'):
  6. path = path + '%s.txt' % Dict
  7. dictionary = open(path, 'r', encoding='utf-8')
  8. dict = []
  9. for word in dictionary:
  10. word = word.strip('\n')
  11. dict.append(word)
  12. return dict
  13. def judgeodd(num):
  14. if (num % 2) == 0:
  15. return 'even'
  16. else:
  17. return 'odd'
  18. 注意,这里你要修改path路径。
  19. deny_word = open_dict(Dict = '否定词', path= r'F:/File_Python/Resources/data/Textming/')
  20. posdict = open_dict(Dict = 'positive', path= r'F:/File_Python/Resources/data/Textming/')
  21. negdict = open_dict(Dict = 'negative', path= r'F:/File_Python/Resources/data/Textming/')
  22. degree_word = open_dict(Dict = '程度级别词语', path= r'F:/File_Python/Resources/data/Textming/')
  23. mostdict = degree_word[degree_word.index('extreme')+1 : degree_word.index('very')]权重4,即在情感词前乘以4
  24. verydict = degree_word[degree_word.index('very')+1 : degree_word.index('more')]权重3
  25. moredict = degree_word[degree_word.index('more')+1 : degree_word.index('ish')]权重2
  26. ishdict = degree_word[degree_word.index('ish')+1 : degree_word.index('last')]权重0.5
  27. def sentiment_score_list(dataset):
  28. seg_sentence = dataset.split('。')
  29. count1 = []
  30. count2 = []
  31. for sen in seg_sentence: 循环遍历每一个评论
  32. segtmp = jieba.lcut(sen, cut_all=False) 把句子进行分词,以列表的形式返回
  33. i = 0 记录扫描到的词的位置
  34. a = 0 记录情感词的位置
  35. poscount = 0 积极词的第一次分值
  36. poscount2 = 0 积极词反转后的分值
  37. poscount3 = 0 积极词的最后分值(包括叹号的分值)
  38. negcount = 0
  39. negcount2 = 0
  40. negcount3 = 0
  41. for word in segtmp:
  42. if word in posdict: 判断词语是否是情感词
  43. poscount += 1
  44. c = 0
  45. for w in segtmp[a:i]: 扫描情感词前的程度词
  46. if w in mostdict:
  47. poscount *= 4.0
  48. elif w in verydict:
  49. poscount *= 3.0
  50. elif w in moredict:
  51. poscount *= 2.0
  52. elif w in ishdict:
  53. poscount *= 0.5
  54. elif w in deny_word:
  55. c += 1
  56. if judgeodd(c) == 'odd': 扫描情感词前的否定词数
  57. poscount *= -1.0
  58. poscount2 += poscount
  59. poscount = 0
  60. poscount3 = poscount + poscount2 + poscount3
  61. poscount2 = 0
  62. else:
  63. poscount3 = poscount + poscount2 + poscount3
  64. poscount = 0
  65. a = i + 1 情感词的位置变化
  66. elif word in negdict: 消极情感的分析,与上面一致
  67. negcount += 1
  68. d = 0
  69. for w in segtmp[a:i]:
  70. if w in mostdict:
  71. negcount *= 4.0
  72. elif w in verydict:
  73. negcount *= 3.0
  74. elif w in moredict:
  75. negcount *= 2.0
  76. elif w in ishdict:
  77. negcount *= 0.5
  78. elif w in degree_word:
  79. d += 1
  80. if judgeodd(d) == 'odd':
  81. negcount *= -1.0
  82. negcount2 += negcount
  83. negcount = 0
  84. negcount3 = negcount + negcount2 + negcount3
  85. negcount2 = 0
  86. else:
  87. negcount3 = negcount + negcount2 + negcount3
  88. negcount = 0
  89. a = i + 1
  90. elif word == '!' or word == '!': 判断句子是否有感叹号
  91. for w2 in segtmp[::-1]: 扫描感叹号前的情感词,发现后权值+2,然后退出循环
  92. if w2 in posdict or negdict:
  93. poscount3 += 2
  94. negcount3 += 2
  95. break
  96. i += 1 扫描词位置前移
  97. 以下是防止出现负数的情况
  98. pos_count = 0
  99. neg_count = 0
  100. if poscount3 < 0 and negcount3 > 0:
  101. neg_count += negcount3 - poscount3
  102. pos_count = 0
  103. elif negcount3 < 0 and poscount3 > 0:
  104. pos_count = poscount3 - negcount3
  105. neg_count = 0
  106. elif poscount3 < 0 and negcount3 < 0:
  107. neg_count = -poscount3
  108. pos_count = -negcount3
  109. else:
  110. pos_count = poscount3
  111. neg_count = negcount3
  112. count1.append([pos_count, neg_count])
  113. count2.append(count1)
  114. count1 = []
  115. return count2
  116. def sentiment_score(senti_score_list):
  117. score = []
  118. for review in senti_score_list:
  119. score_array = np.array(review)
  120. Pos = np.sum(score_array[:, 0])
  121. Neg = np.sum(score_array[:, 1])
  122. AvgPos = np.mean(score_array[:, 0])
  123. AvgPos = float('%.1f'%AvgPos)
  124. AvgNeg = np.mean(score_array[:, 1])
  125. AvgNeg = float('%.1f'%AvgNeg)
  126. StdPos = np.std(score_array[:, 0])
  127. StdPos = float('%.1f'%StdPos)
  128. StdNeg = np.std(score_array[:, 1])
  129. StdNeg = float('%.1f'%StdNeg)
  130. score.append([Pos, Neg, AvgPos, AvgNeg, StdPos, StdNeg]) 积极、消极情感值总和(最重要),积极、消极情感均值,积极、消极情感方差。
  131. return score
  132. def EmotionByScore(data):
  133. result_list=sentiment_score(sentiment_score_list(data))
  134. return result_list[0][0],result_list[0][1]
  135. def JudgingEmotionByScore(Pos, Neg):
  136. if Pos > Neg:
  137. str='1'
  138. elif Pos < Neg:
  139. str='-1'
  140. elif Pos == Neg:
  141. str='0'
  142. return str
  143. data1= '今天上海的天气真好!我的心情非常高兴!如果去旅游的话我会非常兴奋!和你一起去旅游我会更加幸福!'
  144. data2= '救命,你是个坏人,救命,你不要碰我,救命,你个大坏蛋!'
  145. data3= '美国华裔科学家,祖籍江苏扬州市高邮县,生于上海,斯坦福大学物理系,电子工程系和应用物理系终身教授!'
  146. print(sentiment_score(sentiment_score_list(data1)))
  147. print(sentiment_score(sentiment_score_list(data2)))
  148. print(sentiment_score(sentiment_score_list(data3)))
  149. a,b=EmotionByScore(data1)
  150. emotion=JudgingEmotionByScore(a,b)
  151. print(emotion)
文章知识点与官方知识档案匹配,可进一步学习相关知识

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=2769
赞同 0
评论 0 条
狗微笑L0
粉丝 0 发表 8 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2691
【软件正版化】软件正版化工作要点  2655
统信UOS试玩黑神话:悟空  2559
信刻光盘安全隔离与信息交换系统  2247
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1117
grub引导程序无法找到指定设备和分区  769
江波龙2025届校园招聘宣讲会行程大放送  28
点击报名 | 京东2025校招进校行程预告  25
海康威视2025校招|海康机器人,邀你共创工业智能化未来!  24
金山办公2024算法挑战赛 | 报名截止日期更新  22
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!