python实现情感分析

 `https://api.fanyi.baidu.com/doc/21`

首先我们要下载 textblob包,这是我们代码的关键

pip install textblob
from textblob import TextBlob
import requests
import random
from hashlib import md5

appid = 'xxxxx'
appkey = 'xxxxxxx'

#  `https://api.fanyi.baidu.com/doc/21`
from_lang = 'auto'
to_lang = 'en'

endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path

def make_md5(s, encoding='utf-8'):
    return md5(s.encode(encoding)).hexdigest()

def translate_text(query):
    salt = random.randint(32768, 65536)
    sign = make_md5(appid + query + str(salt) + appkey)
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
    r = requests.post(url, params=payload, headers=headers)
    result = r.json()
    translations = [translation["dst"] for translation in result["trans_result"]]
    return translations

def analyze_sentiment(text):
    """分析情感倾向"""
    translations = translate_text(text)
    en_text = translations[0]
    if en_text:
        blob = TextBlob(en_text)
        sentiment = blob.sentiment

        polarity = sentiment.polarity
        subjectivity = sentiment.subjectivity

        if polarity > 0:
            result = 'positive'
        elif polarity < 0:
            result = 'negative'
        else:
            result = 'neutral'

        return result, polarity, subjectivity
    else:
        return None

if __name__ == '__main__':
    while True:
        input_text = input("请输入要分析的文本 (输入 'exit' 退出):")
        if input_text.lower() == 'exit':
            print("感谢使用,再见!")
            break
        result = analyze_sentiment(input_text)
        if result:
            sentiment, polarity, subjectivity = result
            print(f"情感倾向: {sentiment}, 极性: {polarity}, 主观性: {subjectivity}")
        else:
            print("翻译失败,请稍后重试。")

肥肠不错!

相关推荐

  1. Python情感分析

    2024-04-10 20:02:01       63 阅读
  2. 使用Python实现文本分类情感分析模型

    2024-04-10 20:02:01       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 20:02:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 20:02:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 20:02:01       87 阅读
  4. Python语言-面向对象

    2024-04-10 20:02:01       96 阅读

热门阅读

  1. 软件测试常见面试题目合集【测试面试】

    2024-04-10 20:02:01       30 阅读
  2. myweb项目资料集

    2024-04-10 20:02:01       35 阅读
  3. 深入理解与实践:npm常用命令全面解析

    2024-04-10 20:02:01       36 阅读
  4. Git 的基本概念和使用方式

    2024-04-10 20:02:01       30 阅读
  5. QT_数据库

    2024-04-10 20:02:01       35 阅读
  6. oracle恢复异常处理

    2024-04-10 20:02:01       34 阅读
  7. 模板方法模式

    2024-04-10 20:02:01       31 阅读
  8. 题目 2967: 因子分解

    2024-04-10 20:02:01       33 阅读
  9. 软件测试八股文:alpha测试详解

    2024-04-10 20:02:01       38 阅读
  10. 通用MCU使用查表计算正弦值sin和查看计算时间

    2024-04-10 20:02:01       44 阅读
  11. 数据持久化 1 - PlayerPrefs

    2024-04-10 20:02:01       34 阅读
  12. 递推算法4(c++)

    2024-04-10 20:02:01       33 阅读
  13. [HDCTF 2023]Normal_Rsa(revenge)(素数分解)

    2024-04-10 20:02:01       36 阅读