Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chat-bot/translator.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
29 lines (25 sloc)
1.03 KB
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
#Code for translation API | |
# -*- coding: utf-8 -*- | |
import requests | |
from keywords import readCountryCodes | |
import string | |
def tranlateText(text,country): | |
for char in string.punctuation: #To avoid any errors caused by undetected char | |
text = text.replace(char, ' ') | |
url = "https://google-translate20.p.rapidapi.com/translate" | |
userInput = text | |
code = readCountryCodes(country) | |
querystring = {"text":userInput,"tl":code,"sl":"en"} | |
headers = { | |
'x-rapidapi-key': "82f9448eb5msh9013e0006d93ae1p10e70ejsnad262f150737", | |
'x-rapidapi-host': "google-translate20.p.rapidapi.com" | |
} | |
response = requests.request("GET", url, headers=headers, params=querystring) | |
apiList = str(response.text) | |
apiList = apiList.replace('"', '') | |
apiList = list(apiList.split(",")) | |
for word in apiList: | |
if 'data:{translation:' in word: | |
word = word.replace('data:{translation:','') | |
tranlatedText = word | |
return ('Translation from English to ' + country + ' is: ' + tranlatedText) |