Skip to main content

Microsoft Azure Text Analytics Client Library for Python

Project description

Azure Text Analytics client library for Python

Text Analytics is a cloud-based service that provides advanced natural language processing over raw text, and includes six main functions:

  • Sentiment Analysis
  • Named Entity Recognition
  • Linked Entity Recognition
  • Language Detection
  • Key Phrase Extraction

Source code | Package (PyPI) | API reference documentation| Product documentation | Samples

Getting started

Prerequisites

Create a Cognitive Services or Text Analytics resource

Text Analytics 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 Text Analytics access only, create a Text Analytics resource.

You can create the resource using

Option 1: Azure Portal

Option 2: Azure CLI. Below is an example of how you can create a Text Analytics resource using the CLI:

# Create a new resource group to hold the text analytics resource -
# if using an existing resource group, skip this step
az group create --name my-resource-group --location westus2
# Create text analytics
az cognitiveservices account create \
    --name text-analytics-resource \
    --resource-group my-resource-group \
    --kind TextAnalytics \
    --sku F0 \
    --location westus2 \
    --yes

Interaction with this service begins with an instance of a client. To create a client object, you will need the cognitive services or text analytics 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://<region>.api.cognitive.microsoft.com/", credential=credential)

Note that if you create a custom subdomain name for your resource the endpoint may look different than in the above code snippet. For example, https://<my-custom-subdomain>.cognitiveservices.azure.com/.

Install the package

Install the Azure Text Analytics client library for Python with pip:

pip install azure-ai-textanalytics

Note: This version of the client library supports the v3.0 version of the Text Analytics service

Authenticate the client

Get the endpoint

You can find the endpoint for your text analytics resource using the Azure Portal or Azure CLI:

# Get the endpoint for the text analytics resource
az cognitiveservices account show --name "resource-name" --resource-group "resource-group-name" --query "endpoint"

Get the API Key

You can get the API key from the Cognitive Services or Text Analytics 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"

Create a TextAnalyticsClient with an API Key Credential

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:

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

credential = AzureKeyCredential("<api_key>")
text_analytics_client = TextAnalyticsClient(endpoint="https://<region>.api.cognitive.microsoft.com/", credential=credential)

Create a TextAnalyticsClient with an Azure Active Directory Credential

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:

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:

from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
text_analytics_client = TextAnalyticsClient(endpoint="https://<my-custom-subdomain>.api.cognitive.microsoft.com/", credential=credential)

Key concepts

TextAnalyticsClient

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 Analytics, such as language detection or key phrase extraction.

Input

A document is a single unit to be analyzed by the predictive models in the Text Analytics 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."},
    {"id": "3", "language": "en", "text": "What a great movie!"}
]

See service limitations for the input, including document length limits, maximum batch size, and supported text encoding.

Return Value

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 Analytics 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.

Document Error Handling

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]

Examples

The following section provides several code snippets covering some of the most common Text Analytics tasks, including:

Analyze sentiment

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.

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

credential = AzureKeyCredential("<api_key>")
endpoint="https://<region>.api.cognitive.microsoft.com/"

text_analytics_client = TextAnalyticsClient(endpoint, credential)

documents = [
    "I did not like the restaurant. The food was too spicy.",
    "The restaurant was decorated beautifully. The atmosphere was unlike any other restaurant I've been to.",
    "The food was yummy. :)"
]

response = text_analytics_client.analyze_sentiment(documents, language="en")
result = [doc for doc in response if not doc.is_error]

for doc in result:
    print("Overall sentiment: {}".format(doc.sentiment))
    print("Scores: positive={}; neutral={}; negative={} \n".format(
        doc.confidence_scores.positive,
        doc.confidence_scores.neutral,
        doc.confidence_scores.negative,
    ))

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.

Recognize entities

recognize_entities recognizes and categories entities in its input text as people, places, organizations, date/time, quantities, percentages, currencies, and more.

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

credential = AzureKeyCredential("<api_key>")
endpoint="https://<region>.api.cognitive.microsoft.com/"

text_analytics_client = TextAnalyticsClient(endpoint, credential)

documents = [
    "Microsoft was founded by Bill Gates and Paul Allen.",
    "Redmond is a city in King County, Washington, United States, located 15 miles east of Seattle.",
    "Jeff bought three dozen eggs because there was a 50% discount."
]

response = text_analytics_client.recognize_entities(documents, language="en")
result = [doc for doc in response if not doc.is_error]

