예제 #1
0
    def alternative_document_input(self):
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.textanalytics import TextAnalyticsClient
        text_analytics_client = TextAnalyticsClient(
            endpoint=self.endpoint, credential=AzureKeyCredential(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.confidence_score))
            if doc.is_error:
                print(doc.id, doc.error)
    def alternative_scenario_recognize_entities(self):
        """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
        using a list[TextDocumentInput] and supplying your own IDs and language hints along
        with the text.
        """
        from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsApiKeyCredential
        text_analytics_client = TextAnalyticsClient(
            endpoint=self.endpoint,
            credential=TextAnalyticsApiKeyCredential(self.key))

        documents = [
            {
                "id": "0",
                "language": "en",
                "text": "Microsoft was founded by Bill Gates and Paul Allen."
            },
            {
                "id": "1",
                "language": "de",
                "text": "I had a wonderful trip to Seattle last week."
            },
            {
                "id": "2",
                "language": "es",
                "text": "I visited the Space Needle 2 times."
            },
        ]

        extras = []

        def callback(resp):
            extras.append(resp.statistics)
            extras.append(resp.model_version)
            extras.append(resp.raw_response)

        result = text_analytics_client.recognize_entities(
            documents,
            show_stats=True,
            model_version="latest",
            raw_response_hook=callback)
예제 #3
0
def authenticate_client():
    """This function logs the user on Azure's API
    """
    ENDPOINT = "https://cs-groupe-un.cognitiveservices.azure.com/"
    KEY = "7b3d4c450fd84d3485f41b09087d5038"

    ta_credential = AzureKeyCredential(KEY)
    text_analytics_client = TextAnalyticsClient(endpoint=ENDPOINT,
                                                credential=ta_credential)
    return text_analytics_client
예제 #4
0
    def test_passing_only_string(self, resource_group, location,
                                 text_analytics_account,
                                 text_analytics_account_key):
        text_analytics = TextAnalyticsClient(
            text_analytics_account,
            TextAnalyticsApiKeyCredential(text_analytics_account_key))

        docs = [
            u"I should take my cat to the veterinarian.",
            u"Este es un document escrito en Español.", u"猫は幸せ",
            u"Fahrt nach Stuttgart und dann zum Hotel zu Fu.", u""
        ]

        response = text_analytics.detect_language(docs)
        self.assertEqual(response[0].primary_language.name, "English")
        self.assertEqual(response[1].primary_language.name, "Spanish")
        self.assertEqual(response[2].primary_language.name, "Japanese")
        self.assertEqual(response[3].primary_language.name, "German")
        self.assertTrue(response[4].is_error)
예제 #5
0
    def test_country_hint_kwarg(self, resource_group, location,
                                text_analytics_account,
                                text_analytics_account_key):
        text_analytics = TextAnalyticsClient(
            text_analytics_account,
            TextAnalyticsApiKeyCredential(text_analytics_account_key))

        def callback(response):
            country_str = "\"countryHint\": \"ES\""
            self.assertEqual(response.http_request.body.count(country_str), 1)
            self.assertIsNotNone(response.model_version)
            self.assertIsNotNone(response.statistics)

        res = text_analytics.detect_language(
            inputs=["this is written in english"],
            model_version="latest",
            show_stats=True,
            country_hint="ES",
            raw_response_hook=callback)
