From 445088fde85fdff745869a1d9b430c10a6890a72 Mon Sep 17 00:00:00 2001 From: zj3D Date: Sat, 16 Mar 2024 10:45:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E7=A7=8D=E7=BB=88?= =?UTF-8?q?=E7=AB=AF=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../终端/终端命令行/command_line_1.py | 46 +++++++++++++++++++ .../{command_line.py => command_line_2.py} | 4 +- 交互/终端/{ => 终端菜单}/test.txt | 0 交互/终端/{ => 终端菜单}/tf-97.py | 0 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 交互/终端/终端命令行/command_line_1.py rename 交互/终端/终端命令行/{command_line.py => command_line_2.py} (93%) rename 交互/终端/{ => 终端菜单}/test.txt (100%) rename 交互/终端/{ => 终端菜单}/tf-97.py (100%) diff --git a/交互/终端/终端命令行/command_line_1.py b/交互/终端/终端命令行/command_line_1.py new file mode 100644 index 0000000..75421f9 --- /dev/null +++ b/交互/终端/终端命令行/command_line_1.py @@ -0,0 +1,46 @@ +import sys +import re +from collections import Counter + +# 使用 : python command_line_1.py testfilepath 10 + +# 清洗文本,移除标点符号并转换为小写 +def clean_text(text): + return re.sub(r'[^\w\s]', '', text).lower() + +# 统计词频 +def count_frequencies(text): + return Counter(word for word in clean_text(text).split()) + +# 主函数 +def main(): + # 检查命令行参数数量 + if len(sys.argv) != 3: + print("Usage: python command_line_1.py ") + sys.exit(1) + + file_path = sys.argv[1] + n = int(sys.argv[2]) + + try: + # 打开文件并读取内容 + with open(file_path, 'r', encoding='utf-8') as file: + text = file.read() + + # 统计词频 + frequencies = count_frequencies(text) + + # 获取前n个最常见的单词 + most_common = frequencies.most_common(n) + + # 输出结果 + for word, freq in most_common: + print(f"{word}: {freq}") + + except FileNotFoundError: + print(f"File not found: {file_path}") + except ValueError as e: + print(f"Error: {e}") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/交互/终端/终端命令行/command_line.py b/交互/终端/终端命令行/command_line_2.py similarity index 93% rename from 交互/终端/终端命令行/command_line.py rename to 交互/终端/终端命令行/command_line_2.py index 9f85b53..d50fbe7 100644 --- a/交互/终端/终端命令行/command_line.py +++ b/交互/终端/终端命令行/command_line_2.py @@ -11,9 +11,9 @@ def count_frequencies(text): # 交互式提示用户输入文件路径和前n个单词的数量 def interactive_mode(): - file_path = input("请输入文件路径: ") + file_path = input("请输入文件路径 >> ") try: - n = int(input("请输入你想要输出的前n个最常见单词的数量: ")) + n = int(input("请输入你想要输出的前n个最常见单词的数量 >> ")) if n <= 0: raise ValueError("数量必须大于0。") except ValueError as e: diff --git a/交互/终端/test.txt b/交互/终端/终端菜单/test.txt similarity index 100% rename from 交互/终端/test.txt rename to 交互/终端/终端菜单/test.txt diff --git a/交互/终端/tf-97.py b/交互/终端/终端菜单/tf-97.py similarity index 100% rename from 交互/终端/tf-97.py rename to 交互/终端/终端菜单/tf-97.py