WIP: IMPROVE+BUGFIX #38

Draft
liuchangyou wants to merge 3 commits from Bug209-LiuChangYou into Alpha-snapshot20240618
4 changed files with 77 additions and 30 deletions

View File

@ -32,36 +32,40 @@ def get_article_body(s):
return '\n'.join(lst) return '\n'.join(lst)
def get_today_article(user_word_list, articleID): def get_today_article(user_word_list):
rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
if articleID == None: articleID = session['articleID']
rq.instructions("SELECT * FROM article")
else:
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 # Choose article according to reader's level
d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p') d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p')
d2 = load_freq_history(path_prefix + 'static/words_and_tests.p') d2 = load_freq_history(path_prefix + 'static/words_and_tests.p')
d3 = get_difficulty_level(d1, d2) d3 = get_difficulty_level(d1, d2)
d = {}
d_user = load_freq_history(user_word_list) d_user = load_freq_history(user_word_list)
user_level = user_difficulty_level(d_user, d3) # more consideration as user's behaviour is dynamic. Time factor should be considered. # more consideration as user's behaviour is dynamic. Time factor should be considered.
random.shuffle(result) # shuffle list user_level = user_difficulty_level(d_user, d3)
d = random.choice(result)
text_level = text_difficulty_level(d['text'], d3)
if articleID == None:
for reading in result:
text_level = text_difficulty_level(reading['text'], d3)
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
if articleID == None:
if not session['article_id_lst']:
rq.instructions("SELECT * FROM article")
rq.do()
result = rq.get_results()
random.shuffle(result)
lst = []
for reading in result:
text_level = text_difficulty_level(reading['text'], d3)
# a number drawn from Gaussian distribution with a mean of 0.8 and a stand deviation of 1
factor = random.gauss(0.8,0.1)
if within_range(text_level, user_level, (8.0 - user_level) * factor):
lst.append(reading['article_id'])
session['article_id_lst'] = lst
articleID = session['article_id_lst'].pop(0)
rq.instructions('SELECT * FROM article WHERE article_id=%d' % (articleID))
rq.do()
result = rq.get_results()
d = result[0]
text_level = text_difficulty_level(d['text'], d3)
s = '<div class="alert alert-success" role="alert">According to your word list, your level is <span class="badge bg-success">%4.2f</span> and we have chosen an article with a difficulty level of <span class="badge bg-success">%4.2f</span> for you.</div>' % ( s = '<div class="alert alert-success" role="alert">According to your word list, your level is <span class="badge bg-success">%4.2f</span> and we have chosen an article with a difficulty level of <span class="badge bg-success">%4.2f</span> for you.</div>' % (
user_level, text_level) user_level, text_level)
s += '<p class="text-muted">Article added on: %s</p>' % (d['date']) s += '<p class="text-muted">Article added on: %s</p>' % (d['date'])

View File

@ -92,6 +92,7 @@ def mainpage():
return render_template('mainpage_post.html', lst=lst, yml=Yaml.yml) return render_template('mainpage_post.html', lst=lst, yml=Yaml.yml)
elif request.method == 'GET': # when we load a html page elif request.method == 'GET': # when we load a html page
session['article_id_lst'] = None
random_ads = get_random_ads() random_ads = get_random_ads()
number_of_essays = total_number_of_essays() number_of_essays = total_number_of_essays()
d = load_freq_history(path_prefix + 'static/frequency/frequency.p') d = load_freq_history(path_prefix + 'static/frequency/frequency.p')

View File

@ -69,10 +69,9 @@
( (
<a title="{{ word }}">{{ freq }}</a> <a title="{{ word }}">{{ freq }}</a>
) )
<a class="btn btn-success" onclick="updateWordFrequency(this,'{{username}}','{{word}}',-1)" role="button">熟悉</a>
<a class="btn btn-success" href={{ username }}/{{ word }}/familiar role="button">熟悉</a> <a class="btn btn-warning" onclick="updateWordFrequency(this,'{{username}}','{{word}}',1)" role="button">不熟悉</a>
<a class="btn btn-warning" href={{ username }}/{{ word }}/unfamiliar role="button">不熟悉</a> <a class="btn btn-danger" onclick="deleteWord(this,'{{username}}','{{word}}')" role="button">删除</a>
<a class="btn btn-danger" href={{ username }}/{{ word }}/del role="button">删除</a>
</p> </p>
{% else %} {% else %}
<p class="new-word"> <p class="new-word">
@ -81,9 +80,9 @@
( (
<a title="{{ word }}">{{ freq }}</a> <a title="{{ word }}">{{ freq }}</a>
) )
<a class="btn btn-success" href={{ username }}/{{ word }}/familiar role="button">熟悉</a> <a class="btn btn-success" onclick="updateWordFrequency(this,'{{username}}','{{word}}',-1)" role="button">熟悉</a>
<a class="btn btn-warning" href={{ username }}/{{ word }}/unfamiliar role="button">不熟悉</a> <a class="btn btn-warning" onclick="updateWordFrequency(this,'{{username}}','{{word}}',1)" role="button">不熟悉</a>
<a class="btn btn-danger" href={{ username }}/{{ word }}/del role="button">删除</a> <a class="btn btn-danger" onclick="deleteWord(this,'{{username}}','{{word}}')" role="button">删除</a>
</p> </p>
{% endif %} {% endif %}
{% else %} {% else %}
@ -98,6 +97,49 @@
<script src="{{ js }}"></script> <script src="{{ js }}"></script>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
function updateWordFrequency(caller,username,word,difference) {
let type = difference<0?'familiar':'unfamiliar'
//后端更新数据
$.ajax({
method: 'get',
url: '/' + username + '/' + word + '/' + type,
async: true,
error: function(response) {
alert('操作失败,稍后再试')
location.reload()
caller.scrollIntoView()
}
})
//前端更新页面
let par = $(caller).parent()
let freqEle = par.children().eq(1)
let newFreq = parseInt(freqEle.text()) + difference
if(newFreq <= 0) {
par.attr('hidden',true)
}
freqEle.text(newFreq)
}
function deleteWord(caller,username,word) {
$.ajax({
method: 'get',
url: '/' + username + '/' + word + '/del',
async: true,
error: function(response) {
alert('操作失败,稍后再试')
location.reload()
caller.scrollIntoView()
}
})
$(caller).parent().attr('hidden',true)
}
</script>
</body> </body>
<style> <style>
mark { mark {

View File

@ -121,7 +121,7 @@ def userpage(username):
username=username, username=username,
session=session, session=session,
flashed_messages=get_flashed_messages_if_any(), flashed_messages=get_flashed_messages_if_any(),
today_article=get_today_article(user_freq_record, session['articleID']), today_article=get_today_article(user_freq_record),
d_len=len(d), d_len=len(d),
lst3=lst3, lst3=lst3,
yml=Yaml.yml, yml=Yaml.yml,