def get_items_to_learn(user_language, upper_recall_limit=0.5, n=-1): result = list() now = datetime.now() tmp = dict() if (user_language[1] is not None): words = db.fetchall("SELECT hid, model, last_date FROM spaced_repetition WHERE user=%s and language=%s", (user_language[0], user_language[1])) else: words = db.fetchall("SELECT hid, model, last_date FROM spaced_repetition WHERE user=%s", (user_language[0],)) for word in words: hid = word[0] model = tuple(json.loads(word[1])) lastTest = word[2] if lastTest is None: result.append(hid) continue lastTest = datetime.strptime(lastTest, "%Y-%m-%dT%H:%M:%S.%f") recall = ebisu.predictRecall(model, (now - lastTest) / oneHour, exact=True) if recall <= upper_recall_limit: tmp[hid] = recall recalls = list(tmp.values()) recalls.sort() if n > 0: n = min(n, len(recalls)) else: n = len(recalls) for r in recalls: for hid, recall in tmp.items(): if len(result) >= n: break if recall == r: result.append(hid) return result
async def show_command(message: types.Message): logger.info(str(message.from_user.id) + ' ' + str(message.text)) session, isValid = await authorize(message.from_user.id, with_lang=True) if not isValid: return cmd = message.text if ' ' in cmd: cmd2 = str(cmd).split(' ') if cmd2[1] == 'list': # TODO ask for list name and show words from the list pass elif cmd2[1] == 'date': words = mysql_connect.fetchall( "SELECT w.word, w.definition, DATE_FORMAT(s.created_at, '%Y-%m-%d') AS date FROM words w INNER JOIN spaced_repetition s ON w.hid = s.hid WHERE w.user =%s AND w.language=%s AND w.mode = 0 ORDER BY date", (session.get_user_id(), session.active_lang())) elif cmd2[1] == 'last': LIMIT = "" if len(cmd2) == 3: n = cmd2[2] LIMIT = ' LIMIT ' + str(n) words = mysql_connect.fetchall( "SELECT w.word, w.definition, DATE_FORMAT(s.created_at, '%Y-%m-%d') AS date FROM words w INNER JOIN spaced_repetition s ON w.hid = s.hid WHERE w.user =%s AND w.language=%s AND w.mode = 0 ORDER BY date DESC" + LIMIT, (session.get_user_id(), session.active_lang())) else: letter = str(cmd2[1]) + '%' words = mysql_connect.fetchall( "SELECT word, definition FROM words WHERE user =%s AND language=%s AND mode = 0 AND word LIKE %s ORDER BY word", (session.get_user_id(), session.active_lang(), letter)) else: words = mysql_connect.fetchall( "SELECT word, definition FROM words WHERE user=%s AND language=%s AND mode = 0 ORDER BY word", (session.get_user_id(), session.active_lang())) for w in words: date_str = "" if len(w) == 3: date_str = "\n" + str(w[2]) await bot.send_message(session.get_user_id(), "<b>{}</b> : {}".format(w[0], w[1]) + date_str, parse_mode=types.ParseMode.HTML, disable_notification=True) time.sleep(.1) await bot.send_message(session.get_user_id(), "Total: {} words".format(len(words)))
def get_user_words(user_id, lang): result = set() words = mysql_connect.fetchall(QUERY.format(lang), (user_id, )) words = words[:N_LAST] for w in words: result.add(w[0]) return result
async def deleting_word(message): session, isValid = await authorize(message.from_user.id, with_lang=True) if not isValid: return logger.info( str(session.get_user_id()) + " is deleting word " + message.text) data = mysql_connect.fetchall( "SELECT word, definition, hid FROM words " "WHERE user=%s and language=%s and word=%s", (session.get_user_id(), session.active_lang(), message.text)) session.status = "" if data is None: await bot.send_message(session.get_user_id(), 'The word does not exist in you dictionary') return session.hid_cash = list(x[2] for x in data) k = to_one_row_keyboard(["Keep", "Delete"], data=[0, 1], action=["keep", "delete"]) w = data[0][0] d = 'NA' if len(data[0]) > 1: d = data[0][1] await bot.send_message( session.get_user_id(), "Do you want to delete word *{}* with definition\n{}".format(w, d), reply_markup=k)
def get_user_last_activity(user=None): if user is None: print("ERROR, need user_id") return else: query = "SELECT last_date FROM spaced_repetition WHERE user=%s" rows = mysql_connect.fetchall(query, (user, )) return rows
async def get_definitions(language, user_lang, word, user): result = list() sources = mysql_connect.fetchall('SELECT source FROM def_sources WHERE user=%s', (user, )) sources = set(x[0] for x in sources) if user_lang is None: user_lang = 'english' # See list of available sources in generic.py if 'Yandex Dictionary' in sources: if 'Yandex Dictionary_' + user_lang + language + '_' + word in MEM_CACHE.keys(): result = MEM_CACHE['Yandex Dictionary_' + user_lang + language + '_' + word] else: try: response = ya_dict.lookup(word, CODES[language], get_lang_code(user_lang)) result = to_list(json.loads(response)) MEM_CACHE['Yandex Dictionary_' + user_lang + language + '_' + word] = result except Exception as e: logger.warning("Yandex dictionary exception: " + str(e)) if 'Wiktionary' in sources: if 'Wiktionary_' + language + '_' + word in MEM_CACHE.keys(): result.extend(MEM_CACHE['Wiktionary_' + language + '_' + word]) else: try: w = parser.fetch(word.lower(), language=language) except Exception as e: logger.warning("Wiktionary exception: " + str(e)) if w is not None and len(w) > 0: res = process_wiktionary(w) if len(res) > 0: result.extend(res) MEM_CACHE['Wiktionary_' + language + '_' + word] = res if 'Google Translate' in sources or ' ' in word: if 'Google Translate_' + user_lang + language + '_' + word in MEM_CACHE.keys(): result.extend(MEM_CACHE['Google Translate_' + user_lang + language + '_' + word]) else: subscribed = mysql_connect.check_subscribed(user) limit = 50 if subscribed: limit = 500 if len(word) <= limit : try: tr = translate_client.translate( word, target_language=get_lang_code(user_lang)) result.append(tr['translatedText']) MEM_CACHE['Google Translate_' + user_lang + language + '_' + word] = tr['translatedText'] except Exception as e: logger.error(e) return result
def get_user_message(period): query = "SELECT user_id from users where blocked = %s" rows = mysql_connect.fetchall(query, (0, )) result = dict() for row in rows: user = row[0] times = get_user_last_activity(user) if is_within_time(period, times): continue n = number_of_words_to_train(user) result[user] = messages['all'] if n == 0: result[user] += messages["no_words_added"] else: result[user] += messages["has_words_to_learn"].format(n) return result