当前位置:   article > 正文

python 基于情感词典的情感分析之乐,惧,惊,哀,恶,怒和未知七种情感分析_知网情感词典的python

知网情感词典的python

 

背景

情感分析是通过计算技术对文本内容的主观客观性、情绪等挖掘分析,对文本的情感偏向做出判断。目的是识别出文本中的具体情感分类,之前做文本分类都是通过深度学习或者机器学习进行文本分类,但是需要进行数据标注,对于上述其中情感的分类,有相应的情感词典,想通过情感词典的方式进行七分类情感分析。

1.数据准备

1.1 情感词典准备

中文情感词汇本体库是大连理工大学信息检索研究室在林鸿飞教授的指导下经过全体教研室成员的努力整理和标注的一个中文本体资源。该资源从不同角度描述一个中文词汇或者短语,包括词语词性种类、情感类别、情感强度及极性等信息。

中文情感词汇本体的情感分类体系是在国外比较有影响的Ekman的6大类情感分类体系的基础上构建的。在Ekman的基础上,词汇本体加入情感类别“好”对褒义情感进行了更细致的划分。最终词汇本体中的情感共分为7大类21小类。如果需要的话可以自行去下载。http://ir.dlut.edu.cn/EmotionOntologyDownload,需要需要相关的信息才可以哦。也可以通过其他方式去下载。

由于这里的七大类和我要做的七类有点区别只是应用了其中的六类。

1.2 否定词

文本情感分类(一):传统模型中提供了一个情感极性词典的下载包,包中带了一个否定词的txt。

https://kexue.fm/usr/uploads/2017/09/1922797046.zip

  1. 不大
  2. 不丁点儿
  3. 不甚
  4. 不怎么
  5. 没怎么
  6. 不可以
  7. 怎么不
  8. 几乎不
  9. 从来不
  10. 从不
  11. 不用
  12. 不曾
  13. 不该
  14. 不必
  15. 不会
  16. 不好
  17. 不能
  18. 很少
  19. 极少
  20. 没有
  21. 不是
  22. 难以
  23. 放下
  24. 扼杀
  25. 终止
  26. 停止
  27. 放弃
  28. 反对
  29. 缺乏
  30. 缺少
  31. 木有
  32. 请勿
  33. 无须
  34. 并非
  35. 毫无
  36. 决不
  37. 休想
  38. 永不
  39. 不要
  40. 未尝
  41. 未曾
  42. 从未
  43. 从未有过
  44. 尚未
  45. 一无
  46. 并未
  47. 尚无
  48. 从没
  49. 绝非
  50. 远非
  51. 切莫
  52. 绝不
  53. 毫不
  54. 禁止
  55. 拒绝
  56. 杜绝

1.3 程度副词词典

