Skip to content

Commit e7c152c

Browse files
authored
Merge pull request #67 from aahill/ta-sdk-update
[Text analytics] Moving client authentication to method
2 parents 05e3213 + b1cdccb commit e7c152c

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

samples/language/text_analytics_samples.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,29 @@
1818
endpoint = 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>
2634
def 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>
5560
def 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>
9497
def 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>
125126
def 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

Comments
 (0)