Azure Cognitive Services Text Analytics Python SDK

Azure Cognitive Services Text Analytics Python SDK

In this Azure tutorial, we will discuss Azure Cognitive Services Text Analytics Python SDK, Along with this, we will also discuss a few other topics like below

  • Azure Cognitive Services
  • Azure Cognitive Services Text Analytics API
  • Create an Azure Text Analytics API using Azure Portal
  • Create a Python application
  • Detect languages
  • Sentiment Analysis
  • Key phrase extraction
  • Link Entities Identification

Azure Cognitive Services Text Analytics Python SDK

Well, here we will discuss an excellent topic i.e Azure Cognitive Services Text Analytics Python SDK. Before moving to the actual topic here. I would like to give some glance at the Azure Cognitive Services and Azure Cognitive Services Text Analytics API.

Azure Cognitive Services

Azure Cognitive services provide a number of Excellent Azure APIs that help you with the capability to make it easy to enhance your applications in different areas like vision, speech, knowledge, search and language, etc. That actually provides you the ability to implement the AI features in these areas easily.

Azure Cognitive services make the developer’s life easy by providing them the opportunity to implement the AI feature in different applications with very little coding effort. Even though they don’t have the expertise in the AI area but still they can able to implement the AI feature with just a few API calls. Check out some more information on Azure Cognitive Services now.

Azure Cognitive Services Text Analytics API

Azure text analytics service is the part of Azure cognitive services that actually provide you the opportunity to work with different functionalities easily like Key phrase extraction, Sentiment analysis, Language detection, named entity recognition, etc. You will get a lot of requirements in these areas in real-time, Azure Cognitive Services Text Analytics API helps a lot to quickly achieve these functionalities.

This API supports different languages like Chinese, Danish, Dutch, English, Polish, Japanese, Russian, Swedish, Spanish, Turkish, French, German, Greek, Italian, Korean.

Assuming you got a little bit of idea on the Azure Cognitive Services and Azure Cognitive Services Text Analytics API, Let’s start the actual topic i.e Azure Cognitive Services Text Analytics Python SDK.

This article will give you a glance at working with Azure Cognitive Services Text Analytics API and Python to implement different functionalities like Language detection, Sentiment Analysis, Key Phrase Extraction, Link Entities Identification, etc.

Before starting the actual functionality, we need to know the Prerequisites needed here.

Prerequisites

  • You must have a valid Azure Subscription or a Valid Azure Account. If you don’t have till now, create an Azure Free Account now.
  • Python 3.x or later version needs to be installed on your machine.
  • You must install the Python requests library. You can use the below cmdlet to install the Python request library
pip install --upgrade requests
  • The next important thing is, you need to create the Azure Text Analytics service in the Azure Portal and you need to get the Key and Endpoint value to use it in the code.

Assuming, you are ready with all the Prerequisites needed here. As a first step of the implementation, we need to Create an Azure Text Analytics API using Azure Portal.

Create an Azure Text Analytics API using Azure Portal

You need to follow the below steps to configure Azure Text Analytics in Azure Portal.

