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?
chatBot/translation.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
32 lines (29 sloc)
2.18 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
from translate import Translator | |
##DO NOT MODIFY THIS DICTIONARY## | |
#Dictionary created contains languages supported by the translate api and their respective language codes | |
#List of acceptable language codes for source and target languages | |
languages = dict([('amharic','am'),('arabic','ar'),('basque','eu'),('bengali','bn'),('english', 'en'),('portuguese', 'pt'), | |
('bulgarian', 'bg'),('catalan', 'ca'),('cherokee', 'chr'),('croatian', 'hr'),('czech', 'cs'),('danish','da'),('dutch', 'nl'),('estonian', 'et'), | |
('filipino','fil'),('finnish','fi'),('french','fr'),('german','de'),('greek', 'el'),('gujarati','gu'),('hebrew','iw'),('hindi','hi'), | |
('hungarian', 'hu'),('icelandic','is'),('indonesian','id'),('italian', 'it'),('japanese','ja'),('kannada','kn'),('korean', 'ko'),('latvian','lv'), | |
('lithuanian','lt'),('malay', 'ms'),('malayalam', 'ml'),('marathi','mr'),('norwegian', 'no'),('polish','pl'),('romanian','ro'),('russian','ru'), | |
('serbian', 'sr'),('chinese' ,'zh-CN'),('slovak', 'sk'),('slovenian', 'sl'),('spanish','es'),('swahili', 'sw'),('swedish','sv'),('tamil','ta'), | |
('telugu','te'),('thai','th'),('turkish','tr'),('urdu', 'ur'),('ukrainian', 'uk'),('vietnamese','vi'),('welsh', 'cy'),('yoruba','yo')]) | |
def translate(text): | |
lang = input("What language will you like to translate to? ").lower() | |
if lang in languages: | |
fromEnglish = input("Are you translating from english? ").lower() | |
# Allows users to input the language they want to translate to and from | |
# It fetches the corresponding language code using the language as the key from the above dictionary | |
# it then returns the url encoded string that will be pushed to the translation server for parsing | |
if "yes" in fromEnglish: | |
translator = Translator(to_lang=languages[lang]) | |
print(translator.translate(text)) | |
if "no" in fromEnglish: | |
langFrom = input("What language are you translating from? ") | |
translator = Translator(from_lang=languages[langFrom], to_lang=languages[lang]) | |
print(translator.translate(text)) | |
else: | |
return None | |
text = input("What would you like to translate? ") | |
translate(text) |