1818endpoint = os .environ [endpoint_var_name ]
1919# </initialVars>
2020
21+ # <authentication>
22+ def authenticateClient ():
23+ credentials = CognitiveServicesCredentials (subscription_key )
24+ text_analytics_client = TextAnalyticsClient (
25+ endpoint = endpoint , credentials = credentials )
26+ return text_analytics_client
27+ # </authentication>
28+
2129"""Language detection.
2230
2331 This example detects the language of several strings.
2432 """
2533# <languageDetection>
2634def language_detection ():
27-
28- credentials = CognitiveServicesCredentials (subscription_key )
29- text_analytics = TextAnalyticsClient (
30- endpoint = endpoint , credentials = credentials )
35+ client = authenticateClient ()
3136
3237 try :
3338 documents = [
3439 {'id' : '1' , 'text' : 'This is a document written in English.' },
3540 {'id' : '2' , 'text' : 'Este es un document escrito en Español.' },
3641 {'id' : '3' , 'text' : '这是一个用中文写的文件' }
3742 ]
38- response = text_analytics .detect_language (documents = documents )
43+ response = client .detect_language (documents = documents )
3944
4045 for document in response .documents :
4146 print ("Document Id: " , document .id , ", Language: " ,
@@ -54,9 +59,7 @@ def language_detection():
5459# <keyPhrases>
5560def key_phrases ():
5661
57- credentials = CognitiveServicesCredentials (subscription_key )
58- text_analytics = TextAnalyticsClient (
59- endpoint = endpoint , credentials = credentials )
62+ client = authenticateClient ()
6063
6164 try :
6265 documents = [
@@ -72,7 +75,7 @@ def key_phrases():
7275 print (
7376 "Asking key-phrases on '{}' (id: {})" .format (document ['text' ], document ['id' ]))
7477
75- response = text_analytics .key_phrases (documents = documents )
78+ response = client .key_phrases (documents = documents )
7679
7780 for document in response .documents :
7881 print ("Document Id: " , document .id )
@@ -93,9 +96,7 @@ def key_phrases():
9396# <sentimentAnalysis>
9497def sentiment ():
9598
96- credentials = CognitiveServicesCredentials (subscription_key )
97- text_analytics = TextAnalyticsClient (
98- endpoint = endpoint , credentials = credentials )
99+ client = authenticateClient ()
99100
100101 try :
101102 documents = [
@@ -107,7 +108,7 @@ def sentiment():
107108 "text" : "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura." }
108109 ]
109110
110- response = text_analytics .sentiment (documents = documents )
111+ response = client .sentiment (documents = documents )
111112 for document in response .documents :
112113 print ("Document Id: " , document .id , ", Sentiment Score: " ,
113114 "{:.2f}" .format (document .score ))
@@ -124,17 +125,15 @@ def sentiment():
124125# <entityRecognition>
125126def entity_recognition ():
126127
127- credentials = CognitiveServicesCredentials (subscription_key )
128- text_analytics = TextAnalyticsClient (
129- endpoint = endpoint , credentials = credentials )
128+ client = authenticateClient ()
130129
131130 try :
132131 documents = [
133132 {"id" : "1" , "language" : "en" , "text" : "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800." },
134133 {"id" : "2" , "language" : "es" ,
135134 "text" : "La sede principal de Microsoft se encuentra en la ciudad de Redmond, a 21 kilómetros de Seattle." }
136135 ]
137- response = text_analytics .entities (documents = documents )
136+ response = client .entities (documents = documents )
138137
139138 for document in response .documents :
140139 print ("Document Id: " , document .id )
0 commit comments