Bug585-Petou PR with vocabulary_test.py

pull/207/head
Nze Avomo Zenovio Ndong 2025-06-04 19:52:22 +08:00
parent ff6286cf01
commit 9f749874bf
4 changed files with 106 additions and 3 deletions

View File

@ -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__)

View File

@ -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()[:]]

View File

@ -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

52
app/vocabulary.py Normal file
View File

@ -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)