添加了阅读完所有文章的提示

王梓铭 2023-04-20 22:53:30 +08:00
parent da13e5bbd5
commit 8f132ed87b
3 changed files with 33 additions and 18 deletions

View File

@ -39,9 +39,9 @@ def get_today_article(user_word_list, had_read_articles):
"index" : 0, # 为 article_ids 的索引 "index" : 0, # 为 article_ids 的索引
"article_ids": [] # 之前显示文章的id列表越后越新 "article_ids": [] # 之前显示文章的id列表越后越新
} }
if had_read_articles["index"] > len(had_read_articles["article_ids"])-1: if had_read_articles["index"] > len(had_read_articles["article_ids"])-1: # 生成新的文章,因此查找所有的文章
rq.instructions("SELECT * FROM article") rq.instructions("SELECT * FROM article")
else: else: # 生成阅读过的文章,因此查询指定 article_id 的文章
rq.instructions('SELECT * FROM article WHERE article_id=%d' % (had_read_articles["article_ids"][had_read_articles["index"]])) rq.instructions('SELECT * FROM article WHERE article_id=%d' % (had_read_articles["article_ids"][had_read_articles["index"]]))
rq.do() rq.do()
result = rq.get_results() result = rq.get_results()
@ -53,23 +53,33 @@ def get_today_article(user_word_list, had_read_articles):
d3 = get_difficulty_level(d1, d2) d3 = get_difficulty_level(d1, d2)
d = None d = None
result_of_generate_article = "not found"
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. user_level = user_difficulty_level(d_user, d3) # more consideration as user's behaviour is dynamic. Time factor should be considered.
text_level = 0 text_level = 0
if had_read_articles["index"] > len(had_read_articles["article_ids"])-1: # 下一篇 if had_read_articles["index"] > len(had_read_articles["article_ids"])-1: # 生成新的文章
flag_get_article = False amount_of_had_read_articles = len(had_read_articles["article_ids"])
for reading in result: amount_of_existing_articles = result.__len__()
text_level = text_difficulty_level(reading['text'], d3) if amount_of_had_read_articles == amount_of_existing_articles: # 如果当前阅读过的文章的数量 == 存在的文章的数量,即所有的书本都阅读过了
factor = random.gauss(0.8, result_of_generate_article = "had read all articles"
0.1) # a number drawn from Gaussian distribution with a mean of 0.8 and a stand deviation of 1 else:
if reading['article_id'] not in had_read_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平 test_count = 0
d = reading for k in range(3): # 最多尝试3次
had_read_articles["article_ids"].append(d['article_id']) # 列表添加新的文章id下面进行 test_count += 1
flag_get_article = True for reading in result:
break text_level = text_difficulty_level(reading['text'], d3)
if not flag_get_article: 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 had_read_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平
d = reading
had_read_articles["article_ids"].append(d['article_id']) # 列表添加新的文章id下面进行
result_of_generate_article = "found"
break
if result_of_generate_article == "found": # 用于成功找到文章后及时退出外层循环
break
print(test_count)
if result_of_generate_article != "found": # 阅读完所有文章或者循环3次没有找到适合的文章则对 index+=1 进行回滚
had_read_articles["index"] -= 1 had_read_articles["index"] -= 1
else: # 上一篇 else: # 生成已经阅读过的文章
d = random.choice(result) d = random.choice(result)
text_level = text_difficulty_level(d['text'], d3) text_level = text_difficulty_level(d['text'], d3)
@ -86,7 +96,7 @@ def get_today_article(user_word_list, had_read_articles):
"answer": get_answer_part(d['question']) "answer": get_answer_part(d['question'])
} }
return had_read_articles, today_article return had_read_articles, today_article, result_of_generate_article
def load_freq_history(path): def load_freq_history(path):

View File

@ -71,10 +71,14 @@
<button onclick="toggle_visibility('answer');">ANSWER</button> <button onclick="toggle_visibility('answer');">ANSWER</button>
<div id="answer" style="display:none;">{{ today_article['answer'] }}</div><br/> <div id="answer" style="display:none;">{{ today_article['answer'] }}</div><br/>
</div> </div>
{% else %} {% elif result_of_generate_article == "not found" %}
<div class="alert alert-success" role="alert"> <div class="alert alert-success" role="alert">
<p class="text-muted"><span class="badge bg-success">Notes:</span><br>No article is currently available for you. You can try again a few times or mark new words in the passage to improve your level.</p> <p class="text-muted"><span class="badge bg-success">Notes:</span><br>No article is currently available for you. You can try again a few times or mark new words in the passage to improve your level.</p>
</div> </div>
{% elif result_of_generate_article == "had read all articles" %}
<div class="alert alert-success" role="alert">
<p class="text-muted"><span class="badge bg-success">Notes:</span><br>You've read all the articles.</p>
</div>
{% endif %} {% endif %}
</div> </div>

View File

@ -135,7 +135,7 @@ def userpage(username):
words = '' words = ''
for x in lst3: for x in lst3:
words += x[0] + ' ' words += x[0] + ' '
had_read_articles, today_article = get_today_article(user_freq_record, session.get('had_read_articles')) had_read_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('had_read_articles'))
session['had_read_articles'] = had_read_articles session['had_read_articles'] = had_read_articles
if today_article is None: if today_article is None:
session["found_article"] = False session["found_article"] = False
@ -147,6 +147,7 @@ def userpage(username):
session=session, session=session,
# flashed_messages=get_flashed_messages(), 仅有删除单词的时候使用到flash而删除单词是异步执行这里的信息提示是同步执行所以就没有存在的必要了 # flashed_messages=get_flashed_messages(), 仅有删除单词的时候使用到flash而删除单词是异步执行这里的信息提示是同步执行所以就没有存在的必要了
today_article=today_article, today_article=today_article,
result_of_generate_article=result_of_generate_article,
d_len=len(d), d_len=len(d),
lst3=lst3, lst3=lst3,
yml=Yaml.yml, yml=Yaml.yml,