Skip to content
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Operating System files
.DS_Store
.gitignore
Thumbs.db
77 changes: 39 additions & 38 deletions BreakSentence.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
# -*- coding: utf-8 -*-

import http.client, urllib.parse, uuid, json

# **********************************************
# *** Update or verify the following values. ***
# **********************************************

# Replace the subscriptionKey string value with your valid subscription key.
subscriptionKey = 'ENTER KEY HERE'

host = 'api.cognitive.microsofttranslator.com'
# This simple app uses the '/breaksentence' resource to identify the source
# language and determines the numbe of characters in each sentence.

# This sample runs on Python 2.7.x and Python 3.x.
# You may need to install requests and uuid.
# Run: pip install requests uuid


import os, requests, uuid, json

# Checks to see if the Translator Text subscription key is available
# as an environment variable. If you are setting your subscription key as a
# string, then comment these lines out.
if 'TRANSLATOR_TEXT_KEY' in os.environ:
subscriptionKey = os.environ['TRANSLATOR_TEXT_KEY']
else:
print('Environment variable for TRANSLATOR_TEXT_KEY is not set.')
exit()
# If you want to set your subscription key as a string, uncomment the next line.
#subscriptionKey = 'put_your_key_here'

# If you encounter any issues with the base_url or path, make sure
# that you are using the latest endpoint: https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-break-sentence
base_url = 'https://api.cognitive.microsofttranslator.com'
path = '/breaksentence?api-version=3.0'

params = ''

text = 'How are you? I am fine. What did you do today?'

def breakSentences (content):

headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}

conn = http.client.HTTPSConnection(host)
conn.request ("POST", path + params, content, headers)
response = conn.getresponse ()
return response.read ()

requestBody = [{
'Text' : text,
params = '&language=en'
constructed_url = base_url + path + params

headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}

# You can pass more than one object in body.
body = [{
'text': 'How are you? I am fine. What did you do today?'
}]
content = json.dumps(requestBody, ensure_ascii=False).encode('utf-8')
result = breakSentences (content)

# Note: We convert result, which is JSON, to and from an object so we can pretty-print it.
# We want to avoid escaping any Unicode characters that result contains. See:
# https://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence
output = json.dumps(json.loads(result), indent=4, ensure_ascii=False)
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()

print (output)
print(json.dumps(response, sort_keys=True, indent=4, ensure_ascii=False, separators=(',', ': ')))
72 changes: 36 additions & 36 deletions Detect.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
# -*- coding: utf-8 -*-

import http.client, urllib.parse, uuid, json

# **********************************************
# *** Update or verify the following values. ***
# **********************************************

# Replace the subscriptionKey string value with your valid subscription key.
subscriptionKey = 'ENTER KEY HERE'

host = 'api.cognitive.microsofttranslator.com'
# This simple app uses the '/detect' resource to identify the language of
# the provided text or texts.

# This sample runs on Python 2.7.x and Python 3.x.
# You may need to install requests and uuid.
# Run: pip install requests uuid


import os, requests, uuid, json

# Checks to see if the Translator Text subscription key is available
# as an environment variable. If you are setting your subscription key as a
# string, then comment these lines out.
if 'TRANSLATOR_TEXT_KEY' in os.environ:
subscriptionKey = os.environ['TRANSLATOR_TEXT_KEY']
else:
print('Environment variable for TRANSLATOR_TEXT_KEY is not set.')
exit()
# If you want to set your subscription key as a string, uncomment the next line.
#subscriptionKey = 'put_your_key_here'

# If you encounter any issues with the base_url or path, make sure
# that you are using the latest endpoint: https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-detect
base_url = 'https://api.cognitive.microsofttranslator.com'
path = '/detect?api-version=3.0'
constructed_url = base_url + path

params = ''

text = 'Salve, mondo!'
headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}

def detect (content):

headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}

conn = http.client.HTTPSConnection(host)
conn.request ("POST", path, content, headers)
response = conn.getresponse ()
return response.read ()

