
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
azure-ai-textanalytics
Advanced tools
The Azure Cognitive Service for Language is a cloud-based service that provides Natural Language Processing (NLP) features for understanding and analyzing text, and includes the following main features:
Source code | Package (PyPI) | Package (Conda) | API reference documentation | Product documentation | Samples
The Language service supports both multi-service and single-service access. Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Language service access only, create a Language service resource. You can create the resource using the Azure Portal or Azure CLI following the steps in this document.
Interaction with the service using the client library begins with a client.
To create a client object, you will need the Cognitive Services or Language service endpoint to
your resource and a credential that allows you access:
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
credential = AzureKeyCredential("<api_key>")
text_analytics_client = TextAnalyticsClient(endpoint="https://<resource-name>.cognitiveservices.azure.com/", credential=credential)
Note that for some Cognitive Services resources the endpoint might look different from the above code snippet.
For example, https://<region>.api.cognitive.microsoft.com/.
Install the Azure Text Analytics client library for Python with pip:
pip install azure-ai-textanalytics
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key))
Note that
5.2.Xand newer targets the Azure Cognitive Service for Language APIs. These APIs include the text analysis and natural language processing features found in the previous versions of the Text Analytics client library. In addition, the service API has changed from semantic to date-based versioning. This version of the client library defaults to the latest supported API version, which currently is2023-04-01.
This table shows the relationship between SDK versions and supported API versions of the service
| SDK version | Supported API version of service |
|---|---|
| 5.3.X - Latest stable release | 3.0, 3.1, 2022-05-01, 2023-04-01 (default) |
| 5.2.X | 3.0, 3.1, 2022-05-01 (default) |
| 5.1.0 | 3.0, 3.1 (default) |
| 5.0.0 | 3.0 |
API version can be selected by passing the api_version keyword argument into the client. For the latest Language service features, consider selecting the most recent beta API version. For production scenarios, the latest stable version is recommended. Setting to an older version may result in reduced feature compatibility.
You can find the endpoint for your Language service resource using the Azure Portal or Azure CLI:
# Get the endpoint for the Language service resource
az cognitiveservices account show --name "resource-name" --resource-group "resource-group-name" --query "properties.endpoint"
You can get the API key from the Cognitive Services or Language service resource in the Azure Portal. Alternatively, you can use Azure CLI snippet below to get the API key of your resource.
az cognitiveservices account keys list --name "resource-name" --resource-group "resource-group-name"
Once you have the value for the API key, you can pass it as a string into an instance of AzureKeyCredential. Use the key as the credential parameter to authenticate the client:
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key))
To use an Azure Active Directory (AAD) token credential, provide an instance of the desired credential type obtained from the azure-identity library. Note that regional endpoints do not support AAD authentication. Create a custom subdomain name for your resource in order to use this type of authentication.
Authentication with AAD requires some initial setup:
"Cognitive Services Language Reader" role to your service principal.After setup, you can choose which type of credential from azure.identity to use. As an example, DefaultAzureCredential can be used to authenticate the client:
Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
Use the returned token credential to authenticate the client:
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import DefaultAzureCredential
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
credential = DefaultAzureCredential()
text_analytics_client = TextAnalyticsClient(endpoint, credential=credential)
The Text Analytics client library provides a TextAnalyticsClient to do analysis on batches of documents. It provides both synchronous and asynchronous operations to access a specific use of text analysis, such as language detection or key phrase extraction.
A document is a single unit to be analyzed by the predictive models in the Language service. The input for each operation is passed as a list of documents.
Each document can be passed as a string in the list, e.g.
documents = ["I hated the movie. It was so slow!", "The movie made it into my top ten favorites. What a great movie!"]
or, if you wish to pass in a per-item document id or language/country_hint, they can be passed as a list of
DetectLanguageInput or
TextDocumentInput
or a dict-like representation of the object:
documents = [
{"id": "1", "language": "en", "text": "I hated the movie. It was so slow!"},
{"id": "2", "language": "en", "text": "The movie made it into my top ten favorites. What a great movie!"},
]
See service limitations for the input, including document length limits, maximum batch size, and supported text encoding.
The return value for a single document can be a result or error object. A heterogeneous list containing a collection of result and error objects is returned from each operation. These results/errors are index-matched with the order of the provided documents.
A result, such as AnalyzeSentimentResult, is the result of a text analysis operation and contains a prediction or predictions about a document input.
The error object, DocumentError, indicates that the service had trouble processing the document and contains the reason it was unsuccessful.
You can filter for a result or error object in the list by using the is_error attribute. For a result object this is always False and for a
DocumentError this is True.
For example, to filter out all DocumentErrors you might use list comprehension:
response = text_analytics_client.analyze_sentiment(documents)
successful_responses = [doc for doc in response if not doc.is_error]
You can also use the kind attribute to filter between result types:
poller = text_analytics_client.begin_analyze_actions(documents, actions)
response = poller.result()
for result in response:
if result.kind == "SentimentAnalysis":
print(f"Sentiment is {result.sentiment}")
elif result.kind == "KeyPhraseExtraction":
print(f"Key phrases: {result.key_phrases}")
elif result.is_error is True:
print(f"Document error: {result.code}, {result.message}")
Long-running operations are operations which consist of an initial request sent to the service to start an operation, followed by polling the service at intervals to determine whether the operation has completed or failed, and if it has succeeded, to get the result.
Methods that support healthcare analysis, custom text analysis, or multiple analyses are modeled as long-running operations.
The client exposes a begin_<method-name> method that returns a poller object. Callers should wait
for the operation to complete by calling result() on the poller object returned from the begin_<method-name> method.
Sample code snippets are provided to illustrate using long-running operations below.
The following section provides several code snippets covering some of the most common Language service tasks, including:
analyze_sentiment looks at its input text and determines whether its sentiment is positive, negative, neutral or mixed. It's response includes per-sentence sentiment analysis and confidence scores.
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
documents = [
"""I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more.
I developed a deep-connection with my instructor as well, and I feel as if I've made a life-long friend in her.""",
"""This was a waste of my time. All of the views on this drop are extremely boring, all I saw was grass. 0/10 would
not recommend to any divers, even first timers.""",
"""This was pretty good! The sights were ok, and I had fun with my instructors! Can't complain too much about my experience""",
"""I only have one word for my experience: WOW!!! I can't believe I have had such a wonderful skydiving company right
in my backyard this whole time! I will definitely be a repeat customer, and I want to take my grandmother skydiving too,
I know she'll love it!"""
]
result = text_analytics_client.analyze_sentiment(documents, show_opinion_mining=True)
docs = [doc for doc in result if not doc.is_error]
print("Let's visualize the sentiment of each of these documents")
for idx, doc in enumerate(docs):
print(f"Document text: {documents[idx]}")
print(f"Overall sentiment: {doc.sentiment}")
The returned response is a heterogeneous list of result and error objects: list[AnalyzeSentimentResult, DocumentError]
Please refer to the service documentation for a conceptual discussion of sentiment analysis. To see how to conduct more granular analysis into the opinions related to individual aspects (such as attributes of a product or service) in a text, see here.
recognize_entities recognizes and categories entities in its input text as people, places, organizations, date/time, quantities, percentages, currencies, and more.
import os
import typing
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
reviews = [
"""I work for Foo Company, and we hired Contoso for our annual founding ceremony. The food
was amazing and we all can't say enough good words about the quality and the level of service.""",
"""We at the Foo Company re-hired Contoso after all of our past successes with the company.
Though the food was still great, I feel there has been a quality drop since their last time
catering for us. Is anyone else running into the same problem?""",
"""Bar Company is over the moon about the service we received from Contoso, the best sliders ever!!!!"""
]
result = text_analytics_client.recognize_entities(reviews)
result = [review for review in result if not review.is_error]
organization_to_reviews: typing.Dict[str, typing.List[str]] = {}
for idx, review in enumerate(result):
for entity in review.entities:
print(f"Entity '{entity.text}' has category '{entity.category}'")
if entity.category == 'Organization':
organization_to_reviews.setdefault(entity.text, [])
organization_to_reviews[entity.text].append(reviews[idx])
for organization, reviews in organization_to_reviews.items():
print(
"\n\nOrganization '{}' has left us the following review(s): {}".format(
organization, "\n\n".join(reviews)
)
)
The returned response is a heterogeneous list of result and error objects: list[RecognizeEntitiesResult, DocumentError]
Please refer to the service documentation for a conceptual discussion of named entity recognition and supported types.
recognize_linked_entities recognizes and disambiguates the identity of each entity found in its input text (for example, determining whether an occurrence of the word Mars refers to the planet, or to the Roman god of war). Recognized entities are associated with URLs to a well-known knowledge base, like Wikipedia.
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
documents = [
"""
Microsoft was founded by Bill Gates with some friends he met at Harvard. One of his friends,
Steve Ballmer, eventually became CEO after Bill Gates as well. Steve Ballmer eventually stepped
down as CEO of Microsoft, and was succeeded by Satya Nadella.
Microsoft originally moved its headquarters to Bellevue, Washington in January 1979, but is now
headquartered in Redmond.
"""
]
result = text_analytics_client.recognize_linked_entities(documents)
docs = [doc for doc in result if not doc.is_error]
print(
"Let's map each entity to it's Wikipedia article. I also want to see how many times each "
"entity is mentioned in a document\n\n"
)
entity_to_url = {}
for doc in docs:
for entity in doc.entities:
print("Entity '{}' has been mentioned '{}' time(s)".format(
entity.name, len(entity.matches)
))
if entity.data_source == "Wikipedia":
entity_to_url[entity.name] = entity.url
The returned response is a heterogeneous list of result and error objects: list[RecognizeLinkedEntitiesResult, DocumentError]
Please refer to the service documentation for a conceptual discussion of entity linking and supported types.
recognize_pii_entities recognizes and categorizes Personally Identifiable Information (PII) entities in its input text, such as Social Security Numbers, bank account information, credit card numbers, and more.
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
documents = [
"""Parker Doe has repaid all of their loans as of 2020-04-25.
Their SSN is 859-98-0987. To contact them, use their phone number
555-555-5555. They are originally from Brazil and have Brazilian CPF number 998.214.865-68"""
]
result = text_analytics_client.recognize_pii_entities(documents)
docs = [doc for doc in result if not doc.is_error]
print(
"Let's compare the original document with the documents after redaction. "
"I also want to comb through all of the entities that got redacted"
)
for idx, doc in enumerate(docs):
print(f"Document text: {documents[idx]}")
print(f"Redacted document text: {doc.redacted_text}")
for entity in doc.entities:
print("...Entity '{}' with category '{}' got redacted".format(
entity.text, entity.category
))
The returned response is a heterogeneous list of result and error objects: list[RecognizePiiEntitiesResult, DocumentError]
Please refer to the service documentation for supported PII entity types.
Note: The Recognize PII Entities service is available in API version v3.1 and newer.
extract_key_phrases determines the main talking points in its input text. For example, for the input text "The food was delicious and there were wonderful staff", the API returns: "food" and "wonderful staff".
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
articles = [
"""
Washington, D.C. Autumn in DC is a uniquely beautiful season. The leaves fall from the trees
in a city chock-full of forests, leaving yellow leaves on the ground and a clearer view of the
blue sky above...
""",
"""
Redmond, WA. In the past few days, Microsoft has decided to further postpone the start date of
its United States workers, due to the pandemic that rages with no end in sight...
""",
"""
Redmond, WA. Employees at Microsoft can be excited about the new coffee shop that will open on campus
once workers no longer have to work remotely...
"""
]
result = text_analytics_client.extract_key_phrases(articles)
for idx, doc in enumerate(result):
if not doc.is_error:
print("Key phrases in article #{}: {}".format(
idx + 1,
", ".join(doc.key_phrases)
))
The returned response is a heterogeneous list of result and error objects: list[ExtractKeyPhrasesResult, DocumentError]
Please refer to the service documentation for a conceptual discussion of key phrase extraction.
detect_language determines the language of its input text, including the confidence score of the predicted language.
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
documents = [
"""
The concierge Paulette was extremely helpful. Sadly when we arrived the elevator was broken, but with Paulette's help we barely noticed this inconvenience.
She arranged for our baggage to be brought up to our room with no extra charge and gave us a free meal to refurbish all of the calories we lost from
walking up the stairs :). Can't say enough good things about my experience!
""",
"""
最近由于工作压力太大,我们决定去富酒店度假。那儿的温泉实在太舒服了,我跟我丈夫都完全恢复了工作前的青春精神!加油!
"""
]
result = text_analytics_client.detect_language(documents)
reviewed_docs = [doc for doc in result if not doc.is_error]
print("Let's see what language each review is in!")
for idx, doc in enumerate(reviewed_docs):
print("Review #{} is in '{}', which has ISO639-1 name '{}'\n".format(
idx, doc.primary_language.name, doc.primary_language.iso6391_name
))
The returned response is a heterogeneous list of result and error objects: list[DetectLanguageResult, DocumentError]
Please refer to the service documentation for a conceptual discussion of language detection and language and regional support.
Long-running operation begin_analyze_healthcare_entities extracts entities recognized within the healthcare domain, and identifies relationships between entities within the input document and links to known sources of information in various well known databases, such as UMLS, CHV, MSH, etc.
import os
import typing
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient, HealthcareEntityRelation
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint,
credential=AzureKeyCredential(key),
)
documents = [
"""
Patient needs to take 100 mg of ibuprofen, and 3 mg of potassium. Also needs to take
10 mg of Zocor.
""",
"""
Patient needs to take 50 mg of ibuprofen, and 2 mg of Coumadin.
"""
]
poller = text_analytics_client.begin_analyze_healthcare_entities(documents)
result = poller.result()
docs = [doc for doc in result if not doc.is_error]
print("Let's first visualize the outputted healthcare result:")
for doc in docs:
for entity in doc.entities:
print(f"Entity: {entity.text}")
print(f"...Normalized Text: {entity.normalized_text}")
print(f"...Category: {entity.category}")
print(f"...Subcategory: {entity.subcategory}")
print(f"...Offset: {entity.offset}")
print(f"...Confidence score: {entity.confidence_score}")
if entity.data_sources is not None:
print("...Data Sources:")
for data_source in entity.data_sources:
print(f"......Entity ID: {data_source.entity_id}")
print(f"......Name: {data_source.name}")
if entity.assertion is not None:
print("...Assertion:")
print(f"......Conditionality: {entity.assertion.conditionality}")
print(f"......Certainty: {entity.assertion.certainty}")
print(f"......Association: {entity.assertion.association}")
for relation in doc.entity_relations:
print(f"Relation of type: {relation.relation_type} has the following roles")
for role in relation.roles:
print(f"...Role '{role.name}' with entity '{role.entity.text}'")
print("------------------------------------------")
print("Now, let's get all of medication dosage relations from the documents")
dosage_of_medication_relations = [
entity_relation
for doc in docs
for entity_relation in doc.entity_relations if entity_relation.relation_type == HealthcareEntityRelation.DOSAGE_OF_MEDICATION
]
Note: Healthcare Entities Analysis is only available with API version v3.1 and newer.
Long-running operation begin_analyze_actions performs multiple analyses over one set of documents in a single request. Currently it is supported using any combination of the following Language APIs in a single request:
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import (
TextAnalyticsClient,
RecognizeEntitiesAction,
RecognizeLinkedEntitiesAction,
RecognizePiiEntitiesAction,
ExtractKeyPhrasesAction,
AnalyzeSentimentAction,
)
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint,
credential=AzureKeyCredential(key),
)
documents = [
'We went to Contoso Steakhouse located at midtown NYC last week for a dinner party, and we adore the spot! '
'They provide marvelous food and they have a great menu. The chief cook happens to be the owner (I think his name is John Doe) '
'and he is super nice, coming out of the kitchen and greeted us all.'
,
'We enjoyed very much dining in the place! '
'The Sirloin steak I ordered was tender and juicy, and the place was impeccably clean. You can even pre-order from their '
'online menu at www.contososteakhouse.com, call 312-555-0176 or send email to order@contososteakhouse.com! '
'The only complaint I have is the food didn\'t come fast enough. Overall I highly recommend it!'
]
poller = text_analytics_client.begin_analyze_actions(
documents,
display_name="Sample Text Analysis",
actions=[
RecognizeEntitiesAction(),
RecognizePiiEntitiesAction(),
ExtractKeyPhrasesAction(),
RecognizeLinkedEntitiesAction(),
AnalyzeSentimentAction(),
],
)
document_results = poller.result()
for doc, action_results in zip(documents, document_results):
print(f"\nDocument text: {doc}")
for result in action_results:
if result.kind == "EntityRecognition":
print("...Results of Recognize Entities Action:")
for entity in result.entities:
print(f"......Entity: {entity.text}")
print(f".........Category: {entity.category}")
print(f".........Confidence Score: {entity.confidence_score}")
print(f".........Offset: {entity.offset}")
elif result.kind == "PiiEntityRecognition":
print("...Results of Recognize PII Entities action:")
for pii_entity in result.entities:
print(f"......Entity: {pii_entity.text}")
print(f".........Category: {pii_entity.category}")
print(f".........Confidence Score: {pii_entity.confidence_score}")
elif result.kind == "KeyPhraseExtraction":
print("...Results of Extract Key Phrases action:")
print(f"......Key Phrases: {result.key_phrases}")
elif result.kind == "EntityLinking":
print("...Results of Recognize Linked Entities action:")
for linked_entity in result.entities:
print(f"......Entity name: {linked_entity.name}")
print(f".........Data source: {linked_entity.data_source}")
print(f".........Data source language: {linked_entity.language}")
print(
f".........Data source entity ID: {linked_entity.data_source_entity_id}"
)
print(f".........Data source URL: {linked_entity.url}")
print(".........Document matches:")
for match in linked_entity.matches:
print(f"............Match text: {match.text}")
print(f"............Confidence Score: {match.confidence_score}")
print(f"............Offset: {match.offset}")
print(f"............Length: {match.length}")
elif result.kind == "SentimentAnalysis":
print("...Results of Analyze Sentiment action:")
print(f"......Overall sentiment: {result.sentiment}")
print(
f"......Scores: positive={result.confidence_scores.positive}; \
neutral={result.confidence_scores.neutral}; \
negative={result.confidence_scores.negative} \n"
)
elif result.is_error is True:
print(
f"...Is an error with code '{result.error.code}' and message '{result.error.message}'"
)
print("------------------------------------------")
The returned response is an object encapsulating multiple iterables, each representing results of individual analyses.
Note: Multiple analysis is available in API version v3.1 and newer.
Optional keyword arguments can be passed in at the client and per-operation level. The azure-core reference documentation describes available configurations for retries, logging, transport protocols, and more.
The Text Analytics client will raise exceptions defined in Azure Core.
This library uses the standard logging library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.
Detailed DEBUG level logging, including request/response bodies and unredacted
headers, can be enabled on a client with the logging_enable keyword argument:
import sys
import logging
from azure.identity import DefaultAzureCredential
from azure.ai.textanalytics import TextAnalyticsClient
# Create a logger for the 'azure' SDK
logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)
# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
endpoint = "https://<resource-name>.cognitiveservices.azure.com/"
credential = DefaultAzureCredential()
# This client will log detailed information about its HTTP sessions, at DEBUG level
text_analytics_client = TextAnalyticsClient(endpoint, credential, logging_enable=True)
result = text_analytics_client.analyze_sentiment(["I did not like the restaurant. The food was too spicy."])
Similarly, logging_enable can enable detailed logging for a single operation,
even when it isn't enabled for the client:
result = text_analytics_client.analyze_sentiment(documents, logging_enable=True)
These code samples show common scenario operations with the Azure Text Analytics client library.
Authenticate the client with a Cognitive Services/Language service API key or a token credential from azure-identity:
Common scenarios
Advanced scenarios
For more extensive documentation on Azure Cognitive Service for Language, see the Language Service documentation on docs.microsoft.com.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
This version of the client library defaults to the service API version 2023-04-01.
Note: The following changes are only breaking from the previous beta. They are not breaking against previous stable versions.
ExtractSummaryAction to ExtractiveSummaryAction.ExtractSummaryResult to ExtractiveSummaryResult.begin_abstractive_summary to begin_abstract_summary.dynamic_classification client method and related types: DynamicClassificationResult and ClassificationType.fhir_version and document_type from begin_analyze_healthcare_entities and AnalyzeHealthcareEntitiesAction.fhir_bundle from AnalyzeHealthcareEntitiesResult.HealthcareDocumentType.resolutions from CategorizedEntity.ResolutionKind, AgeResolution, AreaResolution,
CurrencyResolution, DateTimeResolution, InformationResolution, LengthResolution,
NumberResolution, NumericRangeResolution, OrdinalResolution, SpeedResolution, TemperatureResolution,
TemporalSpanResolution, VolumeResolution, WeightResolution, AgeUnit, AreaUnit, TemporalModifier,
InformationUnit, LengthUnit, NumberKind, RangeKind, RelativeTo, SpeedUnit, TemperatureUnit,
VolumeUnit, DateTimeSubKind, and WeightUnit.detected_language from RecognizeEntitiesResult, RecognizePiiEntitiesResult, AnalyzeHealthcareEntitiesResult,
ExtractKeyPhrasesResult, RecognizeLinkedEntitiesResult, AnalyzeSentimentResult, RecognizeCustomEntitiesResult,
ClassifyDocumentResult, ExtractSummaryResult, and AbstractSummaryResult.script from DetectedLanguage.HealthcareEntityCategory and HealthcareEntityRelation.This version of the client library defaults to the service API version 2022-10-01-preview.
begin_extract_summary client method to perform extractive summarization on documents.begin_abstractive_summary client method to perform abstractive summarization on documents.BaseResolution and BooleanResolution.BooleanResolution from ResolutionKind.AbstractSummaryAction to AbstractiveSummaryAction.AbstractSummaryResult to AbstractiveSummaryResult.autodetect_default_language from long-running operation APIs.This version of the client library defaults to the service API version 2022-10-01-preview.
ExtractSummaryAction, ExtractSummaryResult, and SummarySentence.
Access the feature through the begin_analyze_actions API.fhir_version and document_type to begin_analyze_healthcare_entities and AnalyzeHealthcareEntitiesAction.fhir_bundle to AnalyzeHealthcareEntitiesResult.confidence_score to HealthcareRelation.HealthcareDocumentType.resolutions to CategorizedEntity.BaseResolution, ResolutionKind, AgeResolution, AreaResolution,
BooleanResolution, CurrencyResolution, DateTimeResolution, InformationResolution, LengthResolution,
NumberResolution, NumericRangeResolution, OrdinalResolution, SpeedResolution, TemperatureResolution,
TemporalSpanResolution, VolumeResolution, WeightResolution, AgeUnit, AreaUnit, TemporalModifier,
InformationUnit, LengthUnit, NumberKind, RangeKind, RelativeTo, SpeedUnit, TemperatureUnit,
VolumeUnit, DateTimeSubKind, and WeightUnit.AbstractSummaryAction, AbstractSummaryResult, AbstractiveSummary,
and SummaryContext. Access the feature through the begin_analyze_actions API.auto into the document language hint to use this feature.autodetect_default_language to long-running operation APIs. Pass as the default/fallback language for automatic language detection.detected_language to RecognizeEntitiesResult, RecognizePiiEntitiesResult, AnalyzeHealthcareEntitiesResult,
ExtractKeyPhrasesResult, RecognizeLinkedEntitiesResult, AnalyzeSentimentResult, RecognizeCustomEntitiesResult,
ClassifyDocumentResult, ExtractSummaryResult, and AbstractSummaryResult to indicate the language detected by automatic language detection.script to DetectedLanguage to indicate the script of the input document.dynamic_classification client method to perform dynamic classification on documents without needing to train a model.msrest.begin_analyze_actions API.This version of the client library marks a stable release and defaults to the service API version 2022-05-01.
Includes all changes from 5.2.0b1 to 5.2.0b5.
The version of this client library defaults to the API version 2022-05-01.
begin_recognize_custom_entities client method to recognize custom named entities in documents.begin_single_label_classify client method to perform custom single label classification on documents.begin_multi_label_classify client method to perform custom multi label classification on documents.details on returned poller objects which contain long-running operation metadata.TextAnalysisLROPoller and AsyncTextAnalysisLROPoller protocols to describe the return types from long-running operations.cancel method on the poller objects. Call it to cancel a long-running operation that's in progress.kind to RecognizeEntitiesResult, RecognizePiiEntitiesResult, AnalyzeHealthcareEntitiesResult,
DetectLanguageResult, ExtractKeyPhrasesResult, RecognizeLinkedEntitiesResult, AnalyzeSentimentResult,
RecognizeCustomEntitiesResult, ClassifyDocumentResult, and DocumentError.TextAnalysisKind.ExtractSummaryAction, ExtractSummaryResult, and SummarySentence. To access this beta feature, install the 5.2.0b4 version of the client library.FHIR feature and related keyword argument and property: fhir_version and fhir_bundle. To access this beta feature, install the 5.2.0b4 version of the client library.SingleCategoryClassifyResult and MultiCategoryClassifyResult models have been merged into one model: ClassifyDocumentResult.SingleCategoryClassifyAction to SingleLabelClassifyActionMultiCategoryClassifyAction to MultiLabelClassifyAction.HttpResponseError will be immediately raised when the call quota volume is exceeded in a F0 tier Language resource.Note that this is the first version of the client library that targets the Azure Cognitive Service for Language APIs which includes the existing text analysis and natural language processing features found in the Text Analytics client library.
In addition, the service API has changed from semantic to date-based versioning. This version of the client library defaults to the latest supported API version, which currently is 2022-04-01-preview. Support for v3.2-preview.2 is removed, however, all functionalities are included in the latest version.
begin_analyze_actions API with the AnalyzeHealthcareEntitiesAction type.fhir_version to begin_analyze_healthcare_entities and AnalyzeHealthcareEntitiesAction. Use the keyword to indicate the version for the fhir_bundle contained on the AnalyzeHealthcareEntitiesResult.fhir_bundle to AnalyzeHealthcareEntitiesResult.display_name to begin_analyze_healthcare_entities.string_index_type now correctly defaults to the Python default UnicodeCodePoint for AnalyzeSentimentAction and RecognizeCustomEntitiesAction.begin_analyze_actions where incorrect action types were being sent in the request if targeting the older API version v3.1 in the beta version of the client library.string_index_type option Utf16CodePoint is corrected to Utf16CodeUnit.This version of the SDK defaults to the latest supported API version, which currently is v3.2-preview.2.
begin_analyze_actions API with the RecognizeCustomEntitiesAction and RecognizeCustomEntitiesResult types.begin_analyze_actions API with the SingleCategoryClassifyAction and SingleCategoryClassifyActionResult types.begin_analyze_actions API with the MultiCategoryClassifyAction and MultiCategoryClassifyActionResult types.begin_analyze_actions.begin_analyze_actions and begin_recognize_healthcare_entities methods.begin_analyze_actions.This version of the SDK defaults to the latest supported API version, which currently is v3.2-preview.1.
ExtractSummaryAction type.RecognizePiiEntitiesAction option disable_service_logs now correctly defaults to True.This version of the SDK defaults to the latest supported API version, which currently is v3.1.
Includes all changes from 5.1.0b1 to 5.1.0b7.
Note: this version will be the last to officially support Python 3.5, future versions will require Python 2.7 or Python 3.6+.
catagories_filter to RecognizePiiEntitiesActionHealthcareEntityCategorybegin_analyze_healthcare_entities methods.being_analyze_actions. Now, we return a list of results, where each result is a list of the action results for the document, in the order the documents and actions were passed.begin_analyze_actions now accepts a single action per type. A ValueError is raised if duplicate actions are passed.AnalyzeActionsTypeAnalyzeActionsResultAnalyzeActionsErrorHealthcareEntityRelationRoleTypeHealthcareEntityRelationType to HealthcareEntityRelationPiiEntityCategoryType to PiiEntityCategoryPiiEntityDomainType to PiiEntityDomainBreaking Changes
begin_analyze_batch_actions to begin_analyze_actions.AnalyzeBatchActionsType to AnalyzeActionsType.AnalyzeBatchActionsResult to AnalyzeActionsResult.AnalyzeBatchActionsError to AnalyzeActionsError.AnalyzeHealthcareEntitiesResultItem to AnalyzeHealthcareEntitiesResult.AnalyzeHealthcareEntitiesResult's statistics to be the correct type, TextDocumentStatisticsRequestStatistics, use TextDocumentBatchStatistics insteadNew Features
EntityConditionality, EntityCertainty, and EntityAssociation.AnalyzeSentimentAction as a supported action type for begin_analyze_batch_actions.disable_service_logs. If set to true, you opt-out of having your text input logged on the service side for troubleshooting.Breaking Changes
v3.1-preview.4 endpoint through enum value TextAnalyticsApiVersion.V3_1_PREVIEWrelated_entities on HealthcareEntity and added entity_relations onto the document response level for healthcareaspect and opinions to target and assessments respectively in class MinedOpinion.AspectSentiment and OpinionSentiment to TargetSentiment and AssessmentSentiment respectively.New Features
RecognizeLinkedEntitiesAction as a supported action type for begin_analyze_batch_actions.categories_filter to the recognize_pii_entities client method.PiiEntityCategoryType.normalized_text to HealthcareEntity. This property is a normalized version of the text property that already
exists on the HealthcareEntityassertion onto HealthcareEntity. This contains assertions about the entity itself, i.e. if the entity represents a diagnosis,
is this diagnosis conditional on a symptom?Known Issues
begin_analyze_healthcare_entities is currently in gated preview and can not be used with AAD credentials. For more information, see the Text Analytics for Health documentation.model_version to begin_analyze_healthcare_entities, it only uses the latest model.Breaking Changes
begin_analyze to begin_analyze_batch_actions.begin_analyze_batch_actions, we accept one parameter actions,
which is a list of actions you would like performed. The results of the actions are returned in the same order as when inputted.begin_analyze_batch_actions has also changed. Now, after the completion of your long running operation, we return a paged iterable
of action results, in the same order they've been inputted. The actual document results for each action are included under property document_results of
each action result.New Features
begin_analyze_healthcare to begin_analyze_healthcare_entities.AnalyzeHealthcareResult to AnalyzeHealthcareEntitiesResult and AnalyzeHealthcareResultItem to AnalyzeHealthcareEntitiesResultItem.HealthcareEntityLink to HealthcareEntityDataSource and renamed its properties id to entity_id and data_source to name.relations from AnalyzeHealthcareEntitiesResultItem and added related_entities to HealthcareEntity.begin_analyze_healthcare_entities.begin_analyze_healthcare_entities.api_version=TextAnalyticsApiVersion.V3_1_PREVIEW_3 when calling begin_analyze and begin_analyze_healthcare_entities. begin_analyze_healthcare_entities is still in gated preview though.string_index_type to the service client methods begin_analyze_healthcare_entities, analyze_sentiment, recognize_entities, recognize_pii_entities, and recognize_linked_entities which tells the service how to interpret string offsets.length to CategorizedEntity, SentenceSentiment, LinkedEntityMatch, AspectSentiment, OpinionSentiment, PiiEntity and
HealthcareEntity.Bug Fixes
New Features
begin_analyze, which supports long-running batch process of Named Entity Recognition, Personally identifiable Information, and Key Phrase Extraction. To use, you must specify api_version=TextAnalyticsApiVersion.V3_1_PREVIEW_3 when creating your client.begin_analyze_healthcare, which supports the service's Health API. Since the Health API is currently only available in a gated preview, you need to have your subscription on the service's allow list, and you must specify api_version=TextAnalyticsApiVersion.V3_1_PREVIEW_3 when creating your client. Note that since this is a gated preview, AAD is not supported. More information here.Breaking changes
length from CategorizedEntity, SentenceSentiment, LinkedEntityMatch, AspectSentiment, OpinionSentiment, and PiiEntity.
To get the length of the text in these models, just call len() on the text property.ValueError instead of a NotImplementedError.ValidationErrors thrown by the client SDK in the case of malformed input. The error will now be thrown by the service through an HttpResponseError.New features
v3.0 to the kwarg api_version when creating your TextAnalyticsClientrecognize_pii_entities which returns entities containing personally identifiable information for a batch of documents. Only available for API version v3.1-preview and up.offset and length properties for CategorizedEntity, SentenceSentiment, and LinkedEntityMatch. These properties are only available for API versions v3.1-preview and up.
length is the number of characters in the text of these modelsoffset is the offset of the text from the start of the documentshow_opinion_mining as True when calling the analyze_sentiment endpointbing_entity_search_api_id to the LinkedEntity class. This property is only available for v3.1-preview and up, and it is to be
used in conjunction with the Bing Entity Search API to fetch additional relevant information about the returned entity.New features
warnings property on each document-level response object returned from the endpoints. It is a list of TextAnalyticsWarnings.text property to SentenceSentimentBreaking changes
score attribute of DetectedLanguage has been renamed to confidence_scoregrapheme_offset and grapheme_length from CategorizedEntity, SentenceSentiment, and LinkedEntityMatchTextDocumentStatistics attribute grapheme_count has been renamed to character_countBreaking changes
recognize_pii_entities endpoint and all related models (RecognizePiiEntitiesResult and PiiEntity)
from this library.TextAnalyticsApiKeyCredential and now using AzureKeyCredential from azure.core.credentials as key credentialscore attribute has been renamed to confidence_score for the CategorizedEntity, LinkedEntityMatch, and
PiiEntity modelsinputs have been renamed to documentsBreaking changes
SentimentScorePerLabel has been renamed to SentimentConfidenceScoresAnalyzeSentimentResult and SentenceSentiment attribute sentiment_scores has been renamed to confidence_scoresTextDocumentStatistics attribute character_count has been renamed to grapheme_countLinkedEntity attribute id has been renamed to data_source_entity_idcountry_hint and language are now passed as keyword argumentsresponse_hook has been renamed to raw_response_hooklength and offset attributes have been renamed to grapheme_length and grapheme_offset for the SentenceSentiment,
CategorizedEntity, PiiEntity, and LinkedEntityMatch modelsNew features
country_hint="none" to not use the default country hint of "US".Dependency updates
Breaking changes
single_detect_language(), single_recognize_entities(), single_extract_key_phrases(), single_analyze_sentiment(), single_recognize_pii_entities(), and single_recognize_linked_entities()
have been removed from the client library. Use the batching methods for optimal performance in production environments.TextAnalyticsApiKeyCredential("<api_key>") must be passed in for the credential parameter.
Passing the API key as a string is no longer supported.detect_languages() is renamed to detect_language().TextAnalyticsError model has been simplified to an object with only attributes code, message, and target.NamedEntity has been renamed to CategorizedEntity and its attributes type to category and subtype to subcategory.RecognizePiiEntitiesResult now contains on the object a list of PiiEntity instead of NamedEntity.AnalyzeSentimentResult attribute document_scores has been renamed to sentiment_scores.SentenceSentiment attribute sentence_scores has been renamed to sentiment_scores.SentimentConfidenceScorePerLabel has been renamed to SentimentScorePerLabel.DetectLanguageResult no longer has attribute detected_languages. Use primary_language to access the detected language in text.New features
TextAnalyticsApiKeyCredential provides an update_key() method which allows you to update the API key for long-lived clients.Fixes and improvements
__repr__ has been added to all of the response objects.DocumentError object, an AttributeError is raised with a custom error message that provides the document ID and error of the invalid document.Version (1.0.0b1) is the first preview of our efforts to create a user-friendly and Pythonic client library for Azure Text Analytics. For more information about this, and preview releases of other Azure SDK libraries, please visit https://azure.github.io/azure-sdk/releases/latest/python.html.
Breaking changes: New API design
New namespace/package name:
azure.cognitiveservices.language.textanalytics to azure.ai.textanalyticsNew operations and naming:
detect_language is renamed to detect_languagesentities is renamed to recognize_entitieskey_phrases is renamed to extract_key_phrasessentiment is renamed to analyze_sentimentrecognize_pii_entities finds personally identifiable information entities in textrecognize_linked_entities provides links from a well-known knowledge base for each recognized entitysingle_detect_language, single_recognize_entities, single_extract_key_phrases, single_analyze_sentiment, single_recognize_pii_entities, and single_recognize_linked_entities perform
function on a single string instead of a batch of text documents and can be imported from the azure.ai.textanalytics namespace.azure.ai.textanalytics.aio.MultiLanguageInput has been renamed to TextDocumentInputLanguageInput has been renamed to DetectLanguageInputDocumentLanguage has been renamed to DetectLanguageResultDocumentEntities has been renamed to RecognizeEntitiesResultDocumentLinkedEntities has been renamed to RecognizeLinkedEntitiesResultDocumentKeyPhrases has been renamed to ExtractKeyPhrasesResultDocumentSentiment has been renamed to AnalyzeSentimentResultDocumentStatistics has been renamed to TextDocumentStatisticsRequestStatistics has been renamed to TextDocumentBatchStatisticsEntity has been renamed to NamedEntityMatch has been renamed to LinkedEntityMatchdocuments parameter has been renamed inputsNew input types:
detect_languages can take as input a list[DetectLanguageInput] or a list[str]. A list of dict-like objects in the same shape as DetectLanguageInput is still accepted as input.recognize_entities, recognize_pii_entities, recognize_linked_entities, extract_key_phrases, analyze_sentiment can take as input a list[TextDocumentInput] or list[str].
A list of dict-like objects in the same shape as TextDocumentInput is still accepted as input.New parameters/keyword arguments:
model_version which allows the user to specify a string referencing the desired model version to be used for analysis. If no string specified, it will default to the latest, non-preview version.detect_languages now takes a parameter country_hint which allows you to specify the country hint for the entire batch. Any per-item country hints will take precedence over a whole batch hint.recognize_entities, recognize_pii_entities, recognize_linked_entities, extract_key_phrases, analyze_sentiment now take a parameter language which allows you to specify the language for the entire batch.
Any per-item specified language will take precedence over a whole batch hint.default_country_hint or default_language keyword argument can be passed at client instantiation to set the default values for all operations.response_hook keyword argument can be passed with a callback to use the raw response from the service. Additionally, values returned for TextDocumentBatchStatistics and model_version used must be retrieved using a response hook.show_stats and model_version parameters move to keyword only arguments.New return types
detect_languages, recognize_entities, recognize_pii_entities, recognize_linked_entities, extract_key_phrases, analyze_sentiment) now return a heterogeneous list of
result objects and document errors in the order passed in with the request. To iterate over the list and filter for result or error, a boolean property on each object called is_error can be used to determine whether the returned response object at
that index is a result or an error:detect_languages now returns a List[Union[DetectLanguageResult, DocumentError]]recognize_entities now returns a List[Union[RecognizeEntitiesResult, DocumentError]]recognize_pii_entities now returns a List[Union[RecognizePiiEntitiesResult, DocumentError]]recognize_linked_entities now returns a List[Union[RecognizeLinkedEntitiesResult, DocumentError]]extract_key_phrases now returns a List[Union[ExtractKeyPhrasesResult, DocumentError]]analyze_sentiment now returns a List[Union[AnalyzeSentimentResult, DocumentError]]single_detect_languages returns a DetectLanguageResultsingle_recognize_entities returns a RecognizeEntitiesResultsingle_recognize_pii_entities returns a RecognizePiiEntitiesResultsingle_recognize_linked_entities returns a RecognizeLinkedEntitiesResultsingle_extract_key_phrases returns a ExtractKeyPhrasesResultsingle_analyze_sentiment returns a AnalyzeSentimentResultNew underlying REST pipeline implementation, based on the new azure-core library.
Client and pipeline configuration is now available via keyword arguments at both the client level, and per-operation. See README for a full list of optional configuration arguments.
Authentication using azure-identity credentials
New error hierarchy:
azure.core.exceptions.HttpResponseErrorClientAuthenticationError: Authentication failed.Features
Breaking changes
General Breaking changes
This version uses a next-generation code generator that might introduce breaking changes.
Model signatures now use only keyword-argument syntax. All positional arguments must be re-written as keyword-arguments. To keep auto-completion in most cases, models are now generated for Python 2 and Python 3. Python 3 uses the "*" syntax for keyword-only arguments.
Enum types now use the "str" mixin (class AzureEnum(str, Enum)) to improve the behavior when unrecognized enum values are encountered. While this is not a breaking change, the distinctions are important, and are documented here: https://docs.python.org/3/library/enum.html#others At a glance:
NameOfEnum.stringvalue. Format syntax should be preferred.Bugfixes
FAQs
Microsoft Azure Text Analytics Client Library for Python
We found that azure-ai-textanalytics demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.