Using Azure Language Detection Service with an Azure Subscription

Azure Python Language Detection
9e1256167ae3.png

Introduction

 

 

Azure's Text Analytics API, part of the Azure Cognitive Services suite, offers powerful language detection capabilities. This publication provides a step-by-step guide on how to use the Azure Language Detection service in a Python application. We'll cover setting up an Azure subscription, obtaining the necessary API keys, and integrating the language detection feature into your Python code.

 

 

Prerequisites

Before starting, ensure you have the following:

  1. Azure Subscription: If you don't have an Azure subscription, you can create a free account at Azure Free Account.
  2. Python Environment: Ensure you have Python installed. You can download it from Python.org.
  3. Required Libraries: Install the necessary Python libraries using pip

pip install python-dotenv

 

 

Setting Up the Azure Text Analytics Service

  1. Create a Text Analytics Resource:

    • Go to the Azure Portal.
    • Click on "Create a resource" and search for "Language service".

 

 

      • Click "Create", then configure the necessary settings (Resource Group, Region, Pricing tier, etc.), and click "Review + Create".
    •  
    •  
    •  
    •  
    •  
      •  
  1. Get the Endpoint and Key:

    • Once the resource is created, navigate to it.
    • Click on "Keys and Endpoint" under the Resource Management section.
    • Copy the Endpoint and one of the Keys. These will be used in your Python application.

 

 

 

    • The following endpoint is only for this tutorial purpose, it is not longer available.

 

 

 

 

Python Code for Language Detection

 

 

Here's a Python code for detecting the language of a given text using the Azure Text Analytics service. The code uses environment variables to manage the API key and endpoint securely.

 

 

 

from dotenv import load_dotenv
import os
import http.client, json

def main():
    global ai_endpoint
    global ai_key
    try:
        # Load environment variables
        load_dotenv()
        ai_endpoint = os.getenv('AI_SERVICE_ENDPOINT')
        ai_key = os.getenv('AI_SERVICE_KEY')

        # Get a single user input
        userText = input('Enter some text:\n')
        if userText:
            GetLanguage(userText)
    except Exception as ex:
        print(ex)

def GetLanguage(text):
    try:
        # Construct the JSON request body
        jsonBody = {
            "documents": [
                {"id": 1, "text": text}
            ]
        }
        print(json.dumps(jsonBody, indent=2))

        # Make an HTTP request to the REST interface
        uri = ai_endpoint.rstrip('/').replace('https://', '')
        conn = http.client.HTTPSConnection(uri)
        headers = {
            'Content-Type': 'application/json',
            'Ocp-Apim-Subscription-Key': ai_key
        }
        conn.request("POST", "/text/analytics/v3.1/languages?", json.dumps(jsonBody).encode('utf-8'), headers)
        response = conn.getresponse()
        data = response.read().decode("UTF-8")

        # If the call was successful, get the response
        if response.status == 200:
            results = json.loads(data)
            print(json.dumps(results, indent=2))
            for document in results["documents"]:
                print("\nLanguage:", document["detectedLanguage"]["name"])
        else:
            print(data)
        conn.close()
    except Exception as ex:
        print(ex)

if __name__ == "__main__":
    main()

 

 

 

Explanation

 

1. Import Necessary Libraries:

 

The required libraries are imported to handle environment variables, HTTP connections, and JSON.

 

2. Load Environment Variables:

 

The .env file is loaded to access the endpoint and key securely:

python
 
 
load_dotenv() ai_endpoint = os.getenv('AI_SERVICE_ENDPOINT') ai_key = os.getenv('AI_SERVICE_KEY')
 
 

3. Main Function:

 

The main function is simplified to handle a single user input and call the GetLanguage function.

 

python
 
 

def main():
    global ai_endpoint
    global ai_key
    try:
        load_dotenv()
        ai_endpoint = os.getenv('AI_SERVICE_ENDPOINT')
        ai_key = os.getenv('AI_SERVICE_KEY')

        # Get a single user input
        userText = input('Enter some text:\n')
        if userText:
            GetLanguage(userText)
    except Exception as ex:
        print(ex)

 

 

4. GetLanguage Function:

 

This function constructs the JSON request body, makes an HTTP request to the Azure service, and processes the response.

python
 
 

def GetLanguage(text):
    try:
        # Construct the JSON request body
        jsonBody = {
            "documents": [
                {"id": 1, "text": text}
            ]
        }
        print(json.dumps(jsonBody, indent=2))

        # Make an HTTP request to the REST interface
        uri = ai_endpoint.rstrip('/').replace('https://', '')
        conn = http.client.HTTPSConnection(uri)
        headers = {
            'Content-Type': 'application/json',
            'Ocp-Apim-Subscription-Key': ai_key
        }
        conn.request("POST", "/text/analytics/v3.1/languages?", json.dumps(jsonBody).encode('utf-8'), headers)
        response = conn.getresponse()
        data = response.read().decode("UTF-8")

        # If the call was successful, get the response
        if response.status == 200:
            results = json.loads(data)
            print(json.dumps(results, indent=2))
            for document in results["documents"]:
                print("\nLanguage:", document["detectedLanguage"]["name"])
        else:
            print(data)
        conn.close()
    except Exception as ex:
        print(ex)

 

 

5. Run the Application:

 

The code ensures it runs when executed directly.

python
 
 
if __name__ == "__main__":
    main()
 
 
 

Conclusion

By following this guide, you can integrate Azure's Language Detection service into your Python applications. This enables your application to automatically detect the language of user-provided text, enhancing its functionality and user experience. The key steps include setting up the Azure Text Analytics service, securely handling the API keys, and making HTTP requests to the Azure endpoint.

 

back to list