for doc in result:
    for entity in doc.entities:
        print("Entity: \t", entity.text, "\tCategory: \t", entity.category,
              "\tConfidence Score: \t", entity.confidence_score)

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

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.

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

credential = AzureKeyCredential("<api_key>")
endpoint="https://<region>.api.cognitive.microsoft.com/"

text_analytics_client = TextAnalyticsClient(endpoint, credential)

documents = [
    "Microsoft was founded by Bill Gates and Paul Allen.",
    "Easter Island, a Chilean territory, is a remote volcanic island in Polynesia."
]

response = text_analytics_client.recognize_linked_entities(documents, language="en")
result = [doc for doc in response if not doc.is_error]

for doc in result:
    for entity in doc.entities:
        print("Entity: {}".format(entity.name))
        print("URL: {}".format(entity.url))
        print("Data Source: {}".format(entity.data_source))
        for match in entity.matches:
            print("Confidence Score: {}".format(match.confidence_score))
            print("Entity as appears in request: {}".format(match.text))

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.

Extract key phrases

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".

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

credential = AzureKeyCredential("<api_key>")
endpoint="https://<region>.api.cognitive.microsoft.com/"

text_analytics_client = TextAnalyticsClient(endpoint, credential)

documents = [
    "Redmond is a city in King County, Washington, United States, located 15 miles east of Seattle.",
    "I need to take my cat to the veterinarian.",
    "I will travel to South America in the summer."
]

response = text_analytics_client.extract_key_phrases(documents, language="en")
result = [doc for doc in response if not doc.is_error]

