首页 > 解决方案 > Microsoft 文本分析 API (v2.1) 示例代码不起作用

问题描述

我想使用 Microsoft Azure 的情绪分析 API。我使用他们的示例代码(https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/quickstarts/python)如下。

# -*- coding: utf-8 -*
import requests
# pprint is used to format the JSON response
from pprint import pprint

import os

subscription_key = "*********"
endpoint = "https://westcentralus.api.cognitive.microsoft.com/text/analytics"


sentiment_url = endpoint + "/text/analytics/v2.1/sentiment"


documents = {"documents": [
    {"id": "1", "language": "en",
        "text": "I had a wonderful experience! The rooms were wonderful and the staff was helpful."},
    {"id": "2", "language": "en",
        "text": "I had a terrible time at the hotel. The staff was rude and the food was awful."},
    {"id": "3", "language": "es",
        "text": "Los caminos que llevan hasta Monte Rainier son espectaculares y hermosos."},
    {"id": "4", "language": "es",
     "text": "La carretera estaba atascada. Había mucho tráfico el día de ayer."}
]}


headers = {"Ocp-Apim-Subscription-Key": subscription_key}
response = requests.post(sentiment_url, headers=headers, json=documents)
sentiments = response.json()
pprint(sentiments)

但它只返回{'error': {'code': '404', 'message': 'Resource not found'}}有什么问题?

标签: azuremicrosoft-cognitivejsonresponse

解决方案


我在这方面的分析方面玩的不多,所以我可能完全偏离了基础,但在我看来,你已经把“文本/分析”加倍了。

endpoint = "https://westcentralus.api.cognitive.microsoft.com/text/analytics"
sentiment_url = endpoint + "/text/analytics/v2.1/sentiment"

将给出 https://westcentralus.api.cognitive.microsoft.com/text/analytics/text/analytics/v2.1/sentiment的最终结果

它可能应该只是:

https://westcentralus.api.cognitive.microsoft.com/text/analytics/v2.1/sentiment


推荐阅读