async def del_list_keep_words(query, callback_data): lists = mysql_connect.get_list_names(query.from_user.id) list_name = lists[int(callback_data['data'])] mysql_connect.del_list_keep_words(query.from_user.id, list_name) await bot.send_message( query.from_user.id, "{} is deleted. " "Word are saved to the general list".format(list_name))
async def start_learning_message(message): session, isValid = await authorize(message.from_user.id, with_lang=True) if not isValid: return if message.text == '/test': # FIXME do I need it? (Used in adding words to specify calls. Should be replaced with normal dp.callback_query_handler session.status = '/test' if message.text == '/learn': session.status = '/learn' await message.reply("OK, let's learn some " + session.active_lang()) hids = sr.get_items_to_learn( (session.get_user_id(), session.active_lang()), upper_recall_limit=0.5) lists = mysql_connect.get_list_names(message.from_user.id) keys = ['Learn all words (use /stop to finish learning)'] data = [-1] actions = ["start_learning"] await bot.send_message(session.get_user_id(), "You have {} words to learn.\n".format(len(hids))) if len(lists) > 0: keys.extend(lists) data.extend(list(range(len(lists)))) actions.extend(["start_learning"] * len(lists)) await bot.send_message(session.get_user_id(), "You have {} lists to learn.\n" .format(len(lists))) kb = to_vertical_keyboard(keys, data=data, action=actions) await bot.send_message(session.get_user_id(), "What do you want to learn now?", reply_markup=kb)
async def del_list_del_words_action(query, callback_data): session, isValid = await authorize(query.from_user.id) if not isValid: return lists = mysql_connect.get_list_names(query.from_user.id, session.active_lang()) list_name = lists[int(callback_data['data'])] mysql_connect.del_list_del_words(query.from_user.id, list_name) await bot.send_message(query.from_user.id, "{} is deleted.".format(list_name))
async def delete_list(session): keys = mysql_connect.get_list_names(session.get_user_id()) data = list(range(len(keys))) actions = ["delete_list"] * len(keys) keys.append("CANCEL") data.append(-1) actions.append("keep_list") k = to_vertical_keyboard(keys, data, actions) await bot.send_message(session.get_user_id(), "Which list do you want to delete?", reply_markup=k)
async def delete_list_action(query, callback_data): session, isValid = await authorize(query.from_user.id) if not isValid: return lists = mysql_connect.get_list_names(session.get_user_id()) list_name = lists[int(callback_data['data'])] keys = [ 'DELETE the list, KEEP the words', 'DELETE the list, DELETE al its words', 'CANCEL' ] data = [callback_data['data']] * 3 actions = ['del_list_keep_words', 'del_list_del_words', "keep_list"] k = to_vertical_keyboard(keys, data, actions) await bot.send_message( query.from_user.id, "You are about to delete list *{}*\nDo you want to delete all the words in the list? " "Or should I keep the words and delete the list only?".format( list_name), reply_markup=k)
async def start_learning_message(message): """ Gets the /learn or /test commands makes a menu to select from :param message: :return: sends messege with a menu to select learning items from """ session, isValid = await authorize(message.from_user.id, with_lang=True) if not isValid: return if message.text == '/test': # FIXME do I need it? (Used in adding words to specify calls. Should be replaced with normal dp.callback_query_handler session.status = '/test' if message.text == '/learn': session.status = '/learn' await message.reply("OK, let's learn some " + session.active_lang()) kb_data = list() kb_data.append(( ('10', 10, "learn_all_words"), ('20', 20, "learn_all_words"), ('30', 30, "learn_all_words"), ('40', 40, "learn_all_words"), )) kb_data.append((('Do all tasks (use /stop to finish learning)', -1, "learn_all_words"), )) lists = mysql_connect.get_list_names(message.from_user.id, session.active_lang()) if len(lists) > 0: for i in range(len(lists)): kb_data.append(((lists[i], i, "learn_words_from_list"), )) kb = flexy_keyboard(kb_data) await bot.send_message(session.get_user_id(), "What do you want to learn now?", reply_markup=kb)
async def learn_words_from_list(query: types.CallbackQuery, callback_data: dict): logger.debug( str(query.from_user.id) + " learn_all_words " + str(callback_data)) session, isValid = await authorize(query.from_user.id, with_lang=True) if not isValid: return lists = mysql_connect.get_list_names(query.from_user.id, session.active_lang()) list_name = lists[int(callback_data['data'])] logger.info("{} learns {}", query.from_user.id, list_name) text_hid = mysql_connect.fetchone( 'SELECT text_hid FROM user_texts WHERE user=%s AND list_name=%s', (session.get_user_id(), list_name)) if text_hid is not None: summary = mysql_connect.fetchone( 'SELECT summary FROM text_summary WHERE user=%s AND hid=%s', (session.get_user_id(), text_hid[0])) if summary is not None: # (word, definition, mode, hid)] session.words_to_learn = list() session.words_to_learn.append( (summary[0], list_name, 20, text_hid[0])) k = to_one_row_keyboard(['Words', 'Summary'], [0, 1], ['text_words', 'text_summary']) await bot.send_message( session.get_user_id(), 'You created a summary for text _{}_.\n' 'Would you like to learn words or continue with your summary?'. format(list_name), reply_markup=k) return hids = mysql_connect.get_hids_for_list(query.from_user.id, list_name) logger.info("{} has {} tasks from list {}", query.from_user.id, len(hids), list_name) hids_all = sr.get_items_to_learn( (session.get_user_id(), session.active_lang()), upper_recall_limit=0.5) logger.info("{} has {} tasks to learn", query.from_user.id, len(hids_all)) hids = list(set(hids) & set(hids_all)) logger.info("{} has {} tasks from list {} to learn", query.from_user.id, len(hids), list_name) # hids = list() #FIXME NOW delete after testing!!! if len(hids) == 0: sentence_hids = mysql_connect.get_sentence_hids( query.from_user.id, list_name) sentence_hids = ilt.get_objects(sentence_hids, '1 day', session.get_user_id(), session.active_lang(), "SENTENCE", 10) logger.info("{} has {} sentences from list {} to learn", query.from_user.id, len(sentence_hids), list_name) await bot.send_message( query.from_user.id, "You have {} sentences from list {} to learn".format( len(sentence_hids), list_name)) if len(sentence_hids) > 0: session.current_level = 10 # Syntax learning await learn_sentences(query.from_user.id, list_name, session, sentence_hids) else: session.current_level = 20 # Text learning await texts.text_summarization(query.from_user.id, list_name, session) else: words = mysql_connect.fetch_by_hids(session.get_user_id(), hids) logger.debug("{}, fetched {} tasks to do", session.get_user_id(), len(words)) session.words_to_learn = words session.current_word = 0 await start_learning(session)
async def del_list_del_words_action(query, callback_data): lists = mysql_connect.get_list_names(query.from_user.id) list_name = lists[int(callback_data['data'])] mysql_connect.del_list_del_words(query.from_user.id, list_name) await bot.send_message(query.from_user.id, "{} is deleted.".format(list_name))
async def learning(query: types.CallbackQuery, callback_data: dict): await query.answer("Let's learn!") logger.debug(query) logger.debug(str(query.from_user.id) + "start_learning " + str(callback_data)) n = int(callback_data['data']) session, isValid = await authorize(query.from_user.id, with_lang=True) if not isValid: return upper_recall_limit = 0.5 if session.status == '/test': upper_recall_limit = 1.0 if n == -1: # General vocabulary selected hids = sr.get_items_to_learn( (session.get_user_id(), session.active_lang()), upper_recall_limit=upper_recall_limit) if len(hids) == 0: if session.status == '/test': await bot.send_message(session.get_user_id(), 'You should add at least one word with /addwords command to start training') else: await bot.send_message(session.get_user_id(), 'You don\'t have words for training.') await bot.send_message(session.get_user_id(), 'Add more words with /addwords command or') await bot.send_message(session.get_user_id(), 'or /test words you learned before.') return True sentences = False if n >= 0: # One of the lists selected lists = mysql_connect.get_list_names(query.from_user.id) list_name = lists[int(callback_data['data'])] logger.info("{} learns {}", query.from_user.id, list_name) text_hid = mysql_connect.fetchone('SELECT text_hid FROM user_texts WHERE user=%s AND list_name=%s', (session.get_user_id(), list_name)) if text_hid is not None: summary = mysql_connect.fetchone('SELECT summary FROM text_summary WHERE user=%s AND hid=%s', (session.get_user_id(), text_hid[0])) if summary is not None: # (word, definition, mode, hid)] session.words_to_learn = list() session.words_to_learn.append((summary[0], list_name, 20, text_hid[0])) k = to_one_row_keyboard(['Words', 'Summary'], [0, 1], ['text_words', 'text_summary']) await bot.send_message(session.get_user_id(), 'You created a summary for text _{}_.\n' 'Would you like to learn words or continue with your summary?' .format(list_name), reply_markup=k) return hids = mysql_connect.get_hids_for_list(query.from_user.id, list_name) logger.info("{} has {} words from list {}", query.from_user.id, len(hids), list_name) hids_all = sr.get_items_to_learn( (session.get_user_id(), session.active_lang()), upper_recall_limit=upper_recall_limit) logger.info("{} has {} words to learn", query.from_user.id, len(hids_all)) hids = list(set(hids) & set(hids_all)) logger.info("{} has {} words from list {} to learn", query.from_user.id, len(hids), list_name) # hids = list() #FIXME NOW delete after testing!!! if len(hids) == 0: sentence_hids = mysql_connect.get_sentence_hids(query.from_user.id, list_name) sentence_hids = ilt.get_objects(sentence_hids, '1 day', session.get_user_id(), session.active_lang(), "SENTENCE", 10) logger.info("{} has {} sentences from list {} to learn", query.from_user.id, len(sentence_hids), list_name) await bot.send_message(query.from_user.id, "You have {} sentences from list {} to learn" .format(len(sentence_hids), list_name)) sentences = True if len(sentence_hids) > 0: session.current_level = 10 #Syntax learning await learn_sentences(query.from_user.id, list_name, session, sentence_hids) else: session.current_level = 20 #Text learning await texts.text_summarization(query.from_user.id, list_name, session) if not sentences: words = mysql_connect.fetch_by_hids(session.get_user_id(), hids) session.words_to_learn = words session.current_word = 0 if not session.has_more_words_to_learn(): # Case 2: doing reading errors await bot.send_message(session.get_user_id(), "Let's revise some words") await reading.do_reading_errors(query, callback_data) else: # Case 1: reading exercises await start_learning(session)