class T9Bot(object): def __init__(self, config : dict): self.blanks = config['blanks'] self.engine = Bot(environ['TOKEN'], parse_mode = types.ParseMode.HTML) self.dispatcher = Dispatcher(self.engine) self.speller = YandexSpeller() def __spelled(self, message : str): result = self.blanks['correctly'] try: corrected = self.speller.spelled(message) except: result = self.blanks['error'] if(message != corrected): result = self.blanks['incorrectly'].format(corrected) return result def __init_handlers(self): @self.dispatcher.message_handler(commands = ['start', 'help']) async def process_command(message : types.Message): response = self.blanks[message.text.replace('/', '')] await self.engine.send_message(message.from_user.id, response) @self.dispatcher.message_handler() async def process_message(message : types.Message): response = self.__spelled(sub(compile('<.*?>') , '', message.text)) await message.reply(response) def run(self): self.__init_handlers() try: executor.start_polling(self.dispatcher) except Exception as error: print(f'Error: {error}') finally: (collect(), self.run())
error_titles = [] all_unique_titles = txtdata['Title'].unique().tolist() words_to_check = [] for text in all_unique_titles: text = re.sub('\s(.)', r' \1', text) # Т.к. может ругаться на неразрывные пробелы for word in text.split(' '): if word not in words_to_check: words_to_check.append(word) for word in words_to_check: fixed = speller.spelled(word) if word != fixed: g = (word + ' => ' + fixed) print(g) error_titles.append(g) df_titles = pd.DataFrame(error_titles) if len(df_titles) > 0: df_titles = df_titles[0].str.split('=>', expand=True) df_titles.columns=['Заголовки_как сейчас:','Предлагаю заменить на:'] else: df_titles = pd.DataFrame({'хей': ['Очепяток в загах'], 'хой': ['Не нашёл!']}) print(df_titles)