Compare commits
	
		
			5 Commits 
		
	
	
		
			master
			...
			Bug504-LiJ
		
	
	| Author | SHA1 | Date | 
|---|---|---|
| 
							
							
								 | 
						7207683d42 | |
| 
							
							
								 | 
						1fff921d79 | |
| 
							
							
								 | 
						6d2191f727 | |
| 
							
							
								 | 
						89773bdcf1 | |
| 
							
							
								
									
								
								 | 
						f98da2696d | 
| 
						 | 
				
			
			@ -8,6 +8,7 @@ import hashlib
 | 
			
		|||
from datetime import datetime
 | 
			
		||||
from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages
 | 
			
		||||
from difficulty import get_difficulty_level, text_difficulty_level, user_difficulty_level
 | 
			
		||||
from model.article import get_number_of_articles, get_article, get_article_by_id
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
path_prefix = '/var/www/wordfreq/wordfreq/'
 | 
			
		||||
| 
						 | 
				
			
			@ -15,11 +16,7 @@ path_prefix = './'  # comment this line in deployment
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
def total_number_of_essays():
 | 
			
		||||
    rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
 | 
			
		||||
    rq.instructions("SELECT * FROM article")
 | 
			
		||||
    rq.do()
 | 
			
		||||
    result = rq.get_results()
 | 
			
		||||
    return len(result)
 | 
			
		||||
    get_number_of_articles()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_article_title(s):
 | 
			
		||||
| 
						 | 
				
			
			@ -33,21 +30,18 @@ def get_article_body(s):
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
def get_today_article(user_word_list, visited_articles):
 | 
			
		||||
    rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
 | 
			
		||||
    if visited_articles is None:
 | 
			
		||||
        visited_articles = {
 | 
			
		||||
            "index" : 0,  # 为 article_ids 的索引
 | 
			
		||||
            "article_ids": []  # 之前显示文章的id列表,越后越新
 | 
			
		||||
        }
 | 
			
		||||
    if visited_articles["index"] > len(visited_articles["article_ids"])-1:  # 生成新的文章,因此查找所有的文章
 | 
			
		||||
        rq.instructions("SELECT * FROM article")
 | 
			
		||||
        result = list(get_article())   # 转为一个list
 | 
			
		||||
    else:  # 生成阅读过的文章,因此查询指定 article_id 的文章
 | 
			
		||||
        if visited_articles["article_ids"][visited_articles["index"]] == 'null':  # 可能因为直接刷新页面导致直接去查询了'null',因此当刷新的页面的时候,需要直接进行“上一篇”操作
 | 
			
		||||
            visited_articles["index"] -= 1
 | 
			
		||||
            visited_articles["article_ids"].pop()
 | 
			
		||||
        rq.instructions('SELECT * FROM article WHERE article_id=%d' % (visited_articles["article_ids"][visited_articles["index"]]))
 | 
			
		||||
    rq.do()
 | 
			
		||||
    result = rq.get_results()
 | 
			
		||||
        result = [get_article_by_id(visited_articles["article_ids"][visited_articles["index"]])]
 | 
			
		||||
    random.shuffle(result)
 | 
			
		||||
 | 
			
		||||
    # Choose article according to reader's level
 | 
			
		||||
| 
						 | 
				
			
			@ -68,11 +62,11 @@ def get_today_article(user_word_list, visited_articles):
 | 
			
		|||
        else:
 | 
			
		||||
            for k in range(3):  # 最多尝试3次
 | 
			
		||||
                for reading in result:
 | 
			
		||||
                    text_level = text_difficulty_level(reading['text'], d3)
 | 
			
		||||
                    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 visited_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor):  # 新的文章之前没有出现过且符合一定范围的水平
 | 
			
		||||
                    if reading.article_id not in visited_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor):  # 新的文章之前没有出现过且符合一定范围的水平
 | 
			
		||||
                        d = reading
 | 
			
		||||
                        visited_articles["article_ids"].append(d['article_id'])  # 列表添加新的文章id;下面进行
 | 
			
		||||
                        visited_articles["article_ids"].append(d.article_id)  # 列表添加新的文章id;下面进行
 | 
			
		||||
                        result_of_generate_article = "found"
 | 
			
		||||
                        break
 | 
			
		||||
                if result_of_generate_article == "found":  # 用于成功找到文章后及时退出外层循环
 | 
			
		||||
| 
						 | 
				
			
			@ -81,7 +75,7 @@ def get_today_article(user_word_list, visited_articles):
 | 
			
		|||
            visited_articles["article_ids"].append('null')
 | 
			
		||||
    else:  # 生成已经阅读过的文章
 | 
			
		||||
        d = random.choice(result)
 | 
			
		||||
        text_level = text_difficulty_level(d['text'], d3)
 | 
			
		||||
        text_level = text_difficulty_level(d.text, d3)
 | 
			
		||||
        result_of_generate_article = "found"
 | 
			
		||||
 | 
			
		||||
    today_article = None
 | 
			
		||||
