1
0
Fork 0

为系统添加一个全局变量记录数据库的单词及其等级,使得数据库单词等级只需在登录时进行一次评级,大致能将点击下一篇的时间缩减为原来的10^-15次以下,感谢章翊、赵煜涵、唐伟、宋江涛同学的建议,没有他们我懒得改的

Bug476-Yuhuangtao
俞黄焘 2023-06-18 21:43:00 +08:00
parent 3361e4ba79
commit 4e1e19d71d
1 changed files with 10 additions and 4 deletions

View File

@ -6,12 +6,14 @@
# Purpose: compute difficulty level of a English text # Purpose: compute difficulty level of a English text
import pickle import pickle
import math
import time import time
from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order
import snowballstemmer import snowballstemmer
from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order
# 定义一个全局的res_d 记录数据库单词评级之后的单词及其等级
res_d = {}
def load_record(pickle_fname): def load_record(pickle_fname):
f = open(pickle_fname, 'rb') f = open(pickle_fname, 'rb')
@ -43,7 +45,8 @@ def convert_test_type_to_difficulty_level(d):
result[k] = 8 result[k] = 8
time_end = time.time() time_end = time.time()
print('convert_test_type_to_difficulty_level totally cost', time_end - time_start) print('convert_test_type_to_difficulty_level totally cost', time_end - time_start)
global res_d
res_d = result
return result # {'apple': 4, ...} return result # {'apple': 4, ...}
@ -55,7 +58,10 @@ def get_difficulty_level_for_user(d1, d2):
""" """
time_start = time.time() time_start = time.time()
# TODO: convert_test_type_to_difficulty_level() should not be called every time. Each word's difficulty level should be pre-computed. # TODO: convert_test_type_to_difficulty_level() should not be called every time. Each word's difficulty level should be pre-computed.
if res_d == {}:
d2 = convert_test_type_to_difficulty_level(d2) # 根据d2的标记评级{'apple': 4, 'abandon': 4, ...} d2 = convert_test_type_to_difficulty_level(d2) # 根据d2的标记评级{'apple': 4, 'abandon': 4, ...}
else:
d2 = res_d
stemmer = snowballstemmer.stemmer('english') stemmer = snowballstemmer.stemmer('english')
for k in d1: # 用户的词 for k in d1: # 用户的词