From 15bb9250248aa6feb249be72c53d3fa6d7005d52 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=80=E9=97=AE=E4=B8=89=E4=B8=8D=E7=9F=A5?=
<178428409@qq.com>
Date: Tue, 4 Apr 2023 22:31:53 +0800
Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E8=AE=B0=E5=BD=95=E9=98=85=E8=AF=BB?=
=?UTF-8?q?=E8=BF=87=E6=96=87=E7=AB=A0=E7=9A=84=E6=95=B0=E6=8D=AE=E7=BB=93?=
=?UTF-8?q?=E6=9E=9C=E6=94=B9=E4=B8=BA=E5=AD=97=E5=85=B8=EF=BC=8C=E4=BB=A5?=
=?UTF-8?q?=E5=8F=8A=E4=BF=AE=E6=94=B9=E4=BA=86flag=E7=9A=84=E9=97=AE?=
=?UTF-8?q?=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/Article.py | 28 +++++++++++++++-------------
app/templates/userpage_get.html | 2 +-
app/user_service.py | 4 ++--
3 files changed, 18 insertions(+), 16 deletions(-)
diff --git a/app/Article.py b/app/Article.py
index 6281c38..e0f006a 100644
--- a/app/Article.py
+++ b/app/Article.py
@@ -35,11 +35,14 @@ def get_article_body(s):
def get_today_article(user_word_list, existing_articles):
rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
if existing_articles is None:
- existing_articles = [0, []] # existing_articles[0]:为existing_articles[1]的索引;existing_articles[1]:之前显示文章的id列表,越后越新
- if existing_articles[0] > len(existing_articles[1])-1:
+ existing_articles = {
+ "index" : 0, # 为 article_ids 的索引
+ "article_ids": [] # 之前显示文章的id列表,越后越新
+ }
+ if existing_articles["index"] > len(existing_articles["article_ids"])-1:
rq.instructions("SELECT * FROM article")
else:
- rq.instructions('SELECT * FROM article WHERE article_id=%d' % (existing_articles[1][existing_articles[0]]))
+ rq.instructions('SELECT * FROM article WHERE article_id=%d' % (existing_articles["article_ids"][existing_articles["index"]]))
rq.do()
result = rq.get_results()
random.shuffle(result)
@@ -49,28 +52,29 @@ def get_today_article(user_word_list, existing_articles):
d2 = load_freq_history(path_prefix + 'static/words_and_tests.p')
d3 = get_difficulty_level(d1, d2)
- d = {}
+ d = None
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.
text_level = 0
- flag = False
- if existing_articles[0] > len(existing_articles[1])-1: # 下一篇
+ if existing_articles["index"] > len(existing_articles["article_ids"])-1: # 下一篇
+ flag_get_article = False
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 reading['article_id'] not in existing_articles[1] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平
+ if reading['article_id'] not in existing_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平
d = reading
- existing_articles[1].append(d['article_id']) # 列表添加新的文章id;下面进行
- flag = True
+ existing_articles["article_ids"].append(d['article_id']) # 列表添加新的文章id;下面进行
+ flag_get_article = True
break
+ if not flag_get_article:
+ existing_articles["index"] -= 1
else: # 上一篇
d = random.choice(result)
text_level = text_difficulty_level(d['text'], d3)
- flag = True
today_article = None
- if flag:
+ if d:
today_article = {
"user_level": '%4.2f' % user_level,
"text_level": '%4.2f' % text_level,
@@ -81,8 +85,6 @@ def get_today_article(user_word_list, existing_articles):
"question": get_question_part(d['question']),
"answer": get_answer_part(d['question'])
}
- else:
- existing_articles[0] -= 1
return existing_articles, today_article
diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html
index c9a04c1..298b559 100644
--- a/app/templates/userpage_get.html
+++ b/app/templates/userpage_get.html
@@ -45,7 +45,7 @@
{# {% endfor %}#}
下一篇 Next Article
- {% if session.get('existing_articles')[0] != None and session.get('existing_articles')[0] !=0 %}
+ {% if session.get('existing_articles') != None and session.get('existing_articles')["index"] !=0 %}
上一篇 Previous Article
{% endif %}
diff --git a/app/user_service.py b/app/user_service.py
index 1b47d74..8d1dbfc 100644
--- a/app/user_service.py
+++ b/app/user_service.py
@@ -31,7 +31,7 @@ def user_reset(username):
'''
if request.method == 'GET':
existing_articles = session.get("existing_articles")
- existing_articles[0] += 1
+ existing_articles["index"] += 1
session["existing_articles"] = existing_articles
return redirect(url_for('user_bp.userpage', username=username))
else:
@@ -46,7 +46,7 @@ def user_back(username):
'''
if request.method == 'GET':
existing_articles = session.get("existing_articles")
- existing_articles[0] -= 1
+ existing_articles["index"] -= 1
session["existing_articles"] = existing_articles
return redirect(url_for('user_bp.userpage', username=username))