2 有用
13 下载

中文情感分析库

文件列表(压缩包大小 395.37K)

免费

概述

一、安装

方法一 pip install cnsenti

方法二 pip install cnsenti -i https://pypi.tuna.tsinghua.edu.cn/simple/

二、快速上手

中文文本情感词正负情感词统计

from cnsenti import Sentiment

senti = Sentiment()
test_text= '我好开心啊,非常非常非常高兴!今天我得了一百分,我很兴奋开心,愉快,开心'
result = senti.sentiment_count(test_text)
print(result)

Run

{'words': 24, 
'sentences': 2, 
'pos': 4, 
'neg': 0}

中文文本情绪统计

from cnsenti import Emotion

emotion = Emotion()
test_text = '我好开心啊,非常非常非常高兴!今天我得了一百分,我很兴奋开心,愉快,开心'
result = emotion.emotion_count(test_text)
print(result)

Run

{'words': 22, 
'sentences': 2, 
'好': 0, 
'乐': 4, 
'哀': 0, 
'怒': 0, 
'惧': 0, 
'恶': 0, 
'惊': 0}

三、文档

cnsenti包括Emotion和Sentiment两大类,其中

  • Emotion 情绪计算类,包括**emotion_count(text)**方法
  • Sentiment 正负情感计算类,包括**sentiment_count(text)和sentiment_calculate(text)**两种方法

3.1 emotion_count(text)

emotion_count(text)y用于统计文本中各种情绪形容词出现的词语数。使用大连理工大学情感本体库词典,支持七种情绪统计(好、乐、哀、怒、惧、恶、惊)。

from cnsenti import Emotion

emotion = Emotion()
test_text = '我好开心啊,非常非常非常高兴!今天我得了一百分,我很兴奋开心,愉快,开心'
result = emotion.emotion_count(test_text)
print(result)

返回

{'words': 22, 
'sentences': 2, 
'好': 0, 
'乐': 4, 
'哀': 0, 
'怒': 0, 
'惧': 0, 
'恶': 0, 
'惊': 0}

其中

  • words 中文文本的词语数
  • sentences 中文文本的句子数
  • 好、乐、哀、怒、惧、恶、惊 text中各自情绪出现的词语数

3.2 sentiment_count(text)

隶属于Sentiment类,可对文本text中的正、负面词进行统计。默认使用Hownet词典,后面会讲到如何导入自定义正、负情感txt词典文件。这里以默认hownet词典进行统计。

from cnsenti import Sentiment

senti = Sentiment()
test_text = '我好开心啊,非常非常非常高兴!今天我得了一百分,我很兴奋开心,愉快,开心'
result = senti.sentiment_count(test_text)
print(result)

Run

{'words': 24, 
'sentences': 2, 
'pos': 4, 
'neg': 0}

其中

  • words 文本中词语数
  • sentences 文本中句子数
  • pos 文本中正面词总个数
  • neg 文本中负面词总个数

3.3 sentiment_calculate(text)

隶属于Sentiment类,可更加精准的计算文本的情感信息。相比于sentiment_count只统计文本正负情感词个数,sentiment_calculate还考虑了

  • 情感词前是否有强度副词的修饰作用
  • 情感词前是否有否定词的情感语义反转作用

比如

from cnsenti import Sentiment

senti = Sentiment()
test_text = '我好开心啊,非常非常非常高兴!今天我得了一百分,我很兴奋开心,愉快,开心'
result1 = senti.sentiment_count(test_text)
result2 = senti.sentiment_calculate(test_text)
print('sentiment_count',result1)
print('sentiment_calculate',result2)

Run

sentiment_count 
{'words': 22, 
'sentences': 2, 
'pos': 4, 
'neg': 0}

sentiment_calculate 
{'sentences': 2, 
'words': 22, 
'pos': 27.0, 
'neg': 0.0}

3.4 自定义词典

我们先看看没有情感形容词的情形

from cnsenti import Sentiment
senti = Sentiment()      #两txt均为utf-8编码
test_text = '这家公司是行业的引领者,是中流砥柱。'
result1 = senti.sentiment_count(test_text)
result2 = senti.sentiment_calculate(test_text)
print('sentiment_count',result1)
print('sentiment_calculate',result2)

Run

sentiment_count {'words': 10, 'sentences': 1, 'pos': 0, 'neg': 0}
sentiment_calculate {'sentences': 1, 'words': 10, 'pos': 0, 'neg': 0}

如我所料,虽然句子是正面的,但是因为cnsenti自带的情感词典仅仅是形容词情感词典,对于很多场景而言,适用性有限,所以pos=0。

3.4.1 自定词典格式

好在cnsenti支持导入自定义词典,但目前只有Sentiment类支持导入自定义正负情感词典,自定义词典需要满足

  • 必须为txt文件
  • 原则上建议encoding为utf-8
  • txt文件每行只有一个词

3.4.2 Sentiment自定义词典参数

senti = Sentiment(pos='正面词自定义.txt',  
                  neg='负面词自定义.txt', 
                  merge=True,  
                  encoding='utf-8')
  • pos 正面情感词典txt文件路径
  • neg 负面情感词典txt文件路径
  • merge 布尔值;merge=True,cnsenti会融合自定义词典和cnsenti自带词典;merge=False,cnsenti只使用自定义词典
  • encoding 两txt均为utf-8编码

3.4.3 自定义词典使用案例

这部分我放到test文件夹内,代码和自定义词典均在test内,所以我使用相对路径设定自定义词典的路径

|test
   |---代码.py
   |---正面词自定义.txt
   |---负面词自定义.txt

正面词自定义.txt

中流砥柱
引领者

from cnsenti import Sentiment

senti = Sentiment(pos='正面词自定义.txt',  #正面词典txt文件相对路径
                  neg='负面词自定义.txt',  #负面词典txt文件相对路径
                  merge=True,             #融合cnsenti自带词典和用户导入的自定义词典
                  encoding='utf-8')      #两txt均为utf-8编码

test_text = '这家公司是行业的引领者,是中流砥柱。今年的业绩非常好。'
result1 = senti.sentiment_count(test_text)
result2 = senti.sentiment_calculate(test_text)
print('sentiment_count',result1)
print('sentiment_calculate',result2)

Run

sentiment_count {'words': 16, 'sentences': 2, 'pos': 2, 'neg': 0}
sentiment_calculate {'sentences': 2, 'words': 16, 'pos': 5, 'neg': 0}

上面参数我们传入了正面自定义词典和负面自定义词典,并且使用了融合模式(merge=True),可以利用cnsenti自带的词典和刚刚导入的自定义词典进行情感计算。

补充:

所设计的这个库目前仅能支持两类型pos和neg,如果你的研究问题是两分类问题,如好坏、美丑、善恶、正邪、友好敌对,你就可以定义两个txt文件,分别赋值给pos和neg,就可以使用cnsenti库。

来源https://github.com/thunderhit/cnsenti

理工酷提示:

如果遇到文件不能下载或其他产品问题,请添加管理员微信:ligongku001,并备注:产品反馈

评论(2)

0/250
Siang • 5 对资源的评价
询问 你上面的 words是计算什么?看起来不是22个字阿?
2021-12-23 回复
天明 • 1284
@Siang 是分词后,词的数量,不是字.
2021-12-27 回复