diff --git a/app/difficulty.py b/app/difficulty.py index 2570f77..3e6007b 100644 --- a/app/difficulty.py +++ b/app/difficulty.py @@ -37,26 +37,28 @@ def difficulty_level_from_frequency(word, d): return level -def get_difficulty_level_for_words_and_tests(d_in): +def get_difficulty_level_for_words_and_tests(d): """ 对原本的单词库中的单词进行难度评级 - :param d_in: 存储了单词库pickle文件中的单词的字典 + :param d: 存储了单词库pickle文件中的单词的字典 :return: """ - d = {} - L = list(d_in.keys()) # in dic, we have test types (e.g., CET4,CET6,BBC) for each word + result = {} + L = list(d.keys()) # in dic, we have test types (e.g., CET4,CET6,BBC) for each word for k in L: - if 'CET4' in d_in[k]: - d[k] = 4 # CET4 word has level 4 - elif 'CET6' in d_in[k]: - d[k] = 6 - elif 'IELTS' in d_in[k] or 'GRADUATE' in d_in[k]: # 雅思或研究生英语 - d[k] = 8 - elif 'EnWords' in d_in[k]: # 除基础词汇外的绝大多数词,包括一些犄角旮旯的专业词汇,近九万个,绝大多数我是真不认识 - d[k] = 7 + if 'CET4' in d[k]: + result[k] = 4 # CET4 word has level 4 + elif 'CET6' in d[k] or 'GRADUATE' in d[k]: + result[k] = 6 + elif 'IELTS' in d[k]: # 雅思或研究生英语 + result[k] = 7 + elif 'BBC' in d[k]: + result[k] = 8 + # elif 'EnWords' in d[k]: # 除基础词汇外的绝大多数词,包括一些犄角旮旯的专业词汇,近九万个,定级不太好处理,绝大多数我是真不认识 + # result[k] = 7 - return d # {'apple': 4, ...} + return result # {'apple': 4, ...} def simplify_the_words_dict(d): """ @@ -65,13 +67,13 @@ def simplify_the_words_dict(d): stem = snowballstemmer.stemmer('english') result = {} - for j in d: # j 在字典中 - temp = stem.stemWord(j) # 提取j得词根 - if not temp in result: # 如果这个词根不在结果字典中,则以词根为键、以dic中的等级作为值添加 - result[temp] = d[j] + for k in d: # j 在字典中 + temp = stem.stemWord(k) # 提取j得词根 + if temp not in result: # 如果这个词根不在结果字典中,则以词根为键、以dic中的等级作为值添加 + result[temp] = d[k] else: # 如果这个词在结果词典中,则比较一下单词的难度等级是否最小 - if result[temp] > d[j]: - result[temp] = d[j] + if result[temp] > d[k]: + result[temp] = d[k] return result