File: sample_recognize_entities_async.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (82 lines) | stat: -rw-r--r-- 3,420 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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_recognize_entities_async.py

DESCRIPTION:
    This sample demonstrates how to recognize named entities in a batch of documents.

    In this sample, we own a catering business. We want to sort the reviews for our business
    based off of which organization hired us.
USAGE:
    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
"""

import os
import asyncio


class RecognizeEntitiesSampleAsync(object):

    async def recognize_entities_async(self):
        print(
            "In this sample, we are a catering business, and we're looking to sort the reviews "
            "for our organization based off of the organization that hired us for catering"
        )
        organization_to_reviews = {}
        # [START recognize_entities_async]
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.textanalytics.aio import TextAnalyticsClient

        endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"]
        key = os.environ["AZURE_TEXT_ANALYTICS_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!!!!"""
        ]

        async with text_analytics_client:
            result = await text_analytics_client.recognize_entities(reviews)

        result = [review for review in result if not review.is_error]

        for idx, review in enumerate(result):
            for entity in review.entities:
                print("Entity '{}' has category '{}'".format(entity.text, entity.category))
        # [END recognize_entities_async]
                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)
                )
            )


async def main():
    sample = RecognizeEntitiesSampleAsync()
    await sample.recognize_entities_async()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())