当前位置:   article > 正文

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

nlp情感分析代码

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

目录

全部代码


相关文章
NLP之TEA:基于python编程(jieba库)实现中文文本情感分析(得到的是情感评分)
NLP之TEA:基于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)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/332006
推荐阅读
相关标签
  

闽ICP备14008679号