1、重新对difficulty中部分函数名和变量名进行了修改,便于理解

2、对原先的词库进行了修改,原先apple和apples等词被错误收录在BBC级别里,被评为8级,现词库采用了近4500个四级词汇、2000个六级词汇、5000个考研词汇、4000个雅思词汇,此处共计7600个左右,有许多词同时具有2/3/4个标签,此外还有近九万个包括但不限于地名、人名、心理或医学等方面的词汇,比较少见,暂定等级为7
Bug476-ZhangWeiHao-YuHuangtao
俞黄焘 2023-05-11 21:32:08 +08:00
parent a39b0bb8e5
commit ddbce62089
2 changed files with 22 additions and 21 deletions

View File

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

Binary file not shown.