44from azure .cognitiveservices .language .textanalytics import TextAnalyticsClient
55from msrest .authentication import CognitiveServicesCredentials
66
7- SUBSCRIPTION_KEY_ENV_NAME = "TEXTANALYTICS_SUBSCRIPTION_KEY"
8- TEXTANALYTICS_LOCATION = os .environ .get (
9- "TEXTANALYTICS_LOCATION" , "westcentralus" )
7+ key_var_name = 'TEXT_ANALYTICS_SUBSCRIPTION_KEY'
8+ if not key_var_name in os .environ :
9+ raise Exception ('Please set/export the environment variable: {}' .format (key_var_name ))
10+ subscription_key = os .environ [key_var_name ]
1011
12+ endpoint_var_name = 'TEXT_ANALYTICS_ENDPOINT'
13+ if not endpoint_var_name in os .environ :
14+ raise Exception ('Please set/export the environment variable: {}' .format (endpoint_var_name ))
15+ endpoint = os .environ [endpoint_var_name ]
1116
1217def language_extraction (subscription_key ):
1318 """Language extraction.
1419
1520 This example detects the language of several strings.
1621 """
1722 credentials = CognitiveServicesCredentials (subscription_key )
18- text_analytics_url = "https://{}.api.cognitive.microsoft.com" .format (
19- TEXTANALYTICS_LOCATION )
2023 text_analytics = TextAnalyticsClient (
21- endpoint = text_analytics_url , credentials = credentials )
24+ endpoint = endpoint , credentials = credentials )
2225
2326 try :
2427 documents = [
@@ -42,10 +45,8 @@ def key_phrases(subscription_key):
4245 Returns the key talking points in several text examples.
4346 """
4447 credentials = CognitiveServicesCredentials (subscription_key )
45- text_analytics_url = "https://{}.api.cognitive.microsoft.com" .format (
46- TEXTANALYTICS_LOCATION )
4748 text_analytics = TextAnalyticsClient (
48- endpoint = text_analytics_url , credentials = credentials )
49+ endpoint = endpoint , credentials = credentials )
4950
5051 try :
5152 documents = [
@@ -79,10 +80,8 @@ def sentiment(subscription_key):
7980 Scores close to 1 indicate positive sentiment, while scores close to 0 indicate negative sentiment.
8081 """
8182 credentials = CognitiveServicesCredentials (subscription_key )
82- text_analytics_url = "https://{}.api.cognitive.microsoft.com" .format (
83- TEXTANALYTICS_LOCATION )
8483 text_analytics = TextAnalyticsClient (
85- endpoint = text_analytics_url , credentials = credentials )
84+ endpoint = endpoint , credentials = credentials )
8685
8786 try :
8887 documents = [
@@ -109,10 +108,8 @@ def entity_extraction(subscription_key):
109108 Extracts the entities from sentences and prints out their properties.
110109 """
111110 credentials = CognitiveServicesCredentials (subscription_key )
112- text_analytics_url = "https://{}.api.cognitive.microsoft.com" .format (
113- TEXTANALYTICS_LOCATION )
114111 text_analytics = TextAnalyticsClient (
115- endpoint = text_analytics_url , credentials = credentials )
112+ endpoint = endpoint , credentials = credentials )
116113
117114 try :
118115 documents = [
0 commit comments