Compare commits
1 Commits
master
...
Bug585-zha
Author | SHA1 | Date |
---|---|---|
|
2239485e40 |
Binary file not shown.
|
@ -0,0 +1,94 @@
|
|||
# Run this test script on the command line:
|
||||
# pytest test_vocabulary.py
|
||||
#
|
||||
# Last modified by Mr Lan Hui on 2025-03-05
|
||||
|
||||
from vocabulary import UserVocabularyLevel, ArticleVocabularyLevel
|
||||
|
||||
|
||||
def test_article_level_empty_content():
|
||||
''' Boundary case test '''
|
||||
article = ArticleVocabularyLevel('')
|
||||
assert article.level == 0
|
||||
|
||||
def test_article_level_punctuation_only():
|
||||
''' Boundary case test '''
|
||||
article = ArticleVocabularyLevel(',')
|
||||
assert article.level == 0
|
||||
|
||||
def test_article_level_digit_only():
|
||||
''' Boundary case test '''
|
||||
article = ArticleVocabularyLevel('1')
|
||||
assert article.level == 0
|
||||
|
||||
def test_article_level_single_word():
|
||||
''' Boundary case test '''
|
||||
article = ArticleVocabularyLevel('source')
|
||||
assert 2 <= article.level <= 4
|
||||
|
||||
def test_article_level_subset_vs_superset():
|
||||
''' Boundary case test '''
|
||||
article1 = ArticleVocabularyLevel('source')
|
||||
article2 = ArticleVocabularyLevel('open source')
|
||||
assert article1.level < article2.level
|
||||
|
||||
def test_article_level_multiple_words():
|
||||
''' Boundary case test '''
|
||||
article = ArticleVocabularyLevel('Producing Open Source Software - How to Run a Successful Free Software Project')
|
||||
assert 3 <= article.level <= 5
|
||||
|
||||
def test_article_level_short_paragraph():
|
||||
''' Boundary case test '''
|
||||
article = ArticleVocabularyLevel('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. In the past, the next question was usually fairly predictable: "How do you make money doing that?" To answer, I\'d summarize the economics of free software: that there are organizations in whose interest it is to have certain software exist, but that they don\'t need to sell copies, they just want to make sure the software is available and maintained, as a tool instead of as a rentable monopoly.')
|
||||
assert 4 <= article.level <= 6
|
||||
|
||||
def test_article_level_medium_paragraph():
|
||||
''' Boundary case test '''
|
||||
article = ArticleVocabularyLevel('In considering the Origin of Species, it is quite conceivable that a naturalist, reflecting on the mutual affinities of organic beings, on their embryological relations, their geographical distribution, geological succession, and other such facts, might come to the conclusion that each species had not been independently created, but had descended, like varieties, from other species. Nevertheless, such a conclusion, even if well founded, would be unsatisfactory, until it could be shown how the innumerable species inhabiting this world have been modified, so as to acquire that perfection of structure and coadaptation which most justly excites our admiration. Naturalists continually refer to external conditions, such as climate, food, etc., as the only possible cause of variation. In one very limited sense, as we shall hereafter see, this may be true; but it is preposterous to attribute to mere external conditions, the structure, for instance, of the woodpecker, with its feet, tail, beak, and tongue, so admirably adapted to catch insects under the bark of trees. In the case of the misseltoe, which draws its nourishment from certain trees, which has seeds that must be transported by certain birds, and which has flowers with separate sexes absolutely requiring the agency of certain insects to bring pollen from one flower to the other, it is equally preposterous to account for the structure of this parasite, with its relations to several distinct organic beings, by the effects of external conditions, or of habit, or of the volition of the plant itself.')
|
||||
assert 5 <= article.level <= 7
|
||||
|
||||
def test_article_level_long_paragraph():
|
||||
''' Boundary case test '''
|
||||
article = ArticleVocabularyLevel('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, or simultaneously, or to an equal degree. The process of modification must be extremely slow. The variability of each species is quite independent of that of all others. Whether such variability be taken advantage of by natural selection, and whether the variations be accumulated to a greater or lesser amount, thus causing a greater or lesser amount of modification in the varying species, depends on many complex contingencies,—on the variability being of a beneficial nature, on the power of intercrossing, on the rate of breeding, on the slowly changing physical conditions of the country, and more especially on the nature of the other inhabitants with which the varying species comes into competition. Hence it is by no means surprising that one species should retain the same identical form much longer than others; or, if changing, that it should change less. We see the same fact in geographical distribution; for instance, in the land-shells and coleopterous insects of Madeira having come to differ considerably from their nearest allies on the continent of Europe, whereas the marine shells and birds have remained unaltered. We can perhaps understand the apparently quicker rate of change in terrestrial and in more highly organised productions compared with marine and lower productions, by the more complex relations of the higher beings to their organic and inorganic conditions of life, as explained in a former chapter. When many of the inhabitants of a country have become modified and improved, we can understand, on the principle of competition, and on that of the many all-important relations of organism to organism, that any form which does not become in some degree modified and improved, will be liable to be exterminated. Hence we can see why all the species in the same region do at last, if we look to wide enough intervals of time, become modified; for those which do not change will become extinct.')
|
||||
assert 6 <= article.level <= 8
|
||||
|
||||
def test_user_level_empty_dictionary():
|
||||
''' Boundary case test '''
|
||||
user = UserVocabularyLevel({})
|
||||
assert user.level == 0
|
||||
|
||||
def test_user_level_one_simple_word():
|
||||
''' Boundary case test '''
|
||||
user = UserVocabularyLevel({'simple':['202408050930']})
|
||||
assert 0 < user.level <= 4
|
||||
|
||||
def test_user_level_invalid_word():
|
||||
''' Boundary case test '''
|
||||
user = UserVocabularyLevel({'xyz':['202408050930']})
|
||||
assert user.level == 0
|
||||
|
||||
def test_user_level_one_hard_word():
|
||||
''' Boundary case test '''
|
||||
user = UserVocabularyLevel({'pasture':['202408050930']})
|
||||
assert 5 <= user.level <= 8
|
||||
|
||||
def test_user_level_multiple_words():
|
||||
''' Boundary case test '''
|
||||
user = UserVocabularyLevel(
|
||||
{'sessile': ['202408050930'], 'putrid': ['202408050930'], 'prodigal': ['202408050930'], 'presumptuous': ['202408050930'], 'prehension': ['202408050930'], 'pied': ['202408050930'], 'pedunculated': ['202408050930'], 'pasture': ['202408050930'], 'parturition': ['202408050930'], 'ovigerous': ['202408050930'], 'ova': ['202408050930'], 'orifice': ['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'], 'aberration': ['202408050930']}
|
||||
)
|
||||
assert 6 <= user.level <= 8
|
||||
|
||||
def test_user_level_consider_only_most_recent_words_difficult_words_most_recent():
|
||||
''' Consider only the most recent three words '''
|
||||
user = UserVocabularyLevel(
|
||||
{'pasture':['202408050930'], 'putrid': ['202408040000'], 'frivolous':['202408030000'], 'simple':['202408020000'], 'apple':['202408010000']}
|
||||
)
|
||||
assert 5 <= user.level <= 8
|
||||
|
||||
def test_user_level_consider_only_most_recent_words_easy_words_most_recent():
|
||||
''' Consider only the most recent three words '''
|
||||
user = UserVocabularyLevel(
|
||||
{'simple':['202408050930'], 'apple': ['202408040000'], 'happy':['202408030000'], 'pasture':['202408020000'], 'putrid':['202408010000'], 'dearth':['202407310000']}
|
||||
)
|
||||
assert 4 <= user.level <= 5
|
|
@ -0,0 +1,122 @@
|
|||
'''
|
||||
Estimate a user's vocabulary level given his vocabulary data
|
||||
Estimate an English article's difficulty level given its content
|
||||
Preliminary design
|
||||
|
||||
Hui, 2024-09-23
|
||||
Last upated: 2024-09-25, 2024-09-30
|
||||
'''
|
||||
|
||||
import pickle
|
||||
import re
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def load_record(pickle_fname):
|
||||
with open(pickle_fname, 'rb') as f:
|
||||
d = pickle.load(f)
|
||||
return d
|
||||
|
||||
|
||||
class VocabularyLevelEstimator:
|
||||
_test = load_record('words_and_tests.p') # 单词到来源的映射
|
||||
_source_levels = { # 来源到难度分数的映射
|
||||
'BBC': 1,
|
||||
'CET4': 2,
|
||||
'CET6': 3,
|
||||
'GRADUATE': 4,
|
||||
'OXFORD3000': 1,
|
||||
'TOEFL': 5,
|
||||
'IELTS': 5,
|
||||
'GRE': 7
|
||||
}
|
||||
|
||||
def get_word_level(self, word):
|
||||
"""获取单词难度分数"""
|
||||
if word in self._test:
|
||||
sources = self._test[word]
|
||||
word_levels = [
|
||||
self._source_levels[src]
|
||||
for src in sources
|
||||
if src in self._source_levels
|
||||
]
|
||||
if word_levels:
|
||||
# 使用最高分
|
||||
return max(word_levels)
|
||||
return 0 # 未知单词难度为0
|
||||
|
||||
|
||||
class UserVocabularyLevel(VocabularyLevelEstimator):
|
||||
def __init__(self, d, recent_count=3):
|
||||
self.d = d
|
||||
# 按时间戳排序(最新的在前)
|
||||
sorted_words = sorted(d.items(), key=lambda x: max(x[1]), reverse=True)
|
||||
# 取最近的单词(默认3个)
|
||||
self.word_lst = [word for word, _ in sorted_words[:recent_count]]
|
||||
|
||||
@property
|
||||
def level(self):
|
||||
if not self.word_lst:
|
||||
return 0.0
|
||||
|
||||
# 使用最高分
|
||||
max_score = 0
|
||||
for word in self.word_lst:
|
||||
score = self.get_word_level(word)
|
||||
if score > max_score:
|
||||
max_score = score
|
||||
return max_score
|
||||
|
||||
|
||||
class ArticleVocabularyLevel(VocabularyLevelEstimator):
|
||||
def __init__(self, content):
|
||||
self.content = content
|
||||
# 更智能的分词,处理连字符和缩写
|
||||
words = re.findall(r'\b[\w-]+\b', content.lower())
|
||||
|
||||
# 计算每个单词的频率和分数
|
||||
word_freq = defaultdict(int)
|
||||
word_scores = {}
|
||||
|
||||
for word in words:
|
||||
if word.isalpha():
|
||||
word_freq[word] += 1
|
||||
if word not in word_scores:
|
||||
word_scores[word] = self.get_word_level(word)
|
||||
|
||||
# 计算加权分数(频率 * 分数)
|
||||
weighted_scores = []
|
||||
for word, score in word_scores.items():
|
||||
if score > 0:
|
||||
weighted_scores.append((score * word_freq[word], score, word))
|
||||
|
||||
# 如果没有有效单词,直接返回
|
||||
if not weighted_scores:
|
||||
self.difficult_words = []
|
||||
return
|
||||
|
||||
# 按加权分数排序
|
||||
weighted_scores.sort(reverse=True)
|
||||
|
||||
# 只保留前20%的单词(至少5个,最多15个)
|
||||
num_top_words = max(5, min(15, len(weighted_scores) // 5))
|
||||
self.difficult_words = [score for _, score, _ in weighted_scores[:num_top_words]]
|
||||
|
||||
@property
|
||||
def level(self):
|
||||
if not self.difficult_words:
|
||||
return 0.0
|
||||
|
||||
# 使用最高分
|
||||
return max(self.difficult_words)
|
||||
|
||||
if __name__ == '__main__':
|
||||
d = load_record('frequency_mrlan85.pickle')
|
||||
print(d)
|
||||
user = UserVocabularyLevel(d)
|
||||
print(user.level) # level is a property
|
||||
article = ArticleVocabularyLevel('This is an interesting article')
|
||||
print(article.level)
|
||||
|
||||
|
||||
|
Binary file not shown.
|
@ -0,0 +1,61 @@
|
|||
"C:\Users\Kq Aseri\Desktop\package\软件项目管理\EnglishPal\EnglishPal\.venv\Scripts\python.exe" "G:/PyCharm 2024.3.1.1/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path "C:\Users\Kq Aseri\Desktop\package\软件项目管理\EnglishPal\EnglishPal\app\test_vocabulary.py"
|
||||
Testing started at 上午12:51 ...
|
||||
Launching pytest with arguments C:\Users\Kq Aseri\Desktop\package\软件项目管理\EnglishPal\EnglishPal\app\test_vocabulary.py --no-header --no-summary -q in C:\Users\Kq Aseri\Desktop\package\软件项目管理\EnglishPal\EnglishPal\app
|
||||
|
||||
============================= test session starts =============================
|
||||
collecting ... collected 16 items
|
||||
|
||||
test_vocabulary.py::test_article_level_empty_content PASSED [ 6%]
|
||||
test_vocabulary.py::test_article_level_punctuation_only PASSED [ 12%]
|
||||
test_vocabulary.py::test_article_level_digit_only PASSED [ 18%]
|
||||
test_vocabulary.py::test_article_level_single_word PASSED [ 25%]
|
||||
test_vocabulary.py::test_article_level_subset_vs_superset PASSED [ 31%]
|
||||
test_vocabulary.py::test_article_level_multiple_words PASSED [ 37%]
|
||||
test_vocabulary.py::test_article_level_short_paragraph PASSED [ 43%]
|
||||
test_vocabulary.py::test_article_level_medium_paragraph PASSED [ 50%]
|
||||
test_vocabulary.py::test_article_level_long_paragraph FAILED [ 56%]
|
||||
test_vocabulary.py:49 (test_article_level_long_paragraph)
|
||||
6 != 5
|
||||
|
||||
Expected :5
|
||||
Actual :6
|
||||
<Click to see difference>
|
||||
|
||||
def test_article_level_long_paragraph():
|
||||
''' Boundary case test '''
|
||||
article = ArticleVocabularyLevel('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, or simultaneously, or to an equal degree. The process of modification must be extremely slow. The variability of each species is quite independent of that of all others. Whether such variability be taken advantage of by natural selection, and whether the variations be accumulated to a greater or lesser amount, thus causing a greater or lesser amount of modification in the varying species, depends on many complex contingencies,—on the variability being of a beneficial nature, on the power of intercrossing, on the rate of breeding, on the slowly changing physical conditions of the country, and more especially on the nature of the other inhabitants with which the varying species comes into competition. Hence it is by no means surprising that one species should retain the same identical form much longer than others; or, if changing, that it should change less. We see the same fact in geographical distribution; for instance, in the land-shells and coleopterous insects of Madeira having come to differ considerably from their nearest allies on the continent of Europe, whereas the marine shells and birds have remained unaltered. We can perhaps understand the apparently quicker rate of change in terrestrial and in more highly organised productions compared with marine and lower productions, by the more complex relations of the higher beings to their organic and inorganic conditions of life, as explained in a former chapter. When many of the inhabitants of a country have become modified and improved, we can understand, on the principle of competition, and on that of the many all-important relations of organism to organism, that any form which does not become in some degree modified and improved, will be liable to be exterminated. Hence we can see why all the species in the same region do at last, if we look to wide enough intervals of time, become modified; for those which do not change will become extinct.')
|
||||
> assert 6 <= article.level <= 8
|
||||
E assert 6 <= 5
|
||||
E + where 5 = <vocabulary.ArticleVocabularyLevel object at 0x000001FF1BB0E5D0>.level
|
||||
|
||||
test_vocabulary.py:53: AssertionError
|
||||
|
||||
test_vocabulary.py::test_user_level_empty_dictionary PASSED [ 62%]
|
||||
test_vocabulary.py::test_user_level_one_simple_word PASSED [ 68%]
|
||||
test_vocabulary.py::test_user_level_invalid_word PASSED [ 75%]
|
||||
test_vocabulary.py::test_user_level_one_hard_word PASSED [ 81%]
|
||||
test_vocabulary.py::test_user_level_multiple_words FAILED [ 87%]
|
||||
test_vocabulary.py:74 (test_user_level_multiple_words)
|
||||
6 != 1
|
||||
|
||||
Expected :1
|
||||
Actual :6
|
||||
<Click to see difference>
|
||||
|
||||
def test_user_level_multiple_words():
|
||||
''' Boundary case test '''
|
||||
user = UserVocabularyLevel(
|
||||
{'sessile': ['202408050930'], 'putrid': ['202408050930'], 'prodigal': ['202408050930'], 'presumptuous': ['202408050930'], 'prehension': ['202408050930'], 'pied': ['202408050930'], 'pedunculated': ['202408050930'], 'pasture': ['202408050930'], 'parturition': ['202408050930'], 'ovigerous': ['202408050930'], 'ova': ['202408050930'], 'orifice': ['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'], 'aberration': ['202408050930']}
|
||||
)
|
||||
> assert 6 <= user.level <= 8
|
||||
E assert 6 <= 1
|
||||
E + where 1 = <vocabulary.UserVocabularyLevel object at 0x000001FF1BB0DCD0>.level
|
||||
|
||||
test_vocabulary.py:80: AssertionError
|
||||
|
||||
test_vocabulary.py::test_user_level_consider_only_most_recent_words_difficult_words_most_recent PASSED [ 93%]
|
||||
test_vocabulary.py::test_user_level_consider_only_most_recent_words_easy_words_most_recent PASSED [100%]
|
||||
|
||||
======================== 2 failed, 14 passed in 0.10s =========================
|
||||
|
||||
Process finished with exit code 1
|
Loading…
Reference in New Issue