Skip to content

Commit 114f6f9

Browse files
iscai-msftlmazuel
authored andcommitted
Text Analytics samples (#27)
* added entity extraction samples, untested because blocked swagger side * text analytics samples working after swagger update * changed samples.tools to tools * Update requirements.txt
1 parent ed27a9f commit 114f6f9

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,3 @@ ENV/
105105

106106
#DS store
107107
.DS_Store
108-
samples/DS_Store

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
azure-cognitiveservices-knowledge-qnamaker
22
azure-cognitiveservices-language-luis
33
azure-cognitiveservices-language-spellcheck
4-
azure-cognitiveservices-language-textanalytics
4+
azure-cognitiveservices-language-textanalytics>=0.2.0 # sample won't work with previous versions
55
azure-cognitiveservices-search-autosuggest
66
azure-cognitiveservices-search-customimagesearch
77
azure-cognitiveservices-search-customsearch>=0.2.0 # sample won't work with previous versions

samples/language/text_analytics_samples.py

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
import os
44

5-
from azure.cognitiveservices.language.textanalytics import TextAnalyticsAPI
5+
from azure.cognitiveservices.language.textanalytics import TextAnalyticsClient
66
from msrest.authentication import CognitiveServicesCredentials
77

88
SUBSCRIPTION_KEY_ENV_NAME = "TEXTANALYTICS_SUBSCRIPTION_KEY"
99
TEXTANALYTICS_LOCATION = os.environ.get("TEXTANALYTICS_LOCATION", "westcentralus")
1010

11+
1112
def 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+
4144
def 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\tEntity Name: {}".format(entity.name))
166+
print("\t\tWikipedia Language: {}".format(entity.wikipedia_language))
167+
print("\t\tWikipedia Url: {}".format(entity.wikipedia_url))
168+
print("\t\tNumber of times appeared on the text: {}".format(len(entity.matches)))
169+
print("\t\tEntity Type: {}".format(entity.type))
170+
print("\t\tEntity SubType: {}".format(entity.sub_type))
171+
print("\n")
172+
173+
except Exception as err:
174+
print("Encountered exception. {}".format(err))
175+
176+
123177
if __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

Comments
 (0)