From 0e7f215a763c440aa72ec09d87f5f7c10d69d149 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Mon, 11 Mar 2024 14:01:57 +0800 Subject: [PATCH 1/5] ADD file via upload --- 基本结构/051享元模式/tf-38.py | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 基本结构/051享元模式/tf-38.py diff --git a/基本结构/051享元模式/tf-38.py b/基本结构/051享元模式/tf-38.py new file mode 100644 index 0000000..ff9c8b9 --- /dev/null +++ b/基本结构/051享元模式/tf-38.py @@ -0,0 +1,52 @@ +# -*- encoding:utf-8 -*- +from cppy.cp_util import * +'''享元模式 享元模式是一种结构型设计模式,在享元模式中,对象被设计为可共享的,可以被多个上下文使用,而不必在每个上下文中都创建新的对象。 +如果我们有大量不同的词频分析需求,有时需要词频前10的单词,有时需要词频前20的单词,有时还需要限定词汇的长度,那就需要创建多个词频统计器,每个词频统计器都独立创建并存储其内部状态, +那么系统的内存占用可能会很大,在这种情况下,享元模式共享相同类型的词频统计器对象,每种类型的词频统计器只需创建一个共享实例,然后通过设置不同的参数个性化 +每个对象,通过共享相同的内部状态,降低了对象的创建和内存占用成本。 +''' +#以需要的词频数量分类 +# 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 d6893ebf7aab97db1231463cce9f4705bd0048c2 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Mon, 11 Mar 2024 14:02:22 +0800 Subject: [PATCH 2/5] ADD file via upload --- 基本结构/051享元模式/readme071.txt | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 基本结构/051享元模式/readme071.txt diff --git a/基本结构/051享元模式/readme071.txt b/基本结构/051享元模式/readme071.txt new file mode 100644 index 0000000..96cb03e --- /dev/null +++ b/基本结构/051享元模式/readme071.txt @@ -0,0 +1,8 @@ +ԪģʽһֽṹģʽԪģʽУΪɹģԱʹãÿжµĶ + +дͬĴƵʱҪƵǰ10ĵʣʱҪƵǰ20ĵʣǾҪƵͳÿƵͳ洢ڲ״̬ +ôϵͳڴռÿܻܴ£Ԫģʽͬ͵ĴƵͳÿ͵ĴƵͳֻ贴һʵȻͨòͬIJԻ +ÿͨͬڲ״̬˶Ĵڴռóɱ + +磬Ҫ3ļȡƵǰʮĵʣ3ļȡƵǰʮĵʣôֻҪ2Ƶͳÿ󶼴洢ͬڲ״̬ȡĴ +һȡǰʮĵʣһȡǰʮĵʣô6 \ No newline at end of file From 8704659c09180488c3ec85cf00e51ea7fcba87ca Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Tue, 12 Mar 2024 12:31:45 +0800 Subject: [PATCH 3/5] =?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/readme071.txt?= =?UTF-8?q?'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/051享元模式/readme071.txt | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 基本结构/051享元模式/readme071.txt diff --git a/基本结构/051享元模式/readme071.txt b/基本结构/051享元模式/readme071.txt deleted file mode 100644 index 96cb03e..0000000 --- a/基本结构/051享元模式/readme071.txt +++ /dev/null @@ -1,8 +0,0 @@ -ԪģʽһֽṹģʽԪģʽУΪɹģԱʹãÿжµĶ - -дͬĴƵʱҪƵǰ10ĵʣʱҪƵǰ20ĵʣǾҪƵͳÿƵͳ洢ڲ״̬ -ôϵͳڴռÿܻܴ£Ԫģʽͬ͵ĴƵͳÿ͵ĴƵͳֻ贴һʵȻͨòͬIJԻ -ÿͨͬڲ״̬˶Ĵڴռóɱ - -磬Ҫ3ļȡƵǰʮĵʣ3ļȡƵǰʮĵʣôֻҪ2Ƶͳÿ󶼴洢ͬڲ״̬ȡĴ -һȡǰʮĵʣһȡǰʮĵʣô6 \ No newline at end of file From e400395770f7563e59b31698c7ccd9f6176e77cb Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Tue, 12 Mar 2024 12:37:57 +0800 Subject: [PATCH 4/5] =?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 | 52 --------------------------- 1 file changed, 52 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 ff9c8b9..0000000 --- a/基本结构/051享元模式/tf-38.py +++ /dev/null @@ -1,52 +0,0 @@ -# -*- encoding:utf-8 -*- -from cppy.cp_util import * -'''享元模式 享元模式是一种结构型设计模式,在享元模式中,对象被设计为可共享的,可以被多个上下文使用,而不必在每个上下文中都创建新的对象。 -如果我们有大量不同的词频分析需求,有时需要词频前10的单词,有时需要词频前20的单词,有时还需要限定词汇的长度,那就需要创建多个词频统计器,每个词频统计器都独立创建并存储其内部状态, -那么系统的内存占用可能会很大,在这种情况下,享元模式共享相同类型的词频统计器对象,每种类型的词频统计器只需创建一个共享实例,然后通过设置不同的参数个性化 -每个对象,通过共享相同的内部状态,降低了对象的创建和内存占用成本。 -''' -#以需要的词频数量分类 -# 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 844f716c14581ba558719197d8345af14bcfc6c9 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Tue, 12 Mar 2024 12:39:05 +0800 Subject: [PATCH 5/5] =?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 --- 基本结构/051享元模式/tf-38.py | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 基本结构/051享元模式/tf-38.py diff --git a/基本结构/051享元模式/tf-38.py b/基本结构/051享元模式/tf-38.py new file mode 100644 index 0000000..32f835d --- /dev/null +++ b/基本结构/051享元模式/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