python - 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。任何帮助将不胜感激!
解决方案
你的意思是有这样的输出吗?
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)
推荐阅读
- javascript - 为什么我在我的 Javascript 中收到 404 错误消息?
- javascript - 如何从 iframe html 代码中解析 url?
- javascript - 如何使用 JavaScript 为音频播放器实现快进功能
- javascript - 为什么 websocket 连接会在 6 到 8 分钟后自动关闭?
- javascript - 使用 JavaScript 将存储为字符串的嵌套数组转换回嵌套数组
- kdb - KDB+/Q:如何理解语言参考中的定义?
- javascript - 如何使用js正则表达式匹配前面没有特殊单词的不确定数字
- vue.js - 路径更改时位置滚动没有改变 - vuejs
- c++ - 在 vs2019 中构建 ffmpeg 失败但不知道为什么
- java - 我是独立的 spark 集群,我们可以使用 map reduce compress 相关属性吗?如果使用这些属性真的有效吗?