数据挖掘笔记-文本情感简单判断

  文本情感简单判断的过程大概如下:经过预处理后的文本,首先识别不同极性类别的特征项,通过构建好的情感词表(褒义词、贬义词)、否定词词表、程度副词词表以及反问句标记词表做相应处理,获取该条文本中每个特征项的权值,最后作求和运算,获得整段文本的情感倾向值,进而判别出情感倾向性。
       文本以句子为单位,以标点符号为分割标志,将每条文本分割为N个句子,提取每个句子中的情感词,根据以下情况计算权值:
1、出现程度副词修饰情感词
2、出现奇数否定词修饰情感词
3、该句子是包含情感词的感叹句
权值 = 情感词权重 * 相应情况出现的词的权重
句子的情感倾向度等于所有情感词计算出来的权值之和。
文本的情感倾向度等于所有句子计算出来的权值之和。
最终情感倾向值会出现下列三种情况: 
文本的情感倾向度权值 > 0;为正面情感
文本的情感倾向度权值 = 0;为中性情感
文本的情感倾向度权值 < 0;为负面情感 

下面是用Java简单实现文本情感简单判断的一个例子:
[java]  view plain copy
  1. public class Emotion {  
  2.       
  3.     //褒义词  
  4.     public static final int COMMENDATORY = 1;  
  5.     //贬义词  
  6.     public static final int DEROGRATORY = 2;  
  7.     //中性词  
  8.     public static final int NEUTRAL = 3;  
  9.       
  10.     public static int judge(String content) {  
  11.         //把内容划分为句子  
  12.         String[] sentences = content.split("[,.,。]");  
  13.         //判断每个句子的情感  
  14.         double cWeight = 0;  
  15.         for (String sentence : sentences) {  
  16.             double sWeight = 0;  
  17.             //判断句子是否是感叹句  
  18.             boolean isExclamatorySentence = WordUtils.isExclamatorySentence(sentence);  
  19.             String[] words = WordUtils.splitByNlp(sentence);  
  20.             ShowUtils.printToConsole(words);  
  21.             for (int i = 0, len = words.length; i < len; i++) {  
  22.                 String word = words[i];  
  23.                 //判断是否是情感词  
  24.                 boolean isEmotion = WordUtils.isEmotionWord(word);  
  25.                 if (!isEmotion) continue;  
  26.                 double weight = WordUtils.getWordWeight(word);  
  27.                 int negativeNum = 0;  
  28.                 String negativeWord = null;  
  29.                 int index = new Integer(i);  
  30.                 while (index > 0) {  
  31.                     String preWord = words[i - 1];  
  32.                     if (WordUtils.isEmotionWord(preWord)) break;  
  33.                     //判断副词  
  34.                     boolean isAdverbs = WordUtils.isAdverbsWord(preWord);  
  35.                     if (isAdverbs) {  
  36.                         double aWeight = WordUtils.getWordWeight(preWord);  
  37.                         weight = weight * aWeight;  
  38.                     }  
  39.                     boolean isNegative = WordUtils.isNegativeWord(preWord);  
  40.                     if (isNegative) {  
  41.                         if (null == negativeWord) negativeWord = preWord;  
  42.                         negativeNum += 1;  
  43.                     }  
  44.                     index--;  
  45.                 }  
  46.                 //判断否定词  
  47.                 if (negativeNum % 2 == 1) {  
  48.                     double nWeight = WordUtils.getWordWeight(negativeWord);  
  49.                     weight = weight * nWeight;  
  50.                 }  
  51.                 //判断感叹词  
  52.                 if (isExclamatorySentence) {  
  53.                     double iWeight = WordUtils.getInterjectionWeight();  
  54.                     weight = weight * iWeight;  
  55.                 }  
  56.                 //句子权值等于情感词的权值求和  
  57.                 sWeight += weight;  
  58.             }  
  59.             System.out.println("sWeight: " + sWeight);  
  60.             //文本权值等于句子的权值求和  
  61.             cWeight += sWeight;  
  62.         }  
  63.         System.out.println("cWeight: " + cWeight);  
  64.         return cWeight == 0 ? NEUTRAL : (cWeight > 0 ? COMMENDATORY : DEROGRATORY);  
  65.     }  
  66.       
  67.     public static void main(String[] args) {  
  68.         String content = "我喜欢这个明星";  
  69.         System.out.println(judge(content));  
  70.         content = "我讨厌这个明星";  
  71.         System.out.println(judge(content));  
  72.         content = "我不讨厌这个明星";  
  73.         System.out.println(judge(content));  
  74.         content = "我不是非常喜欢这个明星!";  
  75.         System.out.println(judge(content));  
  76.         content = "这本小说太无聊了,内容枯燥,文笔特别烂。";  
  77.         System.out.println(judge(content));  
  78.         System.exit(0);  
  79.     }  
  80.       
  81. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python数据挖掘学习笔记主要包括以下几个方面的内容:Python基础知识、Python爬虫技术、Python数据分析与数据挖掘。其中,Python基础知识部分介绍了Python编程语言的基本语法、数据类型、流程控制等内容,为数据挖掘的学习打下了基础。Python爬虫技术部分介绍了如何使用Python编写爬虫程序,从网页中获取所需数据。Python数据分析与数据挖掘部分则介绍了使用Python进行数据分析和数据挖掘的相关技术和工具。 在Python数据挖掘中,还涉及到一些扩展库的使用,可以使用pip或apt-get进行安装,例如numpy库可以使用命令"sudo pip install numpy"或"sudo apt-get install python-numpy"进行安装。 另外,Matplotlib是Python中最常用的绘图库之一,主要用于绘制二维图形,也可以绘制简单的三维图形。下面是一个使用Matplotlib进行简单绘图的示例代码: ```python import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10, 1000) y = np.sin(x) z = np.cos(x ** 2) plt.figure(figsize=(8, 4)) plt.plot(x, y, label='$\sin x$', color='red', linewidth=2) plt.plot(x, z, 'b--', label='$\cos x^2$') plt.xlabel('Time(s)') plt.ylabel('Volt') plt.title('A Simple Example') plt.ylim(0, 2.2) plt.legend() plt.show() ``` 这段代码使用了numpy库生成了一组x轴的数据,然后分别计算了对应的y轴和z轴的数值。接下来使用Matplotlib进行绘图,其中plt.plot函数用于绘制曲线,plt.xlabel和plt.ylabel分别设置x轴和y轴的标签,plt.title设置图的标题,plt.ylim设置y轴的范围,plt.legend用于显示图例,plt.show用于显示图形。 通过学习这些内容,你可以掌握Python数据挖掘的基本知识和常用技术,为进一步的学习和实践打下坚实的基础。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python数据挖掘学习笔记](https://blog.csdn.net/yinghuoai/article/details/88392141)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python数据挖掘笔记](https://blog.csdn.net/djm82755/article/details/101452842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值