1
0
Fork 0

Compare commits

..

1 Commits

Author SHA1 Message Date
李梦蝶 e3d646c10c Fix bug476 2024-05-27 21:22:56 +08:00
10 changed files with 249 additions and 125 deletions

View File

@ -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"],

View File

@ -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<float(article.get('text_level'))):
level+=(float(article.get('text_level'))-level)/20 #根据文章等级提高用户level用户level与用户阅读完后没有添加生词的文章level 差距越大,分数提高程度越大
print(f"用户等级{level}")
print("-----------------------------------")
return level
for date in sorted(d_user2.keys(),
reverse=True): # most recently added words are more important while determining user's level
lst = d_user2[date] # a list of words
@ -115,10 +127,9 @@ def user_difficulty_level(d_user, d):
geometric = geometric * (hard)
count += 1
if count >= 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)

View File

@ -1,3 +1,4 @@
// initialize from localStorage
let isRead = localStorage.getItem('readChecked') !== 'false'; // default to true
let isChoose = localStorage.getItem('chooseChecked') !== 'false';
@ -8,15 +9,8 @@ function getWord() {
function fillInWord() {
let word = getWord();
if (isRead) Reader.read(word, inputSlider.value);
if (!isChoose) {
if(isHighlight){
const element = document.getElementById("selected-words3");
element.value = element.value + " " + word;
}
return;
}
if (!isChoose) return;
const element = document.getElementById("selected-words");
localStorage.setItem('nowWords', element.value);
element.value = element.value + " " + word;
localStorage.setItem('selectedWords', element.value);
}
@ -44,4 +38,3 @@ function onChooseClick() {
if (performance.getEntriesByType("navigation")[0].type == "reload") {
Reader.stopRead();
}

View File

@ -22,48 +22,39 @@ function getWord() {
function highLight() {
if (!isHighlight) return;
let word = (getWord() + "").trim();
let articleContent = document.getElementById("article").innerHTML; // innerHTML保留HTML标签来保持部分格式且适配不同的浏览器
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;
highlightWords = document.getElementById("selected-words3");
allWords = highlightWords == null ? allWords : allWords + " " + highlightWords.value;
const list = allWords.split(" "); // 将所有的生词放入一个list中
if(word !== null && word !== "" && word !== " "){
let articleContent_fb2 = articleContent;
if(localStorage.getItem("nowWords").indexOf(word) !== -1 || localStorage.getItem("nowWords").indexOf(word.toLowerCase()) !== -1){
articleContent = articleContent.replace(new RegExp('<span class="highlighted">' + word + '</span>', "gi"), word);
pickedWords.value = localStorage.getItem("nowWords").replace(word,"");
document.getElementById("article").innerHTML = articleContent;
return;
}
}
let totalSet = new Set();
for (let i = 0; i < list.length; ++i) {
list[i] = list[i].replace(/(^\W*)|(\W*$)/g, ""); // 消除单词两边的非单词字符
list[i] = list[i].replace('|', "");
list[i] = list[i].replace('?', "");
if (list[i] != "" && !totalSet.has(list[i])) {
// 返回所有匹配单词的集合, 正则表达式RegExp()中, "\b"匹配一个单词的边界, g 表示全局匹配, i 表示对大小写不敏感。
let matches = new Set(articleContent.match(new RegExp("\\b" + list[i] + "\\b", "gi")));
if (matches.has("mark")) {
// 优先处理单词为 "mark" 的情况
totalSet = new Set(["mark", ...totalSet]);
}
totalSet = new Set([...totalSet, ...matches]);
}
}
// 删除所有的"<span class='highlighted'>"标签,防止标签发生嵌套
articleContent = articleContent.replace(new RegExp('<span class="highlighted">',"gi"), "")
articleContent = articleContent.replace(new RegExp("</span>","gi"), "");
// 删除所有的mark标签,防止标签发生嵌套
articleContent = articleContent.replace(/<(mark)[^>]*>/gi, "");
articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, "");
// 将文章中所有出现该单词word的地方改为"<span class='highlighted'>" + word + "</span>"。
for (let word of totalSet) {
articleContent = articleContent.replace(new RegExp("\\b" + word + "\\b", "g"), "<span class='highlighted'>" + word + "</span>");
}
document.getElementById("article").innerHTML = articleContent;
}
function cancelHighlighting() {
let articleContent = document.getElementById("article").innerHTML;
articleContent = articleContent.replace(new RegExp('<span class="highlighted">',"gi"), "")
articleContent = articleContent.replace(new RegExp("</span>","gi"), "");
articleContent = articleContent.replace(/<(mark)[^>]*>/gi, "");
articleContent = articleContent.replace(/<(\/mark)[^>]*>/gi, "");
document.getElementById("article").innerHTML = articleContent;
}

View File

@ -166,7 +166,6 @@
<input id="selected-words2" type="hidden" value="{{ words }}">
{% endif %}
</div>
<label id="selected-words3" type="hidden"></label>
{{ yml['footer'] | safe }}
{% if yml['js']['bottom'] %}
{% for js in yml['js']['bottom'] %}
@ -215,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() {
@ -224,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',
@ -236,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',
@ -249,6 +279,7 @@
}
}
});
startTime=Date.now();//在页面加载完下一篇文章,记录起始时间
}
function update(today_article){
$('#user_level').html(today_article['user_level']);
@ -263,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<list.length-1;i++){//list
if(article.search(list[i])!==-1){
return true;
}
}
return false;
}
<!-- 检查是否存在上一篇或下一篇,不存在则对应按钮隐藏-->
function check_pre(visited_articles){
@ -292,7 +344,7 @@
</body>
<style>
mark {
color: red;
color: #{{ yml['highlight']['color'] }};
background-color: rgba(0, 0, 0, 0);
}
</style>

View File

@ -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):
# 获取<textarea>元素的内容,判断是否加入了生词
textarea_element = driver.find_element(By.ID, 'selected-words')
content = textarea_element.get_attribute("value")
return content is None or content.strip() == ""
def get_user_level(driver):
return float(driver.find_element(By.ID, 'user_level').text)
def scroll(total_time, driver):
try:
half_time = (total_time - 2) / 2 # 分成两半,一半时间滚到底,一半时间滚回顶,模拟阅读时间
# 滚动到底部
start_time = time.time()
end_time = start_time + half_time
while time.time() < end_time:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# 确保滚动更平滑
time.sleep(1)
# 滚动回顶部
start_time = time.time()
end_time = start_time + half_time
while time.time() < end_time:
driver.execute_script("window.scrollTo(0, -document.body.scrollHeight);")
time.sleep(1)
except ElementClickInterceptedException:
print("加载出错")
def read_test_with_time(driver, total_time):
# 获取点击前的user_level和text_level
before_user_level = get_user_level(driver)
before_text_level = float(driver.find_element(By.ID, 'text_level').text)
# 检查是否加入了生词簿
check = is_textarea_empty(driver)
scroll(total_time, driver)
# 定位“下一页”按钮
next_button = driver.find_element(By.ID, 'load_next_article')
# 模拟点击
next_button.click()
# 关闭弹窗
try:
WebDriverWait(driver, 1).until(EC.alert_is_present())
driver.switch_to.alert.accept()
except (UnexpectedAlertPresentException, NoAlertPresentException):
pass
# 等待页面加载完毕,正确地获取更新后的值
time.sleep(2)
# 获取点击后的user_level
after_user_level = get_user_level(driver)
# 等待3秒方便观察测试
time.sleep(3)
return before_user_level, after_user_level, before_text_level, check
def test_score_with_enough_time(driver, URL):
try:
# 登录并跳转到测试主页面
signup(URL, driver)
# 测试阅读到足够时间的等级变化
before_user_level, after_user_level, before_text_level, check = read_test_with_time(driver, 18)
# print(check, before_user_level, after_user_level, before_text_level)
if before_user_level < before_text_level and check:
assert before_user_level < after_user_level, 'The user level dose not increase after reading an article with a high level and clicking to navigate away without adding any new vocabulary. '
except (NoSuchElementException, TimeoutException) as e:
print("Error occurs: " + str(e))
finally:
driver.quit()
def test_score_without_enough_time(driver, URL):
try:
# 登录并跳转到测试主页面
signup(URL, driver)
# 测试阅读时间不足够的等级变化
before_user_level, after_user_level, before_text_level, check = read_test_with_time(driver, 6)
# print(check, before_user_level, after_user_level, before_text_level)
if before_user_level < before_text_level and check:
assert before_user_level == after_user_level, 'After the user quickly skipped through the article, the user level does not remain unchanged. '
except (NoSuchElementException, TimeoutException) as e:
print("Error occurs: " + str(e))
finally:
driver.quit()
if __name__ == '__main__':
pytest.main(["--html=report.html"])

