Compare commits

..

2 Commits

1 changed files with 60 additions and 18 deletions

View File

@ -8,6 +8,7 @@
import pickle import pickle
import math import math
from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order
import snowballstemmer
def load_record(pickle_fname): def load_record(pickle_fname):
@ -18,6 +19,12 @@ def load_record(pickle_fname):
def difficulty_level_from_frequency(word, d): def difficulty_level_from_frequency(word, d):
"""
根据单词的频率进行难度的评级
:param word:
:param d:
:return:
"""
level = 1 level = 1
if not word in d: if not word in d:
return level return level
@ -30,27 +37,62 @@ def difficulty_level_from_frequency(word, d):
return level return level
def get_difficulty_level(d1, d2): def get_difficulty_level_for_words_and_tests(dic):
"""
对原本的单词库中的单词进行难度评级
:param dic: 存储了单词库pickle文件中的单词的字典
:return:
"""
d = {} d = {}
L = list(d1.keys()) # in d1, we have freuqence for each word L = list(dic.keys()) # in dic, we have test types (e.g., CET4,CET6,BBC) for each word
L2 = list(d2.keys()) # in d2, we have test types (e.g., CET4,CET6,BBC) for each word
L.extend(L2) for k in L:
L3 = list(set(L)) # L3 contains all words if 'CET4' in dic[k]:
for k in L3:
if k in d2:
if 'CET4' in d2[k]:
d[k] = 4 # CET4 word has level 4 d[k] = 4 # CET4 word has level 4
elif 'CET6' in d2[k]: elif 'CET6' in dic[k]:
d[k] = 6 d[k] = 6
elif 'BBC' in d2[k]: elif 'BBC' in dic[k]:
d[k] = 8 d[k] = 8
if k in d1: # BBC could contain easy words that are not in CET4 or CET6. So 4 is not reasonable. Recompute difficulty level. # print(k, d[k])
d[k] = min(difficulty_level_from_frequency(k, d1), d[k])
elif k in d1:
d[k] = difficulty_level_from_frequency(k, d1)
return d return d # {'apple': 4, ...}
def simplify_the_words_dict(dic):
"""
用于把保存了词库中评级后的词新建一个以词根为键以同词根的最低等级为值
"""
stem = snowballstemmer.stemmer('english')
res = {}
for j in dic: # j 在字典中
temp = stem.stemWord(j) # 提取j得词根
if not temp in res: # 如果这个词根不在结果字典中则以词根为键、以dic中的等级作为值添加
res[temp] = dic[j]
else: # 如果这个词在结果词典中,则比较一下单词的难度等级是否最小
if res[temp] > dic[j]:
res[temp] = dic[j]
return res
def get_difficulty_level(d1, d2):
"""
d2 来自于词库的27000个已标记单词
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, ...}
stem = snowballstemmer.stemmer('english')
for k in d1: # 用户的词
for l in d2_sim: # l是词库的某个单词的词根
if stem.stemWord(k) == l: # 两者相等则视为同一难度的词
d2[k] = d2_sim[l] # 给d2定级
break
else: # 不相等则表明词库中没这词,按照单词的频率定级
d2[k] = difficulty_level_from_frequency(k, d1)
return d2
def revert_dict(d): def revert_dict(d):