How to connect to Text Analysis (Named Entity Recognition) with Python

Vedant Modi 1 Reputation point
2021-09-06T12:28:03.347+00:00

I need to run Named Entity Recognition of Text Analysis using Python.

Here's my code which I've sourced from here.

from azure.ai.textanalytics import TextAnalyticsClient  
from azure.core.credentials import AzureKeyCredential  
  
key = "snip"  
endpoint = "https://centralindia.cognitiveservices.azure.com/text/analytics/v3.1/entities/recognition/general"  
  
def authenticate_client():  
    ta_credential = AzureKeyCredential(key)  
    text_analytics_client = TextAnalyticsClient(  
            endpoint=endpoint,  
            credential=ta_credential)  
    return text_analytics_client  
client = authenticate_client()  
  
def entity_recognition_example(client):  
    try:  
        documents = ["I had a wonderful trip to Seattle last week."]  
        result = client.recognize_entities(documents = documents)[0]  
  
        print("Named Entities:\n")  
        for entity in result.entities:  
            print("\tText: \t", entity.text, "\tCategory: \t", entity.category, "\tSubCategory: \t", entity.subcategory,  
                    "\n\tConfidence Score: \t", round(entity.confidence_score, 2), "\tLength: \t", entity.length, "\tOffset: \t", entity.offset, "\n")  
  
    except Exception as err:  
        print("Encountered exception. {}".format(err))  
entity_recognition_example(client)  

This gives me the error: Encountered exception. <urllib3.connection.HTTPSConnection object at 0x000001D0BD6A26A0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

I'm not sure how to write the endpoint, this page says I have to follow this format: https://<your-custom-subdomain>.cognitiveservices.azure.com/text/analytics/v3.1/entities/recognition/general. Not sure what to put in place of "<your-custom-subdomain>"

Azure AI Language
Azure AI Language
An Azure service that provides natural language capabilities including sentiment analysis, entity extraction, and automated question answering.
359 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Liroy Benaïm 241 Reputation points
    2021-09-06T12:33:38.917+00:00

    Hello,

    Look this code about the certification AI-900 https://github.com/MicrosoftLearning/mslearn-ai900/blob/main/07%20-%20Text%20Analytics.ipynb
    you can use it with azure marchine learning

    Have a great day !
    Please don't forget to Accept as answer and upvote :)

    0 comments No comments

  2. romungi-MSFT 42,286 Reputation points Microsoft Employee
    2021-09-07T05:35:01.013+00:00

    @Vedant Modi The custom subdomain is basically the name of your resource you created from Azure portal. You can find your key and endpoint in the resource's key and endpoint page, under resource management. Since you have already copied the key you can directly copy the endpoint and add the required API name to the call or you can just replace your resource name in the above code and try again.

    0 comments No comments