Skip to content

Commit cf9cab6

Browse files
authoredJun 4, 2018
Cognitive Services chapter code files
1 parent aee7ebd commit cf9cab6

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
 

‎textanalytics_api.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Author: Parashar Shah
2+
# Chapter: Cognitive Services
3+
# Version: 1.0
4+
# Date: May 25, 2018
5+
# Replace <Subscription Key> with your valid subscription's api access key.
6+
subscription_key = "<Access Key>"
7+
assert subscription_key
8+
9+
# Replace the base url with what you see as Endpoint in the portal’s Overview section under your api
10+
11+
text_analytics_base_url = "https://westus2.api.cognitive.microsoft.com/text/analytics/v2.0/"
12+
13+
sentiment_api_url = text_analytics_base_url + "sentiment"
14+
15+
# Send the text you want the api to analyze
16+
# You can send multiple texts
17+
documents = {'documents' : [
18+
{'id': '1', 'text': 'I am excited about using AI offerings by Microsoft.'},
19+
]}
20+
21+
import requests
22+
23+
# Get sentiment of text
24+
headers = {"Ocp-Apim-Subscription-Key": subscription_key}
25+
response = requests.post(sentiment_api_url, headers=headers, json=documents)
26+
sentiments = response.json()
27+
print(sentiments)
28+
29+
# Get the language of text
30+
language_api_url = text_analytics_base_url + "languages"
31+
response = requests.post(language_api_url, headers=headers, json=documents)
32+
languages = response.json()
33+
print(languages)
34+
35+
# Get key phrases from text
36+
key_phrase_api_url = text_analytics_base_url + "keyPhrases"
37+
response = requests.post(key_phrase_api_url, headers=headers, json=documents)
38+
key_phrases = response.json()
39+
print(key_phrases)
40+
41+
# Get well-known entities
42+
entity_linking_api_url = text_analytics_base_url + "entities"
43+
response = requests.post(entity_linking_api_url, headers=headers, json=documents)
44+
entities = response.json()
45+
print(entities)

‎vision_api.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Author: Parashar Shah
2+
# Chapter: Cognitive Services
3+
# Version: 1.0
4+
# Date: May 25, 2018
5+
# Replace <Subscription Key> with your valid subscription's api access key.
6+
subscription_key = "<Access Key>"
7+
assert subscription_key
8+
9+
# Replace the base url with what you see as Endpoint in the portal’s Overview section under your api
10+
11+
vision_base_url = "https://westus.api.cognitive.microsoft.com/vision/v1.0/"
12+
13+
vision_analyze_url = vision_base_url + "analyze"
14+
15+
# Set image_url to the URL of an image that you want to analyze.
16+
image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/" + \
17+
"Flickr_-_Duncan~_-_London_from_Parliament_Hill.jpg/640px-Flickr_-_Duncan~_-_London_from_Parliament_Hill.jpg"
18+
19+
import requests
20+
headers = {'Ocp-Apim-Subscription-Key': subscription_key }
21+
params = {'visualFeatures': 'Categories,Description,Color'}
22+
data = {'url': image_url}
23+
response = requests.post(vision_analyze_url, headers=headers, params=params, json=data)
24+
response.raise_for_status()
25+
26+
# The 'analysis' object contains various fields that describe the image. The most
27+
# relevant caption for the image is obtained from the 'descriptions' property.
28+
analysis = response.json()
29+
print(analysis)
30+
31+
image_caption = analysis["description"]["captions"][0]["text"].capitalize()
32+
33+
# Display the image and overlay it with the caption.
34+
# If you are using a Jupyter notebook, uncomment the following line.
35+
#%matplotlib inline
36+
from PIL import Image
37+
from io import BytesIO
38+
import matplotlib.pyplot as plt
39+
image = Image.open(BytesIO(requests.get(image_url).content))
40+
plt.imshow(image)
41+
plt.axis("off")
42+
_ = plt.title(image_caption, size="x-large", y=-0.1)

0 commit comments

Comments
 (0)