|
| 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) |
0 commit comments