程度副词可以使用从这里《知网》情感分析用词语集(beta版)下载,里面包含程度级别的词语,但是里面的格式是汇总的形式,需要转换为词典的形式,这里做了一个简单的程度副词标记,大于1,表示情感加强,小于1,表示情感弱化,下面主要按照极其1.8,超1.6,很1.5,较1,稍0.7,欠0.5进行了一个简单的标记,如下所示。也可以根据自己的需求及及进行修改。

  1. 百分之百,1.8
  2. 倍加,1.8
  3. 备至,1.8
  4. 不得了,1.8
  5. 不堪,1.8
  6. 不可开交,1.8
  7. 不亦乐乎,1.8
  8. 不折不扣,1.8
  9. 彻头彻尾,1.8
  10. 充分,1.8
  11. 到头,1.8
  12. 地地道道,1.8
  13. 非常,1.8
  14. 极,1.8
  15. 极度,1.8
  16. 极端,1.8
  17. 极其,1.8
  18. 极为,1.8
  19. 截然,1.8
  20. 尽,1.8
  21. 惊人地,1.8
  22. 绝,1.8
  23. 绝顶,1.8
  24. 绝对,1.8
  25. 绝对化,1.8
  26. 刻骨,1.8
  27. 酷,1.8
  28. 满,1.8
  29. 满贯,1.8
  30. 满心,1.8
  31. 莫大,1.8
  32. 奇,1.8
  33. 入骨,1.8
  34. 甚为,1.8
  35. 十二分,1.8
  36. 十分,1.8
  37. 十足,1.8
  38. 死,1.8
  39. 滔天,1.8
  40. 痛,1.8
  41. 透,1.8
  42. 完全,1.8
  43. 完完全全,1.8
  44. 万,1.8
  45. 万般,1.8
  46. 万分,1.8
  47. 万万,1.8
  48. 无比,1.8
  49. 无度,1.8
  50. 无可估量,1.8
  51. 无以复加,1.8
  52. 无以伦比,1.8
  53. 要命,1.8
  54. 要死,1.8
  55. 已极,1.8
  56. 已甚,1.8
  57. 异常,1.8
  58. 逾常,1.8
  59. 贼,1.8
  60. 之极,1.8
  61. 之至,1.8
  62. 至极,1.8
  63. 卓绝,1.8
  64. 最为,1.8
  65. 佼佼,1.8
  66. 郅,1.8
  67. 綦,1.8
  68. 齁,1.8
  69. 最,1.8
  70. 不过,1.5
  71. 不少,1.5
  72. 不胜,1.5
  73. 惨,1.5
  74. 沉,1.5
  75. 沉沉,1.5
  76. 出奇,1.5
  77. 大为,1.5
  78. 多,1.5
  79. 多多,1.5
  80. 多加,1.5
  81. 多么,1.5
  82. 分外,1.5
  83. 格外,1.5
  84. 够瞧的,1.5
  85. 够戗,1.5
  86. 好,1.5
  87. 好不,1.5
  88. 何等,1.5
  89. 很,1.5
  90. 很是,1.5
  91. 坏,1.5
  92. 可,1.5
  93. 老,1.5
  94. 老大,1.5
  95. 良,1.5
  96. 颇,1.5
  97. 颇为,1.5
  98. 甚,1.5
  99. 实在,1.5
  100. 太,1.5
  101. 太甚,1.5
  102. 特,1.5
  103. 特别,1.5
  104. 尤,1.5
  105. 尤其,1.5
  106. 尤为,1.5
  107. 尤以,1.5
  108. 远,1.5
  109. 着实,1.5
  110. 曷,1.5
  111. 碜,1.5
  112. 大不了,0.8
  113. 多,0.8
  114. 更,0.8
  115. 更加,0.8
  116. 更进一步,0.8
  117. 更为,0.8
  118. 还,0.8
  119. 还要,0.8
  120. 较,0.8
  121. 较比,0.8
  122. 较为,0.8
  123. 进一步,0.8
  124. 那般,0.8
  125. 那么,0.8
  126. 那样,0.8
  127. 强,0.8
  128. 如斯,0.8
  129. 益,0.8
  130. 益发,0.8
  131. 尤甚,0.8
  132. 逾,0.8
  133. 愈,0.8
  134. 愈 ... 愈,0.8
  135. 愈发,0.8
  136. 愈加,0.8
  137. 愈来愈,0.8
  138. 愈益,0.8
  139. 远远,0.8
  140. 越 ... 越,0.8
  141. 越发,0.8
  142. 越加,0.8
  143. 越来越,0.8
  144. 越是,0.8
  145. 这般,0.8
  146. 这样,0.8
  147. 足,0.8
  148. 足足,0.8
  149. 点点滴滴,0.7
  150. 多多少少,0.7
  151. 怪,0.7
  152. 好生,0.7
  153. 还,0.7
  154. 或多或少,0.7
  155. 略,0.7
  156. 略加,0.7
  157. 略略,0.7
  158. 略微,0.7
  159. 略为,0.7
  160. 蛮,0.7
  161. 稍,0.7
  162. 稍稍,0.7
  163. 稍微,0.7
  164. 稍为,0.7
  165. 稍许,0.7
  166. 挺,0.7
  167. 未免,0.7
  168. 相当,0.7
  169. 些,0.7
  170. 些微,0.7
  171. 些小,0.7
  172. 一点,0.7
  173. 一点儿,0.7
  174. 一些,0.7
  175. 有点,0.7
  176. 有点儿,0.7
  177. 有些,0.7
  178. 半点,0.5
  179. 不大,0.5
  180. 不丁点儿,0.5
  181. 不甚,0.5
  182. 不怎么,0.5
  183. 聊,0.5
  184. 没怎么,0.5
  185. 轻度,0.5
  186. 弱,0.5
  187. 丝毫,0.5
  188. 微,0.5
  189. 相对,0.5
  190. 不为过,1.6
  191. 超,1.6
  192. 超额,1.6
  193. 超外差,1.6
  194. 超微结构,1.6
  195. 超物质,1.6
  196. 出头,1.6
  197. 多,1.6
  198. 浮,1.6
  199. 过,1.6
  200. 过度,1.6
  201. 过分,1.6
  202. 过火,1.6
  203. 过劲,1.6
  204. 过了头,1.6
  205. 过猛,1.6
  206. 过热,1.6
  207. 过甚,1.6
  208. 过头,1.6
  209. 过于,1.6
  210. 过逾,1.6
  211. 何止,1.6
  212. 何啻,1.6
  213. 开外,1.6
  214. 苦,1.6
  215. 老,1.6
  216. 偏,1.6
  217. 强,1.6
  218. 溢,1.6
  219. 忒,1.6

