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.
Before starting, ensure you have the following:
pip install python-dotenv
Create a Text Analytics Resource:
Get the Endpoint and Key:
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()
The required libraries are imported to handle environment variables, HTTP connections, and JSON.
The .env
file is loaded to access the endpoint and key securely:
load_dotenv()
ai_endpoint = os.getenv('AI_SERVICE_ENDPOINT')
ai_key = os.getenv('AI_SERVICE_KEY')
The main
function is simplified to handle a single user input and call the GetLanguage
function.
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)
This function constructs the JSON request body, makes an HTTP request to the Azure service, and processes the response.
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)
The code ensures it runs when executed directly.
if __name__ == "__main__":
main()
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.