forked from p46318075/CodePattern
				
			
							parent
							
								
									025825cffa
								
							
						
					
					
						commit
						51cb037429
					
				
											
												Binary file not shown.
											
										
									
								
											
												
													File diff suppressed because it is too large
													Load Diff
												
											
										
									
								| @ -0,0 +1,34 @@ | ||||
| from collections import Counter | ||||
| import cppy.cp_util as util | ||||
| 
 | ||||
| def word_frequency( top_n=10 ): | ||||
|     def decorator(func): | ||||
|         def wrapper(*args, **kwargs): | ||||
|             # 执行被装饰的函数 | ||||
|             result = func(*args, **kwargs) | ||||
|              | ||||
|             # 初始化词频计数器 | ||||
|             word_counts = Counter() | ||||
|              | ||||
|             # 分词并计数 | ||||
|             for word in util.extract_str_words(result): | ||||
|                 word_counts[word] += 1             | ||||
|              | ||||
|             # 输出所有词的频率最高的n个词             | ||||
|             most_common = word_counts.most_common(top_n)             | ||||
|             for w, count in most_common: | ||||
|                 print(f"{w} - {count}") | ||||
|             return result | ||||
|          | ||||
|         return wrapper | ||||
|     return decorator | ||||
| 
 | ||||
| 
 | ||||
| # 使用装饰器 | ||||
| @word_frequency( top_n=10 ) | ||||
| def read_file(file_path): | ||||
|     with open(file_path, 'r', encoding='utf-8') as file: | ||||
|         return file.read() | ||||
| 
 | ||||
| 
 | ||||
| read_file( util.testfilepath ) | ||||
					Loading…
					
					
				
		Reference in new issue