当前位置:   article > 正文

python文本情绪分析_python 文本情绪分析

python 文本情绪分析

本文介绍两个情感分析的库, 它们可以根据输入的文字内容, 给出一个打分, 这个分数就是对文本的情感的评价(多好/多坏)

使用的库

英文: textblob

中文: snownlp

项目结构

使用场景

区分好评和差评->由此分析产品or视频的好坏/新闻或文章的情绪分析...

用于测试的文本(英文)

mytext.txt

  1. Yesterday I went hiking
  2. On the way up I hurt my knees! It was not good!
  3. We did not have a lot of fun! Now I have some wounds and I am bleeding
  4. I am also a little bit sad
  5. But all in all it was a great experience

mytext_down.txt

  1. I hate this thing
  2. It destroyed my life! I would never buy it again
  3. Word experience yet! This is a bad product

mytext_up.txt

  1. I loved this product
  2. It was great,I will definitely buy more of it
  3. Excellent experience

英文情感分析

运行结果: 1(正向) 0(中性) -1(负面)
  1. from textblob import TextBlob
  2. # 读取本地文件进行分析
  3. with open('mytext.txt', 'r', encoding='utf-8') as f:
  4. text=f.read()
  5. blob = TextBlob(text)
  6. sentiment = blob.sentiment.polarity # 1 ~ -1
  7. print(sentiment)
  8. with open('mytext_up.txt', 'r', encoding='utf-8') as f:
  9. text=f.read()
  10. blob = TextBlob(text)
  11. sentiment = blob.sentiment.polarity # 1 ~ -1
  12. print(sentiment)
  13. with open('mytext_down.txt', 'r', encoding='utf-8') as f:
  14. text=f.read()
  15. blob = TextBlob(text)
  16. sentiment = blob.sentiment.polarity # 1 ~ -1
  17. print(sentiment)

中文情感分析

运行结果: 0~1 越低表示文本越消极
  1. # 中文nlp,仿textblob
  2. from snownlp import SnowNLP
  3. # 读取本地文件进行分析
  4. text=input("输入分析文本: ")
  5. blob = SnowNLP(text)
  6. sentiment = blob.sentiments # 这个库的情绪分 0 ~ 1,越低越消极
  7. print(sentiment)

结合flask, 开放http服务

  1. # flask创建http接口
  2. from flask import Flask, request, jsonify
  3. from flask_cors import CORS
  4. from textblob import TextBlob
  5. # 中文nlp,仿textblob
  6. from snownlp import SnowNLP
  7. # flask
  8. app = Flask(__name__)
  9. CORS(app, resources=r'/*') # 注册CORS, "/*" 允许访问所有api
  10. @app.route("/api/sentiment/analyze/<text>", methods=['get'])
  11. def analyze_short(text):
  12. blob = TextBlob(text) # 情绪评分值
  13. sentiment = blob.sentiment.polarity
  14. return jsonify({"res": sentiment}), 200
  15. # Content-Type: application/json 请求内容格式
  16. @app.route("/api/sentiment/analyze", methods=['post'])
  17. def analyze_large():
  18. blob = TextBlob(request.json.get('text')) # 情绪评分值
  19. sentiment = blob.sentiment.polarity
  20. return jsonify({"res": sentiment}), 200
  21. # 中文情感分析
  22. @app.route("/api/sentiment/analyze/zh_cn/<text>", methods=['get'])
  23. def cn_analyze_short(text):
  24. s = SnowNLP(text) # 情绪评分值
  25. sentiment = s.sentiments # 这个库的情绪分 0 ~ 1,越低越消极
  26. return jsonify({"res": sentiment}), 200
  27. # Content-Type: application/json 请求内容格式
  28. @app.route("/api/sentiment/analyze/zh_cn", methods=['post'])
  29. def cn_analyze_large():
  30. s = SnowNLP(request.json.get('text')) # 情绪评分值
  31. sentiment = s.sentiments # 这个库的情绪分 0 ~ 1,越低越消极
  32. return jsonify({"res": sentiment}), 200
  33. if __name__ == "__main__":
  34. app.run(host='0.0.0.0', port=8763)
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号