Login to the Azure Portal (https://portal.azure.com/)

Once you logged in to the Azure Portal, from the left side panel, click on the + Create a Resource button.

Create and configure Azure Text Analytics

Search for the Text Analytics as show below

How to Create and configure Azure Text Analytics

For the next step onwards, you can follow my article to Create an Azure Text Analytics API.

Assuming, you have created the Azure Text Analytics API using the above article. Now my Azure Cognitive Services Text Analytics API is ready and you can see it below.

Use Azure Text Analytics In Power BI

Now, the important step here is, we need to get the Key and Endpoint values of this Azure Cognitive Services Text Analytics API. So to get those values, click on the keys and Endpoint link from the left navigation, and then you can able to see the Key1 and Key2 values. Click on the copy button as highlighted to copy the Key1 value and keep it in a notepad.

You can also click on the Show keys button to see the value of Key1 and Key2. You can click on the Regenerate Key1 and Regenerate key2 buttons to regenerated a new key for Key1 and Key2.

How to configure Azure Text Analytics in Azure Portal

So we are done with our first step and we have the Azure text Analytics key and Endpoint values with us. Now Let’s start all the functionalities one by one. As the next step, we need to create a Python application.

Create a Python application

Use an IDE on your choice and Create a Python application. and then add the below import statements

import requests
from pprint import pprint
import os

The next step is, we need to create two variables and those will contain the values of key and Endpoint of the Azure Text Analytics API above.

key = "<Provide your Azure Text Analytics API Key here>"
endpoint = "<Provide your Azure Text Analytics endpoint here>"

Detect languages

We need to append the /text/analytics/v3.0/languages with your Azure Cognitive Services Text Analytics API endpoint URL to work with the Detect language functionality. If you will consider an example, the endpoint URL will be like below

https://<yoursubdomain>.cognitiveservices.azure.com/text/analytics/v3.0/languages
https://demotextanalytics12.cognitiveservices.azure.com/text/analytics/v3.0/languages

You can mention the API URL like below

url = endpoint + "/text/analytics/v3.0/languages"

The API contains lists of documents with Id and text value, So it should be like below

documents =  {
     "documents": [
         {
             "id": "1",
             "text": "I Love AzureLessons"
         },
         {
             "id": "2",
             "text": "ମୁଁ AzureLessons କୁ ଭଲ ପାଏ |"
         },
         {
             "id": "3",
             "text": "我爱Azure课程"
         }
,

         {
             "id": "4",
             "text": "आई लव अज़ुरेलेसन"
         }
,

         {
             "id": "5",
             "text": "Adoro AzureLessons"
         }
,

         {
             "id": "6",
             "text": "
আমি অ্যাজুরিলেসনকে ভালবাসি"
         }
     ]
 }

Now our documents tuple is ready. So the next step is we need to use the Request library to send the document to API. We need to add the key value of our Azure Cognitive Services Text Analytics API to the Ocp-Apim-Subscription-Key header then we need to send our request to the requests.post(). So the python code will be like below

headers = {"Ocp-Apim-Subscription-Key": key}
response = requests.post(url, headers=headers, json=documents)
languages = response.json()
pprint(languages)

Now, once you will run the application, you should get the below output in JSON format

{
    "documents": [
        {
            "id": "1",
            "detectedLanguage": {
                "name": "English",
                "iso6391Name": "en",
                "confidenceScore": 0.9
            },
            "warnings": []
        },
        {
            "id": "2",
            "detectedLanguage": {
                "name": "Oriya",
                "iso6391Name": "or",
                "confidenceScore": 0.83
            },
            "warnings": []
        },
        {
            "id": "3",
            "detectedLanguage": {
                "name": "Chinese_Simplified",
                "iso6391Name": "zh_chs",
                "confidenceScore": 0.8
            },
            "warnings": []
        },
        {
            "id": "4",
            "detectedLanguage": {
                "name": "Hindi",
                "iso6391Name": "hi",
                "confidenceScore": 0.97
            },
            "warnings": []
        },
        {
            "id": "5",
            "detectedLanguage": {
                "name": "English",
                "iso6391Name": "en",
                "confidenceScore": 0.3
            },
            "warnings": []
        },
        {
            "id": "6",
            "detectedLanguage": {
                "name": "Bengali",
                "iso6391Name": "bn",
                "confidenceScore": 1
            },
            "warnings": []
        }
    ],
    "errors": [],
    "modelVersion": "2020-09-01"
}

Sentiment Analysis

We need to append the /text/analytics/v3.0/sentiment with your Azure Cognitive Services Text Analytics API endpoint URL to work with the Sentiment Analysis functionality. If you will consider an example, the endpoint URL will be like below

https://<yoursubdomain>.cognitiveservices.azure.com/text/analytics/v3.0/sentiment
https://demotextanalytics12.cognitiveservices.azure.com/text/analytics/v3.0/sentiment

You can mention the API URL like below

urlsentiment = endpoint + "/text/analytics/v3.0/sentiment"

Now we need to create a dictionary with a documents. Each document is a tuple consisting of the id, the text.

 {
     "documents": [
         {
             "id": "1",
             "language": "en",
             "text": "I Love AzureLessons"
         },
       
         {
             "id": "3",
             "language": "zh_chs",
             "text": "我爱Azure课程"
         }


         
     ]
 }

Now our documents tuple is ready. So the next step is we need to use the Request library to send the document to API. We need to add the key value of our Azure Cognitive Services text Analytics API to the Ocp-Apim-Subscription-Key header then we need to send our request to the requests.post(). So the python code will be like below

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

Now once you will run the application, you will get the below output

{
    "documents": [
        {
            "id": "1",
            "sentiment": "positive",
            "confidenceScores": {
                "positive": 1,
                "neutral": 0,
                "negative": 0
            },
            "sentences": [
                {
                    "sentiment": "positive",
                    "confidenceScores": {
                        "positive": 1,
                        "neutral": 0,
                        "negative": 0
                    },
                    "offset": 0,
                    "length": 19,
                    "text": "I Love AzureLessons"
                }
            ],
            "warnings": []
        },
        {
            "id": "3",
            "sentiment": "positive",
            "confidenceScores": {
                "positive": 0.99,
                "neutral": 0.01,
                "negative": 0
            },
            "sentences": [
                {
                    "sentiment": "positive",
                    "confidenceScores": {
                        "positive": 0.99,
                        "neutral": 0.01,
                        "negative": 0
                    },
                    "offset": 0,
                    "length": 9,
                    "text": "我爱Azure课程"
                }
            ],
            "warnings": []
        }
    ],
    "errors": [],
    "modelVersion": "2020-04-01"
}

Key phrase extraction

Now let’s discuss the Key phrase extraction functionality. We need to append the /text/analytics/v3.0/keyphrases with your Azure Cognitive Services Text Analytics API endpoint URL to work with the Key phrase extraction functionality. If you will consider an example, the endpoint URL will be like below

https://<yoursubdomain>.cognitiveservices.azure.com/text/analytics/v3.0/keyphrases
https://demotextanalytics12.cognitiveservices.azure.com/text/analytics/v3.0/keyphrases

You can mention the API URL like below

urlkeyphrase = endpoint + "/text/analytics/v3.0/keyphrases"

Now we need to create a dictionary with a documents. Each document is a tuple consisting of the id, the text.

 {
     "documents": [
         {
             "id": "1",
             "language": "en",
             "text": "I Love AzureLessons. I will learn so many Azure topics from there."
         },
       
         {
             "id": "3",
             "language": "pl",
             "text": "Uwielbiam AzureLessons. Stamtąd nauczę się wielu tematów dotyczących Azure."
         }


         
     ]
 }

So the next step is we need to use the Request library to send the document to API. We need to add the key value of our Azure text Analytics API to the Ocp-Apim-Subscription-Key header then we need to send our request to the requests.post(). So the python code will be like below

headers = {"Ocp-Apim-Subscription-Key": key}
response = requests.post(urlkeyphrase, headers=headers, json=documents)
keyphrases = response.json()
pprint(keyphrases)

Now if you will run the application, you will get the below output

{
    "documents": [
        {
            "id": "1",
            "keyPhrases": [
                "Azure topics",
                "AzureLessons"
            ],
            "warnings": []
        },
        {
            "id": "3",
            "keyPhrases": [
                "Stamtąd",
                "wielu tematów dotyczących Azure",
                "Uwielbiam AzureLessons"
            ],
            "warnings": []
        }
    ],
    "errors": [],
    "modelVersion": "2020-07-01"
}

Link Entities Identification

We need to append the /text/analytics/v3.0/entities/recognition/general with your Azure Cognitive Services Text Analytics API endpoint URL to work with the Link Entities Identification functionality. If you will consider an example, the endpoint URL will be like below

https://<yoursubdomain>.cognitiveservices.azure.com/text/analytics/v3.0/entities/recognition/general
https://demotextanalytics12.cognitiveservices.azure.com/text/analytics/v3.0/entities/recognition/general

You can mention the API URL like below

urlEnities = endpoint + "/text/analytics/v3.0/entities/recognition/general"

Now we need to create the document consisting of the id, the text.

documents = {"documents": [
    {"id": "1", "text": "TSINFO is an It company."}
]}

Now, use the Request library to send the document to API. We need to add the key value of our Azure text Analytics API to the Ocp-Apim-Subscription-Key header then we need to send our request to the requests.post(). So the python code will be like below

headers = {"Ocp-Apim-Subscription-Key": key}
response = requests.post(urlEnities, headers=headers, json=documents)
entities = response.json()
pprint(entities)

Now, once you will run the application, you will get the output like below

{
    "documents": [
        {
            "id": "1",
            "entities": [
                {
                    "text": "TSINFO",
                    "category": "Organization",
                    "offset": 0,
                    "length": 9,
                    "confidenceScore": 0.56
                },
                {
                    "text": "IT",
                    "category": "Skill",
                    "offset": 16,
                    "length": 2,
                    "confidenceScore": 0.6
                }
            ],
            "warnings": []
        }
    ],
    "errors": [],
    "modelVersion": "2020-04-01"
}

This all about the Azure Cognitive Services Text Analytics Python SDK and the implementation.

You may also like following the below Articles

Wrapping UP

Well, in this article, we have discussed Azure Cognitive Services Text Analytics Python SDK. Hope you like this article !!!