Skip to content

Commit 595c724

Browse files
committed
Add TextAnalytics
1 parent ab258c4 commit 595c724

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This project framework provides examples for the following services:
99
### Language
1010

1111
* Using the **Bing Spell Check SDK** [azure-cognititiveservices-language-spellcheck](http://pypi.python.org/pypi/azure-cognititiveservices-language-spellcheck) for the [Bing Spell Check API](https://azure.microsoft.com/services/cognitive-services/spell-check/)
12+
* Using the **Text Analytics SDK** [azure-cognititiveservices-language-textanalytics](http://pypi.python.org/pypi/azure-cognititiveservices-language-textanalytics) for the [Tet Analytics API](https://azure.microsoft.com/services/cognitive-services/text-analytics/)
1213

1314
### Search
1415

@@ -63,6 +64,7 @@ We provide several meta-packages to help you install several packages at a time.
6364
```
6465
6566
4. Set up the environment variable `SPELLCHECK_SUBSCRIPTION_KEY` with your key if you want to execute SpellCheck tests.
67+
4. Set up the environment variable `TEXTANALYTICS_SUBSCRIPTION_KEY` with your key if you want to execute SpellCheck tests. You might override too `TEXTANALYTICS_LOCATION` (westcentralus by default).
6668
3. Set up the environment variable `CUSTOMSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomSearch tests.
6769
3. Set up the environment variable `ENTITYSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute EntitySearch tests.
6870
4. Set up the environment variable `IMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute ImageSearch tests.
@@ -78,7 +80,7 @@ To run the complete demo, execute `python example.py`
7880
7981
To run each individual demo, point directly to the file. For example (i.e. not complete list):
8082
81-
2. `python samples/language/spellchack_samples.py`
83+
2. `python samples/language/spellcheck_samples.py`
8284
1. `python samples/search/entity_search_samples.py`
8385
2. `python samples/search/video_search_samples.py`
8486

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
azure-cognitiveservices-language-spellcheck
2+
azure-cognitiveservices-language-textanalytics
23
azure-cognitiveservices-search-customsearch
34
azure-cognitiveservices-search-entitysearch
45
azure-cognitiveservices-search-imagesearch
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import os
2+
3+
from azure.cognitiveservices.language.textanalytics import TextAnalyticsAPI
4+
from msrest.authentication import CognitiveServicesCredentials
5+
6+
SUBSCRIPTION_KEY_ENV_NAME = "TEXTANALYTICS_SUBSCRIPTION_KEY"
7+
TEXTANALYTICS_LOCATION = os.environ.get("TEXTANALYTICS_LOCATION", "westcentralus")
8+
9+
def language_extraction(subscription_key):
10+
"""Language extraction.
11+
12+
This will detect the language of a few strings.
13+
"""
14+
client = TextAnalyticsAPI(TEXTANALYTICS_LOCATION, CognitiveServicesCredentials(subscription_key))
15+
16+
try:
17+
documents = [{
18+
'id': 1,
19+
'text': 'This is a document written in English.'
20+
}, {
21+
'id': 2,
22+
'text': 'Este es un document escrito en Español.'
23+
}, {
24+
'id': 3,
25+
'text': '这是一个用中文写的文件'
26+
}]
27+
for document in documents:
28+
print("Asking language detection on '{}' (id: {})".format(document['text'], document['id']))
29+
response = client.detect_language(
30+
documents=documents
31+
)
32+
33+
for document in response.documents:
34+
print("Found out that {} is {}".format(document.id, document.detected_languages[0].name))
35+
36+
except Exception as err:
37+
print("Encountered exception. {}".format(err))
38+
39+
def key_phrases(subscription_key):
40+
"""Key-phrases.
41+
42+
The API returns a list of strings denoting the key talking points in the input text.
43+
"""
44+
client = TextAnalyticsAPI(TEXTANALYTICS_LOCATION, CognitiveServicesCredentials(subscription_key))
45+
46+
try:
47+
documents = [{
48+
'language': 'ja',
49+
'id': 1,
50+
'text': "猫は幸せ"
51+
}, {
52+
'language': 'de',
53+
'id': 2,
54+
'text': "Fahrt nach Stuttgart und dann zum Hotel zu Fu."
55+
}, {
56+
'language': 'en',
57+
'id': 3,
58+
'text': "My cat is stiff as a rock."
59+
}, {
60+
'language': 'es',
61+
'id': 4,
62+
'text': "A mi me encanta el fútbol!"
63+
}]
64+
65+
for document in documents:
66+
print("Asking key-phrases on '{}' (id: {})".format(document['text'], document['id']))
67+
68+
response = client.key_phrases(
69+
documents=documents
70+
)
71+
72+
for document in response.documents:
73+
print("Found out that in document {}, key-phrases are:".format(document.id))
74+
for phrase in document.key_phrases:
75+
print("- {}".format(phrase))
76+
77+
except Exception as err:
78+
print("Encountered exception. {}".format(err))
79+
80+
81+
def sentiment(subscription_key):
82+
"""Sentiment.
83+
84+
Scores close to 1 indicate positive sentiment, while scores close to 0 indicate negative sentiment.
85+
"""
86+
client = TextAnalyticsAPI(TEXTANALYTICS_LOCATION, CognitiveServicesCredentials(subscription_key))
87+
88+
try:
89+
documents = [{
90+
'language': 'en',
91+
'id': 0,
92+
'text': "I had the best day of my life."
93+
}, {
94+
'language': 'en',
95+
'id': 1,
96+
'text': "This was a waste of my time. The speaker put me to sleep."
97+
}, {
98+
'language': 'es',
99+
'id': 2,
100+
'text': "No tengo dinero ni nada que dar..."
101+
}, {
102+
'language': 'it',
103+
'id': 3,
104+
'text': "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura."
105+
}]
106+
107+
for document in documents:
108+
print("Asking sentiment on '{}' (id: {})".format(document['text'], document['id']))
109+
110+
response = client.sentiment(
111+
documents=documents
112+
)
113+
114+
for document in response.documents:
115+
print("Found out that in document {}, sentimet score is {}:".format(document.id, document.score))
116+
117+
except Exception as err:
118+
print("Encountered exception. {}".format(err))
119+
120+
121+
if __name__ == "__main__":
122+
import sys, os.path
123+
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
124+
from tools import execute_samples
125+
execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME)

0 commit comments

Comments
 (0)