NLP之情感分析:基于python实现中文文本情感分析

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

输出结果

1、测试对象
data1= '今天上海的天气真好!我的心情非常高兴!如果去旅游的话我会非常兴奋!和你一起去旅游我会更加幸福!'
data2= '今天上海天气真差,非常讨厌下雨,把我冻坏了,心情太不高兴了,不高兴,我真的很生气!'
data3= '美国华裔科学家,祖籍江苏扬州市高邮县,生于上海,斯坦福大学物理系,电子工程系和应用物理系终身教授!'

2、输出结果
很明显,data1情感更加积极!data2情感消极!data3情感中等!

[[240.0, 104.0, 8.3, 3.6, 8.0, 2.4]]
[[0.0, 134.0, 0.0, 4.8, 0.0, 3.2]]
[[2, 66, 0.1, 3.3, 0.4, 1.7]]
[[2, 2, 0.1, 0.1, 0.4, 0.4]]

 

设计思路

后期更新……

使用的相关文件


主要部分代码实现


 
 
  1. import jieba
  2. import numpy as np
  3. ……
  4. def sentiment_score_list(dataset):
  5. seg_sentence = dataset.split( '。')
  6. count1 = []
  7. count2 = []
  8. for sen in seg_sentence: #循环遍历每一个评论
  9. segtmp = jieba.lcut(sen, cut_all= False) #把句子进行分词,以列表的形式返回
  10. i = 0 #记录扫描到的词的位置
  11. a = 0 #记录情感词的位置
  12. poscount = 0 #积极词的第一次分值
  13. poscount2 = 0 #积极词反转后的分值
  14. poscount3 = 0 #积极词的最后分值(包括叹号的分值)
  15. negcount = 0
  16. negcount2 = 0
  17. negcount3 = 0
  18. for word in segtmp:
  19. if word in posdict: # 判断词语是否是情感词
  20. poscount += 1
  21. c = 0
  22. for w in segtmp[a:i]: # 扫描情感词前的程度词
  23. if w in mostdict:
  24. poscount *= 4.0
  25. elif w in verydict:
  26. poscount *= 3.0
  27. elif w in moredict:
  28. poscount *= 2.0
  29. elif w in ishdict:
  30. poscount *= 0.5
  31. elif w in deny_word:
  32. c += 1
  33. if judgeodd(c) == 'odd': # 扫描情感词前的否定词数
  34. poscount *= -1.0
  35. poscount2 += poscount
  36. poscount = 0
  37. poscount3 = poscount + poscount2 + poscount3
  38. poscount2 = 0
  39. else:
  40. poscount3 = poscount + poscount2 + poscount3
  41. poscount = 0
  42. a = i + 1 # 情感词的位置变化
  43. elif word in negdict: # 消极情感的分析,与上面一致
  44. negcount += 1
  45. d = 0
  46. for w in segtmp[a:i]:
  47. if w in mostdict:
  48. negcount *= 4.0
  49. elif w in verydict:
  50. negcount *= 3.0
  51. elif w in moredict:
  52. negcount *= 2.0
  53. elif w in ishdict:
  54. negcount *= 0.5
  55. elif w in degree_word:
  56. d += 1
  57. if judgeodd(d) == 'odd':
  58. negcount *= -1.0
  59. negcount2 += negcount
  60. negcount = 0
  61. negcount3 = negcount + negcount2 + negcount3
  62. negcount2 = 0
  63. else:
  64. negcount3 = negcount + negcount2 + negcount3
  65. negcount = 0
  66. a = i + 1
  67. elif word == '!' or word == '!': ##判断句子是否有感叹号
  68. for w2 in segtmp[:: -1]: # 扫描感叹号前的情感词,发现后权值+2,然后退出循环
  69. if w2 in posdict or negdict:
  70. poscount3 += 2
  71. negcount3 += 2
  72. break
  73. i += 1 # 扫描词位置前移
  74. # 以下是防止出现负数的情况
  75. pos_count = 0
  76. neg_count = 0
  77. if poscount3 < 0 and negcount3 > 0:
  78. neg_count += negcount3 - poscount3
  79. pos_count = 0
  80. elif negcount3 < 0 and poscount3 > 0:
  81. pos_count = poscount3 - negcount3
  82. neg_count = 0
  83. elif poscount3 < 0 and negcount3 < 0:
  84. neg_count = -poscount3
  85. pos_count = -negcount3
  86. else:
  87. pos_count = poscount3
  88. neg_count = negcount3
  89. count1.append([pos_count, neg_count])
  90. count2.append(count1)
  91. count1 = []
  92. return count2
  93. def sentiment_score(senti_score_list):
  94. score = []
  95. for review in senti_score_list:
  96. score_array = np.array(review)
  97. Pos = np.sum(score_array[:, 0])
  98. Neg = np.sum(score_array[:, 1])
  99. AvgPos = np.mean(score_array[:, 0])
  100. AvgPos = float( '%.1f'%AvgPos)
  101. AvgNeg = np.mean(score_array[:, 1])
  102. AvgNeg = float( '%.1f'%AvgNeg)
  103. StdPos = np.std(score_array[:, 0])
  104. StdPos = float( '%.1f'%StdPos)
  105. StdNeg = np.std(score_array[:, 1])
  106. StdNeg = float( '%.1f'%StdNeg)
  107. score.append([Pos, Neg, AvgPos, AvgNeg, StdPos, StdNeg])
  108. return score
  109. data1= '今天上海的天气真好!我的心情非常高兴!如果去旅游的话我会非常兴奋!和你一起去旅游我会更加幸福!'
  110. data2= '今天上海天气真差,非常讨厌下雨,把我冻坏了,心情太不高兴了,不高兴,我真的很生气!'
  111. data3= '美国华裔科学家,祖籍江苏扬州市高邮县,生于上海,斯坦福大学物理系,电子工程系和应用物理系终身教授!'
  112. print(sentiment_score(sentiment_score_list(data1)))
  113. print(sentiment_score(sentiment_score_list(data2)))
  114. print(sentiment_score(sentiment_score_list(data3)))
  • 6
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值