1.4 停止词

停用词词典有很多,可以使用科院计算所中文自然语言处理开放平台发布了有1208个停用词的中文停用词表,也可以使用其他的停用词词表,但是要注意一下需要将否定词或者是程度副词的词典过滤掉,不然否定词在去除停用词的时候都过滤掉了,就缺少了一些程度副词或者否定词。如果需要过滤的话可以使用下面的方面过滤。

2.数据预处理

2.1 jieba分词

直接使用jieba分词工具进行分词

2.2 去除停用词

在jieba分词之后过滤掉停用词

  1. def seg_word(sentence):
  2. """使用jieba对文档分词"""
  3. seg_list = jieba.cut(sentence)
  4. seg_result = []
  5. for w in seg_list:
  6. seg_result.append(w)
  7. # 读取停用词文件
  8. stopwords = set()
  9. fr = codecs.open('data/stopwords.txt', 'r', 'utf-8')
  10. for word in fr:
  11. stopwords.add(word.strip())
  12. fr.close()
  13. # 去除停用词
  14. return list(filter(lambda x: x not in stopwords, seg_result))

3.3 构建情感字典

词汇本体中的情绪共分为7种: “好、乐、哀、怒、惧、恶、惊”, 共含有情绪词27 466个, 情感强度分为: 1, 3, 5, 7, 9这5档, 9表示强度最大, 1为强度最小。该资源从不同角度描述一个中文词汇或者短语, 包括词语词性种类、情感类别、情感强度及极性等信息。每个词在每一类情感下都对应一个极性。其中, 0代表中性, 1代表褒义, 2代表贬义, 3代表兼有褒贬两性。为了根据词汇的情感强度值计算微博的情感强度, 本文将褒义极性值不变, 贬义极性值取-1。并且根据情感强度和情感极性计算了每个词的情感值。这里采用最简单的方式计算了每个词的情感值,W = 强度值 * 极性值,(这里没有考虑辅助情感类别),有兴趣的可以将辅助情感类别也加进来。

