diff --git a/一盘大棋/A01.py b/10 一盘大棋/A01.py similarity index 100% rename from 一盘大棋/A01.py rename to 10 一盘大棋/A01.py diff --git a/一盘大棋/A02.py b/10 一盘大棋/A02.py similarity index 100% rename from 一盘大棋/A02.py rename to 10 一盘大棋/A02.py diff --git a/一盘大棋/A03.py b/10 一盘大棋/A03.py similarity index 100% rename from 一盘大棋/A03.py rename to 10 一盘大棋/A03.py diff --git a/基本结构/021 函数/A02.py b/11 概念认知/函数/A02.py similarity index 100% rename from 基本结构/021 函数/A02.py rename to 11 概念认知/函数/A02.py diff --git a/基本结构/021 函数/A03.py b/11 概念认知/函数/A03.py similarity index 100% rename from 基本结构/021 函数/A03.py rename to 11 概念认知/函数/A03.py diff --git a/基本结构/021 函数/A04.py b/11 概念认知/函数/A04.py similarity index 100% rename from 基本结构/021 函数/A04.py rename to 11 概念认知/函数/A04.py diff --git a/基本结构/021 函数/A05.py b/11 概念认知/函数/A05.py similarity index 100% rename from 基本结构/021 函数/A05.py rename to 11 概念认知/函数/A05.py diff --git a/基本结构/031 对象化/A21.py b/11 概念认知/对象化/A21.py similarity index 100% rename from 基本结构/031 对象化/A21.py rename to 11 概念认知/对象化/A21.py diff --git a/基本结构/031 对象化/A22.py b/11 概念认知/对象化/A22.py similarity index 100% rename from 基本结构/031 对象化/A22.py rename to 11 概念认知/对象化/A22.py diff --git a/基本结构/松耦合/Agent/80.py b/11 概念认知/松耦合/Agent/80.py similarity index 100% rename from 基本结构/松耦合/Agent/80.py rename to 11 概念认知/松耦合/Agent/80.py diff --git a/基本结构/松耦合/restful/tf-35-app.py b/11 概念认知/松耦合/restful/tf-35-app.py similarity index 100% rename from 基本结构/松耦合/restful/tf-35-app.py rename to 11 概念认知/松耦合/restful/tf-35-app.py diff --git a/基本结构/松耦合/restful/tf-35-request.py b/11 概念认知/松耦合/restful/tf-35-request.py similarity index 100% rename from 基本结构/松耦合/restful/tf-35-request.py rename to 11 概念认知/松耦合/restful/tf-35-request.py diff --git a/基本结构/松耦合/插件/__pycache__/tf-20.cpython-38.pyc b/11 概念认知/松耦合/插件/__pycache__/tf-20.cpython-38.pyc similarity index 100% rename from 基本结构/松耦合/插件/__pycache__/tf-20.cpython-38.pyc rename to 11 概念认知/松耦合/插件/__pycache__/tf-20.cpython-38.pyc diff --git a/基本结构/松耦合/插件/config.ini b/11 概念认知/松耦合/插件/config.ini similarity index 100% rename from 基本结构/松耦合/插件/config.ini rename to 11 概念认知/松耦合/插件/config.ini diff --git a/基本结构/松耦合/插件/plugins-src/compile.sh b/11 概念认知/松耦合/插件/plugins-src/compile.sh similarity index 100% rename from 基本结构/松耦合/插件/plugins-src/compile.sh rename to 11 概念认知/松耦合/插件/plugins-src/compile.sh diff --git a/基本结构/松耦合/插件/plugins-src/frequencies1.py b/11 概念认知/松耦合/插件/plugins-src/frequencies1.py similarity index 100% rename from 基本结构/松耦合/插件/plugins-src/frequencies1.py rename to 11 概念认知/松耦合/插件/plugins-src/frequencies1.py diff --git a/基本结构/松耦合/插件/plugins-src/frequencies2.py b/11 概念认知/松耦合/插件/plugins-src/frequencies2.py similarity index 100% rename from 基本结构/松耦合/插件/plugins-src/frequencies2.py rename to 11 概念认知/松耦合/插件/plugins-src/frequencies2.py diff --git a/基本结构/松耦合/插件/plugins-src/words1.py b/11 概念认知/松耦合/插件/plugins-src/words1.py similarity index 100% rename from 基本结构/松耦合/插件/plugins-src/words1.py rename to 11 概念认知/松耦合/插件/plugins-src/words1.py diff --git a/基本结构/松耦合/插件/plugins-src/words2.py b/11 概念认知/松耦合/插件/plugins-src/words2.py similarity index 100% rename from 基本结构/松耦合/插件/plugins-src/words2.py rename to 11 概念认知/松耦合/插件/plugins-src/words2.py diff --git a/基本结构/松耦合/插件/plugins/frequencies1.pyc b/11 概念认知/松耦合/插件/plugins/frequencies1.pyc similarity index 100% rename from 基本结构/松耦合/插件/plugins/frequencies1.pyc rename to 11 概念认知/松耦合/插件/plugins/frequencies1.pyc diff --git a/基本结构/松耦合/插件/plugins/frequencies2.cpython-38.pyc b/11 概念认知/松耦合/插件/plugins/frequencies2.cpython-38.pyc similarity index 100% rename from 基本结构/松耦合/插件/plugins/frequencies2.cpython-38.pyc rename to 11 概念认知/松耦合/插件/plugins/frequencies2.cpython-38.pyc diff --git a/基本结构/松耦合/插件/plugins/words1.pyc b/11 概念认知/松耦合/插件/plugins/words1.pyc similarity index 100% rename from 基本结构/松耦合/插件/plugins/words1.pyc rename to 11 概念认知/松耦合/插件/plugins/words1.pyc diff --git a/基本结构/松耦合/插件/plugins/words2.cpython-38.pyc b/11 概念认知/松耦合/插件/plugins/words2.cpython-38.pyc similarity index 100% rename from 基本结构/松耦合/插件/plugins/words2.cpython-38.pyc rename to 11 概念认知/松耦合/插件/plugins/words2.cpython-38.pyc diff --git a/基本结构/松耦合/插件/tf-20.py b/11 概念认知/松耦合/插件/tf-20.py similarity index 100% rename from 基本结构/松耦合/插件/tf-20.py rename to 11 概念认知/松耦合/插件/tf-20.py diff --git a/语言特性/内省/tf_93.py b/12 语言特性/内省/tf_93.py similarity index 100% rename from 语言特性/内省/tf_93.py rename to 12 语言特性/内省/tf_93.py diff --git a/语言特性/反射/tf_94.py b/12 语言特性/反射/tf_94.py similarity index 100% rename from 语言特性/反射/tf_94.py rename to 12 语言特性/反射/tf_94.py diff --git a/语言特性/反射/tf_95.py b/12 语言特性/反射/tf_95.py similarity index 100% rename from 语言特性/反射/tf_95.py rename to 12 语言特性/反射/tf_95.py diff --git a/语言特性/尾调用_类方法/tf-10.py b/12 语言特性/尾调用/tf-10.py similarity index 100% rename from 语言特性/尾调用_类方法/tf-10.py rename to 12 语言特性/尾调用/tf-10.py diff --git a/语言特性/尾调用_类方法/tf-25.py b/12 语言特性/尾调用/tf-25.py similarity index 100% rename from 语言特性/尾调用_类方法/tf-25.py rename to 12 语言特性/尾调用/tf-25.py diff --git a/语言特性/尾调用_类方法/tf-26.py b/12 语言特性/尾调用/tf-26.py similarity index 100% rename from 语言特性/尾调用_类方法/tf-26.py rename to 12 语言特性/尾调用/tf-26.py diff --git a/语言特性/尾调用_类方法/tf-26B.py b/12 语言特性/尾调用/tf-26B.py similarity index 100% rename from 语言特性/尾调用_类方法/tf-26B.py rename to 12 语言特性/尾调用/tf-26B.py diff --git a/语言特性/异步/82.py b/12 语言特性/异步/82.py similarity index 100% rename from 语言特性/异步/82.py rename to 12 语言特性/异步/82.py diff --git a/语言特性/异步/数据流/tf-28.py b/12 语言特性/生成器/tf-28.py similarity index 100% rename from 语言特性/异步/数据流/tf-28.py rename to 12 语言特性/生成器/tf-28.py diff --git a/语言特性/装饰/tf-19A.py b/12 语言特性/装饰/tf-19A.py similarity index 100% rename from 语言特性/装饰/tf-19A.py rename to 12 语言特性/装饰/tf-19A.py diff --git a/语言特性/装饰/tf-19B.py b/12 语言特性/装饰/tf-19B.py similarity index 100% rename from 语言特性/装饰/tf-19B.py rename to 12 语言特性/装饰/tf-19B.py diff --git a/交互/桌面/test.txt b/12 语言特性/递归/test.txt similarity index 100% rename from 交互/桌面/test.txt rename to 12 语言特性/递归/test.txt diff --git a/语言特性/递归/tf-08.py b/12 语言特性/递归/tf-08.py similarity index 100% rename from 语言特性/递归/tf-08.py rename to 12 语言特性/递归/tf-08.py diff --git a/计算设备/map-reduce/tf-31.py b/13 计算设备/map-reduce/tf-31.py similarity index 100% rename from 计算设备/map-reduce/tf-31.py rename to 13 计算设备/map-reduce/tf-31.py diff --git a/计算设备/map-reduce/tf-91.py b/13 计算设备/map-reduce/tf-91.py similarity index 95% rename from 计算设备/map-reduce/tf-91.py rename to 13 计算设备/map-reduce/tf-91.py index 4c50a18..cf15f81 100644 --- a/计算设备/map-reduce/tf-91.py +++ b/13 计算设备/map-reduce/tf-91.py @@ -1,56 +1,56 @@ -# -*- coding: utf-8 -*- -from collections import Counter -from cppy.cp_util import * -from multiprocessing.pool import ThreadPool - - -# -# 多线程 -# -def process_chunk(chunk): - # 过滤停用词 - stop_words = get_stopwords() - words = [ w for w in chunk if ( not w in stop_words ) and len(w) >= 3 ] - return Counter(words) - - -def merge_counts(counts_list): - # 合并多个Counter对象 - total_counts = Counter() - for counts in counts_list: - total_counts += counts - return total_counts - - -def thread_function(chunk, counts_list): - word_count = process_chunk(chunk) - counts_list.append(word_count) - - -@timing_decorator -def main(): - # 读取文件内容 - content = re_split(read_file(testfilepath)) - chunk_size = 1000 # 可以根据实际情况调整块大小 - chunks = [content[i:i + chunk_size] for i in range(0, len(content), chunk_size)] - - # 使用多线程池,每个线程处理一个块 - pool = ThreadPool(len(content)//chunk_size+1) - counts_list = pool.map(process_chunk, chunks) - pool.close() - pool.join() - - # 合并计数 - total_counts = merge_counts(counts_list) - - # 输出最高频的n个词 - print_word_freqs(total_counts.most_common(10)) - - -if __name__ == '__main__': - main() - - - - - +# -*- coding: utf-8 -*- +from collections import Counter +from cppy.cp_util import * +from multiprocessing.pool import ThreadPool + + +# +# 多线程 +# +def process_chunk(chunk): + # 过滤停用词 + stop_words = get_stopwords() + words = [ w for w in chunk if ( not w in stop_words ) and len(w) >= 3 ] + return Counter(words) + + +def merge_counts(counts_list): + # 合并多个Counter对象 + total_counts = Counter() + for counts in counts_list: + total_counts += counts + return total_counts + + +def thread_function(chunk, counts_list): + word_count = process_chunk(chunk) + counts_list.append(word_count) + + +@timing_decorator +def main(): + # 读取文件内容 + content = re_split(read_file(testfilepath)) + chunk_size = 1000 # 可以根据实际情况调整块大小 + chunks = [content[i:i + chunk_size] for i in range(0, len(content), chunk_size)] + + # 使用多线程池,每个线程处理一个块 + pool = ThreadPool(len(content)//chunk_size+1) + counts_list = pool.map(process_chunk, chunks) + pool.close() + pool.join() + + # 合并计数 + total_counts = merge_counts(counts_list) + + # 输出最高频的n个词 + print_word_freqs(total_counts.most_common(10)) + + +if __name__ == '__main__': + main() + + + + + diff --git a/计算设备/map-reduce/tf-92.py b/13 计算设备/map-reduce/tf-92.py similarity index 96% rename from 计算设备/map-reduce/tf-92.py rename to 13 计算设备/map-reduce/tf-92.py index cbd949d..c9b7753 100644 --- a/计算设备/map-reduce/tf-92.py +++ b/13 计算设备/map-reduce/tf-92.py @@ -1,49 +1,49 @@ -# -*- coding: utf-8 -*- -import multiprocessing -from collections import Counter -from cppy.cp_util import * - - -# -# 多进程 -# -def process_chunk(chunk): - # 过滤停用词 - stop_words = get_stopwords() - words = [ w for w in chunk if ( not w in stop_words ) and len(w) >= 3 ] - return Counter(words) - - -def merge_counts(counts_list): - # 合并多个Counter对象 - total_counts = Counter() - for counts in counts_list: - total_counts += counts - return total_counts - - -@timing_decorator -def main(): - # 读取文件内容 - content = re_split(read_file(testfilepath)) - - # 分割文件内容为多个块,每个块由一个进程处理 - chunk_size = 1000 # 可以根据实际情况调整块大小 - chunks = [content[i:i + chunk_size] for i in range(0, len(content), chunk_size)] - - # 使用多进程处理每个块 - pool = multiprocessing.Pool(processes=multiprocessing.cpu_count()) - counts_list = pool.map(process_chunk, chunks) - pool.close() - pool.join() - - # 合并计数 - total_counts = merge_counts(counts_list) - - # 输出最高频的n个词 - print_word_freqs(total_counts.most_common(10)) - - -if __name__ == '__main__': - main() - +# -*- coding: utf-8 -*- +import multiprocessing +from collections import Counter +from cppy.cp_util import * + + +# +# 多进程 +# +def process_chunk(chunk): + # 过滤停用词 + stop_words = get_stopwords() + words = [ w for w in chunk if ( not w in stop_words ) and len(w) >= 3 ] + return Counter(words) + + +def merge_counts(counts_list): + # 合并多个Counter对象 + total_counts = Counter() + for counts in counts_list: + total_counts += counts + return total_counts + + +@timing_decorator +def main(): + # 读取文件内容 + content = re_split(read_file(testfilepath)) + + # 分割文件内容为多个块,每个块由一个进程处理 + chunk_size = 1000 # 可以根据实际情况调整块大小 + chunks = [content[i:i + chunk_size] for i in range(0, len(content), chunk_size)] + + # 使用多进程处理每个块 + pool = multiprocessing.Pool(processes=multiprocessing.cpu_count()) + counts_list = pool.map(process_chunk, chunks) + pool.close() + pool.join() + + # 合并计数 + total_counts = merge_counts(counts_list) + + # 输出最高频的n个词 + print_word_freqs(total_counts.most_common(10)) + + +if __name__ == '__main__': + main() + diff --git a/计算设备/共享内存数据空间/30A.py b/13 计算设备/共享内存数据空间/30A.py similarity index 100% rename from 计算设备/共享内存数据空间/30A.py rename to 13 计算设备/共享内存数据空间/30A.py diff --git a/计算设备/共享内存数据空间/30B.py b/13 计算设备/共享内存数据空间/30B.py similarity index 100% rename from 计算设备/共享内存数据空间/30B.py rename to 13 计算设备/共享内存数据空间/30B.py diff --git a/计算设备/数据库/ORM/DataQuery.py b/13 计算设备/数据库/ORM/DataQuery.py similarity index 100% rename from 计算设备/数据库/ORM/DataQuery.py rename to 13 计算设备/数据库/ORM/DataQuery.py diff --git a/计算设备/数据库/ORM/createDb.py b/13 计算设备/数据库/ORM/createDb.py similarity index 100% rename from 计算设备/数据库/ORM/createDb.py rename to 13 计算设备/数据库/ORM/createDb.py diff --git a/计算设备/数据库/ORM/processData.py b/13 计算设备/数据库/ORM/processData.py similarity index 100% rename from 计算设备/数据库/ORM/processData.py rename to 13 计算设备/数据库/ORM/processData.py diff --git a/计算设备/数据库/tf-26.py b/13 计算设备/数据库/tf-26.py similarity index 100% rename from 计算设备/数据库/tf-26.py rename to 13 计算设备/数据库/tf-26.py diff --git a/计算设备/极限内存/tf-01.py b/13 计算设备/极限内存/tf-01.py similarity index 100% rename from 计算设备/极限内存/tf-01.py rename to 13 计算设备/极限内存/tf-01.py diff --git a/交互/MVC/flask/app.py b/14 交互/MVC/flask/app.py similarity index 100% rename from 交互/MVC/flask/app.py rename to 14 交互/MVC/flask/app.py diff --git a/交互/MVC/flask/models.py b/14 交互/MVC/flask/models.py similarity index 100% rename from 交互/MVC/flask/models.py rename to 14 交互/MVC/flask/models.py diff --git a/交互/MVC/flask/templates/index.html b/14 交互/MVC/flask/templates/index.html similarity index 100% rename from 交互/MVC/flask/templates/index.html rename to 14 交互/MVC/flask/templates/index.html diff --git a/交互/MVC/tf-33.py b/14 交互/MVC/tf-33.py similarity index 100% rename from 交互/MVC/tf-33.py rename to 14 交互/MVC/tf-33.py diff --git a/交互/终端/终端菜单/test.txt b/14 交互/桌面/test.txt similarity index 100% rename from 交互/终端/终端菜单/test.txt rename to 14 交互/桌面/test.txt diff --git a/交互/桌面/tf-98.py b/14 交互/桌面/tf-98.py similarity index 100% rename from 交互/桌面/tf-98.py rename to 14 交互/桌面/tf-98.py diff --git a/交互/终端/终端命令行/command_line_1.py b/14 交互/终端/终端命令行/command_line_1.py similarity index 100% rename from 交互/终端/终端命令行/command_line_1.py rename to 14 交互/终端/终端命令行/command_line_1.py diff --git a/交互/终端/终端命令行/command_line_2.py b/14 交互/终端/终端命令行/command_line_2.py similarity index 100% rename from 交互/终端/终端命令行/command_line_2.py rename to 14 交互/终端/终端命令行/command_line_2.py diff --git a/语言特性/递归/test.txt b/14 交互/终端/终端菜单/test.txt similarity index 100% rename from 语言特性/递归/test.txt rename to 14 交互/终端/终端菜单/test.txt diff --git a/交互/终端/终端菜单/tf-97.py b/14 交互/终端/终端菜单/tf-97.py similarity index 100% rename from 交互/终端/终端菜单/tf-97.py rename to 14 交互/终端/终端菜单/tf-97.py diff --git a/工程化/对象接口/tf-14A.py b/15 工程化/对象接口/tf-14A.py similarity index 100% rename from 工程化/对象接口/tf-14A.py rename to 15 工程化/对象接口/tf-14A.py diff --git a/工程化/对象接口/tf-14B.py b/15 工程化/对象接口/tf-14B.py similarity index 100% rename from 工程化/对象接口/tf-14B.py rename to 15 工程化/对象接口/tf-14B.py diff --git a/工程化/类型申明/24A.py b/15 工程化/类型申明/24A.py similarity index 100% rename from 工程化/类型申明/24A.py rename to 15 工程化/类型申明/24A.py diff --git a/工程化/类型申明/24B.py b/15 工程化/类型申明/24B.py similarity index 100% rename from 工程化/类型申明/24B.py rename to 15 工程化/类型申明/24B.py diff --git a/基本结构/函数缓存/84.py b/16 其它/函数缓存/84.py similarity index 100% rename from 基本结构/函数缓存/84.py rename to 16 其它/函数缓存/84.py diff --git a/基本结构/共享对象/tf-38.py b/16 其它/对象设计模式/共享对象/tf-38.py similarity index 100% rename from 基本结构/共享对象/tf-38.py rename to 16 其它/对象设计模式/共享对象/tf-38.py diff --git a/语言特性/注册回调/tf-15.py b/16 其它/对象设计模式/注册回调/tf-15.py similarity index 100% rename from 语言特性/注册回调/tf-15.py rename to 16 其它/对象设计模式/注册回调/tf-15.py diff --git a/语言特性/注册回调_订阅制/tf-16.py b/16 其它/对象设计模式/注册回调_订阅制/tf-16.py similarity index 100% rename from 语言特性/注册回调_订阅制/tf-16.py rename to 16 其它/对象设计模式/注册回调_订阅制/tf-16.py diff --git a/语言特性/消息驱动的对象/tf-12.py b/16 其它/对象设计模式/消息驱动的对象/tf-12.py similarity index 100% rename from 语言特性/消息驱动的对象/tf-12.py rename to 16 其它/对象设计模式/消息驱动的对象/tf-12.py diff --git a/语言特性/消息驱动的对象_线程独立/tf-29.py b/16 其它/对象设计模式/消息驱动的对象_线程独立/tf-29.py similarity index 100% rename from 语言特性/消息驱动的对象_线程独立/tf-29.py rename to 16 其它/对象设计模式/消息驱动的对象_线程独立/tf-29.py diff --git a/基本结构/观察者模式/Observer.py b/16 其它/对象设计模式/观察者模式/Observer.py similarity index 96% rename from 基本结构/观察者模式/Observer.py rename to 16 其它/对象设计模式/观察者模式/Observer.py index 7d73002..16d6e9d 100644 --- a/基本结构/观察者模式/Observer.py +++ b/16 其它/对象设计模式/观察者模式/Observer.py @@ -1,77 +1,77 @@ -import os,re,string,operator -from collections import Counter - -# TextProcessor 类负责处理文本并计算词频。当文本处理完成后,它会通过 notify 方法通知所有注册的观察者。 -# WordFrequencyObserver 类是一个具体的观察者,它实现了 update 方法来接收词频更新并打印前10个最常见的单词。 -class Subject: - def __init__(self): - self._observers = [] - # 不能随意改变,所以肯定是私有 - def attach(self, observer): - self._observers.append(observer) - - def detach(self, observer): - self._observers.remove(observer) - - def notify(self, word_freqs): - for observer in self._observers: - observer.update(word_freqs) -# 关注,取消关注,通知有更新,Subject类是用来创建一个类,对订阅者(即观察者)列表进行维护 - -class Observer: - def update(self, word_freqs): - pass -# 定义一个抽象的Observer -# 而下面的是一个具体的Observer类 -class WordFrequencyObserver(Observer): - def update(self, word_freqs): - print("词频已经被更新:") - self.print_word_freqs(word_freqs) - - def print_word_freqs(self, word_freqs): - sorted_freqs = sorted(word_freqs.items(), key=operator.itemgetter(1), reverse=True) - for (w, c) in sorted_freqs[:10]: - print(f"{w}: {c}") - -# 对文本进行分析 -class TextProcessor: - def __init__(self, subject: Subject): -#subject是Subject的子类,类型注解,单独写也可以 - self._subject = subject - self._stop_words:str = set() -#是一个集合(其实这里需要表明是str) - def load_stop_words(self, path_to_file): - with open(path_to_file, encoding='utf-8') as f: - self._stop_words = set(line.strip().lower() for line in f) - - def process_text(self, path_to_file): - with open(path_to_file, encoding='utf-8') as f: - data = f.read() - word_list = self.re_split(data) - filtered_words = self.filter_words(word_list) - word_freqs = self.count_frequencies(filtered_words) - self._subject.notify(word_freqs) - - def re_split(self, data): - pattern = re.compile('[\W_]+') - return pattern.sub(' ', data).lower().split() - - def filter_words(self, word_list): - return [w for w in word_list if w not in self._stop_words and len(w) >= 3] - - def count_frequencies(self, word_list): - return Counter(word_list) - -# 开始测试 -if __name__ == "__main__": - stopwordfilepath = r'C:\Users\asus\Desktop\cppy余悦批注\cppy\data\stop_words.txt' - testfilepath = r'C:\Users\asus\Desktop\cppy余悦批注\cppy\data\pride-and-prejudice.txt' - - # 调用实例 - subject = Subject() - observer = WordFrequencyObserver() - subject.attach(observer) - - text_processor = TextProcessor(subject) - text_processor.load_stop_words(stopwordfilepath) +import os,re,string,operator +from collections import Counter + +# TextProcessor 类负责处理文本并计算词频。当文本处理完成后,它会通过 notify 方法通知所有注册的观察者。 +# WordFrequencyObserver 类是一个具体的观察者,它实现了 update 方法来接收词频更新并打印前10个最常见的单词。 +class Subject: + def __init__(self): + self._observers = [] + # 不能随意改变,所以肯定是私有 + def attach(self, observer): + self._observers.append(observer) + + def detach(self, observer): + self._observers.remove(observer) + + def notify(self, word_freqs): + for observer in self._observers: + observer.update(word_freqs) +# 关注,取消关注,通知有更新,Subject类是用来创建一个类,对订阅者(即观察者)列表进行维护 + +class Observer: + def update(self, word_freqs): + pass +# 定义一个抽象的Observer +# 而下面的是一个具体的Observer类 +class WordFrequencyObserver(Observer): + def update(self, word_freqs): + print("词频已经被更新:") + self.print_word_freqs(word_freqs) + + def print_word_freqs(self, word_freqs): + sorted_freqs = sorted(word_freqs.items(), key=operator.itemgetter(1), reverse=True) + for (w, c) in sorted_freqs[:10]: + print(f"{w}: {c}") + +# 对文本进行分析 +class TextProcessor: + def __init__(self, subject: Subject): +#subject是Subject的子类,类型注解,单独写也可以 + self._subject = subject + self._stop_words:str = set() +#是一个集合(其实这里需要表明是str) + def load_stop_words(self, path_to_file): + with open(path_to_file, encoding='utf-8') as f: + self._stop_words = set(line.strip().lower() for line in f) + + def process_text(self, path_to_file): + with open(path_to_file, encoding='utf-8') as f: + data = f.read() + word_list = self.re_split(data) + filtered_words = self.filter_words(word_list) + word_freqs = self.count_frequencies(filtered_words) + self._subject.notify(word_freqs) + + def re_split(self, data): + pattern = re.compile('[\W_]+') + return pattern.sub(' ', data).lower().split() + + def filter_words(self, word_list): + return [w for w in word_list if w not in self._stop_words and len(w) >= 3] + + def count_frequencies(self, word_list): + return Counter(word_list) + +# 开始测试 +if __name__ == "__main__": + stopwordfilepath = r'C:\Users\asus\Desktop\cppy余悦批注\cppy\data\stop_words.txt' + testfilepath = r'C:\Users\asus\Desktop\cppy余悦批注\cppy\data\pride-and-prejudice.txt' + + # 调用实例 + subject = Subject() + observer = WordFrequencyObserver() + subject.attach(observer) + + text_processor = TextProcessor(subject) + text_processor.load_stop_words(stopwordfilepath) text_processor.process_text(testfilepath) \ No newline at end of file diff --git a/基本结构/异常/1 软件不能挂掉/tf-21.py b/16 其它/异常/1 软件不能挂掉/tf-21.py similarity index 100% rename from 基本结构/异常/1 软件不能挂掉/tf-21.py rename to 16 其它/异常/1 软件不能挂掉/tf-21.py diff --git a/基本结构/异常/2 时间停止在那一刻/tf-22.py b/16 其它/异常/2 时间停止在那一刻/tf-22.py similarity index 100% rename from 基本结构/异常/2 时间停止在那一刻/tf-22.py rename to 16 其它/异常/2 时间停止在那一刻/tf-22.py diff --git a/基本结构/异常/3 所有错误的应对/tf-23.py b/16 其它/异常/3 所有错误的应对/tf-23.py similarity index 100% rename from 基本结构/异常/3 所有错误的应对/tf-23.py rename to 16 其它/异常/3 所有错误的应对/tf-23.py diff --git a/基本结构/状态机/81A.py b/16 其它/状态机/81A.py similarity index 94% rename from 基本结构/状态机/81A.py rename to 16 其它/状态机/81A.py index 581bf96..53a5f92 100644 --- a/基本结构/状态机/81A.py +++ b/16 其它/状态机/81A.py @@ -1,59 +1,59 @@ -# -*- coding: utf-8 -*- -import cppy.cp_util as util -from collections import Counter - - -class WordFrequencyStateMachine: - def __init__(self, file_path): - self.file_path = file_path - self.content = None - self.words = None - self.word_freq = None - self.state = 'IDLE' - - def transition_to_read_file(self): - try: - with open(self.file_path, 'r', encoding='utf-8') as file: - self.content = file.read() - self.state = 'WORDS_SPLIT' - except FileNotFoundError: - print(f"文件 {self.file_path} 未找到。") - except Exception as e: - print(f"读取文件时发生错误: {e}") - - def transition_to_split_words(self): - if self.content is not None: - self.words = util.extract_str_words(self.content) - self.state = 'CALCULATE_FREQ' - else: - print("文件内容为空,无法分割单词。") - - def transition_to_calculate_freq(self): - if self.words is not None: - self.word_freq = Counter(self.words) - self.state = 'DONE' - else: - print("单词列表为空,无法计算词频。") - - def run(self): - while self.state != 'DONE': - if self.state == 'IDLE': - self.transition_to_read_file() - elif self.state == 'WORDS_SPLIT': - self.transition_to_split_words() - elif self.state == 'CALCULATE_FREQ': - self.transition_to_calculate_freq() - else: - print(f"未知状态: {self.state}") - break - - return self.word_freq - - # 使用状态机计算词频 - - -state_machine = WordFrequencyStateMachine(util.testfilepath) -word_frequencies = state_machine.run() - -# 打印结果 +# -*- coding: utf-8 -*- +import cppy.cp_util as util +from collections import Counter + + +class WordFrequencyStateMachine: + def __init__(self, file_path): + self.file_path = file_path + self.content = None + self.words = None + self.word_freq = None + self.state = 'IDLE' + + def transition_to_read_file(self): + try: + with open(self.file_path, 'r', encoding='utf-8') as file: + self.content = file.read() + self.state = 'WORDS_SPLIT' + except FileNotFoundError: + print(f"文件 {self.file_path} 未找到。") + except Exception as e: + print(f"读取文件时发生错误: {e}") + + def transition_to_split_words(self): + if self.content is not None: + self.words = util.extract_str_words(self.content) + self.state = 'CALCULATE_FREQ' + else: + print("文件内容为空,无法分割单词。") + + def transition_to_calculate_freq(self): + if self.words is not None: + self.word_freq = Counter(self.words) + self.state = 'DONE' + else: + print("单词列表为空,无法计算词频。") + + def run(self): + while self.state != 'DONE': + if self.state == 'IDLE': + self.transition_to_read_file() + elif self.state == 'WORDS_SPLIT': + self.transition_to_split_words() + elif self.state == 'CALCULATE_FREQ': + self.transition_to_calculate_freq() + else: + print(f"未知状态: {self.state}") + break + + return self.word_freq + + # 使用状态机计算词频 + + +state_machine = WordFrequencyStateMachine(util.testfilepath) +word_frequencies = state_machine.run() + +# 打印结果 util.print_word_freqs(word_frequencies.most_common(10)) \ No newline at end of file diff --git a/基本结构/状态机/81B.py b/16 其它/状态机/81B.py similarity index 96% rename from 基本结构/状态机/81B.py rename to 16 其它/状态机/81B.py index 1746beb..413185e 100644 --- a/基本结构/状态机/81B.py +++ b/16 其它/状态机/81B.py @@ -1,33 +1,33 @@ -# -*- coding: utf-8 -*- -import cppy.cp_util as util - -# 每一列是一个数据元素和一个公式,第一列是输入数据,所以没有公式 -all_words = [(), None] -non_stop_words = [(), util.extract_str_words] -frequencies = [(), util.get_frequencies] -sorted_data = [(), util.sort_dict] - -# 整个电子表格 -all_columns = [all_words, non_stop_words,\ - frequencies, sorted_data] - -# 每次输入数据后调用此方法 -def update(): - global all_columns - for c in all_columns[1::]: - if c[1] == util.extract_str_words: - c[0] = c[1](all_words[0]) - elif c[1] == util.get_frequencies: - c[0] = c[1](non_stop_words[0]) - elif c[1] == util.sort_dict: - c[0] = c[1](frequencies[0]) - -# 将固定数据加载到第一列中 -all_words[0] = util.read_file(util.testfilepath) -# 调用update函数遍历列表 -update() - -#打印结果 -util.print_word_freqs(sorted_data[0]) - - +# -*- coding: utf-8 -*- +import cppy.cp_util as util + +# 每一列是一个数据元素和一个公式,第一列是输入数据,所以没有公式 +all_words = [(), None] +non_stop_words = [(), util.extract_str_words] +frequencies = [(), util.get_frequencies] +sorted_data = [(), util.sort_dict] + +# 整个电子表格 +all_columns = [all_words, non_stop_words,\ + frequencies, sorted_data] + +# 每次输入数据后调用此方法 +def update(): + global all_columns + for c in all_columns[1::]: + if c[1] == util.extract_str_words: + c[0] = c[1](all_words[0]) + elif c[1] == util.get_frequencies: + c[0] = c[1](non_stop_words[0]) + elif c[1] == util.sort_dict: + c[0] = c[1](frequencies[0]) + +# 将固定数据加载到第一列中 +all_words[0] = util.read_file(util.testfilepath) +# 调用update函数遍历列表 +update() + +#打印结果 +util.print_word_freqs(sorted_data[0]) + + diff --git a/高性能模式/000 普通做法.py b/20 高性能模式/000 普通做法.py similarity index 100% rename from 高性能模式/000 普通做法.py rename to 20 高性能模式/000 普通做法.py diff --git a/高性能模式/010 多进程.py b/20 高性能模式/010 多进程.py similarity index 100% rename from 高性能模式/010 多进程.py rename to 20 高性能模式/010 多进程.py diff --git a/高性能模式/020 多线程.py b/20 高性能模式/020 多线程.py similarity index 100% rename from 高性能模式/020 多线程.py rename to 20 高性能模式/020 多线程.py diff --git a/高性能模式/030 协程.py b/20 高性能模式/030 协程.py similarity index 100% rename from 高性能模式/030 协程.py rename to 20 高性能模式/030 协程.py diff --git a/高性能模式/040 异步.py b/20 高性能模式/040 异步.py similarity index 100% rename from 高性能模式/040 异步.py rename to 20 高性能模式/040 异步.py diff --git a/高性能模式/readme.md b/20 高性能模式/readme.md similarity index 100% rename from 高性能模式/readme.md rename to 20 高性能模式/readme.md diff --git a/对象设计模式/readme.md b/30 对象设计模式/readme.md similarity index 100% rename from 对象设计模式/readme.md rename to 30 对象设计模式/readme.md diff --git a/对象设计模式/创建型/010 单例.py b/30 对象设计模式/创建型/010 单例.py similarity index 100% rename from 对象设计模式/创建型/010 单例.py rename to 30 对象设计模式/创建型/010 单例.py diff --git a/对象设计模式/创建型/020 工厂.py b/30 对象设计模式/创建型/020 工厂.py similarity index 100% rename from 对象设计模式/创建型/020 工厂.py rename to 30 对象设计模式/创建型/020 工厂.py diff --git a/对象设计模式/创建型/070 建造者.py b/30 对象设计模式/创建型/070 建造者.py similarity index 100% rename from 对象设计模式/创建型/070 建造者.py rename to 30 对象设计模式/创建型/070 建造者.py diff --git a/对象设计模式/创建型/180 享元.py b/30 对象设计模式/创建型/180 享元.py similarity index 100% rename from 对象设计模式/创建型/180 享元.py rename to 30 对象设计模式/创建型/180 享元.py diff --git a/对象设计模式/结构型/050 装饰器.py b/30 对象设计模式/结构型/050 装饰器.py similarity index 100% rename from 对象设计模式/结构型/050 装饰器.py rename to 30 对象设计模式/结构型/050 装饰器.py diff --git a/对象设计模式/结构型/080 适配器.py b/30 对象设计模式/结构型/080 适配器.py similarity index 100% rename from 对象设计模式/结构型/080 适配器.py rename to 30 对象设计模式/结构型/080 适配器.py diff --git a/对象设计模式/结构型/140 代理.py b/30 对象设计模式/结构型/140 代理.py similarity index 100% rename from 对象设计模式/结构型/140 代理.py rename to 30 对象设计模式/结构型/140 代理.py diff --git a/对象设计模式/结构型/150 外观.py b/30 对象设计模式/结构型/150 外观.py similarity index 100% rename from 对象设计模式/结构型/150 外观.py rename to 30 对象设计模式/结构型/150 外观.py diff --git a/对象设计模式/结构型/160 组合.py b/30 对象设计模式/结构型/160 组合.py similarity index 100% rename from 对象设计模式/结构型/160 组合.py rename to 30 对象设计模式/结构型/160 组合.py diff --git a/对象设计模式/结构型/170 桥接.py b/30 对象设计模式/结构型/170 桥接.py similarity index 100% rename from 对象设计模式/结构型/170 桥接.py rename to 30 对象设计模式/结构型/170 桥接.py diff --git a/对象设计模式/行为型/030 策略.py b/30 对象设计模式/行为型/030 策略.py similarity index 100% rename from 对象设计模式/行为型/030 策略.py rename to 30 对象设计模式/行为型/030 策略.py diff --git a/对象设计模式/行为型/040 观察者.py b/30 对象设计模式/行为型/040 观察者.py similarity index 100% rename from 对象设计模式/行为型/040 观察者.py rename to 30 对象设计模式/行为型/040 观察者.py diff --git a/对象设计模式/行为型/060 状态.py b/30 对象设计模式/行为型/060 状态.py similarity index 100% rename from 对象设计模式/行为型/060 状态.py rename to 30 对象设计模式/行为型/060 状态.py diff --git a/对象设计模式/行为型/090 模板方法.py b/30 对象设计模式/行为型/090 模板方法.py similarity index 100% rename from 对象设计模式/行为型/090 模板方法.py rename to 30 对象设计模式/行为型/090 模板方法.py diff --git a/对象设计模式/行为型/110 中介者.py b/30 对象设计模式/行为型/110 中介者.py similarity index 100% rename from 对象设计模式/行为型/110 中介者.py rename to 30 对象设计模式/行为型/110 中介者.py diff --git a/对象设计模式/行为型/120 责任链.py b/30 对象设计模式/行为型/120 责任链.py similarity index 100% rename from 对象设计模式/行为型/120 责任链.py rename to 30 对象设计模式/行为型/120 责任链.py diff --git a/对象设计模式/行为型/130 命令.py b/30 对象设计模式/行为型/130 命令.py similarity index 100% rename from 对象设计模式/行为型/130 命令.py rename to 30 对象设计模式/行为型/130 命令.py diff --git a/对象设计模式/行为型/190 备忘录.py b/30 对象设计模式/行为型/190 备忘录.py similarity index 100% rename from 对象设计模式/行为型/190 备忘录.py rename to 30 对象设计模式/行为型/190 备忘录.py diff --git a/对象设计模式/行为型/200 访问者.py b/30 对象设计模式/行为型/200 访问者.py similarity index 100% rename from 对象设计模式/行为型/200 访问者.py rename to 30 对象设计模式/行为型/200 访问者.py