diff --git a/app/Article.py b/app/Article.py index e0f006a..df9ac3a 100644 --- a/app/Article.py +++ b/app/Article.py @@ -7,7 +7,7 @@ import random, glob import hashlib from datetime import datetime from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages -from difficulty import get_difficulty_level, text_difficulty_level, user_difficulty_level +from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level path_prefix = '/var/www/wordfreq/wordfreq/' @@ -32,17 +32,20 @@ def get_article_body(s): return '\n'.join(lst) -def get_today_article(user_word_list, existing_articles): +def get_today_article(user_word_list, visited_articles): rq = RecordQuery(path_prefix + 'static/wordfreqapp.db') - if existing_articles is None: - existing_articles = { + if visited_articles is None: + visited_articles = { "index" : 0, # 为 article_ids 的索引 "article_ids": [] # 之前显示文章的id列表,越后越新 } - if existing_articles["index"] > len(existing_articles["article_ids"])-1: + if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章,因此查找所有的文章 rq.instructions("SELECT * FROM article") - else: - rq.instructions('SELECT * FROM article WHERE article_id=%d' % (existing_articles["article_ids"][existing_articles["index"]])) + else: # 生成阅读过的文章,因此查询指定 article_id 的文章 + if visited_articles["article_ids"][visited_articles["index"]] == 'null': # 可能因为直接刷新页面导致直接去查询了'null',因此当刷新的页面的时候,需要直接进行“上一篇”操作 + visited_articles["index"] -= 1 + visited_articles["article_ids"].pop() + rq.instructions('SELECT * FROM article WHERE article_id=%d' % (visited_articles["article_ids"][visited_articles["index"]])) rq.do() result = rq.get_results() random.shuffle(result) @@ -50,28 +53,36 @@ def get_today_article(user_word_list, existing_articles): # Choose article according to reader's level d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p') d2 = load_freq_history(path_prefix + 'static/words_and_tests.p') - d3 = get_difficulty_level(d1, d2) + d3 = get_difficulty_level_for_user(d1, d2) d = None + result_of_generate_article = "not found" d_user = load_freq_history(user_word_list) user_level = user_difficulty_level(d_user, d3) # more consideration as user's behaviour is dynamic. Time factor should be considered. text_level = 0 - if existing_articles["index"] > len(existing_articles["article_ids"])-1: # 下一篇 - flag_get_article = False - for reading in result: - text_level = text_difficulty_level(reading['text'], d3) - factor = random.gauss(0.8, - 0.1) # a number drawn from Gaussian distribution with a mean of 0.8 and a stand deviation of 1 - if reading['article_id'] not in existing_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平 - d = reading - existing_articles["article_ids"].append(d['article_id']) # 列表添加新的文章id;下面进行 - flag_get_article = True - break - if not flag_get_article: - existing_articles["index"] -= 1 - else: # 上一篇 + if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章 + amount_of_visited_articles = len(visited_articles["article_ids"]) + amount_of_existing_articles = result.__len__() + if amount_of_visited_articles == amount_of_existing_articles: # 如果当前阅读过的文章的数量 == 存在的文章的数量,即所有的书本都阅读过了 + result_of_generate_article = "had read all articles" + else: + for k in range(3): # 最多尝试3次 + for reading in result: + text_level = text_difficulty_level(reading['text'], d3) + factor = random.gauss(0.8, 0.1) # a number drawn from Gaussian distribution with a mean of 0.8 and a stand deviation of 1 + if reading['article_id'] not in visited_articles["article_ids"] and within_range(text_level, user_level, (8.0 - user_level) * factor): # 新的文章之前没有出现过且符合一定范围的水平 + d = reading + visited_articles["article_ids"].append(d['article_id']) # 列表添加新的文章id;下面进行 + result_of_generate_article = "found" + break + if result_of_generate_article == "found": # 用于成功找到文章后及时退出外层循环 + break + if result_of_generate_article != "found": # 阅读完所有文章,或者循环3次没有找到适合的文章,则放入空(“null”) + visited_articles["article_ids"].append('null') + else: # 生成已经阅读过的文章 d = random.choice(result) text_level = text_difficulty_level(d['text'], d3) + result_of_generate_article = "found" today_article = None if d: @@ -86,7 +97,7 @@ def get_today_article(user_word_list, existing_articles): "answer": get_answer_part(d['question']) } - return existing_articles, today_article + return visited_articles, today_article, result_of_generate_article def load_freq_history(path): diff --git a/app/account_service.py b/app/account_service.py index c1bd64c..a7ed0c4 100644 --- a/app/account_service.py +++ b/app/account_service.py @@ -37,7 +37,7 @@ def signup(): session[username] = username session['username'] = username session['expiry_date'] = get_expiry_date(username) - session['existing_articles'] = None + session['visited_articles'] = None return jsonify({'status': '2'}) else: return jsonify({'status': '1'}) @@ -66,7 +66,7 @@ def login(): session['username'] = username user_expiry_date = get_expiry_date(username) session['expiry_date'] = user_expiry_date - session['existing_articles'] = None + session['visited_articles'] = None return jsonify({'status': '1'}) else: return jsonify({'status': '0'}) diff --git a/app/admin_service.py b/app/admin_service.py index 1d1ba6e..a604b5e 100644 --- a/app/admin_service.py +++ b/app/admin_service.py @@ -91,10 +91,7 @@ def article(): question = data.get("question", "") level = data.get("level", "4") if content: - try: # check level - if level not in ['1', '2', '3', '4']: - raise ValueError - except ValueError: + if level not in ['1', '2', '3', '4']: return "Level must be between 1 and 4." add_article(content, source, level, question) _update_context() diff --git a/app/difficulty.py b/app/difficulty.py index 50aa179..cb93768 100644 --- a/app/difficulty.py +++ b/app/difficulty.py @@ -8,6 +8,7 @@ import pickle import math from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order, sort_in_ascending_order +import snowballstemmer def load_record(pickle_fname): @@ -17,41 +18,51 @@ def load_record(pickle_fname): return d -def difficulty_level_from_frequency(word, d): - level = 1 - if not word in 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) +def convert_test_type_to_difficulty_level(d): + """ + 对原本的单词库中的单词进行难度评级 + :param d: 存储了单词库pickle文件中的单词的字典 + :return: + """ + result = {} + L = list(d.keys()) # in d, we have test types (e.g., CET4,CET6,BBC) for each word - level = min(level, 8) - return level + for k in L: + if 'CET4' in d[k]: + result[k] = 4 # CET4 word has level 4 + elif 'OXFORD3000' in d[k]: + result[k] = 5 + elif 'CET6' in d[k] or 'GRADUATE' in d[k]: + result[k] = 6 + elif 'OXFORD5000' in d[k] or 'IELTS' in d[k]: + result[k] = 7 + elif 'BBC' in d[k]: + result[k] = 8 + + return result # {'apple': 4, ...} -def get_difficulty_level(d1, d2): - d = {} - L = list(d1.keys()) # in d1, we have freuqence for each word - L2 = list(d2.keys()) # in d2, we have test types (e.g., CET4,CET6,BBC) for each word - L.extend(L2) - L3 = list(set(L)) # L3 contains all words - for k in L3: - if k in d2: - if 'CET4' in d2[k]: - d[k] = 4 # CET4 word has level 4 - elif 'CET6' in d2[k]: - d[k] = 6 - elif 'BBC' in d2[k]: - 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. - d[k] = min(difficulty_level_from_frequency(k, d1), d[k]) - elif k in d1: - d[k] = difficulty_level_from_frequency(k, d1) +def get_difficulty_level_for_user(d1, d2): + """ + d2 来自于词库的35511个已标记单词 + d1 用户不会的词 + 在d2的后面添加单词,没有新建一个新的字典 + """ + # TODO: convert_test_type_to_difficulty_level() should not be called every time. Each word's difficulty level should be pre-computed. + d2 = convert_test_type_to_difficulty_level(d2) # 根据d2的标记评级{'apple': 4, 'abandon': 4, ...} + stemmer = snowballstemmer.stemmer('english') - return d + for k in d1: # 用户的词 + if k in d2: # 如果用户的词以原型的形式存在于词库d2中 + continue # 无需评级,跳过 + else: + stem = stemmer.stemWord(k) + if stem in d2: # 如果用户的词的词根存在于词库d2的词根库中 + d2[k] = d2[stem] # 按照词根进行评级 + else: + d2[k] = 3 # 如果k的词根都不在,那么就当认为是3级 + return d2 - def revert_dict(d): ''' @@ -62,12 +73,13 @@ def revert_dict(d): for k in d: if type(d[k]) is list: # d[k] is a list of dates. lst = d[k] - elif type(d[k]) is int: # for backward compatibility. d was sth like {'word':1}. The value d[k] is not a list of dates, but a number representing how frequent this word had been added to the new word book. + elif type(d[ + k]) is int: # for backward compatibility. d was sth like {'word':1}. The value d[k] is not a list of dates, but a number representing how frequent this word had been added to the new word book. freq = d[k] - lst = freq*['2021082019'] # why choose this date? No particular reasons. I fix the bug in this date. + lst = freq * ['2021082019'] # why choose this date? No particular reasons. I fix the bug in this date. for time_info in lst: - date = time_info[:10] # until hour + date = time_info[:10] # until hour if not date in d2: d2[date] = [k] else: @@ -76,42 +88,44 @@ def revert_dict(d): def user_difficulty_level(d_user, d): - d_user2 = revert_dict(d_user) # key is date, and value is a list of words added in that date + d_user2 = revert_dict(d_user) # key is date, and value is a list of words added in that date count = 0 geometric = 1 - for date in sorted(d_user2.keys(), reverse=True): # most recently added words are more important while determining user's level - lst = d_user2[date] # a list of words - lst2 = [] # a list of tuples, (word, difficulty level) - for word in lst: + for date in sorted(d_user2.keys(), + reverse=True): # most recently added words are more important while determining user's level + lst = d_user2[date] # a list of words + lst2 = [] # a list of tuples, (word, difficulty level) + for word in lst: if word in d: lst2.append((word, d[word])) - lst3 = sort_in_ascending_order(lst2) # easiest tuple first - #print(lst3) + lst3 = sort_in_ascending_order(lst2) # easiest tuple first + # print(lst3) for t in lst3: word = t[0] hard = t[1] - #print('WORD %s HARD %4.2f' % (word, hard)) + # print('WORD %s HARD %4.2f' % (word, hard)) geometric = geometric * (hard) count += 1 if count >= 10: - return geometric**(1/count) + return geometric ** (1 / count) - return geometric**(1/max(count,1)) + return geometric ** (1 / max(count, 1)) def text_difficulty_level(s, d): s = remove_punctuation(s) L = freq(s) - lst = [] # a list of tuples, each tuple being (word, difficulty level) + lst = [] # a list of tuples, each tuple being (word, difficulty level) + stop_words = {'the':1, 'and':1, 'of':1, 'to':1, 'what':1, 'in':1, 'there':1, 'when':1, 'them':1, 'would':1, 'will':1, 'out':1, 'his':1, 'mr':1, 'that':1, 'up':1, 'more':1, 'your':1, 'it':1, 'now':1, 'very':1, 'then':1, 'could':1, 'he':1, 'any':1, 'some':1, 'with':1, 'into':1, 'you':1, 'our':1, 'man':1, 'other':1, 'time':1, 'was':1, 'than':1, 'know':1, 'about':1, 'only':1, 'like':1, 'how':1, 'see':1, 'is':1, 'before':1, 'such':1, 'little':1, 'two':1, 'its':1, 'as':1, 'these':1, 'may':1, 'much':1, 'down':1, 'for':1, 'well':1, 'should':1, 'those':1, 'after':1, 'same':1, 'must':1, 'say':1, 'first':1, 'again':1, 'us':1, 'great':1, 'where':1, 'being':1, 'come':1, 'over':1, 'good':1, 'himself':1, 'am':1, 'never':1, 'on':1, 'old':1, 'here':1, 'way':1, 'at':1, 'go':1, 'upon':1, 'have':1, 'had':1, 'without':1, 'my':1, 'day':1, 'be':1, 'but':1, 'though':1, 'from':1, 'not':1, 'too':1, 'another':1, 'this':1, 'even':1, 'still':1, 'her':1, 'yet':1, 'under':1, 'by':1, 'let':1, 'just':1, 'all':1, 'because':1, 'we':1, 'always':1, 'off':1, 'yes':1, 'so':1, 'while':1, 'why':1, 'which':1, 'me':1, 'are':1, 'or':1, 'no':1, 'if':1, 'an':1, 'also':1, 'thus':1, 'who':1, 'cannot':1, 'she':1, 'whether':1} # ignore these words while computing the artile's difficulty level for x in L: word = x[0] - if word in d: + if word not in stop_words and word in d: lst.append((word, d[word])) - lst2 = sort_in_descending_order(lst) # most difficult words on top - #print(lst2) + lst2 = sort_in_descending_order(lst) # most difficult words on top + # print(lst2) count = 0 geometric = 1 for t in lst2: @@ -119,24 +133,20 @@ def text_difficulty_level(s, d): hard = t[1] geometric = geometric * (hard) count += 1 - if count >= 20: # we look for n most difficult words - return geometric**(1/count) - - return geometric**(1/max(count,1)) + if count >= 20: # we look for n most difficult words + return geometric ** (1 / count) + return geometric ** (1 / max(count, 1)) if __name__ == '__main__': - - d1 = load_record('frequency.p') - #print(d1) + # print(d1) d2 = load_record('words_and_tests.p') - #print(d2) + # print(d2) - - d3 = get_difficulty_level(d1, d2) + d3 = get_difficulty_level_for_user(d1, d2) s = ''' South Lawn @@ -197,7 +207,6 @@ Amidst the aftermath of this shocking referendum vote, there is great uncertaint ''' - s = ''' British Prime Minister Boris Johnson walks towards a voting station during the Brexit referendum in Britain, June 23, 2016. (Photo: EPA-EFE) @@ -218,7 +227,6 @@ The prime minister was forced to ask for an extension to Britain's EU departure Johnson has repeatedly pledged to finalize the first stage, a transition deal, of Britain's EU divorce battle by Oct. 31. A second stage will involve negotiating its future relationship with the EU on trade, security and other salient issues. ''' - s = ''' Thank you very much. We have a Cabinet meeting. We’ll have a few questions after grace. And, if you would, Ben, please do the honors. @@ -233,17 +241,11 @@ We need — for our farmers, our manufacturers, for, frankly, unions and non-uni ''' - - - - #f = open('bbc-fulltext/bbc/entertainment/001.txt') + # f = open('bbc-fulltext/bbc/entertainment/001.txt') f = open('wordlist.txt') s = f.read() f.close() - - - print(text_difficulty_level(s, d3)) - + diff --git a/app/pickle_idea2.py b/app/pickle_idea2.py index 4055fc4..0da55bc 100644 --- a/app/pickle_idea2.py +++ b/app/pickle_idea2.py @@ -68,7 +68,7 @@ def save_frequency_to_pickle(d, pickle_fname): d2 = {} for k in d: if not k in exclusion_lst and not k.isnumeric() and not len(k) < 2: - d2[k] = list(sorted(set(d[k]))) + d2[k] = list(sorted(d[k])) # 原先这里是d2[k] = list(sorted(set(d[k]))) pickle.dump(d2, f) f.close() diff --git a/app/static/config.yml b/app/static/config.yml index 20aa396..e6f7ef1 100644 --- a/app/static/config.yml +++ b/app/static/config.yml @@ -1,16 +1,16 @@ # 全局引入的css文件地址 css: item: - - static/css/bootstrap.css + - ../static/css/bootstrap.css # 全局引入的js文件地址 js: head: # 在页面加载之前加载 - - static/js/jquery.js - - static/js/word_operation.js + - ../static/js/jquery.js + - ../static/js/word_operation.js bottom: # 在页面加载完之后加载 - - static/js/fillword.js - - static/js/highlight.js + - ../static/js/fillword.js + - ../static/js/highlight.js # 高亮样式,目前仅支持修改颜色 highlight: diff --git a/app/static/words_and_tests.p b/app/static/words_and_tests.p index 62afd6d..5c46c5a 100644 Binary files a/app/static/words_and_tests.p and b/app/static/words_and_tests.p differ diff --git a/app/templates/admin_index.html b/app/templates/admin_index.html index 68ee68f..f62a137 100644 --- a/app/templates/admin_index.html +++ b/app/templates/admin_index.html @@ -30,7 +30,7 @@
diff --git a/app/templates/admin_manage_user.html b/app/templates/admin_manage_user.html index a3f0ca0..cee4667 100644 --- a/app/templates/admin_manage_user.html +++ b/app/templates/admin_manage_user.html @@ -68,9 +68,9 @@ {% endfor %} {% endif %} +