22
33import os
44
5- from azure .cognitiveservices .language .textanalytics import TextAnalyticsAPI
5+ from azure .cognitiveservices .language .textanalytics import TextAnalyticsClient
66from msrest .authentication import CognitiveServicesCredentials
77
88SUBSCRIPTION_KEY_ENV_NAME = "TEXTANALYTICS_SUBSCRIPTION_KEY"
99TEXTANALYTICS_LOCATION = os .environ .get ("TEXTANALYTICS_LOCATION" , "westcentralus" )
1010
11+
1112def language_extraction (subscription_key ):
1213 """Language extraction.
1314
1415 This will detect the language of a few strings.
1516 """
16- client = TextAnalyticsAPI (TEXTANALYTICS_LOCATION , CognitiveServicesCredentials (subscription_key ))
17+ endpoint = "https://{}.api.cognitive.microsoft.com" .format (TEXTANALYTICS_LOCATION )
18+ client = TextAnalyticsClient (endpoint = endpoint , credentials = CognitiveServicesCredentials (subscription_key ))
1719
1820 try :
1921 documents = [{
@@ -38,12 +40,14 @@ def language_extraction(subscription_key):
3840 except Exception as err :
3941 print ("Encountered exception. {}" .format (err ))
4042
43+
4144def key_phrases (subscription_key ):
4245 """Key-phrases.
4346
4447 The API returns a list of strings denoting the key talking points in the input text.
4548 """
46- client = TextAnalyticsAPI (TEXTANALYTICS_LOCATION , CognitiveServicesCredentials (subscription_key ))
49+ endpoint = "https://{}.api.cognitive.microsoft.com" .format (TEXTANALYTICS_LOCATION )
50+ client = TextAnalyticsClient (endpoint = endpoint , credentials = CognitiveServicesCredentials (subscription_key ))
4751
4852 try :
4953 documents = [{
@@ -85,7 +89,8 @@ def sentiment(subscription_key):
8589
8690 Scores close to 1 indicate positive sentiment, while scores close to 0 indicate negative sentiment.
8791 """
88- client = TextAnalyticsAPI (TEXTANALYTICS_LOCATION , CognitiveServicesCredentials (subscription_key ))
92+ endpoint = "https://{}.api.cognitive.microsoft.com" .format (TEXTANALYTICS_LOCATION )
93+ client = TextAnalyticsClient (endpoint = endpoint , credentials = CognitiveServicesCredentials (subscription_key ))
8994
9095 try :
9196 documents = [{
@@ -120,8 +125,59 @@ def sentiment(subscription_key):
120125 print ("Encountered exception. {}" .format (err ))
121126
122127
128+ def entity_extraction (subscription_key ):
129+ """EntityExtraction.
130+
131+ Extracts the entities from sentences and prints out their properties
132+ """
133+ endpoint = "https://{}.api.cognitive.microsoft.com" .format (TEXTANALYTICS_LOCATION )
134+ client = TextAnalyticsClient (endpoint = endpoint , credentials = CognitiveServicesCredentials (subscription_key ))
135+
136+ try :
137+ documents = [{
138+ 'language' : 'en' ,
139+ 'id' : 0 ,
140+ 'text' : "Microsoft released win10. Microsoft also released Hololens"
141+ }, {
142+ 'language' : 'en' ,
143+ 'id' : 1 ,
144+ 'text' : "Microsoft is an IT company."
145+ }, {
146+ 'language' : 'es' ,
147+ 'id' : 2 ,
148+ 'text' : "Microsoft lanzó win10. Microsoft también lanzó Hololens"
149+ }, {
150+ 'language' : 'es' ,
151+ 'id' : 3 ,
152+ 'text' : "Microsoft es una empresa de TI."
153+ }]
154+ for document in documents :
155+ print ("Extracting entities from '{}' (id: {})" .format (document ['text' ], document ['id' ]))
156+
157+ response = client .entities (
158+ documents = documents
159+ )
160+
161+ for document in response .documents :
162+ print ("Document ID: {}" .format (document ['Id' ]))
163+ print ("\t Entities:" )
164+ for entity in document ['Entities' ]:
165+ print ("\t \t Entity Name: {}" .format (entity .name ))
166+ print ("\t \t Wikipedia Language: {}" .format (entity .wikipedia_language ))
167+ print ("\t \t Wikipedia Url: {}" .format (entity .wikipedia_url ))
168+ print ("\t \t Number of times appeared on the text: {}" .format (len (entity .matches )))
169+ print ("\t \t Entity Type: {}" .format (entity .type ))
170+ print ("\t \t Entity SubType: {}" .format (entity .sub_type ))
171+ print ("\n " )
172+
173+ except Exception as err :
174+ print ("Encountered exception. {}" .format (err ))
175+
176+
123177if __name__ == "__main__" :
124178 import sys , os .path
125- sys .path .append (os .path .abspath (os .path .join (__file__ , ".." , ".." )))
179+
180+ sys .path .append (os .path .abspath (os .path .join (__file__ , ".." , ".." )))
126181 from tools import execute_samples
182+
127183 execute_samples (globals (), SUBSCRIPTION_KEY_ENV_NAME )
0 commit comments