| 
						 | 
				
			
			@ -89,12 +83,12 @@ def get_today_article(user_word_list, visited_articles):
 | 
			
		|||
        today_article = {
 | 
			
		||||
            "user_level": '%4.2f' % user_level,
 | 
			
		||||
            "text_level": '%4.2f' % text_level,
 | 
			
		||||
            "date": d['date'],
 | 
			
		||||
            "article_title": get_article_title(d['text']),
 | 
			
		||||
            "article_body": get_article_body(d['text']),
 | 
			
		||||
            "source": d["source"],
 | 
			
		||||
            "question": get_question_part(d['question']),
 | 
			
		||||
            "answer": get_answer_part(d['question'])
 | 
			
		||||
            "date": d.date,
 | 
			
		||||
            "article_title": get_article_title(d.text),
 | 
			
		||||
            "article_body": get_article_body(d.text),
 | 
			
		||||
            "source": d.source,
 | 
			
		||||
            "question": get_question_part(d.question),
 | 
			
		||||
            "answer": get_answer_part(d.question)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    return visited_articles, today_article, result_of_generate_article
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -32,3 +32,14 @@ def get_page_articles(num, size):
 | 
			
		|||
            x
 | 
			
		||||
            for x in Article.select().order_by(desc(Article.article_id)).page(num, size)
 | 
			
		||||
        ]
 | 
			
		||||
 | 
			
		||||
def get_article():
 | 
			
		||||
    with db_session:
 | 
			
		||||
        return Article.select()[:]
 | 
			
		||||
 | 
			
		||||
