建構 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.