From d183fa0f748f0a292855f06a2d45f1a003138be7 Mon Sep 17 00:00:00 2001 From: zj3D Date: Fri, 8 Mar 2024 15:17:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 计算设备/缓存/84.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 计算设备/缓存/84.py diff --git a/计算设备/缓存/84.py b/计算设备/缓存/84.py new file mode 100644 index 0000000..1eb56ca --- /dev/null +++ b/计算设备/缓存/84.py @@ -0,0 +1,25 @@ +import re +from collections import Counter +from cppy.cp_util import * + +# 缓存系统,使用字典来存储文件路径和对应的词频结果; 可以存到本地磁盘,就可重复利用计算结果 +word_freq_cache = {} + +def calculate_word_frequency(file_path): + # 如果文件路径已经在缓存中,直接返回缓存的结果 + if file_path in word_freq_cache: + return word_freq_cache[file_path] + + # 计算词频 + words = extract_file_words(file_path) + word_counts = Counter(words) + + # 将结果存储到缓存中 + word_freq_cache[file_path] = word_counts.most_common(10) + + return word_freq_cache[file_path] + +# 测试函数 +top_10_words = calculate_word_frequency(testfilepath) +for word, freq in top_10_words: + print(f"{word}: {freq}") \ No newline at end of file