def get_article_by_id(article_id):
 | 
			
		||||
    article_id &= 0xFFFFFFFF  # max 32 bits
 | 
			
		||||
    with db_session:
 | 
			
		||||
        article = Article.select(article_id=article_id)
 | 
			
		||||
        if article:
 | 
			
		||||
            return article.first()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,6 +23,8 @@ function getWord() {
 | 
			
		|||
function highLight() {
 | 
			
		||||
    if (!isHighlight) return;
 | 
			
		||||
    let articleContent = document.getElementById("article").innerText; //将原来的.innerText改为.innerHtml,使用innerText会把原文章中所包含的<br>标签去除,导致处理后的文章内容失去了原来的格式
 | 
			
		||||
    let articleTitle = document.getElementById("article_title").innerText;//获取文章标题对象的引用
 | 
			
		||||
    let articleQuestion = document.getElementById("question").innerText;//获取文章问题对象的引用
 | 
			
		||||
    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 = "";  //初始化allWords的值,避免进入判断后编译器认为allWords未初始化的问题
 | 
			
		||||
| 
						 | 
				
			
			@ -42,6 +44,8 @@ function highLight() {
 | 
			
		|||
 | 
			
		||||
            //修改代码
 | 
			
		||||
            let articleContent_fb = articleContent;  //文章副本
 | 
			
		||||
            let articleTitle_fb = articleTitle;
 | 
			
		||||
            let articleQuestion_fb = articleQuestion;
 | 
			
		||||
            while(articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase()) !== -1 && list[i]!=""){
 | 
			
		||||
                //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
 | 
			
		||||
                const index = articleContent_fb.toLowerCase().indexOf(list[i].toLowerCase());
 | 
			
		||||
| 
						 | 
				
			
			@ -50,13 +54,33 @@ function highLight() {
 | 
			
		|||
                articleContent_fb = articleContent_fb.substring(index + list[i].length);    // 使用副本中list[i]之后的子串替换掉副本
 | 
			
		||||
                articleContent = articleContent.replace(new RegExp("\\b"+list[i]+"\\b","g"),"<mark>" + list[i] + "</mark>");
 | 
			
		||||
            }
 | 
			
		||||
            while(articleTitle_fb.toLowerCase().indexOf(list[i].toLowerCase()) !== -1 && list[i]!=""){
 | 
			
		||||
                //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
 | 
			
		||||
                const index = articleTitle_fb.toLowerCase().indexOf(list[i].toLowerCase());
 | 
			
		||||
                list[i] = articleTitle_fb.substring(index, index + list[i].length);
 | 
			
		||||
 | 
			
		||||
                articleTitle_fb = articleTitle_fb.substring(index + list[i].length);    // 使用副本中list[i]之后的子串替换掉副本
 | 
			
		||||
                articleTitle = articleTitle.replace(new RegExp("\\b"+list[i]+"\\b","g"),"<mark>" + list[i] + "</mark>");
 | 
			
		||||
            }
 | 
			
		||||
            while(articleQuestion_fb.toLowerCase().indexOf(list[i].toLowerCase()) !== -1 && list[i]!=""){
 | 
			
		||||
                //找到副本中和list[i]匹配的第一个单词(第一种匹配情况),并赋值给list[i]。
 | 
			
		||||
                const index = articleQuestion_fb.toLowerCase().indexOf(list[i].toLowerCase());
 | 
			
		||||
                list[i] = articleQuestion_fb.substring(index, index + list[i].length);
 | 
			
		||||
 | 
			
		||||
                articleQuestion_fb = articleQuestion_fb.substring(index + list[i].length);    // 使用副本中list[i]之后的子串替换掉副本
 | 
			
		||||
                articleQuestion = articleQuestion.replace(new RegExp("\\b"+list[i]+"\\b","g"),"<mark>" + list[i] + "</mark>");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    document.getElementById("article").innerHTML = articleContent;
 | 
			
		||||
    document.getElementById("article_title").innerHTML = articleTitle;
 | 
			
		||||
    document.getElementById("question").innerHTML = articleQuestion;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function cancelHighlighting() {
 | 
			
		||||
    let articleContent = document.getElementById("article").innerText;//将原来的.innerText改为.innerHtml,原因同上
 | 
			
		||||
    let articleTitle = document.getElementById("article_title").innerText;//获取文章标题对象的引用
 | 
			
		||||
    let articleQuestion = document.getElementById("question").innerText;//获取文章问题对象的引用
 | 
			
		||||
    let pickedWords = document.getElementById("selected-words");
 | 
			
		||||
    const dictionaryWords = document.getElementById("selected-words2");    
 | 
			
		||||
    const list = pickedWords.value.split(" ");    
 | 
			
		||||
| 
						 | 
				
			
			@ -65,6 +89,8 @@ function cancelHighlighting() {
 | 
			
		|||
            list[i] = list[i].replace(/(^\s*)|(\s*$)/g, "");
 | 
			
		||||
            if (list[i] !== "") { //原来判断的代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容
 | 
			
		||||
                articleContent = articleContent.replace(new RegExp("<mark>"+list[i]+"</mark>", "g"), list[i]);
 | 
			
		||||
                articleTitle = articleTitle.replace(new RegExp("<mark>"+list[i]+"</mark>", "g"), list[i]);
 | 
			
		||||
                articleQuestion = articleQuestion.replace(new RegExp("<mark>"+list[i]+"</mark>", "g"), list[i]);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -75,10 +101,14 @@ function cancelHighlighting() {
 | 
			
		|||
            list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, "");
 | 
			
		||||
            if (list2[i] !== "") { //原来代码中,替换的内容为“list[i]”这个字符串,这明显是错误的,我们需要替换的是list[i]里的内容
 | 
			
		||||
                articleContent = articleContent.replace(new RegExp("<mark>"+list2[i]+"</mark>", "g"), list2[i]);
 | 
			
		||||
                articleTitle = articleTitle.replace(new RegExp("<mark>"+list2[i]+"</mark>", "g"), list2[i]);
 | 
			
		||||
                articleQuestion = articleQuestion.replace(new RegExp("<mark>"+list2[i]+"</mark>", "g"), list2[i]);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    document.getElementById("article").innerHTML = articleContent;
 | 
			
		||||
    document.getElementById("article_title").innerHTML = articleTitle;
 | 
			
		||||
    document.getElementById("question").innerHTML = articleQuestion;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function fillInWord() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -62,10 +62,10 @@
 | 
			
		|||
            <div class="alert alert-success" role="alert">According to your word list, your level is <span class="badge bg-success">{{ today_article["user_level"] }}</span>  and we have chosen an article with a difficulty level of <span class="badge bg-success">{{ today_article["text_level"] }}</span> for you.</div>
 | 
			
		||||
                <p class="text-muted">Article added on: {{ today_article["date"] }}</p><br/>
 | 
			
		||||
            <div class="p-3 mb-2 bg-light text-dark"><br/>
 | 
			
		||||
            <p class="display-5">{{ today_article["article_title"] }}</p><br/>
 | 
			
		||||
            <p class="display-5"><font id="article_title">{{ today_article["article_title"] }}</font></p><br/>
 | 
			
		||||
            <p class="lead"><font id="article" size=2>{{ today_article["article_body"] }}</font></p><br/>
 | 
			
		||||
            <p><small class="text-muted">{{ today_article['source'] }}</small></p><br/>
 | 
			
		||||
            <p><b>{{ today_article['question'] }}</b></p><br/>
 | 
			
		||||
            <p><b><font id="question">{{ today_article['question'] }}</font></b></p><br/>
 | 
			
		||||
                <script type="text/javascript">
 | 
			
		||||
                    function toggle_visibility(id) { {# https://css-tricks.com/snippets/javascript/showhide-element/#}
 | 
			
		||||
                        const e = document.getElementById(id);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue