-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a62db8d
commit 6130950
Showing
19 changed files
with
201 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...s/azure-ai-textanalytics/samples/async_samples/sample_alternative_document_input_async.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
sdk/textanalytics/azure-ai-textanalytics/samples/sample_alternative_document_input.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.