예제 #6
0
def Speech2Text(input_file, config):
    # Make tempfile for extracted audio
    tf = tempfile.NamedTemporaryFile(suffix='.wav')

    # Extract audio from video
    video = VideoFileClip(input_file)
    video.audio.write_audiofile(tf.name)

    # Build speech recognizer
    speech_key = config.get('AZURE_SPEECH_KEY', '')
    region = config.get('AZURE_REGION', 'koreacentral')
    speech_config = speechsdk.SpeechConfig(subscription=speech_key,
                                           region=region)
    audio_config = speechsdk.audio.AudioConfig(filename=tf.name)
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config,
                                                   audio_config=audio_config)

    # Build text analaytics client
    ta_key = config.get('AZURE_TEXT_ANALYSIS_KEY', '')
    endpoint = config.get('AZURE_ENDPOINT', '')
    ta_credential = AzureKeyCredential(ta_key)
    ta_client = TextAnalyticsClient(endpoint=endpoint,
                                    credential=ta_credential)

    # Loop to get transcription and key phrases
    time_unit = 1e-7
    results = []
    while True:
        result = speech_recognizer.recognize_once()
        if result.reason == speechsdk.ResultReason.RecognizedSpeech:
            row = {
                "timestr":
                time.strftime(
                    '%H:%M:%S',
                    time.gmtime(math.floor(result.offset * time_unit))),
                "seconds":
                math.floor(result.offset * time_unit),
                "text":
                result.text,
                "key_phrases":
                Text2KeyPhrases(ta_client, [result.text]),
            }
            #print("Recognized: {}".format(result.text))
            results.append(row)
        elif result.reason == speechsdk.ResultReason.NoMatch:
            #print("No speech could be recognized: {}".format(result.no_match_details))
            break
        elif result.reason == speechsdk.ResultReason.Canceled:
            #cancellation_details = result.cancellation_details
            #print("Speech Recognition canceled: {}".format(cancellation_details.reason))
            #if cancellation_details.reason == speechsdk.CancellationReason.Error:
            #    print("Error details: {}".format(cancellation_details.error_details))
            break

    return results, video.duration
    def analyze_healthcare(self):
        # [START analyze_healthcare]
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.textanalytics 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),
            api_version="v3.1-preview.3")

        documents = ["Subject is taking 100mg of ibuprofen twice daily"]

        poller = text_analytics_client.begin_analyze_healthcare(
            documents, show_stats=True)
        result = poller.result()

        docs = [doc for doc in result if not doc.is_error]

        print("Results of Healthcare Analysis:")
        for idx, doc in enumerate(docs):
            for entity in doc.entities:
                print("Entity: {}".format(entity.text))
                print("...Category: {}".format(entity.category))
                print("...Subcategory: {}".format(entity.subcategory))
                print("...Offset: {}".format(entity.offset))
                print("...Confidence score: {}".format(
                    entity.confidence_score))
                if entity.links is not None:
                    print("...Links:")
                    for link in entity.links:
                        print("......ID: {}".format(link.id))
                        print("......Data source: {}".format(link.data_source))
            for relation in doc.relations:
                print("Relation:")
                print("...Source: {}".format(relation.source.text))
                print("...Target: {}".format(relation.target.text))
                print("...Type: {}".format(relation.relation_type))
                print("...Bidirectional: {}".format(relation.is_bidirectional))
            print("------------------------------------------")
    def analyze_sentiment(self):
        # [START batch_analyze_sentiment]
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.textanalytics 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))
        documents = [
            "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."
        ]

        result = text_analytics_client.analyze_sentiment(documents)
        docs = [doc for doc in result if not doc.is_error]

        for idx, doc in enumerate(docs):
            print("Document text: {}".format(documents[idx]))
            print("Overall sentiment: {}".format(doc.sentiment))
            # [END batch_analyze_sentiment]
            print(
                "Overall confidence scores: positive={}; neutral={}; negative={} \n"
                .format(
                    doc.confidence_scores.positive,
                    doc.confidence_scores.neutral,
                    doc.confidence_scores.negative,
                ))
            for sentence in doc.sentences:
                print("Sentence '{}' has sentiment: {}".format(
                    sentence.text, sentence.sentiment))
                print(
                    "Sentence confidence scores: positive={}; neutral={}; negative={}"
                    .format(
                        sentence.confidence_scores.positive,
                        sentence.confidence_scores.neutral,
                        sentence.confidence_scores.negative,
                    ))
            print("------------------------------------")
