diff --git a/app/account_service.py b/app/account_service.py index a7ed0c4..e079514 100644 --- a/app/account_service.py +++ b/app/account_service.py @@ -1,6 +1,7 @@ from flask import * -from Login import check_username_availability, verify_user, add_user, get_expiry_date, change_password, WarningMessage - +from flask import escape, redirect +from Login import (WarningMessage, add_user, change_password, + check_username_availability, get_expiry_date, verify_user) # 初始化蓝图 accountService = Blueprint("accountService", __name__) diff --git a/app/model/article.py b/app/model/article.py index a3b4bf7..517a856 100644 --- a/app/model/article.py +++ b/app/model/article.py @@ -1,6 +1,8 @@ -from model import * from datetime import datetime +from model import * + + def add_article(content, source="manual_input", level="5", question="No question"): with db_session: # add one article to sqlite @@ -32,3 +34,15 @@ def get_page_articles(num, size): x for x in Article.select().order_by(desc(Article.article_id)).page(num, size) ] + +def get_article_by_id(article_id): + article_id &= 0xFFFFFFFF # max 32 bits + with db_session: + article = Article.select(article_id=article_id).first() + if article: + return article.to_dict() + + +def get_all_articles(): + with db_session: + return [art.to_dict() for art in Article.select()[:]] \ No newline at end of file diff --git a/app/test/test_vocabulary.py b/app/test/test_vocabulary.py new file mode 100644 index 0000000..9b09b3e --- /dev/null +++ b/app/test/test_vocabulary.py @@ -0,0 +1,36 @@ +import math + +from vocabulary import ArticleVocabularyLevel, UserVocabularyLevel + + +def test_article_level(): + ''' Boundary case test ''' + article = ArticleVocabularyLevel('') + assert article.level == 0 + +def test_user_level(): + ''' Boundary case test ''' + user = UserVocabularyLevel({}) + assert user.level == 0 + +def test_article_level_3(): + ''' Normal case test ''' + article = ArticleVocabularyLevel('This is an interesting article that frequently uses the word "banana".') + assert article.level == 3.090909090909091 + +def test_user_level_4(): + ''' Normal case test ''' + d = {'apple': ['BBC', 'CTE4'], 'banana': ['BBC', 'CTE4']} + user = UserVocabularyLevel(d) + assert user.level == 4.0 + +def test_article_level_5(): + ''' Abnormal case test ''' + article = ArticleVocabularyLevel('This is an interesting article that frequently uses the word "banana".') + assert article.level != 5.0 + +def test_user_level_6(): + ''' Abnormal case test ''' + d = {'apple': ['BBC', 'CTE4'], 'banana': ['BBC', 'CTE4']} + user = UserVocabularyLevel(d) + assert user.level != 6.0 \ No newline at end of file diff --git a/app/vocabulary.py b/app/vocabulary.py new file mode 100644 index 0000000..37e7dcb --- /dev/null +++ b/app/vocabulary.py @@ -0,0 +1,52 @@ +import pickle + + +def load_record(pickle_fname): + with open(pickle_fname, 'rb') as f: + d = pickle.load(f) + return d + + +class VocabularyLevelEstimator: + _test = load_record('static/words_and_tests.p') # map a word to the sources where it appears + + @property + def level(self): + if self.word_lst == []: + return 0 + total = 0.0 # TODO: need to compute this number + num = 0 + for word in self.word_lst: + num += 1 + if word in self._test: + total += len(self._test[word]) + print(f'{word} : {self._test[word]}') + else: + print(f'{word}') + return total / num + + +class UserVocabularyLevel(VocabularyLevelEstimator): + def __init__(self, d): + self.d = d + self.word_lst = list(d.keys()) + # just look at the most recently-added words + + +class ArticleVocabularyLevel(VocabularyLevelEstimator): + def __init__(self, content): + self.content = content + self.word_lst = content.lower().split() + # select the 10 most difficult words + + def top_ten_difficult_words(self): + pass + + +if __name__ == '__main__': + d = load_record('frequency_arnold.pickle') + # print(d) + user = UserVocabularyLevel(d) + print(user.level) # level is a property + article = ArticleVocabularyLevel('This is an interesting article that frequently uses the word "banana".') + print(article.level)