diff --git a/app/difficulty.py b/app/difficulty.py index 8e2d84b..2570f77 100644 --- a/app/difficulty.py +++ b/app/difficulty.py @@ -30,65 +30,66 @@ def difficulty_level_from_frequency(word, d): return level if 'what' in d: - ratio = (d['what']+1)/(d[word]+1) # what is a frequent word - level = math.log( max(ratio, 1), 2) + ratio = (d['what']+1)/(d[word]+1) # what is a frequent word + level = math.log(max(ratio, 1), 2) level = min(level, 8) return level -def get_difficulty_level_for_words_and_tests(dic): +def get_difficulty_level_for_words_and_tests(d_in): """ 对原本的单词库中的单词进行难度评级 - :param dic: 存储了单词库pickle文件中的单词的字典 + :param d_in: 存储了单词库pickle文件中的单词的字典 :return: """ d = {} - L = list(dic.keys()) # in dic, we have test types (e.g., CET4,CET6,BBC) for each word + L = list(d_in.keys()) # in dic, we have test types (e.g., CET4,CET6,BBC) for each word for k in L: - if 'CET4' in dic[k]: + if 'CET4' in d_in[k]: d[k] = 4 # CET4 word has level 4 - elif 'CET6' in dic[k]: + elif 'CET6' in d_in[k]: d[k] = 6 - elif 'BBC' in dic[k]: + elif 'IELTS' in d_in[k] or 'GRADUATE' in d_in[k]: # 雅思或研究生英语 d[k] = 8 - # print(k, d[k]) + elif 'EnWords' in d_in[k]: # 除基础词汇外的绝大多数词,包括一些犄角旮旯的专业词汇,近九万个,绝大多数我是真不认识 + d[k] = 7 return d # {'apple': 4, ...} -def simplify_the_words_dict(dic): +def simplify_the_words_dict(d): """ 用于把保存了词库中评级后的词新建一个以词根为键、以同词根的最低等级为值 """ stem = snowballstemmer.stemmer('english') - res = {} - for j in dic: # j 在字典中 + result = {} + for j in d: # j 在字典中 temp = stem.stemWord(j) # 提取j得词根 - if not temp in res: # 如果这个词根不在结果字典中,则以词根为键、以dic中的等级作为值添加 - res[temp] = dic[j] + if not temp in result: # 如果这个词根不在结果字典中,则以词根为键、以dic中的等级作为值添加 + result[temp] = d[j] else: # 如果这个词在结果词典中,则比较一下单词的难度等级是否最小 - if res[temp] > dic[j]: - res[temp] = dic[j] + if result[temp] > d[j]: + result[temp] = d[j] - return res + return result def get_difficulty_level(d1, d2): """ d2 来自于词库的27000个已标记单词 - d1 你个老六不会的词 + d1 用户不会的词 在d2的后面添加单词,没有新建一个新的字典 """ d2 = get_difficulty_level_for_words_and_tests(d2) # 根据d2的标记评级{'apple': 4, 'abandon': 4, ...} - d2_sim = simplify_the_words_dict(d2) # 提取d2的词根 {'appl': 4, 'abandon': 4, ...} + d2_simplified = simplify_the_words_dict(d2) # 提取d2的词根 {'appl': 4, 'abandon': 4, ...} stem = snowballstemmer.stemmer('english') for k in d1: # 用户的词 - for l in d2_sim: # l是词库的某个单词的词根 + for l in d2_simplified: # l是词库的某个单词的词根 if stem.stemWord(k) == l: # 两者相等则视为同一难度的词 - d2[k] = d2_sim[l] # 给d2定级 + d2[k] = d2_simplified[l] # 给d2定级 break else: # 不相等则表明词库中没这词,按照单词的频率定级 d2[k] = difficulty_level_from_frequency(k, d1) diff --git a/app/static/words_and_tests.p b/app/static/words_and_tests.p index 62afd6d..35e7cfc 100644 Binary files a/app/static/words_and_tests.p and b/app/static/words_and_tests.p differ