예제 #9
0
    def alternative_scenario_recognize_pii_entities(self):
        """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
        using a list[TextDocumentInput] and supplying your own IDs and language hints along
        with the text.
        """
        from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsAPIKeyCredential
        text_analytics_client = TextAnalyticsClient(
            endpoint=self.endpoint,
            credential=TextAnalyticsAPIKeyCredential(self.key))

        documents = [{
            "id": "0",
            "language": "en",
            "text": "The employee's SSN is 555-55-5555."
        }, {
            "id":
            "1",
            "language":
            "en",
            "text":
            "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check."
        }, {
            "id": "2",
            "language": "en",
            "text": "Is 998.214.865-68 your Brazilian CPF number?"
        }]

        extras = []

        def callback(resp):
            extras.append(resp.statistics)
            extras.append(resp.model_version)
            extras.append(resp.raw_response)

        result = text_analytics_client.recognize_pii_entities(
            documents,
            show_stats=True,
            model_version="latest",
            response_hook=callback)
예제 #10
0
    def analyze_healthcare_entities_with_cancellation(self):
        # [START analyze_healthcare_entities_with_cancellation]
        from azure.core.credentials import AzureKeyCredential
        from azure.ai.textanalytics 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),
        )

        documents = [
            "RECORD #333582770390100 | MH | 85986313 | | 054351 | 2/14/2001 12:00:00 AM | \
            CORONARY ARTERY DISEASE | Signed | DIS | Admission Date: 5/22/2001 \
            Report Status: Signed Discharge Date: 4/24/2001 ADMISSION DIAGNOSIS: \
            CORONARY ARTERY DISEASE. HISTORY OF PRESENT ILLNESS: \
            The patient is a 54-year-old gentleman with a history of progressive angina over the past several months. \
            The patient had a cardiac catheterization in July of this year revealing total occlusion of the RCA and \
            50% left main disease , with a strong family history of coronary artery disease with a brother dying at \
            the age of 52 from a myocardial infarction and another brother who is status post coronary artery bypass grafting. \
            The patient had a stress echocardiogram done on July , 2001 , which showed no wall motion abnormalities ,\
            but this was a difficult study due to body habitus. The patient went for six minutes with minimal ST depressions \
            in the anterior lateral leads , thought due to fatigue and wrist pain , his anginal equivalent. Due to the patient's \
            increased symptoms and family history and history left main disease with total occasional of his RCA was referred \
            for revascularization with open heart surgery."
        ]

        poller = text_analytics_client.begin_analyze_healthcare_entities(
            documents)

        try:
            cancellation_poller = poller.cancel()
            cancellation_poller.wait()

        except HttpResponseError as e:
            # If the operation has already reached a terminal state it cannot be cancelled.
            print(e)

        else:
            print("Healthcare entities analysis was successfully cancelled.")
    def test_duplicate_ids_error(self, resource_group, location,
                                 text_analytics_account,
                                 text_analytics_account_key):
        text_analytics = TextAnalyticsClient(
            text_analytics_account,
            TextAnalyticsApiKeyCredential(text_analytics_account_key))

        # Duplicate Ids
        docs = [{
            "id": "1",
            "text": "hello world"
        }, {
            "id": "1",
            "text": "I did not like the hotel we stayed at."
        }]
        try:
            result = text_analytics.detect_language(docs)
        except HttpResponseError as err:
            self.assertEqual(err.error.code, "InvalidDocument")
            self.assertIsNotNone(err.error.message)
예제 #12
0
    def recognize_entities(self):
        # [START batch_recognize_entities]
        from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsAPIKeyCredential
        text_analytics_client = TextAnalyticsClient(
            endpoint=self.endpoint,
            credential=TextAnalyticsAPIKeyCredential(self.key))
        documents = [
            "Microsoft was founded by Bill Gates and Paul Allen.",
            "I had a wonderful trip to Seattle last week.",
            "I visited the Space Needle 2 times.",
        ]

        result = text_analytics_client.recognize_entities(documents)
        docs = [doc for doc in result if not doc.is_error]

        for idx, doc in enumerate(docs):
            print("\nDocument text: {}".format(documents[idx]))
            for entity in doc.entities:
                print("Entity: \t", entity.text, "\tType: \t", entity.type,
                      "\tConfidence Score: \t", round(entity.score, 3))
