In [27]:
## Translation tool for OpenContracting, uses google's tranlation apis
In [28]:
import json
import requests
In [ ]:
## enter the google translate API key here
my_key = (".")
Working with results
You can translate text from one language to another language by sending an HTTP GET request to its URI. The URI for a request has the following format:
https://www.googleapis.com/language/translate/v2?parameters
Three query parameters are required with each translation request:
API key Use the key query parameter to identify your application. Target language Use the target query parameter to specify the language you want to translate into. Source text string Use the q query parameter to identify the string to translate.
All other query parameters are optional. The URL for the GET, including parameters, must be less than 2K characters.
In [30]:
def get_lang(source_text, key = '', print_meta_data=False):
"""
Inputs:
source_text - source text as a string or iterable of strings
key - google api key, needed or function will raise and error
returns list of language identifiers
"""
#set up url request to google translate api
url_shell = 'https://www.googleapis.com/language/translate/v2/detect?key={0}&q={1}'
url = url_shell.format(key, source_text)
response = requests.get(url)
#parse response
data_dict = json.loads(response.text)
source_lang = data_dict['data']['detections'][0][0]['language']
if print_meta_data:
print 'Is detection reliable: {0}'.format(data_dict['data']['detections']['isReliable'])
print 'Confidence: {0}'.format(data_dict['data']['detections']['confidence'])
return source_lang
## can return comma seperated
In [31]:
'''
I added a line of code,
https://www.googleapis.com/language/translate/v2?parameters
but it was the general google translate tool,
the code in the method tells you what the language is!
'''
Out[31]:
In [32]:
text_French = ('La litterature française comprend l')
In [33]:
get_lang(text_French, my_key)
Out[33]:
In [34]:
text_German = ("Contracting (englisch die Kontrahierung bzw. adjektivisch vertragschließend)")
text_list_German_and_French = [text_French, text_German]
In [35]:
def get_trans(source_text, key = '', target_lang = 'en', source_lang = ''):
"""
Inputs:
source_text - source text as a string or iterable of strings
key - google api key, needed or function will raise and error
target_lang - target language, defaults to 'en' - english
source_lang - source language, defaults to '' (detect), can also be entered
as a string or iterable of strings, size must agree with source_text
returns dictionary with keys of source_text, values of translated text
"""
#CODE!!
##I think the source language should be the returned object from another method
##source_lang = get_lang_list(source_text, key)
##but I will keep these methods seperate for now
## I read the google docs api and this API call needs three parameters, API key, target lang, and source key
##url_shell = 'https://www.googleapis.com/language/translate/v2/detect?key={0}&q={1}'
url_shell = 'https://www.googleapis.com/language/translate/v2?parameters?key={0}&q={1}&q={2}'
url = url_shell.format(key, target_lang,source_text)
response = requests.get(url)
## I am copying the data format for the get_lang_list method
data_dict = json.loads(response.text)
##source_lang_data_dict = data_dict['data']['detections'][0][0]['language']
translated_source.append(data_dict)
return translated_source
In [36]:
get_trans(text_French, my_key)
Out[36]:
In [37]:
def get_lang_list(source_text, key = '', print_meta_data=False):
"""
Inputs:
source_text - source text as iterable of strings
key - google api key, needed or function will raise and error
returns list of language identifiers
"""
#set up url request to google translate api
lang_list = []
for item in source_text:
url_shell = 'https://www.googleapis.com/language/translate/v2/detect?key={0}&q={1}'
url = url_shell.format(key, item)
response = requests.get(url)
#parse response
data_dict = json.loads(response.text)
source_lang = data_dict['data']['detections'][0][0]['language']
lang_list.append(source_lang)
if print_meta_data:
print 'Is detection reliable: {0}'.format(data_dict['data']['detections']['isReliable'])
print 'Confidence: {0}'.format(data_dict['data']['detections']['confidence'])
return lang_list
In [38]:
## a test of
get_lang_list(text_list_German_and_French, my_key)
Out[38]:
In [42]:
Python specific information for working with google apis