使用azure错误“未找到资源”进行情绪分析

2024-04-25 13:24:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Encountered exception. Operation returned an invalid status code 'Resource Not Found'

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()

Tags: keytextnameinimportclientformatos
1条回答
网友
1楼 · 发布于 2024-04-25 13:24:41

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

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()

结果:

enter image description here

相关问题 更多 >

    热门问题