View File

@ -1,7 +1,5 @@
import random
import string
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

View File

@ -1,44 +0,0 @@
import random
import string
import time
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.webdriver.common.action_chains import ActionChains
from helper import signup
def has_punctuation(s):
return any(c in string.punctuation for c in s)
def select_one(driver):
elem = driver.find_element(By.ID, 'article')
essay_content = elem.text
valid_word = random.choice([word for word in essay_content.split() if len(word) >= 6 and not has_punctuation(
word) and 'font>' not in word and 'br>' not in word and 'p>' not in word])
driver.find_element(By.ID, 'selected-words').send_keys(valid_word)
driver.find_element(By.ID, 'article').click()
return valid_word
def select_two(driver):
word = driver.find_element(By.CLASS_NAME, 'highlighted')
# 创建ActionChains对象
actions = ActionChains(driver)
actions.move_to_element(word)
# 模拟鼠标按下并拖动以选择文本
actions.double_click()
actions.perform()
def test_selected_second_word(driver, URL):
try:
signup(URL, driver)
selected_words = select_one(driver);
assert selected_words.strip() != "", "选中的单词被放置框中"
select_two(driver)
selected_second_words = driver.find_element(By.ID, 'selected-words').get_attribute('value')
assert selected_second_words.strip() == "", "选中的单词被删除"
finally:
driver.quit()

