diff --git a/app/difficulty.py b/app/difficulty.py index ccd9b6f..50aa179 100644 --- a/app/difficulty.py +++ b/app/difficulty.py @@ -55,11 +55,17 @@ def get_difficulty_level(d1, d2): def revert_dict(d): ''' - In d2, time is the key, and the value is a list of words picked at that time. + In d, word is the key, and value is a list of dates. + In d2 (the returned value of this function), time is the key, and the value is a list of words picked at that time. ''' d2 = {} for k in d: - lst = d[k] + if type(d[k]) is list: # d[k] is a list of dates. + lst = d[k] + elif type(d[k]) is int: # for backward compatibility. d was sth like {'word':1}. The value d[k] is not a list of dates, but a number representing how frequent this word had been added to the new word book. + freq = d[k] + lst = freq*['2021082019'] # why choose this date? No particular reasons. I fix the bug in this date. + for time_info in lst: date = time_info[:10] # until hour if not date in d2: diff --git a/app/main.py b/app/main.py index 9008101..301d1bb 100644 --- a/app/main.py +++ b/app/main.py @@ -13,7 +13,7 @@ import pickle_idea, pickle_idea2 import os import random, glob from datetime import datetime -from flask import Flask, request, redirect, render_template, url_for, session, abort, flash +from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages from difficulty import get_difficulty_level, text_difficulty_level, user_difficulty_level app = Flask(__name__) @@ -81,6 +81,15 @@ def within_range(x, y, r): return x > y and abs(x - y) <= r +def get_article_title(s): + return s.split('\n')[0] + + +def get_article_body(s): + lst = s.split('\n') + lst.pop(0) # remove the first line + return '\n'.join(lst) + def get_today_article(user_word_list, articleID): rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') @@ -90,6 +99,7 @@ def get_today_article(user_word_list, articleID): rq.instructions('SELECT * FROM article WHERE article_id=%d' % (articleID)) rq.do() result = rq.get_results() + random.shuffle(result) # Choose article according to reader's level d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p') @@ -105,18 +115,23 @@ def get_today_article(user_word_list, articleID): if articleID == None: for reading in result: text_level = text_difficulty_level(reading['text'], d3) - #print('TEXT_LEVEL %4.2f' % (text_level)) - if within_range(text_level, user_level, 0.5): + factor = random.gauss(0.8, 0.1) # a number drawn from Gaussian distribution with a mean of 0.8 and a stand deviation of 1 + if within_range(text_level, user_level, (8.0 - user_level)*factor): d = reading break - s = '
According to your word list, your level is %4.2f and we have chosen an article with a difficulty level of %4.2f for you.
' % (user_level, text_level) - s += '%s
' % (d['date']) - s += '%s
' % (d['text']) - s += '%s
' % (d['source']) + s = 'Article added on: %s
' % (d['date']) + s += '%s
' % (article_title) + s += '%s
' % (article_body) + s += '%s
' % (d['source']) s += '%s
' % (get_question_part(d['question'])) s = s.replace('\n', 'English Pal - Learn English smartly!
' if session.get('logged_in'): page += ' %s\n' % (session['username'], session['username']) @@ -242,7 +269,7 @@ def mainpage(): page += '\n' #page += '' % (get_random_image(path_prefix + 'static/img/')) page += '%s
' % (get_random_ads()) - page += '共有文章%d篇' % (total_number_of_essays()) + page += '
粘帖1篇文章 (English only)
' page += '