由于词汇本题中的词汇是21个类别,需要根据他们的小类别将其汇总到大类别中,并且这里只应用了6个类别,好这个类别的词汇并没有使用。需要的可以自行去添加。

  1. def get_label_data(path):
  2. """
  3. 根据情感词汇本体返回相应的标签
  4. """
  5. sem_data = pd.read_excel(path)
  6. for i in range(sem_data.shape[0]):
  7. score = 0
  8. if sem_data.iloc[i,6] == 2:
  9. sem_data.iat[i,6] = -1
  10. if sem_data.iloc[i,9] == 2:
  11. sem_data.iat[i,9] = -1
  12. # 是否需要辅助情感分类,目前先不要啦
  13. # if sem_data.iloc[i,8] >= 0:
  14. # score += sem_data.iloc[i, 8] * sem_data.iloc[i, 9]
  15. # print(score)
  16. # 增加每个词语的情感强度值
  17. score += sem_data.iloc[i,5] * sem_data.iloc[i,6]
  18. sem_data.iat[i, -1] = score
  19. # 定义情感字典
  20. match_dict = {
  21. "joy":["PA","PE"],
  22. "surprise":["PC"],
  23. "anger":["NA"],
  24. "sadness":["NB", "NJ", "NH", "PF"],
  25. "fear":["NI", "NC", "NG"],
  26. "disgust":["ND", "NE", "NN", "NK", "NL"],
  27. }
  28. # 获取情感类别
  29. label_dict = {}
  30. keys = match_dict.keys()
  31. for k in keys:
  32. word_dict = {}
  33. for i in range(sem_data.shape[0]):
  34. label = sem_data.iloc[i, 4]
  35. if label in match_dict[k]:
  36. word_dict[sem_data.iloc[i, 0]] = sem_data.iloc[i, -1]
  37. label_dict[k] = word_dict
  38. return label_dict
  39. path = "data/情感词汇本体.xlsx"
  40. label_dict_7_class = get_label_data(path)

3.模型构建

3.1 找出文本中的情感词,否定词和程度副词

  1. def classify_words(word_dict):
  2. """词语分类,找出情感词、否定词、程度副词"""
  3. # 读取情感字典文件
  4. sen_file = open('data/BosonNLP_sentiment_score.txt', 'r+', encoding='utf-8')
  5. # 获取字典文件内容
  6. sen_list = sen_file.readlines()
  7. # 创建情感字典
  8. sen_dict = defaultdict()
  9. # 读取字典文件每一行内容,将其转换为字典对象,key为情感词,value为对应的分值
  10. for s in sen_list:
  11. # 每一行内容根据空格分割,索引0是情感词,索引01是情感分值
  12. sen_dict[s.split(' ')[0]] = s.split(' ')[1]
  13. # 读取否定词文件
  14. not_word_file = open('data/notDict.txt', 'r+', encoding='utf-8')
  15. # 由于否定词只有词,没有分值,使用list即可
  16. not_word_list = not_word_file.readlines()
  17. # 读取程度副词文件
  18. degree_file = open('data/degree.txt', 'r+', encoding='utf-8')
  19. degree_list = degree_file.readlines()
  20. degree_dic = defaultdict()
  21. # 程度副词与情感词处理方式一样,转为程度副词字典对象,key为程度副词,value为对应的程度值
  22. for d in degree_list:
  23. # print(d)
  24. degree_dic[d.split(',')[0]] = d.split(',')[1]
  25. # 分类结果,词语的index作为key,词语的分值作为value,否定词分值设为-1
  26. sen_word = dict()
  27. not_word = dict()
  28. degree_word = dict()
  29. # 分类
  30. for word in word_dict.keys():
  31. if word in sen_dict.keys() and word not in not_word_list and word not in degree_dic.keys():
  32. # 找出分词结果中在情感字典中的词
  33. sen_word[word_dict[word]] = sen_dict[word]
  34. elif word in not_word_list and word not in degree_dic.keys():
  35. # 分词结果中在否定词列表中的词
  36. not_word[word_dict[word]] = -1
  37. elif word in degree_dic.keys():
  38. # 分词结果中在程度副词中的词
  39. degree_word[word_dict[word]] = degree_dic[word]
  40. sen_file.close()
  41. degree_file.close()
  42. not_word_file.close()
  43. # 将分类结果返回
  44. return sen_word, not_word, degree_word

