def read_comments(self): for comment in self.comment_stream: if comment is None: break if comment.created_utc > self.executed_timestamp: if comment.subreddit not in self.database.blacklisted_subs: if comment.body.startswith(patterns.main_trigger): if comment.parent_id == comment.link_id: author = str(comment.author) comment_match = re.match( patterns.full_trigger_pattern, comment.body) try: lang, query = comment_match.groups() lang = lang.lower() if lang in self.database.valid_langs: translation_response = translate.translate( f'{lang}', f'{query}') print(f'replying to: {author}') comment.reply( messages.translated_comment( query, **translation_response)) # If the parameters passed to "lang" is not recognized by Yandex, send a message to the user notifying him. else: print( f'sending syntax error message to: {author}' ) self.reddit.redditor(author).message( 'Notification from TranslateService', messages.syntax_error(author)) # If the message does not match the pattern, send a syntax message to the user. except AttributeError as e: print(e) print( f'sending syntax error message to: {author}' ) self.reddit.redditor(author).message( 'Notification from TranslateService', messages.syntax_error(author))
def test_translate(self): out = translate("plomo o plata", input='es') self.assertEqual(('lead or silver', 'es'), out)
cin = cin.lower() lastcmd = cin try: while pcval >= 0: if pcval == -1: # That would be the exit command. break elif cin == "quiz": # Run quiz mode, and track the return value # so we can see if we're exiting the program itself. lastcmd = quiz(curstats.quiz, data) if lastcmd == "/exit!" or lastcmd == "/rq": # Kind of hacky but w/e break print "Type \"/exit\" to exit Morsetrainer, or " print "enter a command or select a mode." elif cin == "translate": lastcmd = translate(curstats.quiz, data) if lastcmd == "/exit!" or lastcmd == "/rq": break print "Type \"/exit\" to exit Morsetrainer, or " print "enter a command or select a mode." elif cin == "listen": lastcmd = listen(curstats.listen, data) if lastcmd == "/exit!" or lastcmd == "/rq": break print "Type \"/exit\" to exit Morsetrainer, or " print "enter a command or select a mode." elif cin == "transcribe": lastcmd = transcribe(curstats.listen, data) if lastcmd == "/exit!" or lastcmd == "/rq": break print "Type \"/exit\" to exit Morsetrainer, or "
def translateru(client, message): translate(client, message, "en", "ru", "#ru ")
def translateen(client, message): translate(client, message, "ru", "en", "#en ")