Skip to content

Commit

Permalink
addressed comments in pr
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed Mar 5, 2020
1 parent a62db8d commit 6130950
Show file tree
Hide file tree
Showing 19 changed files with 201 additions and 76 deletions.
35 changes: 19 additions & 16 deletions sdk/textanalytics/azure-ai-textanalytics/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@ languages:
products:
- azure
- azure-ai-textanalytics
urlFragment: cognitiveservices-textanalytics-samples
urlFragment: textanalytics-samples
---

# Samples for Azure Text Analytics client library for Python

These code samples show common scenario operations with the Azure Text Analytics client library.
The async versions of the samples require [Python 3.5][python_35] or later.
The async versions of the samples require Python 3.5 or later.

You can authenticate your client with a Cognitive Services/Text Analytics API key or through Azure Active Directory with a token credential from [azure-identity][azure_identity]:
* See [sample_authentication.py][sample_authentication] and [sample_authentication_async.py][sample_authentication_async] for how to authenticate in the above cases
* See [sample_authentication.py][sample_authentication] and [sample_authentication_async.py][sample_authentication_async] for how to authenticate in the above cases.

These sample programs show common scenarios for the Text Analytics client's offerings.

|**File Name**|**Description**|
|----------------|-------------|
|[sample_detect_language.py][detect_language] and [sample_detect_language_async.py][detect_language_async]|Detect language in documents|
|[sample_recognize_entities.py][recognize_entities] and [sample_recognize_entities_async.py][recognize_entities_async]|Recognize named entities in documents|
|[sample_recognize_linked_entities.py][recognize_linked_entities] and [sample_recognize_linked_entities_async.py][recognize_linked_entities_async]|Recognize linked entities|
|[sample_recognize_pii_entities.py][recognize_pii_entities] and [sample_recognize_pii_entities_async.py][recognize_pii_entities_async]|Recognize personally identifiable information|
|[sample_recognize_linked_entities.py][recognize_linked_entities] and [sample_recognize_linked_entities_async.py][recognize_linked_entities_async]|Recognize linked entities in documents|
|[sample_recognize_pii_entities.py][recognize_pii_entities] and [sample_recognize_pii_entities_async.py][recognize_pii_entities_async]|Recognize personally identifiable information in documents|
|[sample_extract_key_phrases.py][extract_key_phrases] and [sample_extract_key_phrases_async.py][extract_key_phrases_async]|Extract key phrases from documents|
|[sample_analyze_sentiment.py][analyze_sentiment] and [sample_analyze_sentiment_async.py][analyze_sentiment_async]|Analye the sentiment of text|
|[sample_analyze_sentiment.py][analyze_sentiment] and [sample_analyze_sentiment_async.py][analyze_sentiment_async]|Analyze the sentiment of documents|