예제 #13
0
    def test_input_with_some_errors(self, resource_group, location,
                                    text_analytics_account,
                                    text_analytics_account_key):
        text_analytics = TextAnalyticsClient(
            text_analytics_account,
            TextAnalyticsApiKeyCredential(text_analytics_account_key))

        docs = [{
            "id": "1",
            "language": "English",
            "text": "Microsoft was founded by Bill Gates and Paul Allen"
        }, {
            "id": "2",
            "language": "es",
            "text": "Microsoft fue fundado por Bill Gates y Paul Allen"
        }]

        response = text_analytics.extract_key_phrases(docs)
        self.assertTrue(response[0].is_error)
        self.assertFalse(response[1].is_error)
    def recognize_pii_entities(self):
        # [START batch_recognize_pii_entities]
        from azure.ai.textanalytics import TextAnalyticsClient
        text_analytics_client = TextAnalyticsClient(endpoint=self.endpoint,
                                                    credential=self.key)
        documents = [
            "The employee's SSN is 555-55-5555.",
            "Your ABA number - 111000025 - is the first 9 digits in the lower left hand corner of your personal check.",
            "Is 998.214.865-68 your Brazilian CPF number?"
        ]

        result = text_analytics_client.recognize_pii_entities(documents)
        docs = [doc for doc in result if not doc.is_error]

        for idx, doc in enumerate(docs):
            print("Document text: {}".format(documents[idx]))
            for entity in doc.entities:
                print("Entity: {}".format(entity.text))
                print("Type: {}".format(entity.type))
                print("Confidence Score: {}\n".format(entity.score))
예제 #15
0
    def test_whole_batch_dont_use_country_hint(self, resource_group, location,
                                               cognitiveservices_account,
                                               cognitiveservices_account_key):
        text_analytics = TextAnalyticsClient(cognitiveservices_account,
                                             cognitiveservices_account_key)

        def callback(resp):
            country_str = "\"countryHint\": \"\""
            country = resp.http_request.body.count(country_str)
            self.assertEqual(country, 3)

        docs = [
            u"This was the best day of my life.",
            u"I did not like the hotel we stayed it. It was too expensive.",
            u"The restaurant was not as good as I hoped."
        ]

        response = text_analytics.detect_languages(docs,
                                                   country_hint="",
                                                   response_hook=callback)
예제 #16
0
    def test_client_passed_default_country_hint(self, resource_group, location, text_analytics_account, text_analytics_account_key):
        text_analytics = TextAnalyticsClient(text_analytics_account, AzureKeyCredential(text_analytics_account_key), default_country_hint="CA")

        def callback(resp):
            country_str = "\"countryHint\": \"CA\""
            country = resp.http_request.body.count(country_str)
            self.assertEqual(country, 3)

        def callback_2(resp):
            country_str = "\"countryHint\": \"DE\""
            country = resp.http_request.body.count(country_str)
            self.assertEqual(country, 3)

        docs = [{"id": "1", "text": "I will go to the park."},
                {"id": "2", "text": "I did not like the hotel we stayed at."},
                {"id": "3", "text": "The restaurant had really good food."}]

        response = text_analytics.detect_language(docs, raw_response_hook=callback)
        response = text_analytics.detect_language(docs, country_hint="DE", raw_response_hook=callback_2)
        response = text_analytics.detect_language(docs, raw_response_hook=callback)
    def test_active_directory_auth(self):
        token = self.generate_oauth_token()
        endpoint = self.get_oauth_endpoint()
        text_analytics = TextAnalyticsClient(endpoint, token)

        docs = [{
            "id": "1",
            "text": "I should take my cat to the veterinarian."
        }, {
            "id": "2",
            "text": "Este es un document escrito en Español."
        }, {
            "id": "3",
            "text": "猫は幸せ"
        }, {
            "id": "4",
            "text": "Fahrt nach Stuttgart und dann zum Hotel zu Fu."
        }]

        response = text_analytics.detect_language(docs)