3.2 计算分数

规则:分数采用情感词的分数和计算

遍历所有的情感词,查看当前情感词的前面是否有否定词和程度副词,如果没有否定词,就对当前情感词乘以1,如果有否定词或者有多个否定词,可以乘以(-1)^否定词的个数;如果有程度副词,就在当前情感词前面乘以程度副词的程度等级。

伪代码如下:

  1. finalSentiScore = (-1) ^ (num of notWords) * degreeNum * sentiScore
  2. finalScore = sum(finalSentiScore)
  1. def socre_sentiment(sen_word, not_word, degree_word, seg_result):
  2. """计算得分"""
  3. # 权重初始化为1
  4. W = 1
  5. score = 0
  6. # 情感词下标初始化
  7. sentiment_index = -1
  8. # 情感词的位置下标集合
  9. sentiment_index_list = list(sen_word.keys())
  10. # 遍历分词结果(遍历分词结果是为了定位两个情感词之间的程度副词和否定词)
  11. for i in range(0, len(seg_result)):
  12. # 如果是情感词(根据下标是否在情感词分类结果中判断)
  13. if i in sen_word.keys():
  14. # 权重*情感词得分
  15. score += W * float(sen_word[i])
  16. # 情感词下标加1,获取下一个情感词的位置
  17. sentiment_index += 1
  18. if sentiment_index < len(sentiment_index_list) - 1:
  19. # 判断当前的情感词与下一个情感词之间是否有程度副词或否定词
  20. for j in range(sentiment_index_list[sentiment_index], sentiment_index_list[sentiment_index + 1]):
  21. # 更新权重,如果有否定词,取反
  22. if j in not_word.keys():
  23. W *= -1
  24. elif j in degree_word.keys():
  25. # 更新权重,如果有程度副词,分值乘以程度副词的程度分值
  26. W *= float(degree_word[j])
  27. # 定位到下一个情感词
  28. if sentiment_index < len(sentiment_index_list) - 1:
  29. i = sentiment_index_list[sentiment_index + 1]
  30. return score

3.3 计算每句话的分数

  1. def setiment_score(sententce):
  2. # 1.对文档分词
  3. seg_list = seg_word(sententce)
  4. # 2.将分词结果列表转为dic,然后找出情感词、否定词、程度副词
  5. sen_word, not_word, degree_word = classify_words(list_to_dict(seg_list))
  6. # 3.计算得分
  7. score = socre_sentiment(sen_word, not_word, degree_word, seg_list)
  8. return score

 3.4 返回文本的标签和预测值

这里简单的说一下,预测值采用累加和的形式,最后哪一个类别的分值越大就预测为哪个类别。其中会有负数的情况,将其求绝对值之后再进行比较。

  1. def mul_classifier_7_class(content, label_dict):
  2. "根据新闻返回预测标签"
  3. scores = {}
  4. for k, v in label_dict.items():
  5. score = setiment_score_7_class(content, label_dict[k])
  6. scores[k] = score
  7. score_max = scores[max(scores, key=lambda x:abs(scores[x]))]
  8. if score_max == 0:
  9. return "unknow", 0
  10. else:
  11. return max(scores, key=lambda x:abs(scores[x])), scores[max(scores, key=lambda x:abs(scores[x]))]

