def test_single_extract_key_phrases_non_text_input(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(TypeError): response = single_extract_key_phrases( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text={"id": "1", "text": "hello world"} )
def test_single_extract_key_phrases_empty_text_input(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(HttpResponseError): response = single_extract_key_phrases( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="", )
def test_single_extract_key_phrases_none_credentials(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(ValueError): response = single_extract_key_phrases( endpoint=cognitiveservices_account, credential=None, input_text="Microsoft was founded by Bill Gates.", )
def test_single_extract_key_phrases_bad_credentials(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(ClientAuthenticationError): response = single_extract_key_phrases( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential("xxxxxxxxxxxx"), input_text="Microsoft was founded by Bill Gates.", )
def test_single_extract_key_phrases_empty_text_input(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(HttpResponseError): response = single_extract_key_phrases( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text="", )
def test_single_extract_key_phrases_bad_type_credentials(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(TypeError): response = single_extract_key_phrases( endpoint=text_analytics_account, credential=[], input_text="Microsoft was founded by Bill Gates.", )
def test_single_extract_key_phrases_non_text_input(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(TypeError): response = single_extract_key_phrases( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text={"id": "1", "text": "hello world"} )
def test_single_extract_key_phrases_bad_language_hint(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): with self.assertRaises(HttpResponseError): response = single_extract_key_phrases( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="Microsoft was founded by Bill Gates.", language="English" )
def test_single_extract_key_phrases_bad_language_hint(self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(HttpResponseError): response = single_extract_key_phrases( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text="Microsoft was founded by Bill Gates.", language="English" )
def test_single_extract_key_phrases_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_extract_key_phrases( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text=text, )
def test_successful_single_extract_key_phrases(self, resource_group, location, cognitiveservices_account, cognitiveservices_account_key): response = single_extract_key_phrases( endpoint=cognitiveservices_account, credential=cognitiveservices_account_key, input_text="Microsoft was founded by Bill Gates.", language="en" ) self.assertIn("Microsoft", response.key_phrases) self.assertIn("Bill Gates", response.key_phrases)
def test_single_extract_key_phrases_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_extract_key_phrases( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text=text, )
def test_single_extract_key_phrases_bad_model_version( self, resource_group, location, text_analytics_account, text_analytics_account_key): with self.assertRaises(HttpResponseError): response = single_extract_key_phrases( endpoint=text_analytics_account, credential=text_analytics_account_key, input_text="Microsoft was founded by Bill Gates.", language="en", model_version="old")
def test_successful_single_extract_key_phrases(self, resource_group, location, text_analytics_account, text_analytics_account_key): response = single_extract_key_phrases( endpoint=text_analytics_account, credential=TextAnalyticsAPIKeyCredential(text_analytics_account_key), input_text="Microsoft was founded by Bill Gates.", language="en" ) self.assertIn("Microsoft", response.key_phrases) self.assertIn("Bill Gates", response.key_phrases)
def test_single_extract_key_phrases_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_extract_key_phrases( 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 extract_key_phrases(self): # [START single_extract_key_phrases] from azure.ai.textanalytics import single_extract_key_phrases, TextAnalyticsApiKeyCredential text = "Redmond is a city in King County, Washington, United States, located 15 miles east of Seattle." result = single_extract_key_phrases( endpoint=self.endpoint, credential=TextAnalyticsApiKeyCredential(self.key), input_text=text, language="en") print("Key phrases found:\n") for phrase in result.key_phrases: print(phrase)
def key_phrase_extraction(endpoint, key): try: document = text_str response = single_extract_key_phrases(endpoint=endpoint, credential=key, input_text= document) key_phrases = [] if not response.is_error: # print("\tKey Phrases:") for phrase in response.key_phrases: key_phrases.append(phrase) # print("\t\t", phrase) return key_phrases else: print(response.id, response.error) except Exception as err: print("Encountered exception. {}".format(err))