forked from mrlan/EnglishPal
53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
|
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)
|