下面附上完整代码:

  1. import pandas as pd
  2. from collections import defaultdict
  3. import os
  4. import re
  5. import jieba
  6. import codecs
  7. import pandas as pd
  8. def get_label_data(path):
  9. """
  10. 根据情感词汇本体返回相应的标签
  11. """
  12. sem_data = pd.read_excel(path)
  13. for i in range(sem_data.shape[0]):
  14. score = 0
  15. if sem_data.iloc[i,6] == 2:
  16. sem_data.iat[i,6] = -1
  17. if sem_data.iloc[i,9] == 2:
  18. sem_data.iat[i,9] = -1
  19. # 是否需要辅助情感分类,目前先不要啦
  20. # if sem_data.iloc[i,8] >= 0:
  21. # score += sem_data.iloc[i, 8] * sem_data.iloc[i, 9]
  22. # print(score)
  23. # 增加每个词语的情感强度值
  24. score += sem_data.iloc[i,5] * sem_data.iloc[i,6]
  25. sem_data.iat[i, -1] = score
  26. # 定义情感字典
  27. match_dict = {
  28. "joy":["PA","PE"],
  29. "surprise":["PC"],
  30. "anger":["NA"],
  31. "sadness":["NB", "NJ", "NH", "PF"],
  32. "fear":["NI", "NC", "NG"],
  33. "disgust":["ND", "NE", "NN", "NK", "NL"],
  34. }
  35. # 获取情感类别
  36. label_dict = {}
  37. keys = match_dict.keys()
  38. for k in keys:
  39. word_dict = {}
  40. for i in range(sem_data.shape[0]):
  41. label = sem_data.iloc[i, 4]
  42. if label in match_dict[k]:
  43. word_dict[sem_data.iloc[i, 0]] = sem_data.iloc[i, -1]
  44. label_dict[k] = word_dict
  45. return label_dict
  46. path = "data/情感词汇本体.xlsx"
  47. label_dict_7_class = get_label_data(path)
  48. def seg_word(sentence):
  49. """使用jieba对文档分词"""
  50. seg_list = jieba.cut(sentence)
  51. seg_result = []
  52. for w in seg_list:
  53. seg_result.append(w)
  54. # 读取停用词文件
  55. stopwords = set()
  56. fr = codecs.open('data/stopwords.txt', 'r', 'utf-8')
  57. for word in fr:
  58. stopwords.add(word.strip())
  59. fr.close()
  60. # 去除停用词
  61. return list(filter(lambda x: x not in stopwords, seg_result))
  62. def classify_words_7_class(word_dict, sen_dict):
  63. """词语分类,找出情感词、否定词、程度副词"""
  64. # 读取否定词文件
  65. not_word_file = open('data/notDict.txt', 'r+', encoding='utf-8')
  66. # 由于否定词只有词,没有分值,使用list即可
  67. not_word_list = not_word_file.readlines()
  68. # 读取程度副词文件
  69. degree_file = open('data/degree.txt', 'r+', encoding='utf-8')
  70. degree_list = degree_file.readlines()
  71. degree_dic = defaultdict()
  72. # 程度副词与情感词处理方式一样,转为程度副词字典对象,key为程度副词,value为对应的程度值
  73. for d in degree_list:
  74. # print(d)
  75. degree_dic[d.split(',')[0]] = d.split(',')[1]
  76. # 分类结果,词语的index作为key,词语的分值作为value,否定词分值设为-1
  77. sen_word = dict()
  78. not_word = dict()
  79. degree_word = dict()
  80. # 分类
  81. for word in word_dict.keys():
  82. if word in sen_dict.keys() and word not in not_word_list and word not in degree_dic.keys():
  83. # 找出分词结果中在情感字典中的词
  84. sen_word[word_dict[word]] = sen_dict[word]
  85. elif word in not_word_list and word not in degree_dic.keys():
  86. # 分词结果中在否定词列表中的词
  87. not_word[word_dict[word]] = -1
  88. elif word in degree_dic.keys():
  89. # 分词结果中在程度副词中的词
  90. degree_word[word_dict[word]] = degree_dic[word]
  91. degree_file.close()
  92. not_word_file.close()
  93. # 将分类结果返回
  94. return sen_word, not_word, degree_word
  95. def list_to_dict(word_list):
  96. """将分词后的列表转为字典,key为单词,value为单词在列表中的索引,索引相当于词语在文档中出现的位置"""
  97. data = {}
  98. for x in range(0, len(word_list)):
  99. data[word_list[x]] = x
  100. return data
  101. def get_init_weight(sen_word, not_word, degree_word):
  102. # 权重初始化为1
  103. W = 1
  104. # 将情感字典的key转为list
  105. sen_word_index_list = list(sen_word.keys())
  106. if len(sen_word_index_list) == 0:
  107. return W
  108. # 获取第一个情感词的下标,遍历从0到此位置之间的所有词,找出程度词和否定词
  109. for i in range(0, sen_word_index_list[0]):
  110. if i in not_word.keys():
  111. W *= -1
  112. elif i in degree_word.keys():
  113. # 更新权重,如果有程度副词,分值乘以程度副词的程度分值
  114. W *= float(degree_word[i])
  115. return W
  116. def socre_sentiment(sen_word, not_word, degree_word, seg_result):
  117. """计算得分"""
  118. # 权重初始化为1
  119. W = 1
  120. score = 0
  121. # 情感词下标初始化
  122. sentiment_index = -1
  123. # 情感词的位置下标集合
  124. sentiment_index_list = list(sen_word.keys())
  125. # 遍历分词结果(遍历分词结果是为了定位两个情感词之间的程度副词和否定词)
  126. for i in range(0, len(seg_result)):
  127. # 如果是情感词(根据下标是否在情感词分类结果中判断)
  128. if i in sen_word.keys():
  129. # 权重*情感词得分
  130. score += W * float(sen_word[i])
  131. # 情感词下标加1,获取下一个情感词的位置
  132. sentiment_index += 1
  133. if sentiment_index < len(sentiment_index_list) - 1:
  134. # 判断当前的情感词与下一个情感词之间是否有程度副词或否定词
  135. for j in range(sentiment_index_list[sentiment_index], sentiment_index_list[sentiment_index + 1]):
  136. # 更新权重,如果有否定词,取反
  137. if j in not_word.keys():
  138. W *= -1
  139. elif j in degree_word.keys():
  140. # 更新权重,如果有程度副词,分值乘以程度副词的程度分值
  141. W *= float(degree_word[j])
  142. # 定位到下一个情感词
  143. if sentiment_index < len(sentiment_index_list) - 1:
  144. i = sentiment_index_list[sentiment_index + 1]
  145. return score
  146. # 计算得分
  147. def setiment_score_7_class(sententce, sen_word):
  148. # 1.对文档分词
  149. seg_list = seg_word(sententce)
  150. # print(seg_list)
  151. # print(seg_list)
  152. # 2.将分词结果列表转为dic,然后找出情感词、否定词、程度副词
  153. sen_word, not_word, degree_word = classify_words_7_class(list_to_dict(seg_list), sen_word)
  154. # # 3.计算得分
  155. score = socre_sentiment(sen_word, not_word, degree_word, seg_list)
  156. return score
  157. def mul_classifier_7_class(content, label_dict):
  158. "根据新闻返回预测标签"
  159. scores = {}
  160. for k, v in label_dict.items():
  161. score = setiment_score_7_class(content, label_dict[k])
  162. scores[k] = score
  163. score_max = scores[max(scores, key=lambda x:abs(scores[x]))]
  164. if score_max == 0:
  165. return "unknow", 0
  166. else:
  167. return max(scores, key=lambda x:abs(scores[x])), scores[max(scores, key=lambda x:abs(scores[x]))]

先写这么多,后期优化的再补充。

Reference:

https://blog.csdn.net/cjjwwss/article/details/79953397

https://blog.csdn.net/lom9357bye/article/details/79058946

参考论文:http://manu44.magtech.com.cn/Jwk_infotech_wk3/article/2017/2096-3467/2096-3467-1-7-61.shtml

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

闽ICP备14008679号