Created
August 2, 2017 06:15
-
-
Save gamebusterz/6c4121ea2296ed46e7304b83cbaf84b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import requests | |
import os | |
path = '/home/yankee/FrenchBot/otherData/' | |
path2 = '/home/yankee/FrenchBotTranslated/' | |
os.system('mkdir -p /home/yankee/FrenchBotTranslated/') | |
# API_KEY = "YOUR-GOOGLE-TRANSLATE-API-KEY-HERE" | |
API_KEY = "AIzaSyAJmdCjhVnKgEhPvfq6_p-anSZRUeyJbIg" | |
def translate_call(string): | |
PARAMETERS= {'key': API_KEY, 'source':'en','target':'fr','q':string} | |
r=requests.post(url='https://translation.googleapis.com/language/translate/v2?parameters',params=PARAMETERS) | |
translate_response=r.json() | |
print(translate_response) | |
return translate_response['data']['translations'][0]['translatedText'] | |
for datafilename in os.listdir(path): | |
datafile = path+datafilename | |
output= [] | |
with open (datafile) as df: | |
for line in df: | |
if line[0] != '*' and line.strip() != '': | |
output.append(translate_call(line)) | |
elif line.strip() == '': | |
output.append('\n') | |
else: | |
output.append(line) | |
outputfile = open(path2+datafilename,'w+') | |
for item in output: | |
outputfile.write(item.encode("UTF-8")) | |
outputfile.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment