使用senta模型进行简单的文本情感分析

0 介绍

paddlepaddle是百度开源的一套机器学习(AI)框架

Senta_lstm是基于lstm模型、采用paddlepaddle框架实现的nlp模型,支持语义分析和情感分析

SKEP是上述模型的改进版

paddlehub集成了模型的预训练结果,提供开箱可用的模型。

1 安装

sudo pip3 install paddlepaddle -i https://mirror.baidu.com/pypi/simple
sudo pip3 install paddlehub -i https://mirror.baidu.com/pypi/simple

2 情感分析(lstm)

import paddlehub as hub
 
# load model
senta = hub.Module(name="senta_lstm")
 
# text
test_text = [
 "不错呦",
 "哎,一般"
]
 
# classify
results = senta.sentiment_classify(data={"text": test_text})
 
# result
for result in results:
  print(result)

#
{'text': '不错呦', 'sentiment_label': 1, 'sentiment_key': 'positive', 'positive_probs': 0.9924, 'negative_probs': 0.0076}
{'text': '哎,一般', 'sentiment_label': 0, 'sentiment_key': 'negative', 'positive_probs': 0.0266, 'negative_probs': 0.9734}

3 情感分析(SKEP)

import paddlehub as hub

# load model 
skep = hub.Module(name="ernie_skep_sentiment_analysis")

# text 
test_text = [
 "不错呦",
 "哎,一般"
]

# predict
results = skep.predict_sentiment(texts=test_text, use_gpu=True)

# result
for result in results:
  print(result)

# output
{'text': '不错呦', 'sentiment_label': 'positive', 'positive_probs': 0.9260105490684509, 'negative_probs': 0.07398953288793564}
{'text': '哎,一般', 'sentiment_label': 'negative', 'positive_probs': 0.09273454546928406, 'negative_probs': 0.9072654247283936}

参考链接:

  1. https://github.com/baidu/Senta
  2. https://www.paddlepaddle.org.cn/modelbasedetail/senta
  3. https://github.com/PaddlePaddle/PaddleHub
  4. https://www.paddlepaddle.org.cn/hub

 

Leave a Reply

Your email address will not be published. Required fields are marked *