예제 #18
0
    def test_whole_batch_dont_use_language_hint(self, resource_group, location,
                                                text_analytics_account,
                                                text_analytics_account_key):
        text_analytics = TextAnalyticsClient(
            text_analytics_account,
            TextAnalyticsApiKeyCredential(text_analytics_account_key))

        def callback(resp):
            language_str = "\"language\": \"\""
            language = resp.http_request.body.count(language_str)
            self.assertEqual(language, 3)

        docs = [
            u"This was the best day of my life.",
            u"I did not like the hotel we stayed at. It was too expensive.",
            u"The restaurant was not as good as I hoped."
        ]

        response = text_analytics.recognize_pii_entities(
            docs, language="", raw_response_hook=callback)
예제 #19
0
    def authentication_with_azure_active_directory(self):
        """DefaultAzureCredential will use the values from the environment
        variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
        """
        # [START create_ta_client_with_aad]
        from azure.ai.textanalytics import TextAnalyticsClient
        from azure.identity import DefaultAzureCredential

        endpoint = os.getenv("AZURE_TEXT_ANALYTICS_ENDPOINT")
        credential = DefaultAzureCredential()

        text_analytics_client = TextAnalyticsClient(endpoint,
                                                    credential=credential)
        # [END create_ta_client_with_aad]

        doc = ["I need to take my cat to the veterinarian."]
        result = text_analytics_client.detect_languages(doc)

        print("Language detected: {}".format(result[0].primary_language.name))
        print("Confidence score: {}".format(result[0].primary_language.score))
예제 #20
0
    def test_client_passed_default_language_hint(self, resource_group, location, text_analytics_account, text_analytics_account_key):
        text_analytics = TextAnalyticsClient(text_analytics_account, TextAnalyticsApiKeyCredential(text_analytics_account_key), default_language="es")

        def callback(resp):
            language_str = "\"language\": \"es\""
            language = resp.http_request.body.count(language_str)
            self.assertEqual(language, 3)

        def callback_2(resp):
            language_str = "\"language\": \"en\""
            language = resp.http_request.body.count(language_str)
            self.assertEqual(language, 3)

        docs = [{"id": "1", "text": "I will go to the park."},
                {"id": "2", "text": "I did not like the hotel we stayed at."},
                {"id": "3", "text": "The restaurant had really good food."}]

        response = text_analytics.analyze_sentiment(docs, raw_response_hook=callback)
        response = text_analytics.analyze_sentiment(docs, language="en", raw_response_hook=callback_2)
        response = text_analytics.analyze_sentiment(docs, raw_response_hook=callback)
예제 #21
0
    def test_whole_batch_language_hint(self, resource_group, location,
                                       cognitiveservices_account,
                                       cognitiveservices_account_key):
        text_analytics = TextAnalyticsClient(cognitiveservices_account,
                                             cognitiveservices_account_key)

        def callback(resp):
            language_str = "\"language\": \"fr\""
            language = resp.http_request.body.count(language_str)
            self.assertEqual(language, 3)

        docs = [
            u"This was the best day of my life.",
            u"I did not like the hotel we stayed it. It was too expensive.",
            u"The restaurant was not as good as I hoped."
        ]

        response = text_analytics.analyze_sentiment(docs,
                                                    language="fr",
                                                    response_hook=callback)
    def test_all_successful_passing_dict(self, resource_group, location,
                                         text_analytics_account,
                                         text_analytics_account_key):
        text_analytics = TextAnalyticsClient(
            text_analytics_account,
            TextAnalyticsApiKeyCredential(text_analytics_account_key))

        docs = [{
            "id":
            "1",
            "language":
            "en",
            "text":
            "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975."
        }, {
            "id":
            "2",
            "language":
            "es",
            "text":
            "Microsoft fue fundado por Bill Gates y Paul Allen el 4 de abril de 1975."
        }, {
            "id":
            "3",
            "language":
            "de",
            "text":
            "Microsoft wurde am 4. April 1975 von Bill Gates und Paul Allen gegründet."
        }]

        response = text_analytics.recognize_entities(docs, show_stats=True)
        for doc in response:
            self.assertEqual(len(doc.entities), 4)
            self.assertIsNotNone(doc.id)
            self.assertIsNotNone(doc.statistics)
            for entity in doc.entities:
                self.assertIsNotNone(entity.text)
                self.assertIsNotNone(entity.category)
                self.assertIsNotNone(entity.grapheme_offset)
                self.assertIsNotNone(entity.grapheme_length)
                self.assertIsNotNone(entity.score)
    def alternative_scenario_extract_key_phrases(self):
        """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
        using a list[TextDocumentInput] and supplying your own IDs and language hints along
        with the text.
        """
        from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsApiKeyCredential
        text_analytics_client = TextAnalyticsClient(
            endpoint=self.endpoint,
            credential=TextAnalyticsApiKeyCredential(self.key))

        documents = [{
            "id":
            "0",
            "language":
            "en",
            "text":
            "Redmond is a city in King County, Washington, United States, located 15 miles east of Seattle."
        }, {
            "id": "1",
            "language": "en",
            "text": "I need to take my cat to the veterinarian."
        }, {
            "id": "2",
            "language": "en",
            "text": "I will travel to South America in the summer."
        }]
        extras = []

        def callback(resp):
            extras.append(resp.statistics)
            extras.append(resp.model_version)
            extras.append(resp.raw_response)

        result = text_analytics_client.extract_key_phrases(
            documents,
            show_stats=True,
            model_version="latest",
            response_hook=callback)
예제 #24
0
def sample_authentication_with_api_key_credential():
    print("\n.. authentication_with_api_key_credential")
    # [START create_ta_client_with_key]
    from azure.core.credentials import AzureKeyCredential
    from azure.ai.textanalytics import TextAnalyticsClient
    endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"]
    key = os.environ["AZURE_TEXT_ANALYTICS_KEY"]

    text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key))
    # [END create_ta_client_with_key]

    doc = [
        """
        I need to take my cat to the veterinarian. She's been coughing for a while and I thought it was just a hairball,
        but now I'm now worried it might be something else. She's still very healthy so I'm not too worried though.
        """
    ]
    result = text_analytics_client.detect_language(doc)

    print("Language detected: {}".format(result[0].primary_language.name))
    print("Confidence score: {}".format(result[0].primary_language.confidence_score))
예제 #25
0
    def test_document_attribute_error_no_result_attribute(self, resource_group, location, text_analytics_account, text_analytics_account_key):
        text_analytics = TextAnalyticsClient(text_analytics_account, AzureKeyCredential(text_analytics_account_key))

        docs = [{"id": "1", "text": ""}]
        response = text_analytics.detect_language(docs)

        # Attributes on DocumentError
        self.assertTrue(response[0].is_error)
        self.assertEqual(response[0].id, "1")
        self.assertIsNotNone(response[0].error)

        # Result attribute not on DocumentError, custom error message
        try:
            primary_language = response[0].primary_language
        except AttributeError as custom_error:
            self.assertEqual(
                custom_error.args[0],
                '\'DocumentError\' object has no attribute \'primary_language\'. '
                'The service was unable to process this document:\nDocument Id: 1\nError: '
                'invalidDocument - Document text is empty.\n'
            )
    def test_context_manager(self, textanalytics_test_endpoint,
                             textanalytics_test_api_key):
        transport = mock.MagicMock()
        client = TextAnalyticsClient(
            textanalytics_test_endpoint,
            AzureKeyCredential(textanalytics_test_api_key),
            transport=transport)

        with client:
            assert transport.__enter__.call_count == 1
        assert transport.__enter__.call_count == 1
        assert transport.__exit__.call_count == 1
예제 #27
0
    def test_context_manager(self, resource_group, location, text_analytics_account, text_analytics_account_key):
        transport = mock.MagicMock()
        client = TextAnalyticsClient(
            text_analytics_account,
            AzureKeyCredential(text_analytics_account_key),
            transport=transport
        )

        with client:
            assert transport.__enter__.call_count == 1
        assert transport.__enter__.call_count == 1
        assert transport.__exit__.call_count == 1
예제 #28
0
파일: azure.py 프로젝트: jhk523/news-trends
def load_client():
    # pip install azure-ai-textanalytics
    from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsApiKeyCredential
    global _CLIENT
    if _CLIENT is None:
        path = os.path.join(_ROOT_DIR, 'data/azure_info.json')
        azure_info = json.loads(open(path).read())
        key = azure_info['key']
        endpoint = azure_info['endpoint']
        _CLIENT = TextAnalyticsClient(endpoint,
                                      TextAnalyticsApiKeyCredential(key))
    return _CLIENT
예제 #29
0
    def test_validate_language_input(self, resource_group, location,
                                     cognitiveservices_account,
                                     cognitiveservices_account_key):
        text_analytics = TextAnalyticsClient(cognitiveservices_account,
                                             cognitiveservices_account_key)

        docs = [
            DetectLanguageInput(
                id="1", text="I should take my cat to the veterinarian."),
            DetectLanguageInput(
                id="2", text="Este es un document escrito en Español."),
            DetectLanguageInput(id="3", text="猫は幸せ"),
            DetectLanguageInput(
                id="4", text="Fahrt nach Stuttgart und dann zum Hotel zu Fu.")
        ]

        response = text_analytics.detect_languages(docs)
        self.assertEqual(response[0].detected_languages[0].name, "English")
        self.assertEqual(response[1].detected_languages[0].name, "Spanish")
        self.assertEqual(response[2].detected_languages[0].name, "Japanese")
        self.assertEqual(response[3].detected_languages[0].name, "German")
예제 #30
0
    def analyze_sentiment(self):
        # [START batch_analyze_sentiment]
        from azure.ai.textanalytics import TextAnalyticsClient, TextAnalyticsApiKeyCredential
        text_analytics_client = TextAnalyticsClient(
            endpoint=self.endpoint,
            credential=TextAnalyticsApiKeyCredential(self.key))
        documents = [
            "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."
        ]

        result = text_analytics_client.analyze_sentiment(documents)
        docs = [doc for doc in result if not doc.is_error]

        for idx, doc in enumerate(docs):
            print("Document text: {}".format(documents[idx]))
            print("Overall sentiment: {}".format(doc.sentiment))
            # [END batch_analyze_sentiment]
            print(
                "Overall confidence scores: positive={0:.3f}; neutral={1:.3f}; negative={2:.3f} \n"
                .format(
                    doc.confidence_scores.positive,
                    doc.confidence_scores.neutral,
                    doc.confidence_scores.negative,
                ))
            for idx, sentence in enumerate(doc.sentences):
                print("Sentence {} sentiment: {}".format(
                    idx + 1, sentence.sentiment))
                print(
                    "Sentence confidence scores: positive={0:.3f}; neutral={1:.3f}; negative={2:.3f}"
                    .format(
                        sentence.confidence_scores.positive,
                        sentence.confidence_scores.neutral,
                        sentence.confidence_scores.negative,
                    ))
                print("Offset: {}".format(sentence.offset))
                print("Length: {}\n".format(sentence.length))
            print("------------------------------------")