From a39b0bb8e5c0914f289c6ebe4118e7efb1203d11 Mon Sep 17 00:00:00 2001 From: PlutoCtx Date: Wed, 10 May 2023 13:35:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E4=BA=86simplify=5Fthe=5Fwor?= =?UTF-8?q?ds=5Fdict=E5=87=BD=E6=95=B0=E4=B8=93=E9=97=A8=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E7=BB=99=E8=AF=84=E7=BA=A7=E5=90=8E=E7=9A=84=E8=AF=8D=E5=BA=93?= =?UTF-8?q?=E5=8D=95=E8=AF=8D=E6=B8=85=E6=B4=97=E8=AF=8D=E6=A0=B9=EF=BC=8C?= =?UTF-8?q?=E8=AF=8D=E6=A0=B9=E7=9A=84=E9=9A=BE=E5=BA=A6=E6=8C=89=E7=85=A7?= =?UTF-8?q?=E5=85=B6=E5=8F=98=E5=BC=8F=E7=9A=84=E6=9C=80=E4=BD=8E=E7=AD=89?= =?UTF-8?q?=E7=BA=A7=E5=86=B3=E5=AE=9A=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/difficulty.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/app/difficulty.py b/app/difficulty.py index 34e7717..8e2d84b 100644 --- a/app/difficulty.py +++ b/app/difficulty.py @@ -53,29 +53,46 @@ def get_difficulty_level_for_words_and_tests(dic): d[k] = 6 elif 'BBC' in dic[k]: d[k] = 8 - print(k, d[k]) + # print(k, d[k]) 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 = 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: # k是用户不会的词 - for l in d2: # l是已经完成评级的词库的词 - if k == l: # k == l,这个用户也不会的词刚好以原型的形式出现在词库中,因为词库已经评过难度了,所以啥也不用干 + for k in d1: # 用户的词 + for l in d2_sim: # l是词库的某个单词的词根 + if stem.stemWord(k) == l: # 两者相等则视为同一难度的词 + d2[k] = d2_sim[l] # 给d2定级 break - elif stem.stemWord(k) in l: # 这个词的词根与词库中的某个词一样,我们认为是同一难度的词 - d1[k] = d2[l] - else: # 这个词不在词库中,按频率来评定难度 + else: # 不相等则表明词库中没这词,按照单词的频率定级 d2[k] = difficulty_level_from_frequency(k, d1) - return d2 - def revert_dict(d):