From 9fd26708ca0fd81ed3ca6531dc02731107a42af3 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Tue, 12 Mar 2024 14:53:47 +0800 Subject: [PATCH 1/7] =?UTF-8?q?Delete=20'=E5=9F=BA=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84/051=E4=BA=AB=E5=85=83=E6=A8=A1=E5=BC=8F/tf-38.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/051享元模式/tf-38.py | 57 --------------------------- 1 file changed, 57 deletions(-) delete mode 100644 基本结构/051享元模式/tf-38.py diff --git a/基本结构/051享元模式/tf-38.py b/基本结构/051享元模式/tf-38.py deleted file mode 100644 index 32f835d..0000000 --- a/基本结构/051享元模式/tf-38.py +++ /dev/null @@ -1,57 +0,0 @@ -# -*- encoding:utf-8 -*- -from cppy.cp_util import * -''' -享元模式 享元模式是一种结构型设计模式,在享元模式中,对象被设计为可共享的,可以被多个上下文使用,而不必在每个上下文中都创建新的对象。 -如果我们有大量不同的词频分析需求,有时需要词频前10的单词,有时需要词频前20的单词,有时还需要限定词汇的长度,那就需要创建多个词频统计器,每个词频统 -计器都独立创建并存储其内部状态,那么系统的内存占用可能会很大,在这种情况下,享元模式共享相同类型的词频统计器对象,每种类型的词频统计器只需创建一个 -共享实例,然后通过设置不同的参数个性化每个对象,通过共享相同的内部状态,降低了对象的创建和内存占用成本。 -例如,我需要对3个文件获取词频前十的单词,对另外3个文件获取词频前二十的单词,那么我只需要创建2个词频统计器对象,每个对象存储相同的内部状态,一个对象 -获取前十的单词,一个对象获取前二十的单词,而不用创建6个对象 -''' -#以需要的词频数量分类 -# class Type(number): -def get_number(): - number = int(input("请输入需要显示词频前几的单词")) - return number - -#定义享元接口 -class WordFrequencyController(): - def print_word_freqs(self,number): - pass - -#定义具体的享元类 -class ConcreteWordFrequencyController(WordFrequencyController): - def __init__(self, controllertype,filepath): - self.word_list = extract_words(filepath) - self.word_freq = get_frequencies(self.word_list) - self.word_freq = sort_dict(self.word_freq) - def print_word_freqs(self, number): - for (w, c) in self.word_freq[:number]: - print(w, '-', c) - -#定义享元工厂 -class WordFrequencyControllerFactory(): - def __init__(self): - self.types = {} - - def get_WordFrequencyController(self, controller_type,testfilepath): - if controller_type not in self.types: - self.types[controller_type] = ConcreteWordFrequencyController(controller_type,testfilepath) - #创建新的享元对象 - print(self.types)#显示已存在的享元对象 - return self.types[controller_type]#重复使用已存在的享元对象 - -def process_command(factory: WordFrequencyControllerFactory, number: str): - controller_type = number - WordFrequencyController = factory.get_WordFrequencyController(controller_type,testfilepath) - WordFrequencyController.print_word_freqs(int(number)) - - -if __name__ == "__main__": - factory = WordFrequencyControllerFactory() - while True: - try: - number = input("请输入需要显示词频前几的单词") - process_command(factory, number) - except EOFError: - break \ No newline at end of file From f63379b5f388d3988f46646857c11d2d12ee5b72 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Tue, 12 Mar 2024 15:05:04 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BA=AB=E5=85=83=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/享元模式/tf-38.py | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 基本结构/享元模式/tf-38.py diff --git a/基本结构/享元模式/tf-38.py b/基本结构/享元模式/tf-38.py new file mode 100644 index 0000000..32f835d --- /dev/null +++ b/基本结构/享元模式/tf-38.py @@ -0,0 +1,57 @@ +# -*- encoding:utf-8 -*- +from cppy.cp_util import * +''' +享元模式 享元模式是一种结构型设计模式,在享元模式中,对象被设计为可共享的,可以被多个上下文使用,而不必在每个上下文中都创建新的对象。 +如果我们有大量不同的词频分析需求,有时需要词频前10的单词,有时需要词频前20的单词,有时还需要限定词汇的长度,那就需要创建多个词频统计器,每个词频统 +计器都独立创建并存储其内部状态,那么系统的内存占用可能会很大,在这种情况下,享元模式共享相同类型的词频统计器对象,每种类型的词频统计器只需创建一个 +共享实例,然后通过设置不同的参数个性化每个对象,通过共享相同的内部状态,降低了对象的创建和内存占用成本。 +例如,我需要对3个文件获取词频前十的单词,对另外3个文件获取词频前二十的单词,那么我只需要创建2个词频统计器对象,每个对象存储相同的内部状态,一个对象 +获取前十的单词,一个对象获取前二十的单词,而不用创建6个对象 +''' +#以需要的词频数量分类 +# class Type(number): +def get_number(): + number = int(input("请输入需要显示词频前几的单词")) + return number + +#定义享元接口 +class WordFrequencyController(): + def print_word_freqs(self,number): + pass + +#定义具体的享元类 +class ConcreteWordFrequencyController(WordFrequencyController): + def __init__(self, controllertype,filepath): + self.word_list = extract_words(filepath) + self.word_freq = get_frequencies(self.word_list) + self.word_freq = sort_dict(self.word_freq) + def print_word_freqs(self, number): + for (w, c) in self.word_freq[:number]: + print(w, '-', c) + +#定义享元工厂 +class WordFrequencyControllerFactory(): + def __init__(self): + self.types = {} + + def get_WordFrequencyController(self, controller_type,testfilepath): + if controller_type not in self.types: + self.types[controller_type] = ConcreteWordFrequencyController(controller_type,testfilepath) + #创建新的享元对象 + print(self.types)#显示已存在的享元对象 + return self.types[controller_type]#重复使用已存在的享元对象 + +def process_command(factory: WordFrequencyControllerFactory, number: str): + controller_type = number + WordFrequencyController = factory.get_WordFrequencyController(controller_type,testfilepath) + WordFrequencyController.print_word_freqs(int(number)) + + +if __name__ == "__main__": + factory = WordFrequencyControllerFactory() + while True: + try: + number = input("请输入需要显示词频前几的单词") + process_command(factory, number) + except EOFError: + break \ No newline at end of file From 0913eee8af720a638476b2a24ed686c1c3c5bf91 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Wed, 13 Mar 2024 14:18:23 +0800 Subject: [PATCH 3/7] ADD file via upload --- 基本结构/状态机/81B.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 基本结构/状态机/81B.py diff --git a/基本结构/状态机/81B.py b/基本结构/状态机/81B.py new file mode 100644 index 0000000..1746beb --- /dev/null +++ b/基本结构/状态机/81B.py @@ -0,0 +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]) + + From 97ed3a4adfb83ee202827d0d0843c9f1721c2911 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Wed, 13 Mar 2024 14:18:36 +0800 Subject: [PATCH 4/7] =?UTF-8?q?Delete=20'=E5=9F=BA=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84/=E7=8A=B6=E6=80=81=E6=9C=BA/81B.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/状态机/81B.py | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 基本结构/状态机/81B.py diff --git a/基本结构/状态机/81B.py b/基本结构/状态机/81B.py deleted file mode 100644 index 1746beb..0000000 --- a/基本结构/状态机/81B.py +++ /dev/null @@ -1,33 +0,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]) - - From 8946f39e2efd7ba25b4364327908c660b1a1204d Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Wed, 13 Mar 2024 14:19:31 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=9C=BA=20=E7=94=B5?= =?UTF-8?q?=E5=AD=90=E8=A1=A8=E6=A0=BC=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/状态机/81B.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 基本结构/状态机/81B.py diff --git a/基本结构/状态机/81B.py b/基本结构/状态机/81B.py new file mode 100644 index 0000000..1746beb --- /dev/null +++ b/基本结构/状态机/81B.py @@ -0,0 +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]) + + From ff99197647a91a01e3a7c7aaf9d678832609ab22 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Wed, 13 Mar 2024 14:19:42 +0800 Subject: [PATCH 6/7] =?UTF-8?q?Delete=20'=E5=9F=BA=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84/=E7=8A=B6=E6=80=81=E6=9C=BA/81.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/状态机/81.py | 56 ------------------------------------ 1 file changed, 56 deletions(-) delete mode 100644 基本结构/状态机/81.py diff --git a/基本结构/状态机/81.py b/基本结构/状态机/81.py deleted file mode 100644 index cc083d4..0000000 --- a/基本结构/状态机/81.py +++ /dev/null @@ -1,56 +0,0 @@ -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() - -# 打印结果 -for word, freq in word_frequencies.most_common(10): - print(f"{word}: {freq}") \ No newline at end of file From 183cd741ba3c0b1cb29c96c1d01e7a6fc5674253 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Wed, 13 Mar 2024 14:20:39 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=9C=BA=20=E6=A0=87?= =?UTF-8?q?=E5=87=86=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/状态机/81A.py | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 基本结构/状态机/81A.py diff --git a/基本结构/状态机/81A.py b/基本结构/状态机/81A.py new file mode 100644 index 0000000..c7c726b --- /dev/null +++ b/基本结构/状态机/81A.py @@ -0,0 +1,60 @@ +# -*- 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() + +# 打印结果 +for word, freq in word_frequencies.most_common(10): + print(f"{word}: {freq}") \ No newline at end of file