当前位置:   article > 正文

Python 情感分析_python情感分析输出积极情感词消极情感词前十名

python情感分析输出积极情感词消极情感词前十名

今天修改了情感分析的程序发现之前有一些不足。这个最简单的实现一个string情感分析的小函数,加载了积极词典,消极词典,程度词典,以及一些反转词等的词典。这里我没有做符号的分析和判断,因为的东西暂时用不到,需要的童鞋可以自己添加。。。大笑

由于这里不能放文件所以...我放到网盘啦,,哇哇哇 链接: http://pan.baidu.com/s/1mhDqfWG  密码: vm38偷笑偷笑偷笑

  1. import jieba
  2. import cPickle as pickle
  3. """载入情感词典"""
  4. g_pos_dict = pickle.load(open("../sentiment_dic/posdict.pkl", "r"))
  5. g_neg_dict = pickle.load(open("../sentiment_dic/negdict.pkl", "r"))
  6. g_most_dict = pickle.load(open("../sentiment_dic/mostdict.pkl", "r"))
  7. g_more_dict = pickle.load(open("../sentiment_dic/moredict.pkl", "r"))
  8. g_very_dict = pickle.load(open("../sentiment_dic/verydict.pkl", "r"))
  9. g_ish_dict = pickle.load(open("../sentiment_dic/ishdict.pkl", "r"))
  10. g_insufficient_dict = pickle.load(open("../sentiment_dic/insufficentdict.pkl", "r"))
  11. g_inverse_dict = pickle.load(open("../sentiment_dic/inversedict.pkl", "r"))
  12. def emotion_test(string):
  13. """情感分析函数"""
  14. words = list(jieba.cut(string))
  15. a = 0 # 记录情感词位置
  16. poscount = 0 # 积极词的分值
  17. poscount3 = 0 # 积极词最后分值
  18. negcount = 0
  19. negcount3 = 0
  20. for index, word in enumerate(words):
  21. word = word.encode("utf8")
  22. if word in g_pos_dict: # 判断词语是否是积极情感词
  23. poscount += 1
  24. c = 0 # 反转词
  25. for w in words[a:index]:
  26. w = w.encode("utf8")
  27. if w in g_most_dict:
  28. poscount *= 4.0
  29. elif w in g_very_dict:
  30. poscount *= 3.0
  31. elif w in g_more_dict:
  32. poscount *= 2.0
  33. elif w in g_ish_dict:
  34. poscount /= 2.0
  35. elif w in g_insufficient_dict:
  36. poscount /= 4.0
  37. elif w in g_inverse_dict:
  38. c += 1
  39. if c % 2 == 1:
  40. poscount *= -1.0
  41. poscount3 += poscount
  42. poscount = 0
  43. a = index + 1 # 情感词的位置变化
  44. elif word in g_neg_dict: # 消极情感
  45. negcount += 1
  46. d = 0 # 反转词
  47. for w in words[a:index]:
  48. w = w.encode("utf8")
  49. if w in g_most_dict:
  50. negcount *= 4.0
  51. elif w in g_very_dict:
  52. negcount *= 3.0
  53. elif w in g_more_dict:
  54. negcount *= 2.0
  55. elif w in g_ish_dict:
  56. negcount /= 2.0
  57. elif w in g_insufficient_dict:
  58. negcount /= 4.0
  59. elif w in g_inverse_dict:
  60. d += 1
  61. if d % 2 == 1:
  62. negcount *= -1.0
  63. negcount3 += negcount
  64. negcount = 0
  65. a = index + 1
  66. if poscount3 <= 0 and negcount3 <= 0:
  67. t = poscount3
  68. poscount3 = -negcount3
  69. negcount3 = -t
  70. elif poscount3 <= 0 and negcount3 >= 0:
  71. negcount3 -= poscount3
  72. poscount3 = 0
  73. elif poscount3 >= 0 and negcount3 <= 0:
  74. poscount3 -= negcount3
  75. negcount3 = 0
  76. return poscount3, negcount3
  77. print emotion_test("我真的非常的烦心")




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

闽ICP备14008679号