Skip to content

Commit 2824715

Browse files
authored
Merge pull request #60 from v-jaswel/patch-4
[Text Analytics] Get subscription key and endpoint from env vars
2 parents 32881fe + 20b8ed1 commit 2824715

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

samples/language/text_analytics_samples.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44
from azure.cognitiveservices.language.textanalytics import TextAnalyticsClient
55
from 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

1217
def 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

Comments
 (0)