View File

@ -1,39 +0,0 @@
from selenium.webdriver.common.action_chains import ActionChains
from helper import signup
def test_highlight(driver, URL):
try:
# 打开网页
driver.get(URL)
driver.maximize_window()
# 注册
signup(URL, driver)
# 取消勾选“划词入库按钮”
highlight_checkbox = driver.find_element_by_id("chooseCheckbox")
driver.execute_script("arguments[0].click();", highlight_checkbox)
article = driver.find_element_by_id("article")
# 创建 ActionChains 对象
actions = ActionChains(driver)
# 移动鼠标到起点位置
actions.move_to_element(article)
# actions.move_to_element_with_offset(article, 50, 100)
# 按下鼠标左键
actions.click_and_hold()
# 拖动鼠标到结束位置
actions.move_by_offset(400,50)
# 释放鼠标左键
actions.release()
# 执行操作链
actions.perform()
# time.sleep(10)
assert driver.find_elements_by_class_name("highlighted") is not None
finally:
# 测试结束后关闭浏览器
driver.quit()

View File

@ -1,3 +1,5 @@
import pickle
import os.path
from datetime import datetime
from admin_service import ADMIN_NAME
from flask import *
@ -26,6 +28,8 @@ path_prefix = './' # comment this line in deployment
@userService.route("/get_next_article/<username>",methods=['GET','POST'])
def get_next_article(username):
user_articlesWithoutNewWords_record = path_prefix + 'static/frequency/' + 'ArticlesWithoutNewWords_%s.pickle' % (username)
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
session['old_articleID'] = session.get('articleID')
if request.method == 'GET':
@ -36,7 +40,7 @@ def get_next_article(username):
visited_articles["index"] += 1
session["visited_articles"] = visited_articles
logging.debug('/get_next_article: start calling get_today_arcile()')
visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'),user_articlesWithoutNewWords_record)
logging.debug('/get_next_arcile: done.')
data = {
'visited_articles': visited_articles,
@ -47,8 +51,37 @@ def get_next_article(username):
return 'Under construction'
return json.dumps(data)
#保存前端传来 用户阅读时长超过15秒且不含高亮生词的文章用户阅读完后没有添加生词的文章的id,level
@userService.route("/submit_article_id_without_new_words/<username>/<article_id>/<text_level>",methods=['GET'])
def set_article_id_without_new_words(username,article_id,text_level):
user_articles_without_new_words_record_path = path_prefix + 'static/frequency/' + 'ArticlesWithoutNewWords_%s.pickle' % (username)
if os.path.exists(user_articles_without_new_words_record_path)==False: #若不存在 存储用户阅读时长超过15秒且不含高亮生词的文章索引信息的文件 则新建
f = open(user_articles_without_new_words_record_path, 'wb')
list=[]
pickle.dump(list,f)
f.close()
fp=open(user_articles_without_new_words_record_path,'rb')
user_article_id_record=pickle.load(fp)
fp.close()
dic = {}
dic['article_id'] = article_id
dic['text_level'] = text_level
if dic not in user_article_id_record: #若已存在记录(即这篇文章之前被阅读过且没有添加生词,且已加入索引信息记录文件中),则不再重复加入,避免重复刷分
user_article_id_record.append(dic)
print(f"编号为{article_id}的文章阅读时间大于15秒且未有生词记录于ArticlesWithoutNewWords_<username>.pickle")
print(user_article_id_record)
f=open(user_articles_without_new_words_record_path,'wb')
pickle.dump(user_article_id_record,f)
f.close()
return "success"
@userService.route("/get_pre_article/<username>",methods=['GET'])
def get_pre_article(username):
user_articlesWithoutNewWords_record = path_prefix + 'static/frequency/' + 'ArticlesWithoutNewWords_%s.pickle' % (
username)
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
if request.method == 'GET':
visited_articles = session.get("visited_articles")
@ -59,7 +92,7 @@ def get_pre_article(username):
if visited_articles['article_ids'][-1] == "null": # 如果当前还是“null”则将“null”pop出来
visited_articles['article_ids'].pop()
session["visited_articles"] = visited_articles
visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'),user_articlesWithoutNewWords_record)
data = {
'visited_articles': visited_articles,
'today_article': today_article,
@ -130,7 +163,8 @@ def userpage(username):
# 获取session里的用户名
username = session.get('username')
user_articlesWithoutNewWords_record = path_prefix + 'static/frequency/' + 'ArticlesWithoutNewWords_%s.pickle' % (
username)
user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
if request.method == 'POST': # when we submit a form
@ -149,7 +183,7 @@ def userpage(username):
words = ''
for x in lst3:
words += x[0] + ' '
visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'),user_articlesWithoutNewWords_record)
session['visited_articles'] = visited_articles
# 通过 today_article加载前端的显示页面
return render_template('userpage_get.html',