Skip to main content

Chinese Sentiment Classifier

Project description

PyPI version License Apache 2.0 Language Python3

pysenti

Chinese Sentiment Classification Tool for Python. 中文情感极性分析工具。

pysenti基于规则词典的情感极性分析,扩展性强,可作为工业用的基准方法。

方法

规则的解决思路

  1. 中文情感极性分析,文本切分为段落,再切词,通过情感词标识出各个词语的情感极性,包括积极、中立、消极。
  2. 结合句子结构(包括连词、否定词、副词、标点等)给各情感词语的情感极性赋予权重,然后加权求和得到文本的情感极性得分。
  3. 优点:泛化性好,规则可扩展性强,所有领域通用。
  4. 缺点:规则词典收集困难,专家系统的权重设定有局限,单一领域准确率相比模型方法低。

模型的解决思路

  1. 常见的NLP文本分类模型均可,包括经典文本分类模型(LR、SVM、Xgboost等)和深度文本分类模型(TextCNN、Bi-LSTM、BERT等)。
  2. 优点:单一领域准召率高。
  3. 缺点:不通用,有标注数据的样本收集困难,扩展性弱。

特征

规则

模型

  • bayes 文本分类模型
  • 样本数据来自商品评论数据,分为积极、消极两类。

demo

https://www.borntowin.cn/product/sentiment_classify

安装

  • 全自动安装:pip3 install pysenti
  • 半自动安装:
git clone https://github.com/shibing624/pysenti.git
cd pysenti
python3 setup.py install

使用示例

规则方法

import pysenti

texts = ["苹果是一家伟大的公司",
         "土豆丝很好吃",
         "土豆丝很难吃"]
for i in texts:
    r = pysenti.classify(i)
    print(i, r['score'], r)

output:

苹果是一家伟大的公司 3.4346924811096997 {'score': 3.4346924811096997, 'sub_clause0': {'score': 3.4346924811096997, 'sentiment': [{'key': '苹果', 'adverb': [], 'denial': [], 'value': 1.37846341627, 'score': 1.37846341627}, {'key': '是', 'adverb': [], 'denial': [], 'value': -0.252600480826, 'score': -0.252600480826}, {'key': '一家', 'adverb': [], 'denial': [], 'value': 1.48470161748, 'score': 1.48470161748}, {'key': '伟大', 'adverb': [], 'denial': [], 'value': 1.14925252286, 'score': 1.14925252286}, {'key': '的', 'adverb': [], 'denial': [], 'value': 0.0353323193687, 'score': 0.0353323193687}, {'key': '公司', 'adverb': [], 'denial': [], 'value': -0.360456914043, 'score': -0.360456914043}], 'conjunction': []}}
土豆丝很好吃 2.294311221077 {'score': 2.294311221077, 'sub_clause0': {'score': 2.294311221077, 'sentiment': [{'key': '土豆丝', 'adverb': [], 'denial': [], 'value': 0.294892711165, 'score': 0.294892711165}, {'key': '很', 'adverb': [], 'denial': [], 'value': 0.530242664632, 'score': 0.530242664632}, {'key': '好吃', 'adverb': [], 'denial': [], 'value': 1.46917584528, 'score': 1.46917584528}], 'conjunction': []}}
土豆丝很难吃 -2.381874203563 {'score': -2.381874203563, 'sub_clause0': {'score': -2.381874203563, 'sentiment': [{'key': '土豆丝', 'adverb': [], 'denial': [], 'value': 0.294892711165, 'score': 0.294892711165}, {'key': '很', 'adverb': [], 'denial': [], 'value': 0.530242664632, 'score': 0.530242664632}, {'key': '难吃', 'adverb': [], 'denial': [], 'value': -3.20700957936, 'score': -3.20700957936}], 'conjunction': []}}

score: 正值是积极情感;负值是消极情感。

模型方法

from pysenti import model_classifier

texts = ["苹果是一家伟大的公司",
         "土豆丝很好吃",
         "土豆丝很难吃"]
for i in texts:
    result = model_classifier.classify(i)
    print(i, result)

output:

苹果是一家伟大的公司 {'positive_prob': 0.682, 'negative_prob': 0.318}
土豆丝很好吃 {'positive_prob': 0.601, 'negative_prob': 0.399}
土豆丝很难吃 {'positive_prob': 0.283, 'negative_prob': 0.717}

感谢

  1. snownlp
  2. SentimentPolarityAnalysis

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysenti-0.1.3.tar.gz (4.7 MB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page