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 = '' % (user_level, text_level) + s += '

Article added on: %s

' % (d['date']) + s += '
' + article_title = get_article_title(d['text']) + article_body = get_article_body(d['text']) + s += '

%s

' % (article_title) + s += '

%s

' % (article_body) + s += '

%s

' % (d['source']) s += '

%s

' % (get_question_part(d['question'])) s = s.replace('\n', '
') s += '%s' % (get_answer_part(d['question'])) + s += '
' session['articleID'] = d['article_id'] return s @@ -178,6 +193,15 @@ def get_answer_part(s): return html_code +def get_flashed_messages_if_any(): + messages = get_flashed_messages() + s = '' + for message in messages: + s += '' + return s + @app.route("//reset", methods=['GET', 'POST']) def user_reset(username): @@ -230,11 +254,14 @@ def mainpage(): + + EnglishPal 英文单词高效记 ''' + page += '
' page += '

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 += '

advertisement

' % (get_random_image(path_prefix + 'static/img/')) page += '

%s

' % (get_random_ads()) - page += '

共有文章%d篇' % (total_number_of_essays()) + page += '

' % (total_number_of_essays()) page += '

粘帖1篇文章 (English only)

' page += '
' page += '
' @@ -257,6 +284,8 @@ def mainpage(): break page += '%s %d\n' % (youdao_link(x[0]), x[0], x[1]) + page += ' ' + page += '
' page += '' return page @@ -283,6 +312,7 @@ def unfamiliar(username,word): user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) pickle_idea.unfamiliar(user_freq_record,word) session['thisWord'] = word # 1. put a word into session + session['time'] = 1 return redirect(url_for('userpage', username=username)) @app.route("///familiar", methods=['GET', 'POST']) @@ -290,12 +320,14 @@ def familiar(username,word): user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) pickle_idea.familiar(user_freq_record,word) session['thisWord'] = word # 1. put a word into session + session['time'] = 1 return redirect(url_for('userpage', username=username)) @app.route("///del", methods=['GET', 'POST']) def deleteword(username,word): user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username) pickle_idea2.deleteRecord(user_freq_record,word) + flash(f'{word} is no longer in your word list.') return redirect(url_for('userpage', username=username)) @app.route("/", methods=['GET', 'POST']) @@ -334,10 +366,13 @@ def userpage(username): page = '\n' page += '\n' page += '\n' # forbid treating numbers as cell numbers in smart phones + page += '' page += 'EnglishPal Study Room for %s' % (username) - page += '

English Pal for %s 登出

' % (username) - page += '

下一篇

' % (username) + page += '
' + page += '

English Pal for %s 登出

' % (username) + page += get_flashed_messages_if_any() page += '

阅读文章并回答问题

\n' + page += '

下一篇 Next Article

' % (username) page += '
%s
' % (get_today_article(user_freq_record, session['articleID'])) page += '

收集生词吧 (可以在正文中划词,也可以复制黏贴)

' page += '' % (username) @@ -362,7 +397,12 @@ def userpage(username): if session.get('thisWord'): page += ''' ''' @@ -376,15 +416,18 @@ def userpage(username): for x in sort_in_descending_order(lst2): word = x[0] freq = x[1] - if session.get('thisWord') == x[0]: + if session.get('thisWord') == x[0] and session.get('time') == 1: page += '' # 3. anchor + session['time'] = 0 # discard anchor if isinstance(d[word], list): # d[word] is a list of dates if freq > 1: - page += '

%s(%d) 熟悉 不熟悉 删除

\n' % (youdao_link(word), word, '; '.join(d[word]), freq,username, word,username,word, username,word) + page += '

%s(%d) 熟悉 不熟悉 删除

\n' % (youdao_link(word), word, '; '.join(d[word]), freq,username, word,username,word, username,word) else: - page += '

%s(%d) 熟悉 不熟悉 删除

\n' % (youdao_link(word), word, '; '.join(d[word]), freq,username, word,username,word, username,word) + page += '

%s(%d) 熟悉 不熟悉 删除

\n' % (youdao_link(word), word, '; '.join(d[word]), freq,username, word,username,word, username,word) elif isinstance(d[word], int): # d[word] is a frequency. to migrate from old format. page += '%s%d\n' % (youdao_link(word), word, freq) + page += '' + page += '
' return page ### Sign-up, login, logout ### @@ -409,6 +452,8 @@ def signup(): session['logged_in'] = True session[username] = username session['username'] = username + session['expiry_date'] = get_expiry_date(username) + session['articleID'] = None return '

恭喜,你已成功注册, 你的用户名是 %s

\

开始使用 返回首页

' % (username, username, username) else: @@ -451,4 +496,3 @@ if __name__ == '__main__': app.run(debug=True) #app.run(debug=True, port='6000') #app.run(host='0.0.0.0', debug=True, port='6000') - diff --git a/build.sh b/build.sh index c7c3019..89d6543 100644 --- a/build.sh +++ b/build.sh @@ -4,12 +4,13 @@ cd /home/lanhui/englishpal # Stop service sudo docker stop EnglishPal +sudo docker rm EnglishPal # Rebuild container. Run this after modifying the source code. sudo docker build -t englishpal . # Run the application -sudo docker run -d --name EnglishPal -p 90:80 -v /home/lanhui/englishpal/app/static/frequency:/app/static/frequency -t englishpal # for permanently saving data +sudo docker run -d --name EnglishPal -p 90:80 -v /home/lanhui/englishpal/app/static/frequency:/app/static/frequency -v /home/lanhui/englishpal/app/static/:/app/static/ -t englishpal # for permanently saving data # Save space. Run it after sudo docker run sudo docker system prune -a -f diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d42ebf8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Flask==1.1.2 +selenium==3.141.0