## Prerequisites
* Python [2.7][python_27], or [3.5][python_35] or later is required to use this package (3.5 or later if using asyncio)
* You must have an [Azure subscription](https://azure.microsoft.com/free/) and an
[Azure Text Analytics account](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account?tabs=singleservice%2Cwindows) to run these samples.
* Python 2.7, or 3.5 or later is required to use this package (3.5 or later if using asyncio)
* You must have an [Azure subscription][azure_subscription] and an
[Azure Text Analytics account][azure_text_analytics_account] to run these samples.

## Setup

1. Install the Azure Text Analytics client library for Python with [pip](https://pypi.org/project/pip/):
1. Install the Azure Text Analytics client library for Python with [pip][pip]:

```bash
pip install azure-ai-textanalytics --pre
```
* If authenticating with Azure Active Directory, make sure you have [azure-identity](https://pypi.org/project/azure-identity/) installed:
* If authenticating with Azure Active Directory, make sure you have [azure-identity][azure_identity_pip] installed:
```bash
pip install azure-identity
```
Expand All @@ -55,16 +55,14 @@ pip install azure-ai-textanalytics --pre

## Next steps

Check out the [API reference documentation](https://aka.ms/azsdk-python-textanalytics-ref-docs) to learn more about
Check out the [API reference documentation][api_reference_documentation] to learn more about
what you can do with the Azure Text Analytics client library.

|**Advanced Sample File Name**|**Description**|
|----------------|-------------|
|[sample_get_detailed_diagnostics_information.py][get_detailed_diagnostics_information] and [sample_get_detailed_diagnostics_information_async.py][get_detailed_diagnostics_information_async]|Get the request batch statistics, model version, and raw response through a callback|

[python_35]: https://www.python.org/downloads/release/python-350/
[python_27]: https://www.python.org/downloads/release/python-270/
[azure-identity]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity
[azure_identity]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity
[sample_authentication]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/textanalytics/azure-ai-textanalytics/samples/sample_authentication.py
[sample_authentication_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_authentication_async.py
[detect_language]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/textanalytics/azure-ai-textanalytics/samples/sample_detect_language.py
Expand All @@ -80,4 +78,9 @@ what you can do with the Azure Text Analytics client library.
[analyze_sentiment]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment.py
[analyze_sentiment_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_async.py
[get_detailed_diagnostics_information]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/textanalytics/azure-ai-textanalytics/samples/sample_get_detailed_diagnostics_information.py
[get_detailed_diagnostics_information_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_get_detailed_diagnostics_information_async.py
[get_detailed_diagnostics_information_async]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_get_detailed_diagnostics_information_async.py
[pip]: https://pypi.org/project/pip/
[azure_subscription]: https://azure.microsoft.com/free/
[azure_text_analytics_account]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account?tabs=singleservice%2Cwindows
[azure_identity_pip]: https://pypi.org/project/azure-identity/
[api_reference_documentation]: https://aka.ms/azsdk-python-textanalytics-ref-docs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: sample_alternative_document_input_async.py
DESCRIPTION:
This sample shows an alternative way to pass in the input documents.
Here we specify our own IDs and the text language along with the text.
USAGE:
python sample_alternative_document_input_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
import asyncio


class AlternativeDocumentInputSampleAsync(object):

endpoint = os.getenv("AZURE_TEXT_ANALYTICS_ENDPOINT")
key = os.getenv("AZURE_TEXT_ANALYTICS_KEY")

async def alternative_document_input(self):
from azure.ai.textanalytics.aio import TextAnalyticsClient
from azure.ai.textanalytics import TextAnalyticsApiKeyCredential
text_analytics_client = TextAnalyticsClient(endpoint=self.endpoint, credential=TextAnalyticsApiKeyCredential(self.key))

documents = [
{"id": "0", "language": "en", "text": "I had the best day of my life."},
{"id": "1", "language": "en",
"text": "This was a waste of my time. The speaker put me to sleep."},
{"id": "2", "language": "es", "text": "No tengo dinero ni nada que dar..."},
{"id": "3", "language": "fr",
"text": "L'hôtel n'était pas très confortable. L'éclairage était trop sombre."}
]
async with text_analytics_client:
result = await text_analytics_client.detect_language(documents)

for idx, doc in enumerate(result):
if not doc.is_error:
print("Document text: {}".format(documents[idx]))
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.score))
if doc.is_error:
print(doc.id, doc.error)


async def main():
sample = AlternativeDocumentInputSampleAsync()
await sample.alternative_document_input()


if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
python sample_analyze_sentiment_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your cognitive services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your text analytics subscription key
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
FILE: sample_authentication_async.py
DESCRIPTION:
This sample demonstrates how to authenticate to the text analytics service.
This sample demonstrates how to authenticate to the Text Analytics service.
There are two supported methods of authentication:
1) Use a cognitive services/text analytics API key with TextAnalyticsApiKeyCredential
1) Use a Cognitive Services/Text Analytics API key with TextAnalyticsApiKeyCredential
2) Use a token credential from azure-identity to authenticate with Azure Active Directory
See more details about authentication here:
Expand All @@ -23,8 +23,8 @@
python sample_authentication_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your cognitive services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your cognitive services/text analytics API key
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Cognitive Services/Text Analytics API key
3) AZURE_CLIENT_ID - the client ID of your active directory application.
4) AZURE_TENANT_ID - the tenant ID of your active directory application.
5) AZURE_CLIENT_SECRET - the secret of your active directory application.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
python sample_detect_language_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your cognitive services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your text analytics subscription key
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
python sample_extract_key_phrases_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your cognitive services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your text analytics subscription key
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
This sample demonstrates how to retrieve batch statistics, the
model version used, and the raw response returned from the service.
It additionally shows an alternative way to pass in the input documents.
Here we supply our own IDs and language hints along with the text.
USAGE:
python sample_get_detailed_diagnostics_information_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your cognitive services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your text analytics subscription key
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
Expand All @@ -41,17 +38,17 @@ async def get_detailed_diagnostics_information_async(self):
text_analytics_client = TextAnalyticsClient(endpoint=self.endpoint, credential=TextAnalyticsApiKeyCredential(self.key))

