0
0
Fork 0

Fix Bug533

Bug533-ZhangXuDong
张旭东 2024-07-04 10:44:41 +08:00
parent 1f718e201f
commit 5657d8d5ee
5 changed files with 5988 additions and 5 deletions

View File

@ -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 model.article import get_all_articles, get_article_by_id, get_number_of_articles
import logging
import re
path_prefix = './'
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():
return get_number_of_articles()
@ -86,6 +108,9 @@ def get_today_article(user_word_list, visited_articles):
today_article = None
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 = {
"user_level": '%4.1f' % user_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']),
"source": d["source"],
"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

5942
app/db/oxford_words.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -79,6 +79,19 @@ def mainpage():
根据GET或POST方法来返回不同的主界面
: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
content = escape(request.form['content'])
f = WordFreq(content)
@ -102,7 +115,8 @@ def mainpage():
d_len=d_len,
lst=lst,
yml=Yaml.yml,
number_of_essays=number_of_essays)
number_of_essays=number_of_essays,
ratio = ratio)
if __name__ == '__main__':

View File

@ -31,7 +31,7 @@
<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>
{% 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> &nbsp;&nbsp;&nbsp;&nbsp;其中Oxford单词覆盖率为: <span class="badge bg-success"> {{ (ratio * 100) | int }}% </span></div>
<p>粘贴1篇文章 (English only)</p>
<form method="post" action="/">
<textarea name="content" id="article" rows="10" cols="120"></textarea><br/>

View File

@ -96,7 +96,7 @@
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.
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/>
<div class="p-3 mb-2 bg-light text-dark" style="margin: 0 0.5%;"><br/>
@ -286,6 +286,7 @@
$('#source').html(today_article['source']);
$('#question').html(today_article["question"]);
$('#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
setTimeout(() => {
document.querySelector('#text_level').classList.remove('mark');