129 lines
5.8 KiB
Python
129 lines
5.8 KiB
Python
# Run this test script on the command line:
|
||
# pytest test_vocabulary.py
|
||
#
|
||
# Last modified by Mr Lan Hui on 2025-05-08
|
||
|
||
from difficulty import (
|
||
user_difficulty_level,
|
||
text_difficulty_level,
|
||
get_difficulty_level_for_user,
|
||
load_record
|
||
)
|
||
|
||
# 词库加载(只加载一次)
|
||
d_word_test = load_record("../../../updated_source_code/English Pal/app/oxford_words.txt")
|
||
d_difficulty_default = get_difficulty_level_for_user({}, d_word_test)
|
||
print(d_difficulty_default)
|
||
# ----------------------------- 文章难度测试 -----------------------------
|
||
|
||
def test_article_level_empty_content():
|
||
level = text_difficulty_level('', d_difficulty_default)
|
||
assert level == 0
|
||
|
||
def test_article_level_punctuation_only():
|
||
level = text_difficulty_level(',', d_difficulty_default)
|
||
assert level == 0
|
||
|
||
def test_article_level_digit_only():
|
||
level = text_difficulty_level('1', d_difficulty_default)
|
||
assert level == 0
|
||
|
||
def test_article_level_single_word():
|
||
level = text_difficulty_level('source', d_difficulty_default)
|
||
assert 2 <= level <= 4
|
||
|
||
def test_article_level_subset_vs_superset():
|
||
level1 = text_difficulty_level('source', d_difficulty_default)
|
||
level2 = text_difficulty_level('open source', d_difficulty_default)
|
||
print(f"Word: 'source', Difficulty: {d_difficulty_default.get('source', 'Unknown')}")
|
||
assert level1 <= level2
|
||
|
||
def test_article_level_multiple_words():
|
||
content = 'Producing Open Source Software - How to Run a Successful Free Software Project'
|
||
level = text_difficulty_level(content, d_difficulty_default)
|
||
assert 3 <= level <= 5
|
||
|
||
def test_article_level_short_paragraph():
|
||
content = ("At parties, people no longer give me a blank stare when I tell them I work in open source software. "
|
||
"‘Oh, yes — like Linux?’ they say. I nod eagerly in agreement. ‘Yes, exactly! That’s what I do.’ "
|
||
"It’s nice not to be completely fringe anymore...")
|
||
level = text_difficulty_level(content, d_difficulty_default)
|
||
assert 4 <= level <= 6
|
||
|
||
def test_article_level_medium_paragraph():
|
||
content = ("In considering the Origin of Species it is quite conceivable that a naturalist reflecting on the mutual affinities of organic being ") # truncated for brevity
|
||
level = text_difficulty_level(content, d_difficulty_default)
|
||
for word in content.split():
|
||
print(f"Word: {word} , Difficulty: {d_difficulty_default.get(word.lower(), 'Unknown')}")
|
||
|
||
assert 5 <= level <= 7
|
||
|
||
def test_article_level_long_paragraph():
|
||
content = ("These several facts accord well with my theory. I believe in no fixed law of development, causing all the "
|
||
"inhabitants of a country to change abruptly...") # truncated for brevity
|
||
level = text_difficulty_level(content, d_difficulty_default)
|
||
assert 4 <= level <= 8
|
||
|
||
# ----------------------------- 用户词汇难度测试 -----------------------------
|
||
|
||
def test_user_level_empty_dictionary():
|
||
d_user = {}
|
||
d_diff = get_difficulty_level_for_user(d_user, d_word_test)
|
||
level = user_difficulty_level(d_user, d_diff)
|
||
assert level == 0
|
||
|
||
def test_user_level_one_simple_word():
|
||
d_user = {'simple': ['202408050930']}
|
||
d_diff = get_difficulty_level_for_user(d_user, d_word_test)
|
||
level = user_difficulty_level(d_user, d_diff)
|
||
assert 0 < level <= 4
|
||
|
||
def test_user_level_invalid_word():
|
||
d_user = {'xyz': ['202408050930']}
|
||
d_diff = get_difficulty_level_for_user(d_user, d_word_test)
|
||
level = user_difficulty_level(d_user, d_diff)
|
||
assert level == 0
|
||
|
||
def test_user_level_one_hard_word():
|
||
d_user = {'pasture': ['202408050930']}
|
||
d_diff = get_difficulty_level_for_user(d_user, d_word_test)
|
||
level = user_difficulty_level(d_user, d_diff)
|
||
assert 5 <= level <= 8
|
||
|
||
def test_user_level_multiple_words():
|
||
d_user = {
|
||
'sessile': ['202408050930'], 'putrid': ['202408050930'], 'prodigal': ['202408050930'],
|
||
'presumptuous': ['202408050930'], 'prehension': ['202408050930'], 'pied': ['202408050930'],
|
||
'pedunculated': ['202408050930'], 'pasture': ['202408050930'], 'parturition': ['202408050930'],
|
||
'ovigerous': ['202408050930'], 'orifice': ['202408050930'], 'aberration': ['202408050930'],
|
||
'obliterate': ['202408050930'], 'niggard': ['202408050930'], 'neuter': ['202408050930'],
|
||
'locomotion': ['202408050930'], 'lineal': ['202408050930'], 'glottis': ['202408050930'],
|
||
'frivolous': ['202408050930'], 'frena': ['202408050930'], 'flotation': ['202408050930'],
|
||
'ductus': ['202408050930'], 'dorsal': ['202408050930'], 'dearth': ['202408050930'],
|
||
'crustacean': ['202408050930'], 'cornea': ['202408050930'], 'contrivance': ['202408050930'],
|
||
'collateral': ['202408050930'], 'cirriped': ['202408050930'], 'canon': ['202408050930'],
|
||
'branchiae': ['202408050930'], 'auditory': ['202408050930'], 'articulata': ['202408050930'],
|
||
'alimentary': ['202408050930'], 'adduce': ['202408050930'],
|
||
}
|
||
d_diff = get_difficulty_level_for_user(d_user, d_word_test)
|
||
level = user_difficulty_level(d_user, d_diff)
|
||
assert 6 <= level <= 8
|
||
|
||
def test_user_level_consider_only_most_recent_words_difficult_words_most_recent():
|
||
d_user = {
|
||
'pasture': ['202408050930'], 'putrid': ['202408040000'], 'frivolous': ['202408030000'],
|
||
'simple': ['202408020000'], 'apple': ['202408010000']
|
||
}
|
||
d_diff = get_difficulty_level_for_user(d_user, d_word_test)
|
||
level = user_difficulty_level(d_user, d_diff)
|
||
assert 5 <= level <= 8
|
||
|
||
def test_user_level_consider_only_most_recent_words_easy_words_most_recent():
|
||
d_user = {
|
||
'simple': ['202408050930'], 'apple': ['202408040000'], 'happy': ['202408030000'],
|
||
'pasture': ['202408020000'], 'putrid': ['202408010000'], 'dearth': ['202407310000']
|
||
}
|
||
d_diff = get_difficulty_level_for_user(d_user, d_word_test)
|
||
level = user_difficulty_level(d_user, d_diff)
|
||
assert 3 <= level <= 5
|