Merge pull request 'Bug533-ZhangXuDong' (#161) from Bug533-ZhangXuDong into Alpha-snapshot20240618
Reviewed-on: #161Bug572-ZhongYi2
commit
dc3ef2bc9f
|
@ -9,10 +9,32 @@ from flask import Flask, request, redirect, render_template, url_for, session, a
|
||||||
from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level
|
from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level
|
||||||
from model.article import get_all_articles, get_article_by_id, get_number_of_articles
|
from model.article import get_all_articles, get_article_by_id, get_number_of_articles
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
path_prefix = './'
|
path_prefix = './'
|
||||||
db_path_prefix = './db/' # comment this line in deployment
|
db_path_prefix = './db/' # comment this line in deployment
|
||||||
|
oxford_words_path='./db/oxford_words.txt'
|
||||||
|
|
||||||
|
def count_oxford_words(text, oxford_words):
|
||||||
|
words = re.findall(r'\b\w+\b', text.lower())
|
||||||
|
total_words = len(words)
|
||||||
|
oxford_word_count = sum(1 for word in words if word in oxford_words)
|
||||||
|
return oxford_word_count, total_words
|
||||||
|
|
||||||
|
def calculate_ratio(oxford_word_count, total_words):
|
||||||
|
if total_words == 0:
|
||||||
|
return 0
|
||||||
|
return oxford_word_count / total_words
|
||||||
|
|
||||||
|
def load_oxford_words(file_path):
|
||||||
|
oxford_words = {}
|
||||||
|
with open(file_path, 'r', encoding='utf-8') as file:
|
||||||
|
for line in file:
|
||||||
|
parts = line.strip().split()
|
||||||
|
word = parts[0]
|
||||||
|
pos = parts[1]
|
||||||
|
level = parts[2]
|
||||||
|
oxford_words[word] = {'pos': pos, 'level': level}
|
||||||
|
return oxford_words
|
||||||
|
|
||||||
def total_number_of_essays():
|
def total_number_of_essays():
|
||||||
return get_number_of_articles()
|
return get_number_of_articles()
|
||||||
|
@ -86,6 +108,9 @@ def get_today_article(user_word_list, visited_articles):
|
||||||
|
|
||||||
today_article = None
|
today_article = None
|
||||||
if d:
|
if d:
|
||||||
|
oxford_words = load_oxford_words(oxford_words_path)
|
||||||
|
oxford_word_count, total_words = count_oxford_words(d['text'],oxford_words)
|
||||||
|
ratio = calculate_ratio(oxford_word_count,total_words)
|
||||||
today_article = {
|
today_article = {
|
||||||
"user_level": '%4.1f' % user_level,
|
"user_level": '%4.1f' % user_level,
|
||||||
"text_level": '%4.1f' % text_level,
|
"text_level": '%4.1f' % text_level,
|
||||||
|
@ -94,7 +119,8 @@ def get_today_article(user_word_list, visited_articles):
|
||||||
"article_body": get_article_body(d['text']),
|
"article_body": get_article_body(d['text']),
|
||||||
"source": d["source"],
|
"source": d["source"],
|
||||||
"question": get_question_part(d['question']),
|
"question": get_question_part(d['question']),
|
||||||
"answer": get_answer_part(d['question'])
|
"answer": get_answer_part(d['question']),
|
||||||
|
"ratio" : ratio
|
||||||
}
|
}
|
||||||
|
|
||||||
return visited_articles, today_article, result_of_generate_article
|
return visited_articles, today_article, result_of_generate_article
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
16
app/main.py
16
app/main.py
|
@ -79,6 +79,19 @@ def mainpage():
|
||||||
根据GET或POST方法来返回不同的主界面
|
根据GET或POST方法来返回不同的主界面
|
||||||
:return: 主界面
|
:return: 主界面
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
article_text = get_all_articles()
|
||||||
|
texts = [item['text'] for item in article_text]
|
||||||
|
oxford_words = load_oxford_words(oxford_words_path)
|
||||||
|
|
||||||
|
# 提取所有单词
|
||||||
|
all_words = []
|
||||||
|
for text in texts:
|
||||||
|
words = re.findall(r'\b\w+\b', text.lower())
|
||||||
|
all_words.extend(words)
|
||||||
|
oxford_word_count = sum(1 for word in all_words if word in oxford_words)
|
||||||
|
ratio = calculate_ratio(oxford_word_count, len(all_words))
|
||||||
|
|
||||||
if request.method == 'POST': # when we submit a form
|
if request.method == 'POST': # when we submit a form
|
||||||
content = escape(request.form['content'])
|
content = escape(request.form['content'])
|
||||||
f = WordFreq(content)
|
f = WordFreq(content)
|
||||||
|
@ -102,7 +115,8 @@ def mainpage():
|
||||||
d_len=d_len,
|
d_len=d_len,
|
||||||
lst=lst,
|
lst=lst,
|
||||||
yml=Yaml.yml,
|
yml=Yaml.yml,
|
||||||
number_of_essays=number_of_essays)
|
number_of_essays=number_of_essays,
|
||||||
|
ratio = ratio)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
<p><a href="/login">登录</a> <a href="/signup">注册</a> <a href="/static/usr/instructions.html">使用说明</a></p >
|
<p><a href="/login">登录</a> <a href="/signup">注册</a> <a href="/static/usr/instructions.html">使用说明</a></p >
|
||||||
<p><b> {{ random_ads }}。 <a href="/signup">试试</a>吧!</b></p>
|
<p><b> {{ random_ads }}。 <a href="/signup">试试</a>吧!</b></p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<div class="alert alert-success" role="alert">共有文章 <span class="badge bg-success"> {{ number_of_essays }} </span> 篇</div>
|
<div class="alert alert-success" role="alert">共有文章 <span class="badge bg-success"> {{ number_of_essays }} </span> 篇,覆盖 <span class="badge bg-success"> {{ (ratio * 100) | int }}% </span> 的 Oxford5000 单词</div>
|
||||||
<p>粘贴1篇文章 (English only)</p>
|
<p>粘贴1篇文章 (English only)</p>
|
||||||
<form method="post" action="/">
|
<form method="post" action="/">
|
||||||
<textarea name="content" id="article" rows="10" cols="120"></textarea><br/>
|
<textarea name="content" id="article" rows="10" cols="120"></textarea><br/>
|
||||||
|
|
|
@ -66,6 +66,7 @@
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
{% for message in get_flashed_messages() %}
|
{% for message in get_flashed_messages() %}
|
||||||
|
|
||||||
<div class="alert alert-warning alert-dismissible fade show" role="alert">
|
<div class="alert alert-warning alert-dismissible fade show" role="alert">
|
||||||
{{ message }}
|
{{ message }}
|
||||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||||
|
@ -78,7 +79,7 @@
|
||||||
<p><b>阅读文章并回答问题</b></p>
|
<p><b>阅读文章并回答问题</b></p>
|
||||||
<div id="text-content">
|
<div id="text-content">
|
||||||
<div id="found">
|
<div id="found">
|
||||||
<div class="alert alert-success" role="alert">According to your word list, your level is <span class="text-decoration-underline" id="user_level">{{ today_article["user_level"] }}</span> and we have chosen an article with a difficulty level of <span class="text-decoration-underline" id="text_level">{{ today_article["text_level"] }}</span> for you.</div>
|
<div class="alert alert-success" role="alert">According to your word list, your level is <span class="text-decoration-underline" id="user_level">{{ today_article["user_level"] }}</span> and we have chosen an article with a difficulty level of <span class="text-decoration-underline" id="text_level">{{ today_article["text_level"] }}</span> for you. The Oxford word coverage is <span class="text-decoration-underline" id="ratio">{{ (today_article["ratio"] * 100) | int }}%.</span></div>
|
||||||
<p class="text-muted" id="date">Article added on: {{ today_article["date"] }}</p><br/>
|
<p class="text-muted" id="date">Article added on: {{ today_article["date"] }}</p><br/>
|
||||||
<div class="p-3 mb-2 bg-light text-dark" style="margin: 0 0.5%;"><br/>
|
<div class="p-3 mb-2 bg-light text-dark" style="margin: 0 0.5%;"><br/>
|
||||||
<p class="display-6" id="article_title">{{ today_article["article_title"] }}</p><br/>
|
<p class="display-6" id="article_title">{{ today_article["article_title"] }}</p><br/>
|
||||||
|
@ -88,6 +89,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p><b id="question">{{ today_article['question'] }}</b></p><br/>
|
<p><b id="question">{{ today_article['question'] }}</b></p><br/>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
function toggle_visibility(id) { {# https://css-tricks.com/snippets/javascript/showhide-element/#}
|
function toggle_visibility(id) { {# https://css-tricks.com/snippets/javascript/showhide-element/#}
|
||||||
const e = document.getElementById(id);
|
const e = document.getElementById(id);
|
||||||
|
@ -112,6 +114,7 @@
|
||||||
<input type="checkbox" id="highlightCheckbox" onclick="toggleHighlighting()" />生词高亮
|
<input type="checkbox" id="highlightCheckbox" onclick="toggleHighlighting()" />生词高亮
|
||||||
<input type="checkbox" id="readCheckbox" onclick="onReadClick()" />大声朗读
|
<input type="checkbox" id="readCheckbox" onclick="onReadClick()" />大声朗读
|
||||||
<input type="checkbox" id="chooseCheckbox" onclick="onChooseClick()" />划词入库
|
<input type="checkbox" id="chooseCheckbox" onclick="onChooseClick()" />划词入库
|
||||||
|
|
||||||
<div class="range">
|
<div class="range">
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<div class="sliderValue">
|
<div class="sliderValue">
|
||||||
|
@ -224,7 +227,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function load_next_article(){
|
|
||||||
|
function load_next_article() {
|
||||||
|
$("#load_next_article").prop("disabled", true)
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/get_next_article/{{username}}',
|
url: '/get_next_article/{{username}}',
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
|
@ -257,7 +262,8 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function update(today_article){
|
|
||||||
|
function update(today_article) {
|
||||||
$('#user_level').html(today_article['user_level']);
|
$('#user_level').html(today_article['user_level']);
|
||||||
$('#text_level').html(today_article["text_level"]);
|
$('#text_level').html(today_article["text_level"]);
|
||||||
$('#date').html('Article added on: '+today_article["date"]);
|
$('#date').html('Article added on: '+today_article["date"]);
|
||||||
|
@ -266,6 +272,7 @@
|
||||||
$('#source').html(today_article['source']);
|
$('#source').html(today_article['source']);
|
||||||
$('#question').html(today_article["question"]);
|
$('#question').html(today_article["question"]);
|
||||||
$('#answer').html(today_article["answer"]);
|
$('#answer').html(today_article["answer"]);
|
||||||
|
$('#ratio').html(Math.round(today_article["ratio"] * 100) + '%');
|
||||||
document.querySelector('#text_level').classList.add('mark'); // highlight text difficult level for 2 seconds
|
document.querySelector('#text_level').classList.add('mark'); // highlight text difficult level for 2 seconds
|
||||||
setTimeout(() => {document.querySelector('#text_level').classList.remove('mark');}, 2000);
|
setTimeout(() => {document.querySelector('#text_level').classList.remove('mark');}, 2000);
|
||||||
document.querySelector('#user_level').classList.add('mark'); // do the same thing for user difficulty level
|
document.querySelector('#user_level').classList.add('mark'); // do the same thing for user difficulty level
|
||||||
|
@ -276,7 +283,7 @@
|
||||||
if((visited_articles=='')||(visited_articles['index']<=0)){
|
if((visited_articles=='')||(visited_articles['index']<=0)){
|
||||||
$('#load_pre_article').hide();
|
$('#load_pre_article').hide();
|
||||||
sessionStorage.setItem('pre_page_button', 'display')
|
sessionStorage.setItem('pre_page_button', 'display')
|
||||||
}else{
|
} else {
|
||||||
$('#load_pre_article').show();
|
$('#load_pre_article').show();
|
||||||
sessionStorage.setItem('pre_page_button', 'show')
|
sessionStorage.setItem('pre_page_button', 'show')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue