From e3d646c10ca83a1e69162c8b6f8d2393dde71045 Mon Sep 17 00:00:00 2001 From: limengdie <2233601742@qq.com> Date: Mon, 27 May 2024 21:22:56 +0800 Subject: [PATCH] Fix bug476 --- app/Article.py | 13 +++- app/difficulty.py | 19 ++++- app/templates/userpage_get.html | 53 +++++++++++++ app/test/test_bug476_LiMengdie.py | 119 ++++++++++++++++++++++++++++++ app/user_service.py | 42 ++++++++++- 5 files changed, 236 insertions(+), 10 deletions(-) create mode 100644 app/test/test_bug476_LiMengdie.py diff --git a/app/Article.py b/app/Article.py index 566ceb6..f19002e 100644 --- a/app/Article.py +++ b/app/Article.py @@ -1,3 +1,4 @@ + from WordFreq import WordFreq from wordfreqCMD import youdao_link, sort_in_descending_order import pickle_idea, pickle_idea2 @@ -10,6 +11,7 @@ from difficulty import get_difficulty_level_for_user, text_difficulty_level, use from model.article import get_all_articles, get_article_by_id, get_number_of_articles import logging + path_prefix = './' db_path_prefix = './db/' # comment this line in deployment @@ -28,7 +30,8 @@ def get_article_body(s): return '\n'.join(lst) -def get_today_article(user_word_list, visited_articles): +#user_articlesWithoutNewWords_record 保存前端传来的用户阅读时长超过15秒且不含高亮生词的文章索引 +def get_today_article(user_word_list, visited_articles,user_articlesWithoutNewWords_record): if visited_articles is None: visited_articles = { "index" : 0, # 为 article_ids 的索引 @@ -57,7 +60,12 @@ def get_today_article(user_word_list, visited_articles): d_user = load_freq_history(user_word_list) logging.debug('* get_today_article(): user_difficulty_level() start') - user_level = user_difficulty_level(d_user, d3) # more consideration as user's behaviour is dynamic. Time factor should be considered. + + articles_id_list=None + if os.path.exists(user_articlesWithoutNewWords_record) != False: + articles_id_list = pickle_idea.load_record(user_articlesWithoutNewWords_record) + #将 用户阅读时长超过15秒且不含高亮生词的文章记录 传入user_difficulty_level并据此 提高用户level + user_level = user_difficulty_level(d_user, d3,articles_id_list) # more consideration as user's behaviour is dynamic. Time factor should be considered. logging.debug('* get_today_article(): done') text_level = 0 if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章 @@ -90,6 +98,7 @@ def get_today_article(user_word_list, visited_articles): "user_level": '%4.1f' % user_level, "text_level": '%4.1f' % text_level, "date": d['date'], + "article_id":d['article_id'],#该变量存储 用户阅读时长超过15秒且不含高亮生词的文章索引 "article_title": get_article_title(d['text']), "article_body": get_article_body(d['text']), "source": d["source"], diff --git a/app/difficulty.py b/app/difficulty.py index 1bd8d68..b75db6b 100644 --- a/app/difficulty.py +++ b/app/difficulty.py @@ -7,6 +7,7 @@ import pickle import math + from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order import snowballstemmer @@ -94,10 +95,21 @@ def revert_dict(d): return d2 -def user_difficulty_level(d_user, d): +#articlesWithoutNewWords 存储用户阅读时长超过15秒且不含高亮生词的文章索引信息(用户阅读完后没有添加生词的文章) +def user_difficulty_level(d_user, d,articlesWithoutNewWords): d_user2 = revert_dict(d_user) # key is date, and value is a list of words added in that date count = 0 geometric = 1 + #传入根据用户生词计算出的用户level + def get_level_sum(level): + if articlesWithoutNewWords!=None:#若用户阅读完后没有添加生词的文章索引不为空,则根据这些文章的等级适度提高用户的level + for article in articlesWithoutNewWords: + if(level= 10: - return geometric ** (1 / count) - - return geometric ** (1 / max(count, 1)) + return get_level_sum(geometric ** (1 / count)) + return get_level_sum(geometric ** (1 / max(count, 1))) def text_difficulty_level(s, d): s = remove_punctuation(s) diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html index edb8bf2..ac1eaa6 100644 --- a/app/templates/userpage_get.html +++ b/app/templates/userpage_get.html @@ -214,6 +214,12 @@ elements.rangeValueDisplay.textContent = `${rangeValue}x`; localStorage.setItem('rangeValue', rangeValue); }); + + startTime=Date.now();//记录用户页面的初始时间 + if(window.performance.navigation.type!=1){//将第一次加载页面时的文章id,level保存到本地 + sessionStorage.setItem('article_id','{{today_article['article_id']}}'); + sessionStorage.setItem('text_level','{{today_article['text_level']}}'); + } }; function clearSelectedWords() { @@ -223,6 +229,18 @@ function load_next_article(){ + //在进入下一篇之前处理 + endTime=Date.now();//记录用户点击下一篇文章时的时间 + spendtime=endTime-startTime;//计算出用户浏览该页面的时长,单位毫秒 + if(spendtime/1000>15){//若浏览时长大于15,说明用户认真阅读了该篇文章(时长小于15代表用户不感兴趣,跳过,不做任何处理) + if(!checkMarkedWords()){//检查该篇文章是否有生词高亮,若没有将文章id,level发送到后台,保存至文件中,在计算用户level时根据文件中保存的文章等级计算 + $.ajax({ + url: '/submit_article_id_without_new_words/{{username}}/'+sessionStorage.getItem('article_id')+'/'+sessionStorage.getItem('text_level'), + dataType: 'json', + success: function(data) {} + }); + } + } $.ajax({ url: '/get_next_article/{{username}}', dataType: 'json', @@ -235,8 +253,21 @@ } } }); + startTime=Date.now();//在页面加载完下一篇文章,记录起始时间 } function load_pre_article(){ + //在进入上一篇之前处理 + endTime=Date.now();//记录用户点击上一篇文章时的时间 + spendtime=endTime-startTime;//计算出用户浏览该文章的时长,单位毫秒 + if(spendtime/1000>15){ + if(!checkMarkedWords()){ + $.ajax({ + url: '/submit_article_id_without_new_words/{{username}}/'+sessionStorage.getItem('article_id')+'/'+sessionStorage.getItem('text_level'), + dataType: 'json', + success: function(data) {} + }); + } + } $.ajax({ url: '/get_pre_article/{{username}}', dataType: 'json', @@ -248,6 +279,7 @@ } } }); + startTime=Date.now();//在页面加载完下一篇文章,记录起始时间 } function update(today_article){ $('#user_level').html(today_article['user_level']); @@ -262,6 +294,27 @@ setTimeout(() => {document.querySelector('#text_level').classList.remove('mark');}, 2000); document.querySelector('#user_level').classList.add('mark'); // do the same thing for user difficulty level setTimeout(() => {document.querySelector('#user_level').classList.remove('mark');}, 2000); + + sessionStorage.setItem('article_id',today_article["article_id"]);//将更新后的文章id,level保存到本地 + sessionStorage.setItem('text_level',today_article['text_level']); + } + function checkMarkedWords(){//检查文章中是否有高亮生词 + let article_title= document.getElementById("article_title").innerHTML;//从页面中获取文章名和内容 + let article_body=document.getElementById("article").innerHTML; + let article=article_title+" "+article_body; + let pickedWords = document.getElementById("selected-words"); // words picked to the text area + let dictionaryWords = document.getElementById("selected-words2"); // words appearing in the user's new words list + let allWords = dictionaryWords === null ? pickedWords.value + " " : pickedWords.value + " " + dictionaryWords.value; + const list = allWords.split(" "); // 将所有的生词放入一个list中 + for (let i = 0; i < list.length; ++i) { + list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); + } + for (let i=1;i function check_pre(visited_articles){ diff --git a/app/test/test_bug476_LiMengdie.py b/app/test/test_bug476_LiMengdie.py new file mode 100644 index 0000000..4e3331d --- /dev/null +++ b/app/test/test_bug476_LiMengdie.py @@ -0,0 +1,119 @@ +import time + +import pytest +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.common.exceptions import UnexpectedAlertPresentException, NoAlertPresentException, \ + ElementClickInterceptedException, NoSuchElementException, TimeoutException +from helper import signup + + +def is_textarea_empty(driver): + # 获取