示例#1
0
 def test_mixing_inputs(self, client):
     docs = [
         {"id": "1", "text": "Microsoft was founded by Bill Gates and Paul Allen."},
         DetectLanguageInput(id="2", text="I did not like the hotel we stayed at. It was too expensive."),
         "You cannot mix string input with the above inputs"
     ]
     with pytest.raises(TypeError):
         response = client.detect_language(docs)
    def test_whole_batch_country_hint_and_obj_input(self, client):
        def callback(resp):
            country_str = "\"countryHint\": \"CA\""
            country = resp.http_request.body.count(country_str)
            self.assertEqual(country, 3)

        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="猫は幸せ"),
        ]

        response = client.detect_language(docs,
                                          country_hint="CA",
                                          raw_response_hook=callback)
示例#3
0
    def test_whole_batch_country_hint_and_obj_per_item_hints(self, resource_group, location, text_analytics_account, text_analytics_account_key):
        text_analytics = TextAnalyticsClient(text_analytics_account, AzureKeyCredential(text_analytics_account_key))

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

        docs = [
            DetectLanguageInput(id="1", text="I should take my cat to the veterinarian.", country_hint="CA"),
            DetectLanguageInput(id="4", text="Este es un document escrito en Español.", country_hint="CA"),
            DetectLanguageInput(id="3", text="猫は幸せ"),
        ]

        response = text_analytics.detect_language(docs, country_hint="US", raw_response_hook=callback)
    def test_all_successful_passing_text_document_input(self, client):
        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 = client.detect_language(docs)

        self.assertEqual(response[0].primary_language.name, "English")
        # self.assertEqual(response[1].primary_language.name, "Spanish")  # https://msazure.visualstudio.com/Cognitive%20Services/_workitems/edit/10363878
        self.assertEqual(response[2].primary_language.name, "Japanese")
        self.assertEqual(response[3].primary_language.name, "German")
        self.assertEqual(response[0].primary_language.iso6391_name, "en")
        # self.assertEqual(response[1].primary_language.iso6391_name, "es") # https://msazure.visualstudio.com/Cognitive%20Services/_workitems/edit/10363878
        self.assertEqual(response[2].primary_language.iso6391_name, "ja")
        self.assertEqual(response[3].primary_language.iso6391_name, "de")

        for doc in response:
            self.assertIsNotNone(doc.primary_language.confidence_score)
示例#5
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")
示例#6
0
    async def test_all_successful_passing_text_document_input(self, client):
        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 = await client.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.assertEqual(response[0].primary_language.iso6391_name, "en")
        self.assertEqual(response[1].primary_language.iso6391_name, "es")
        self.assertEqual(response[2].primary_language.iso6391_name, "ja")
        self.assertEqual(response[3].primary_language.iso6391_name, "de")

        for doc in response:
            self.assertIsNotNone(doc.primary_language.confidence_score)
    async def test_whole_batch_country_hint_and_obj_input_async(
            self, resource_group, location, text_analytics_account,
            text_analytics_account_key):
        text_analytics = TextAnalyticsClient(text_analytics_account,
                                             text_analytics_account_key)

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

        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="猫は幸せ"),
        ]

        response = await text_analytics.detect_languages(
            docs, country_hint="CA", response_hook=callback)
示例#8
0
    async def test_whole_batch_country_hint_and_obj_per_item_hints(
            self, client):
        def callback(resp):
            country_str = "\"countryHint\": \"CA\""
            country = resp.http_request.body.count(country_str)
            assert country == 2
            country_str = "\"countryHint\": \"US\""
            country = resp.http_request.body.count(country_str)
            assert country == 1

        docs = [
            DetectLanguageInput(
                id="1",
                text="I should take my cat to the veterinarian.",
                country_hint="CA"),
            DetectLanguageInput(id="4",
                                text="Este es un document escrito en Español.",
                                country_hint="CA"),
            DetectLanguageInput(id="3", text="猫は幸せ"),
        ]

        response = await client.detect_language(docs,
                                                country_hint="US",
                                                raw_response_hook=callback)
    def test_country_hint_none(self, resource_group, location,
                               text_analytics_account,
                               text_analytics_account_key):
        text_analytics = TextAnalyticsClient(
            text_analytics_account,
            TextAnalyticsApiKeyCredential(text_analytics_account_key))

        # service will eventually support this and we will not need to send "" for input == "none"
        documents = [{
            "id": "0",
            "country_hint": "none",
            "text": "This is written in English."
        }]
        documents2 = [
            DetectLanguageInput(id="1",
                                country_hint="none",
                                text="This is written in English.")
        ]

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

        # test dict
        result = text_analytics.detect_language(documents,
                                                raw_response_hook=callback)
        # test DetectLanguageInput
        result2 = text_analytics.detect_language(documents2,
                                                 raw_response_hook=callback)
        # test per-operation
        result3 = text_analytics.detect_language(
            documents=["this is written in english"],
            country_hint="none",
            raw_response_hook=callback)
        # test client default
        new_client = TextAnalyticsClient(
            text_analytics_account,
            TextAnalyticsApiKeyCredential(text_analytics_account_key),
            default_country_hint="none")
        result4 = new_client.detect_language(
            documents=["this is written in english"],
            raw_response_hook=callback)
示例#10
0
    async def test_country_hint_none(self, textanalytics_test_endpoint,
                                     textanalytics_test_api_key):
        client = TextAnalyticsClient(
            textanalytics_test_endpoint,
            AzureKeyCredential(textanalytics_test_api_key))
        # service will eventually support this and we will not need to send "" for input == "none"
        documents = [{
            "id": "0",
            "country_hint": "none",
            "text": "This is written in English."
        }]
        documents2 = [
            DetectLanguageInput(id="1",
                                country_hint="none",
                                text="This is written in English.")
        ]

        def callback(response):
            country_str = "\"countryHint\": \"\""
            country = response.http_request.body.count(country_str)
            assert country == 1

        # test dict
        result = await client.detect_language(documents,
                                              raw_response_hook=callback)
        # test DetectLanguageInput
        result2 = await client.detect_language(documents2,
                                               raw_response_hook=callback)
        # test per-operation
        result3 = await client.detect_language(
            documents=["this is written in english"],
            country_hint="none",
            raw_response_hook=callback)
        # test client default
        new_client = TextAnalyticsClient(
            textanalytics_test_endpoint,
            AzureKeyCredential(textanalytics_test_api_key),
            default_country_hint="none")
        result4 = await new_client.detect_language(
            documents=["this is written in english"],
            raw_response_hook=callback)