From 9bf690d62c7f37bd18b4546db701814116296f52 Mon Sep 17 00:00:00 2001 From: zj3D Date: Sun, 17 Mar 2024 10:14:42 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A3=8E=E6=A0=BC=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/031 对象化/A22.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/基本结构/031 对象化/A22.py b/基本结构/031 对象化/A22.py index 629cfaf..8fb9b14 100644 --- a/基本结构/031 对象化/A22.py +++ b/基本结构/031 对象化/A22.py @@ -3,9 +3,6 @@ from cppy.cp_util import * def extract_words(obj, path_to_file): obj['data'] = extract_file_words(path_to_file) -def load_stop_words(obj): - obj['stop_words'] = get_stopwords() - def increment_count(obj, w): obj['freqs'][w] = 1 if w not in obj['freqs'] else obj['freqs'][w]+1 @@ -15,12 +12,6 @@ data_storage_obj = { 'words' : lambda : data_storage_obj['data'] } -stop_words_obj = { - 'stop_words' : [], - 'init' : lambda : load_stop_words(stop_words_obj), - 'is_stop_word' : lambda word : word in stop_words_obj['stop_words'] -} - word_freqs_obj = { 'freqs' : {}, 'increment_count' : lambda w : increment_count(word_freqs_obj, w), @@ -29,13 +20,10 @@ word_freqs_obj = { if __name__ == '__main__': - data_storage_obj['init']( testfilepath ) - stop_words_obj['init']() + data_storage_obj['init']( testfilepath ) - for w in data_storage_obj['words'](): - if not stop_words_obj['is_stop_word'](w): - word_freqs_obj['increment_count'](w) + for word in data_storage_obj['words'](): + word_freqs_obj['increment_count'](word) word_freqs = word_freqs_obj['sorted']() - for (w, c) in word_freqs[0:10]: - print(w, '-', c) \ No newline at end of file + print_word_freqs(word_freqs) \ No newline at end of file