Compare commits

..

5 Commits

Author SHA1 Message Date
lin b53e7031e5 Bug412-JiangLetian 2022-06-13 11:40:20 +08:00
缪宸硕 e48008550a Merge branch 'Bug394-MiaoChenShuo' into master 2022-06-12 21:50:36 +08:00
李凯 fde3be4c23 更新 'app/Article.py' 2022-06-11 23:21:17 +08:00
李凯 5d5f4cf8f2 更新 'app/Article.py' 2022-06-11 23:13:08 +08:00
miaochenshuo 260f62967b 修复 Bug394 2022-06-05 23:36:55 +08:00
9 changed files with 19 additions and 220 deletions

2
.gitignore vendored
View File

@ -10,5 +10,3 @@ app/static/frequency/frequency.p
app/static/wordfreqapp.db
app/static/donate-the-author.jpg
app/static/donate-the-author-hidden.jpg
**/__pycache__

View File

@ -5,6 +5,10 @@ from UseSqlite import InsertQuery, RecordQuery
path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = './' # comment this line in deployment
def verify_pass(newpass,oldpass):
if(newpass==oldpass):
return True
def verify_user(username, password):
rq = RecordQuery(path_prefix + 'static/wordfreqapp.db')
@ -47,6 +51,8 @@ def change_password(username, old_password, new_password):
if not verify_user(username, old_password): # 旧密码错误
return False
# 将用户名和密码一起加密,以免暴露不同用户的相同密码
if verify_pass(new_password,old_password): #新旧密码一致
return False
password = md5(username + new_password)
rq = InsertQuery(path_prefix + 'static/wordfreqapp.db')
rq.instructions_with_parameters("UPDATE user SET password=:password WHERE name=:username", dict(

View File

View File

@ -1,122 +0,0 @@
import random
import sqlite3,re
from nltk.corpus import wordnet as wn
class Essay:
def __init__(self):
self._article_id = 0
self._essay = None
self._difficulty = None
self._answers = None
self._questions = None
pass
@property
def article_id(self):
pass
@article_id.setter
def article_id(self,article_id):
self._article_id = article_id
self.find_essay_in_database(self._article_id)
#获取数据库中的文章和等级
def find_essay_in_database(self,id):
try:
# 连接数据库
conn = sqlite3.connect("static/wordfreqapp.db")
# 创建游标
cursor = conn.cursor()
cursor.execute("select text,level from article where article_id = "+str(id))
results = cursor.fetchall()
conn.commit()
cursor.close()
conn.close()
self._essay = results[0][0]
self._difficulty = results[0][1]
return results
except Exception as e:
print(e)
#将文章分割成单词列表
def split_essay_to_word(self):
article = "".join(self._essay)
words = re.split(r"\b[\.,\s\n\r\n\$\']+?\b", article)
word_list = [word.lower() for word in words]
return word_list
#从数据库中查找和文章难度相同的单词
def find_same_difficulty_words(self):
result_list = []
try:
# 连接数据库
conn = sqlite3.connect("static/wordfreqapp.db")
# 创建游标
cursor = conn.cursor()
cursor.execute("select word from words where difficulty = " + str(self._difficulty))
results = cursor.fetchall()
conn.commit()
cursor.close()
conn.close()
for result in results:
for res in result:
result_list.append(res)
return result_list
except Exception as e:
print(e)
#获取单词的近义词
def get_word_synsets(self,word):
synsets_set = wn.synsets(word)
synset_list = []
for synset in synsets_set:
synset_list.append(synset.name().split(".")[0])
return synset_list
#生成完形填空
def create_clozeTest(self,essay):
essay.article_id = self._article_id
word = [] # 存放文章中含有的与文章难度相同的单词
answers = [] # 存放正确答案
questions = [] # 存放题目
database_words = essay.find_same_difficulty_words()
essay_words = essay.split_essay_to_word()
for essay_word in essay_words:
if database_words.__contains__(essay_word) and essay_word not in word:
word.append(essay_word)
if len(word) <= 10:
answers = word
else:
for i in range(0, 10):
w = word[random.randint(0, len(word) - 1)]
if not answers.__contains__(w):
answers.append(w)
self._answers = answers
No = 1
for answer in answers:
questions.append(list(answer.split(",")))
self._essay = self._essay.replace(answer,'('+str(No)+')____', 1)
No += 1
for question in questions:
synset = list(set(essay.get_word_synsets(question[0])))
if len(synset) == 0 or len(synset) == 1:
question.append(word[random.randint(0, len(word) - 1)])
else:
syn = synset[random.randint(0, len(synset) - 1)]
while (syn == question[0]):
syn = synset[random.randint(0, len(synset) - 1)]
question.append(syn)
while len(question) < 4:
add_word = word[random.randint(0, len(word) - 1)]
while question.__contains__(add_word):
add_word = word[random.randint(0, len(word) - 1)]
question.append(add_word)
random.shuffle(question)
self._questions = questions

View File

@ -11,7 +11,6 @@ from Article import *
import Yaml
from user_service import userService
from account_service import accountService
from cloze.create_clozeTest import Essay
app = Flask(__name__)
app.secret_key = 'lunch.time!'
@ -69,10 +68,9 @@ def mark_word():
for word in request.form.getlist('marked'):
lst.append((word, 1))
d = pickle_idea.merge_frequency(lst, lst_history)
pickle_idea.save_frequency_to_pickle(
d, path_prefix + 'static/frequency/frequency.p')
pickle_idea.save_frequency_to_pickle(d, path_prefix + 'static/frequency/frequency.p')
return redirect(url_for('mainpage'))
else: # 不回应GET请求
else: # 不回应GET请求
return 'Under construction'
@ -90,8 +88,7 @@ def mainpage():
d = load_freq_history(path_prefix + 'static/frequency/frequency.p')
lst_history = pickle_idea.dict2lst(d)
d = pickle_idea.merge_frequency(lst, lst_history)
pickle_idea.save_frequency_to_pickle(
d, path_prefix + 'static/frequency/frequency.p')
pickle_idea.save_frequency_to_pickle(d, path_prefix + 'static/frequency/frequency.p')
return render_template('mainpage_post.html', lst=lst, yml=Yaml.yml)
elif request.method == 'GET': # when we load a html page
@ -104,34 +101,14 @@ def mainpage():
d_len=d_len, lst=lst, yml=Yaml.yml)
essay = Essay()
@app.route('/goCroze', methods=['GET', 'POST'])
def go_ClozeTest():
if request.method == 'GET':
essay._article_id += 1
essay.create_crozeTest(essay)
return render_template('clozeTest.html', essay=essay, answers=None)
if request.method == 'POST':
score = 0
# submit_answers = []
questions = essay._questions
answers = essay._answers
for i in range(0, len(answers)):
ans = request.form.get('answer' + str(i+1))
if ans:
ans = ans.upper()
no = ord(ans)-ord('A')
if questions[i][no] == answers[i]:
score += 10
return render_template('clozeTest.html', essay=essay, msg=f'你的分数是: {score}', answers=answers)
if __name__ == '__main__':
'''
运行程序
'''
# app.secret_key = os.urandom(16)
# app.run(debug=False, port='6000')
app.run(debug=True)
# app.run(debug=True, port='6000')
# app.run(host='0.0.0.0', debug=True, port='6000')
# print(mod5('123'))

View File

@ -29,9 +29,10 @@ function highLight() {
if (sel_word1 != null) {
var list = sel_word1.value.split(" ");
for (var i = 0; i < list.length; ++i) {
list[i] = list[i].replace(/(^\s*)|(\s*$)/g, "");
list[i] = list[i].replace(/(^\s*)|(\s*$)/g, "");//消除字符串两边空字符
if (list[i] != "" && "<mark>".indexOf(list[i]) == -1 && "</mark>".indexOf(list[i]) == -1) {
txt = txt.replace(new RegExp(list[i], "g"), "<mark>" + list[i] + "</mark>");
txt = txt.replace(new RegExp("\\s"+list[i]+"\\s", "g"), " <mark>" + list[i] + "</mark> ");
}
}
}
@ -40,7 +41,7 @@ function highLight() {
for (var i = 0; i < list2.length; ++i) {
list2[i] = list2[i].replace(/(^\s*)|(\s*$)/g, "");
if (list2[i] != "" && "<mark>".indexOf(list2[i]) == -1 && "</mark>".indexOf(list2[i]) == -1) {
txt = txt.replace(new RegExp(list2[i], "g"), "<mark>" + list2[i] + "</mark>");
txt = txt.replace(new RegExp("\\s"+list2[i]+"\\s", "g"), " <mark>" + list2[i] + "</mark> ");
}
}
}

View File

@ -1,56 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>clozeTest</title>
<style>
pre {
font-size: large;
white-space: pre-wrap; /*css-3*/
background-color: aliceblue;
word-wrap: break-word; /*InternetExplorer5.5+*/
}
#button{
text-align: center;
}
#content{
background-color: azure;
margin: auto;
}
</style>
</head>
<body>
<p style="color: red; font-size: 20px">{{ msg }}</p>
{% set ascll = ['A','B','C','D'] %}
<pre>{{ essay._essay }}</pre>
<form action="/gocloze" id="button" method="GET">
<input type="submit" style="background-color: aquamarine; width: 100px; height: 40px" value="下一题">
</form>
<form action="/" id="button">
<input type="submit" style="background-color: aquamarine; width: 100px; height: 40px" value="返回">
</form>
<p style="color: red; font-size: 20px">请输入abcd或其大写</p>
<p style="color: red; font-size: 20px">tip:异常输入会被自动清除</p>
<form id="content" action="/gocloze" method="POST" >
<tr>
{% for question in essay._questions %}
<td>
<p><input type="text" onkeyup="this.value=this.value.replace(/[^a-dA-D]/g,'')" maxlength="1" name="answer{{ loop.index }}" placeholder="请输入答案" >
{{ loop.index }}.
{% for q in question %}
{{ ascll[loop.index0] }}.
{{ q }}
{% endfor %}
</p>
<p style="color: red">{{ answers[loop.index0] }}</p>
</td>
{% endfor %}
<p><input type="submit" value="提交" style="background-color: cornsilk; width: 100px; height: 40px"></p>
</tr>
</form>
</body>
</html>

View File

@ -32,10 +32,6 @@
<p><b>阅读文章并回答问题</b></p>
<div id="text-content">{{ today_article|safe }}</div>
<form action="/gocloze" method="GET">
<input type="submit" style.display="block" value="去做完型填空">
</form>
<input type="checkbox" onclick="ChangeHighlight()" checked/>生词高亮
<input type="checkbox" onclick="onReadClick()" checked/>大声朗读
<input type="checkbox" onclick="onChooseClick()" checked/>划词入库

View File

@ -1,4 +1,3 @@
Flask==2.1.2
Flask==1.1.2
selenium==3.141.0
PyYAML~=6.0
nltk==3.7