def test_single_recognize_entities_non_text_input(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(TypeError): response = single_recognize_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text={"id": "1", "text": "hello world"} )
def test_single_recognize_entities_non_text_input(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(TypeError): response = single_recognize_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text={"id": "1", "text": "hello world"} )
def test_single_recognize_entities_empty_text_input(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(HttpResponseError): response = single_recognize_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="", )
def test_single_recognize_entities_none_credentials(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(ValueError): response = single_recognize_entities( endpoint=cognitiveservices_account, credential=None, input_text="Microsoft was founded by Bill Gates.", )
def test_single_recognize_entities_empty_text_input(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(HttpResponseError): response = single_recognize_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text="", )
def test_single_recognize_entities_bad_type_credentials(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(TypeError): response = single_recognize_entities( endpoint=text_analytics_account, credential=[], input_text="Microsoft was founded by Bill Gates.", )
def test_single_recognize_entities_bad_credentials(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(ClientAuthenticationError): response = single_recognize_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential("xxxxxxxxxxxx"), input_text="Microsoft was founded by Bill Gates.", )
def test_single_recognize_entities_bad_language_hint(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(HttpResponseError): response = single_recognize_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="Microsoft was founded by Bill Gates.", language="English" )
def test_single_recognize_entities_bad_language_hint(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(HttpResponseError): response = single_recognize_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text="Microsoft was founded by Bill Gates.", language="English" )
def test_single_recognize_entities_bad_model_version( self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(HttpResponseError): response = single_recognize_entities( endpoint=text_analytics_account, credential=text_analytics_account_key, input_text="Microsoft was founded by Bill Gates.", language="en", model_version="old")
def test_single_recognize_entities_too_many_chars(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): text = "" for _ in range(5121): text += "x" with self.assertRaises(HttpResponseError): response = single_recognize_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text=text, )
def test_single_recognize_entities_too_many_chars(self, resource_group, location, text_analytics_account, text_analytics_account_key): text = "" for _ in range(5121): text += "x" with self.assertRaises(HttpResponseError): response = single_recognize_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text=text, )
def test_single_recognize_entities_response_hook(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): def callback(resp): self.assertIsNotNone(resp.statistics) self.assertIsNotNone(resp.model_version) response = single_recognize_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="Microsoft was founded by Bill Gates.", show_stats=True, model_version="latest", response_hook=callback )
def test_successful_single_recognize_entities(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): response = single_recognize_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="Microsoft was founded by Bill Gates.", language="en" ) self.assertEqual(response.entities[0].text, "Microsoft") self.assertEqual(response.entities[1].text, "Bill Gates") for entity in response.entities: self.assertIsNotNone(entity.type) self.assertIsNotNone(entity.offset) self.assertIsNotNone(entity.length) self.assertIsNotNone(entity.score)
def recognize_entities(self): # [START single_recognize_entities] from azure.ai.textanalytics import single_recognize_entities text = "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975," \ " to develop and sell BASIC interpreters for the Altair 8800." result = single_recognize_entities(endpoint=self.endpoint, credential=self.key, input_text=text, language="en") for entity in result.entities: print("Entity: {}".format(entity.text)) print("Type: {}".format(entity.type)) print("Confidence Score: {0:.3f}\n".format(entity.score))