def test_single_recognize_linked_entities_empty_text_input(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(HttpResponseError): response = single_recognize_linked_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="", )
def test_single_recognize_linked_entities_non_text_input(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(TypeError): response = single_recognize_linked_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text={"id": "1", "text": "hello world"} )
def test_single_recognize_linked_entities_none_credentials(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(ValueError): response = single_recognize_linked_entities( endpoint=cognitiveservices_account, credential=None, input_text="Microsoft was founded by Bill Gates.", )
def test_single_recognize_linked_entities_non_text_input(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(TypeError): response = single_recognize_linked_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text={"id": "1", "text": "hello world"} )
def test_single_recognize_linked_entities_empty_text_input(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(HttpResponseError): response = single_recognize_linked_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text="", )
def test_single_recognize_linked_entities_bad_type_credentials(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(TypeError): response = single_recognize_linked_entities( endpoint=text_analytics_account, credential=[], input_text="Microsoft was founded by Bill Gates.", )
def test_single_recognize_linked_entities_bad_credentials(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(ClientAuthenticationError): response = single_recognize_linked_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential("xxxxxxxxxxxx"), input_text="Microsoft was founded by Bill Gates.", )
def test_single_recognize_linked_entities_bad_language_hint(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(HttpResponseError): response = single_recognize_linked_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="Microsoft was founded by Bill Gates.", language="English" )
def test_single_recognize_linked_entities_bad_model_version(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(HttpResponseError): response = single_recognize_linked_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text="Microsoft was founded by Bill Gates.", language="en", model_version="old" )
def test_single_recognize_linked_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_linked_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text=text, )
def test_single_recognize_linked_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_linked_entities( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text=text, )
def test_single_recognize_linked_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_linked_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_linked_entities(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): response = single_recognize_linked_entities( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="Microsoft was founded by Bill Gates.", language="en" ) self.assertEqual(response.entities[0].name, "Bill Gates") self.assertEqual(response.entities[1].name, "Microsoft") for entity in response.entities: self.assertIsNotNone(entity.matches) self.assertIsNotNone(entity.language) self.assertIsNotNone(entity.id) self.assertIsNotNone(entity.url) self.assertIsNotNone(entity.data_source)
def recognize_linked_entities(self): # [START single_recognize_linked_entities] from azure.ai.textanalytics import single_recognize_linked_entities, TextAnalyticsApiKeyCredential text = "Easter Island, a Chilean territory, is a remote volcanic island in Polynesia. " \ "Its native name is Rapa Nui." result = single_recognize_linked_entities( endpoint=self.endpoint, credential=TextAnalyticsApiKeyCredential(self.key), input_text=text, language="en") for entity in result.entities: print("Entity: {}".format(entity.name)) print("Url: {}".format(entity.url)) print("Data Source: {}\n".format(entity.data_source)) print("Where this entity appears in the text:") for idx, match in enumerate(entity.matches): print("Match {}: {}".format(idx + 1, match.text)) print("Score: {0:.3f}".format(match.score)) print("Offset: {}".format(match.offset)) print("Length: {}\n".format(match.length))