This is similar to yesterday’s entry, but today, the request to redact and analyze for any PII is done via code. Azure Language Service returns a response with a predictable schema.
key = "settosomething"
endpoint = "https://your-foundry-resource.cognitiveservices.azure.com/"
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
# Authenticate the client using your key and endpoint
def authenticate_client():
ta_credential = AzureKeyCredential(key)
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint,
credential=ta_credential)
return text_analytics_client
client = authenticate_client()
# Example method for detecting sensitive information (PII) from text
def pii_recognition_example(client):
documents = [
"""Tailspin Toys Ltd
Invoice
14 September 1984
Customer:
Margaret Ellis
128 High Street, Reading, Berkshire RG1 2AB
Telephone: 021 685 4215
Item: ZX Spectrum 48K home computer (includes power supply, RF lead, and user manual)
Price: £79.00
Payment received: £79.00"""
]
response = client.recognize_pii_entities(documents, language="en")
result = [doc for doc in response if not doc.is_error]
for doc in result:
print("Redacted Text: {}".format(doc.redacted_text))
for entity in doc.entities:
print("Entity: {}".format(entity.text))
print(" Category: {}".format(entity.category))
print(" Confidence Score: {}".format(entity.confidence_score))
print(" Offset: {}".format(entity.offset))
print(" Length: {}".format(entity.length))
pii_recognition_example(client)
.venv/bin/python azlang.py
Redacted Text: *****************
Invoice
*****************
********:
**************
*******************************************
Telephone: ************
Item: ZX Spectrum 48K home computer (includes power supply, RF lead, and user manual)
Price: £79.00
Payment received: £79.00
Entity: Tailspin Toys Ltd
Category: Organization
Confidence Score: 1.0
Offset: 0
Length: 17
Entity: 14 September 1984
Category: DateTime
Confidence Score: 1.0
Offset: 26
Length: 17
Entity: Customer
Category: PersonType
Confidence Score: 0.99
Offset: 45
Length: 8
Entity: Margaret Ellis
Category: Person
Confidence Score: 1.0
Offset: 56
Length: 14
Entity: 128 High Street, Reading, Berkshire RG1 2AB
Category: Address
Confidence Score: 1.0
Offset: 72
Length: 43
Entity: 021 685 4215
Category: PhoneNumber
Confidence Score: 1.0
Offset: 128
Length: 12