构建 CLI 核心
只有在记录预测之后才允许智能体协助;必须检查并测试每一处生成的差异。
先决条件
第 01–12 天:字符串、字典、文件、异常、测试与终端使用。
学习目标
实现或检查 projects/text_tools/core.py 与 cli.py,覆盖统计、确定性分割、出处清单(manifest)以及显式的编码错误。
操作说明
必做:先创建本练习专用的私有工作区(WORKDIR=$(mktemp -d)),把小型语料写入 $WORKDIR/corpus.txt,记录预测,然后运行完整调用 python3 projects/text_tools/cli.py --input "$WORKDIR/corpus.txt" --stats --json --split --output-dir "$WORKDIR/artifacts";检查差异,并逐一练习 --input、--stats、--split、--output-dir、--seed、--encoding 和 --errors。选做:对比 Python 文档链接。
练习与预测
使用一个包含重复词元、中日韩文字、带重音字符和一个表情符号的小型语料库。先预测字符数/词元数,然后验证 stats.json 和 manifest.json——包括 source_sha256 与原始输入字节的 SHA256(如 sha256sum "$WORKDIR/corpus.txt")一致,且 input_decoding 记录了编码、错误处理与换行策略。
计算放在 core.py,展示放在 cli.py。使用 random.Random(seed)、有序索引、SHA256,以及零词元保护。
合理方案:用词元/字符 Counter 计数,用 len(text) 统计 Unicode 码点数,在 list(range(n)) 上使用 random.Random(seed),保证索引集合互不相交,并在清单中记录源/分割哈希、比例、计数、环境和 no_leakage_verified 字段。 清单还会记录原始字节的 source_sha256、decoded_text_sha256、input_decoding 策略与 normalized_ratios,并在任何写入前拒绝输入/输出路径冲突。
Learning Project Submission & Evidence
学习者能够讲解并运行真实的 text_tools CLI,记录预测与证据,且不会把智能体写出的差异当作独立理解的证明。
来源资源与出处
Python Standard Library: Built-in Types
对应章节 / 时间戳: str methods and Unicode semantics used by analyze_text (split, join, isalpha, isdigit)
验证版本与日期: Python 3.12 documentation; verified 2026-10-30
Primary reference for the string methods and Unicode semantics behind corpus statistics.
Study Days 01–12 Python fundamentals
The lesson explains all required behavior locally and the reference implementation ships in the repository; pydoc mirrors the same documentation offline.
对比候选资源与选型分析 (2)
Powerful system tools, but the learning target is implementing and testing the behavior in Python, not delegating to platform-specific utilities.
Deeper Unicode background for the encoding edge cases; str method reference remains the primary doc.
argparse — Parser for command-line options, arguments and subcommands
对应章节 / 时间戳: add_argument flags, choices, and type conversion used by cli.py
验证版本与日期: Python 3.12 documentation; verified 2026-10-30
Primary reference for the CLI flags the learner exercises on the real project.
Study Days 01–12 Python fundamentals
argparse is part of the standard library; python3 -c "import argparse; help(argparse)" works fully offline.
对比候选资源与选型分析 (2)
Strong third-party CLI framework, but the project intentionally uses only the standard library so the exercise needs no extra dependency.
Usage-string DSL for CLIs, but an external dependency is unjustified when argparse covers the project.