首页 > 解决方案 > Azure 文本分析和 Python - 从函数中提取值

问题描述

这是一个非常基本的问题,我使用 Azure 的文本分析情绪 API 构建了一个请求,并希望提取整体的正面、中性和负面数值。

代码如下:

def sentiment_analysis_example(client):

    documents = ["I wish you were here with me"]
    response = client.analyze_sentiment(documents=documents)[0]
    print("Document Sentiment: {}".format(response.sentiment))
    print("Overall scores: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} \n".format(
        response.confidence_scores.positive,
        response.confidence_scores.neutral,
        response.confidence_scores.negative,

    ))
          
sentiment_analysis_example(client)
Document Sentiment: neutral
Overall scores: positive=0.04; neutral=0.94; negative=0.02 

基本上只想分别拉出 0.04、0.94 和 0.02 数字(即在打印“正数”时返回 0.04)。

我包含了我认为相关的尽可能多的代码,但我完全意识到这可能很难复制,因为它需要 API。任何帮助将不胜感激!

标签: pythonazure

解决方案


你的意思是有这样的输出吗?

positive=0.04
neutral=0.94
negative=0.02 

例子:

def sentiment_analysis_example(client):

    documents = ["I wish you were here with me"]
    response = client.analyze_sentiment(documents=documents)[0]
    print("positive={0:.2f}".format(response.confidence_scores.positive))
    print("neutral={0:.2f}".format(response.confidence_scores.neutral))
    print("negative={0:.2f}".format(response.confidence_scores.negative))

sentiment_analysis_example(client)

推荐阅读