async def translate(self, ctx): msg = ctx.message words = msg.content.split(" ") to_language = words[-1].lower() text = str() arr = msg.content.split('"') if len(arr) == 3: text = arr[1] else: text = " ".join(words[2:-1]) try: # LANGCODES has keys as languages, values as codes # LANGUAGES has keys as codes, values as languages code = None if to_language in LANGCODES.keys(): # if language name was passed # print("lang name was passed!!") code = LANGCODES[to_language] elif to_language in LANGCODES.values(): # if code was passed # print("lang code was passed!!") code = to_language else: await ctx.send( "Please enter a valid language!" + "\nUsage: ```pepe translate <text> <language-name>```" + '\nFor a list of supported languages, use "pepe languages"' ) trans = Translator() translated_text = trans.translate(text, dest=code) await ctx.send(translated_text.text) # , tts=True) except: traceback.print_exc()
def list_questions(language): if language in LANGCODES.values(): translator = Translator() return_data = [ Question(id=int(question[1].get('id')), created=question[1].get('created'), topic=int(question[1].get('topic')), question=translator.translate(question[1].get('question'), dest=language).text, answer=translator.translate(question[1].get('answer'), dest=language).text) for question in data.items() ] return JSONResponse(return_data, status_code=200) return JSONResponse({}, status_code=400)
'Floods': 'other', 'Shootings': 'crimeviolence', 'Derailment': 'other', 'Wildfire': 'other', 'Haze': 'other' } docroot = '/afs/crc.nd.edu/user/n/nsmith9/NLP_final_project/googleTrain' trans = Translator() scorrect = True with open(sys.argv[1]) as json_data: d = json.load(json_data) lkeys = list(d.keys()) tmp = trans.detect(lkeys[0]) print(tmp.lang) if tmp.lang not in LANGCODES.values(): scorrect = False def words(text): return re.findall(r'\w+', text.lower()) WORDS = Counter(words(docroot + '/' + tmp.lang + '.txt')) def P(word, N=sum(WORDS.values())): "Probability of `word`." return WORDS[word] / N
from googletrans import Translator, LANGCODES import random lc = list(LANGCODES.values()) translator = Translator() def add_hooks(client, table): table["mangle"] = mangle table["translate"] = translate async def mangle(args, client, message): try: times = int(args[1]) text = " ".join(args[2:-1]) + "\n" + args[-1] except: times = 10 text = " ".join(args[1:-1] + "\n" + args[-1]) tmp_msg = "Translating..." tmp = await client.send_message(message.channel, tmp_msg) for i in range(times): text = translator.translate(text, dest=random.choice(lc)).text tmp_msg += str(i + 1) + "..." await client.edit_message(tmp, tmp_msg) text = translator.translate(text, dest="en").text await client.edit_message(tmp, text) async def translate(args, client, message):
def languageList(): return LANGCODES.values()
def test_language_translation(language, string): if language in LANGCODES.values(): translator = Translator() return translator.translate(string, dest=language).text return None
async def translate(ctx): message = ctx.message # do not want the bot to reply to itself so if message.author == client.user: return request = '{}'.format(message.content) try: request = request.split("!translate ")[1] except IndexError: print( "E - No message provided to translate, please refer to the help command (-h) for more info: {}" .format(request)) return await ctx.send( "Error - No message provided to translate, please refer to the help command (-h) for more info: {}" .format(request)) print("Request: {}".format(request)) print("=" * 50) print("S - New request received") args = request.split(" ") l_ind = find_in_list(args, "-l") n_ind = find_in_list(args, "-n") t_ind = find_in_list(args, "-t") h_ind = find_in_list(args, "-h") if h_ind != -1: print("S - Help information requested") return await ctx.send(HELP_TEXT) await ctx.send("Translation request received (⌐■_■)") if max(t_ind, l_ind, n_ind) == -1: text = request n = 10 langs = ['de', 'ko', 'la', 'ja', 'eo'] # default else: if t_ind == -1 or len(args[t_ind + 1:]) == 0: print("E - No text provided (-t): {}".format(request)) return await ctx.send( "Error - No text provided (-t): {}".format(request)) text = " ".join(args[t_ind + 1:]) if max(l_ind, n_ind, t_ind) != t_ind: print( "E - Optional arguments (-l,-n) should be positioned before the text argument (-t), please refer to the help command (-h) for more info: {}" .format(request)) return await ctx.send( "Error - Optional arguments (-l,-n) should be positioned before the text argument (-t), please refer to the help command (-h) for more info: {}" .format(request)) if n_ind == -1: n = 10 else: try: n = int(args[n_ind + 1]) except ValueError: print( "E - Invalid no. of translations (-n), please refer to the help command (-h) for more info: {}" .format(request)) return await ctx.send( "Error - Invalid no. of translations (-n), please refer to the help command (-h) for more info: {}" .format(request)) if l_ind == -1: langs = ['de', 'ko', 'la', 'ja', 'eo'] # default else: ind = l_ind while not ind in (n_ind, t_ind, h_ind): ind += 1 langs = args[l_ind + 1:ind] if len(langs) == 0: print( "E - No language codes provided (-l), please refer to the help command (-h) for valid codes : {}" .format(request)) return await ctx.send( "Error - No language codes provided (-l), please refer to the help command (-h) for valid codes : {}" .format(request)) invalid = [ lang for lang in langs if not lang in list(LANGCODES.values()) ] if len(invalid) != 0: print( "E - Invalid language code(s) provided (-l): {}. Please refer to the help command (-h) for valid codes: {}" .format(" ".join(invalid), request)) return await ctx.send( "Error - Invalid language code(s) provided (-l): {}. Please refer to the help command (-h) for valid codes: {}" .format(" ".join(invalid), request)) print("S - Languages: {}".format(langs)) print("S - No. of iterations: {}".format(n)) print("S - Text to be translated: {}".format(text)) await background_translate(ctx, text, langs, n)