【问题标题】:Sentiment Analysis using azure error 'Resource not found'使用天蓝色错误“找不到资源”的情绪分析
【发布时间】:2020-03-30 01:23:03
【问题描述】:

我创建了一个 python 程序,它接受一个字符串作为输入并对其执行情绪分析。

我已经按照文档中的说明创建了环境变量,并且还重新启动了 cmd 以及 Visual Studio,但仍然出现以下错误:

遇到异常。操作返回无效的状态代码“未找到资源”

python程序如下:

import os
from azure.cognitiveservices.language.textanalytics import TextAnalyticsClient
from msrest.authentication import CognitiveServicesCredentials

#TEXT_ANALYTICS_SUBSCRIPTION_KEY = 'b2c4f0ee35c941078d9e6971c15c3472'
#TEXT_ANALYTICS_ENDPOINT = 'https://westcentralus.api.cognitive.microsoft.com/text/analytics'

key_var_name = 'TEXT_ANALYTICS_SUBSCRIPTION_KEY'
if not key_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(key_var_name))
subscription_key = os.environ[key_var_name]

endpoint_var_name = 'TEXT_ANALYTICS_ENDPOINT'
if not endpoint_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(endpoint_var_name))
endpoint = os.environ[endpoint_var_name]

def authenticateClient():
    credentials = CognitiveServicesCredentials(subscription_key)
    text_analytics_client = TextAnalyticsClient(
        endpoint=endpoint, credentials=credentials)
    return text_analytics_client

sentence = input("Enter the string:")

def sentiment():

    client = authenticateClient()

    try:
        documents = [
            {"id": "1", "language": "en", "text": sentence}
        ]

        response = client.sentiment(documents=documents)
        for document in response.documents:
            print("Entered Text: ", document.text, ", Sentiment Score: ",
                  "{:.2f}".format(document.score))

    except Exception as err:
        print("Encountered exception. {}".format(err))
sentiment()

【问题讨论】:

    标签: python azure sentiment-analysis microsoft-cognitive azure-cognitive-services


    【解决方案1】:

    不建议在此处共享您的订阅密钥,请尽快撤销此订阅密钥。对于您的问题,请尝试以下操作:

    import os
    from azure.cognitiveservices.language.textanalytics import TextAnalyticsClient
    from msrest.authentication import CognitiveServicesCredentials
    
    subscription_key = 'b2c4f0ee35c941078d9e6971c15c3472'
    endpoint = 'https://westcentralus.api.cognitive.microsoft.com/'
    
    def authenticateClient():
        credentials = CognitiveServicesCredentials(subscription_key)
        text_analytics_client = TextAnalyticsClient(
            endpoint=endpoint, credentials=credentials)
        return text_analytics_client
    
    sentence = input("Enter the string:")
    
    def sentiment():
    
        client = authenticateClient()
    
        try:
            documents = [
                {"id": "1", "language": "en", "text": sentence}
            ]
    
    
    
            response = client.sentiment(documents=documents)
            for document in response.documents:
                print("Entered Text: ",sentence ,", Sentiment Score: ",
                      "{:.2f}".format(document.score))
    
    
        except Exception as err:
            print("Encountered exception. {}".format(err))
    
    sentiment()
    

    结果:

    【讨论】:

    • 该 api 仅用于演示目的,在 azure 上免费提供,有效期仅为 7 天。谢谢你的解决方案。我喜欢它兄弟
    猜你喜欢
    • 2015-03-07
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2018-11-26
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多