requestBody = [{
'Text' : text,
# You can pass more than one object in body.
body = [{
'text': 'Salve, mondo!'
}]
content = json.dumps(requestBody, ensure_ascii=False).encode('utf-8')
result = detect (content)

# Note: We convert result, which is JSON, to and from an object so we can pretty-print it.
# We want to avoid escaping any Unicode characters that result contains. See:
# https://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence
output = json.dumps(json.loads(result), indent=4, ensure_ascii=False)
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()

print (output)
print(json.dumps(response, sort_keys=True, indent=4, ensure_ascii=False, separators=(',', ': ')))
79 changes: 40 additions & 39 deletions DictionaryExample.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
# -*- coding: utf-8 -*-

import http.client, urllib.parse, uuid, json

# **********************************************
# *** Update or verify the following values. ***
# **********************************************

# Replace the subscriptionKey string value with your valid subscription key.
subscriptionKey = 'ENTER KEY HERE'

host = 'api.cognitive.microsofttranslator.com'
# This simple app uses the '/dictionary/examples' resource to illustrate
# how terms in a dictionary are contexutalized.

# This sample runs on Python 2.7.x and Python 3.x.
# You may need to install requests and uuid.
# Run: pip install requests uuid


import os, requests, uuid, json

# Checks to see if the Translator Text subscription key is available
# as an environment variable. If you are setting your subscription key as a
# string, then comment these lines out.
if 'TRANSLATOR_TEXT_KEY' in os.environ:
subscriptionKey = os.environ['TRANSLATOR_TEXT_KEY']
else:
print('Environment variable for TRANSLATOR_TEXT_KEY is not set.')
exit()
# If you want to set your subscription key as a string, uncomment the next line.
#subscriptionKey = 'put_your_key_here'

# If you encounter any issues with the base_url or path, make sure
# that you are using the latest endpoint: https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-dictionary-examples
base_url = 'https://api.cognitive.microsofttranslator.com'
path = '/dictionary/examples?api-version=3.0'

params = '&from=en&to=fr';

text = 'great'
translation = 'formidable'

def examples (content):

headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}

conn = http.client.HTTPSConnection(host)
conn.request ("POST", path + params, content, headers)
response = conn.getresponse ()
return response.read ()

requestBody = [{
'Text' : text,
'Translation' : translation,
constructed_url = base_url + path + params

headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}

# Each object takes two key/value pairs: 'text' and 'translation'.
# Note: You can pass more than one object in body.
body = [{
'text': 'great',
'translation': 'formidable'
}]
content = json.dumps(requestBody, ensure_ascii=False).encode('utf-8')
result = examples (content)

# Note: We convert result, which is JSON, to and from an object so we can pretty-print it.
# We want to avoid escaping any Unicode characters that result contains. See:
# https://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence
output = json.dumps(json.loads(result), indent=4, ensure_ascii=False)
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()

print (output)
print(json.dumps(response, sort_keys=True, indent=4, ensure_ascii=False, separators=(',', ': ')))
77 changes: 39 additions & 38 deletions DictionaryLookup.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
# -*- coding: utf-8 -*-

import http.client, urllib.parse, uuid, json

# **********************************************
# *** Update or verify the following values. ***
# **********************************************

# Replace the subscriptionKey string value with your valid subscription key.
subscriptionKey = 'ENTER KEY HERE'

host = 'api.cognitive.microsofttranslator.com'
# This simple app uses the '/dictionary/lookup' resource to return alternative
# translations for a word and a small number of idiomatic phrases.

# This sample runs on Python 2.7.x and Python 3.x.
# You may need to install requests and uuid.
# Run: pip install requests uuid


import os, requests, uuid, json

# Checks to see if the Translator Text subscription key is available
# as an environment variable. If you are setting your subscription key as a
# string, then comment these lines out.
if 'TRANSLATOR_TEXT_KEY' in os.environ:
subscriptionKey = os.environ['TRANSLATOR_TEXT_KEY']
else:
print('Environment variable for TRANSLATOR_TEXT_KEY is not set.')
exit()
# If you want to set your subscription key as a string, uncomment the next line.
#subscriptionKey = 'put_your_key_here'

# If you encounter any issues with the base_url or path, make sure
# that you are using the latest endpoint: https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-dictionary-lookup
base_url = 'https://api.cognitive.microsofttranslator.com'
path = '/dictionary/lookup?api-version=3.0'

params = '&from=en&to=fr';

text = 'great'

def lookup (content):

headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}

conn = http.client.HTTPSConnection(host)
conn.request ("POST", path + params, content, headers)
response = conn.getresponse ()
return response.read ()

requestBody = [{
'Text' : text,
params = '&from=en&to=es';
constructed_url = base_url + path + params

headers = {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}

# You can pass more than one object in body.
body = [{
'text': 'Elephants'
}]
content = json.dumps(requestBody, ensure_ascii=False).encode('utf-8')
result = lookup (content)

# Note: We convert result, which is JSON, to and from an object so we can pretty-print it.
# We want to avoid escaping any Unicode characters that result contains. See:
# https://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence
output = json.dumps(json.loads(result), indent=4, ensure_ascii=False)
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()

print (output)
print(json.dumps(response, sort_keys=True, indent=4, ensure_ascii=False, separators=(',', ': ')))
Loading