for doc in result:
    print(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

detect_language determines the language of its input text, including the confidence score of the predicted language.

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

credential = AzureKeyCredential("<api_key>")
endpoint="https://<region>.api.cognitive.microsoft.com/"

text_analytics_client = TextAnalyticsClient(endpoint, credential)

documents = [
    "This is written in English.",
    "Il documento scritto in italiano.",
    "Dies ist in deutsche Sprache verfasst."
]

response = text_analytics_client.detect_language(documents)
result = [doc for doc in response if not doc.is_error]

for doc in result:
    print("Language detected: {}".format(doc.primary_language.name))
    print("ISO6391 name: {}".format(doc.primary_language.iso6391_name))
    print("Confidence score: {}\n".format(doc.primary_language.confidence_score))

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.

Optional Configuration

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.

Troubleshooting

General

The Text Analytics client will raise exceptions defined in Azure Core.

Logging

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://<region>.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)

Next steps

More sample code

These code samples show common scenario operations with the Azure Text Analytics client library. The async versions of the samples (the python sample files appended with _async) show asynchronous operations with Text Analytics and require Python 3.5 or later.

Authenticate the client with a Cognitive Services/Text Analytics API key or a token credential from azure-identity:

In a batch of documents:

Additional documentation

For more extensive documentation on Azure Cognitive Services Text Analytics, see the Text Analytics documentation on docs.microsoft.com.

Contributing

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.

Change Log azure-ai-textanalytics

5.0.0 (2020-07-27)

  • Re-release of GA version 1.0.0 with an updated version

1.0.0 (2020-06-09)

  • First stable release of the azure-ai-textanalytics package. Targets the service's v3.0 API.

1.0.0b6 (2020-05-27)

New features

  • We now have a warnings property on each document-level response object returned from the endpoints. It is a list of TextAnalyticsWarnings.
  • Added text property to SentenceSentiment

Breaking changes

  • Now targets only the service's v3.0 API, instead of the v3.0-preview.1 API
  • score attribute of DetectedLanguage has been renamed to confidence_score
  • Removed grapheme_offset and grapheme_length from CategorizedEntity, SentenceSentiment, and LinkedEntityMatch
  • TextDocumentStatistics attribute grapheme_count has been renamed to character_count

1.0.0b5

  • This was a broken release

1.0.0b4 (2020-04-07)

Breaking changes

  • Removed the recognize_pii_entities endpoint and all related models (RecognizePiiEntitiesResult and PiiEntity) from this library.
  • Removed TextAnalyticsApiKeyCredential and now using AzureKeyCredential from azure.core.credentials as key credential
  • score attribute has been renamed to confidence_score for the CategorizedEntity, LinkedEntityMatch, and PiiEntity models
  • All input parameters inputs have been renamed to documents

1.0.0b3 (2020-03-10)

Breaking changes

  • SentimentScorePerLabel has been renamed to SentimentConfidenceScores
  • AnalyzeSentimentResult and SentenceSentiment attribute sentiment_scores has been renamed to confidence_scores
  • TextDocumentStatistics attribute character_count has been renamed to grapheme_count
  • LinkedEntity attribute id has been renamed to data_source_entity_id
  • Parameters country_hint and language are now passed as keyword arguments
  • The keyword argument response_hook has been renamed to raw_response_hook
  • length and offset attributes have been renamed to grapheme_length and grapheme_offset for the SentenceSentiment, CategorizedEntity, PiiEntity, and LinkedEntityMatch models

New features

  • Pass country_hint="none" to not use the default country hint of "US".

Dependency updates

1.0.0b2 (2020-02-11)

Breaking changes

  • The single text, module-level operations 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.
  • To use an API key as the credential for authenticating the client, a new credential class 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().
  • The 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

  • Credential class 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.
  • If you try to access a result attribute on a DocumentError object, an AttributeError is raised with a custom error message that provides the document ID and error of the invalid document.

1.0.0b1 (2020-01-09)

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:

    • The namespace/package name for Azure Text Analytics client library has changed from azure.cognitiveservices.language.textanalytics to azure.ai.textanalytics
  • New operations and naming:

    • detect_language is renamed to detect_languages
    • entities is renamed to recognize_entities
    • key_phrases is renamed to extract_key_phrases
    • sentiment is renamed to analyze_sentiment
    • New operation recognize_pii_entities finds personally identifiable information entities in text
    • New operation recognize_linked_entities provides links from a well-known knowledge base for each recognized entity
    • New module-level operations single_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.
    • New client and module-level async APIs added to subnamespace azure.ai.textanalytics.aio.
    • MultiLanguageInput has been renamed to TextDocumentInput
    • LanguageInput has been renamed to DetectLanguageInput
    • DocumentLanguage has been renamed to DetectLanguageResult
    • DocumentEntities has been renamed to RecognizeEntitiesResult
    • DocumentLinkedEntities has been renamed to RecognizeLinkedEntitiesResult
    • DocumentKeyPhrases has been renamed to ExtractKeyPhrasesResult
    • DocumentSentiment has been renamed to AnalyzeSentimentResult
    • DocumentStatistics has been renamed to TextDocumentStatistics
    • RequestStatistics has been renamed to TextDocumentBatchStatistics
    • Entity has been renamed to NamedEntity
    • Match has been renamed to LinkedEntityMatch
    • The batching methods' documents parameter has been renamed inputs
  • New 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:

    • All operations now take a keyword argument 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.
    • A default_country_hint or default_language keyword argument can be passed at client instantiation to set the default values for all operations.
    • A 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

    • The return types for the batching methods (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]]
    • The module-level, single text operations will return a single result object or raise the error found on the document:
    • single_detect_languages returns a DetectLanguageResult
    • single_recognize_entities returns a RecognizeEntitiesResult
    • single_recognize_pii_entities returns a RecognizePiiEntitiesResult
    • single_recognize_linked_entities returns a RecognizeLinkedEntitiesResult
    • single_extract_key_phrases returns a ExtractKeyPhrasesResult
    • single_analyze_sentiment returns a AnalyzeSentimentResult
  • New 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:

    • All service errors will now use the base type: azure.core.exceptions.HttpResponseError
    • There is one exception type derived from this base type for authentication errors:
      • ClientAuthenticationError: Authentication failed.

0.2.0 (2019-03-12)

Features

  • Client class can be used as a context manager to keep the underlying HTTP session open for performance
  • New method "entities"
  • Model KeyPhraseBatchResultItem has a new parameter statistics
  • Model KeyPhraseBatchResult has a new parameter statistics
  • Model LanguageBatchResult has a new parameter statistics
  • Model LanguageBatchResultItem has a new parameter statistics
  • Model SentimentBatchResult has a new parameter statistics

Breaking changes

  • TextAnalyticsAPI main client has been renamed TextAnalyticsClient
  • TextAnalyticsClient parameter is no longer a region but a complete endpoint

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:

    • "is" should not be used at all.
    • "format" will return the string value, where "%s" string formatting will return NameOfEnum.stringvalue. Format syntax should be prefered.

Bugfixes

  • Compatibility of the sdist with wheel 0.31.0

0.1.0 (2018-01-12)

  • Initial Release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for azure-ai-textanalytics, version 5.0.0
Filename, size File type Python version Upload date Hashes
Filename, size azure_ai_textanalytics-5.0.0-py2.py3-none-any.whl (55.3 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size azure-ai-textanalytics-5.0.0.zip (151.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page