documents = [
{"id": "0", "language": "en", "text": "I had the best day of my life."},
{"id": "1", "language": "en",
"text": "This was a waste of my time. The speaker put me to sleep."},
{"id": "2", "language": "es", "text": "No tengo dinero ni nada que dar..."},
{"id": "3", "language": "fr",
"text": "L'hôtel n'était pas très confortable. L'éclairage était trop sombre."}
"I had the best day of my life.",
"This was a waste of my time. The speaker put me to sleep.",
"No tengo dinero ni nada que dar...",
"L'hôtel n'était pas très confortable. L'éclairage était trop sombre."
]

def callback(resp):
for statistic, value in resp.statistics.items():
_LOGGER.info("{}: {}".format(statistic, value))
_LOGGER.info("document_count: {}".format(resp.statistics["document_count"]))
_LOGGER.info("valid_document_count: {}".format(resp.statistics["valid_document_count"]))
_LOGGER.info("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"]))
_LOGGER.info("transaction_count: {}".format(resp.statistics["transaction_count"]))
_LOGGER.info("model_version: {}".format(resp.model_version))
_LOGGER.info("raw_response: {}".format(resp.raw_response))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
python sample_recognize_entities_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your cognitive services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your text analytics subscription key
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
python sample_recognize_linked_entities_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your cognitive services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your text analytics subscription key
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
Expand Down Expand Up @@ -54,7 +54,8 @@ async def recognize_linked_entities_async(self):
print("Url: {}".format(entity.url))
print("Data Source: {}".format(entity.data_source))
for match in entity.matches:
print("Score: {}".format(match.score))
print("Confidence Score: {}".format(match.score))
print("Entity as appears in request: {}".format(match.text))
print("------------------------------------------")
# [END batch_recognize_linked_entities_async]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
python sample_recognize_pii_entities_async.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your cognitive services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your text analytics subscription key
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: sample_alternative_document_input.py
DESCRIPTION:
This sample shows an alternative way to pass in the input documents.
Here we specify our own IDs and the text language along with the text.
USAGE:
python sample_alternative_document_input.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
import logging

_LOGGER = logging.getLogger(__name__)

class AlternativeDocumentInputSample(object):
endpoint = os.getenv("AZURE_TEXT_ANALYTICS_ENDPOINT")
key = os.getenv("AZURE_TEXT_ANALYTICS_KEY")

def alternative_document_input(self):
from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsApiKeyCredential
text_analytics_client = TextAnalyticsClient(endpoint=self.endpoint, credential=TextAnalyticsApiKeyCredential(self.key))

documents = [
{"id": "0", "language": "en", "text": "I had the best day of my life."},
{"id": "1", "language": "en",
"text": "This was a waste of my time. The speaker put me to sleep."},
{"id": "2", "language": "es", "text": "No tengo dinero ni nada que dar..."},
{"id": "3", "language": "fr",
"text": "L'hôtel n'était pas très confortable. L'éclairage était trop sombre."}
]

result = text_analytics_client.detect_language(documents)

for idx, doc in enumerate(result):
if not doc.is_error:
print("Document text: {}".format(documents[idx]))
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.score))
if doc.is_error:
print(doc.id, doc.error)


if __name__ == '__main__':
sample = AlternativeDocumentInputSample()
sample.alternative_document_input()
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
python sample_analyze_sentiment.py
Set the environment variables with your own values before running the sample:
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your cognitive services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your text analytics subscription key
1) AZURE_TEXT_ANALYTICS_ENDPOINT - the endpoint to your Cognitive Services resource.
2) AZURE_TEXT_ANALYTICS_KEY - your Text Analytics subscription key
"""

import os
Expand Down
Loading

0 comments on commit 6130950

Please sign in to comment.