學術文獻翻譯技能
在嚴格的執行預算(64 次模型請求 / 900 秒)下,執行可重現、高保真的科學翻譯。每個翻譯單元都與其原始頁面、欄位、章節路徑和邊界框保持 1:1 的錨定關係,並對術語、數字、引用、交叉引用和邏輯限定詞進行確定性驗證。
1. 環境常量與路徑慣例
在執行指令前設定標準執行目錄(Linux/Harbor 使用 python3;Windows 本機執行可替換為 python):
export SKILL_DIR="${SKILL_DIR:-/skills/weekly-skill}"
export INPUT_DIR="${INPUT_DIR:-/input}"
export WORK_DIR="${WORK_DIR:-/app/work}"
export OUTPUT_DIR="${OUTPUT_DIR:-/app/output}"
2. 任務模式路由(優先檢查輸入)
在規劃執行前,務必檢查 $INPUT_DIR/case.json、$INPUT_DIR/task.md 和原始檔案:
| 模式 | 觸發條件 | 執行路徑 |
|---|---|---|
| 模式 A:完整文件(預設) | case.json/task.md 中無頁碼或章節限制。 | 執行完整的步驟 1 → 步驟 5 流程。 |
| 模式 B:限定範圍翻譯 | task.md/case.json 指定頁碼(例如 pages 1-2)或章節(摘要、章節 2)。 | case_ingestor.py 自動過濾範圍內的單元;僅翻譯 in_scope == true 的批次。 |
| 模式 C:審計與修復現有翻譯 | 輸入已提供原始文字和待審查/驗證/修訂的候選翻譯。 | 執行步驟 1(case_ingestor.py)→ 可選的術語表編譯 + entity_guard.py --work-dir "$WORK_DIR" → 透過 merge_checkpoint.py --add-batch 合併草稿 → 步驟 4(translation_verifier.py + 修復)→ 步驟 5。 |
| 模式 D:中→英 / 混合語言 | 原始文件為中文或中英混合。 | case_ingestor.py 自動檢測 zh->en 方向;維持相同的術語鎖定與六維審計規則。 |
模式 C 直接執行捷徑(當輸入中已提供候選草稿翻譯時):
python3 "$SKILL_DIR/scripts/case_ingestor.py" --input-dir "$INPUT_DIR" --work-dir "$WORK_DIR"
python3 "$SKILL_DIR/scripts/entity_guard.py" --work-dir "$WORK_DIR"
# 將提供的草稿翻譯映射為 [{"unit_id": ..., "translated_text": ...}] 並存入 $WORK_DIR/draft_out.json,然後:
python3 "$SKILL_DIR/scripts/merge_checkpoint.py" --work-dir "$WORK_DIR" --output-dir "$OUTPUT_DIR" --input-dir "$INPUT_DIR" --add-batch "$WORK_DIR/draft_out.json"
# 直接進入步驟 4(translation_verifier.py + 精準修復)和步驟 5。
3. 六項不可違反的規則
- 優先讀取真實輸入:切勿根據文件名稱或領域標籤推斷科學數據、標題或事實。務必檢查
$INPUT_DIR/case.json、$INPUT_DIR/task.md和實際提取的單元。 - 忠實的逐句翻譯:對於每個單元中的每個句子,切勿以摘要、釋義或「大意」替代完整翻譯。
- 嚴格執行術語表與實體指紋:嚴格遵循
/app/work/glossary.json中的規範目標,並保留每個單元guard_hints中列出的所有標記(must_keep_numbers_units、must_keep_citations、must_keep_cross_refs、must_keep_inline_math、high_risk_logic)。 - 對不可讀資產零編造:遇到缺失文字層、模糊掃描、純圖像公式或亂碼字體(
manual_review_asset)時,切勿編造內容;保留untranslatable_asset標記並如實聲明uncertainty。 - 每個批次後立即建立早退檢查點:完成每個批次後立即執行
merge_checkpoint.py,確保$OUTPUT_DIR/result.json始終包含最新的有效交付成果。 - 遵守預算上限(64 次請求 / 900 秒):如果剩餘請求預算低(< 8 次請求)或時間超過 700 秒,停止翻譯新批次,對已完成單元執行
translation_verifier.py,產出如實的status="partial"交付成果,並傳遞validate_output.py。
4. 五步驟執行工作流程
步驟 1:擷取、診斷與自適應批次切分
提取標準化的原子單元,解析跨頁/跨欄段落,檢測外部術語表,並打包批次(自動上限為 <= 18 個批次以保護 64 次請求預算):
python3 "$SKILL_DIR/scripts/case_ingestor.py" \
--input-dir "$INPUT_DIR" \
--work-dir "$WORK_DIR" \
--max-batch-words 1600 \
--max-batch-units 25 \
--target-max-batches 18
立即檢查 $WORK_DIR/ingest_manifest.json 以驗證 language_direction、scope_constraint、batch_count、glossary_files_detected 和 diagnosis_summary。
- 快速完成捷徑(
batch_count == 0):如果batch_count == 0(例如,無法讀取的掃描或純粹的manual_review_asset回退,無可翻譯單元),則跳過步驟 2–4,直接進入步驟 5(render_deliverables.py --final+validate_output.py --strict)以交付如實的status="unable_to_process"和uncertainty,不浪費預算。
步驟 2:候選術語提取、鎖定與指紋注入
- 提取候選術語(並自動加載任何外部
.csv/.tsv/.json/.md術語表作為external_locked):
python3 "$SKILL_DIR/scripts/term_extractor.py" extract \
--work-dir "$WORK_DIR" \
--max-terms 60
-
確認並鎖定術語表(
$WORK_DIR/glossary.json): 讀取$WORK_DIR/term_candidates.json(以及如果由外部術語表預先填充的$WORK_DIR/glossary.json)。在一次性的決策過程中,撰寫/更新$WORK_DIR/glossary.json,使其符合$SKILL_DIR/templates/glossary_template.json的格式,適用於高頻核心領域術語(通常 15–35 個術語)。確保每個條目都指定term_id、source_term、aliases、canonical_target、forbidden_targets、do_not_translate和rationale。 -
編譯術語表並豐富批次:
python3 "$SKILL_DIR/scripts/term_extractor.py" compile \
--work-dir "$WORK_DIR" \
--glossary "$WORK_DIR/glossary.json"
python3 "$SKILL_DIR/scripts/entity_guard.py" \
--work-dir "$WORK_DIR" \
--enrich-batches
步驟 3:逐批次忠實翻譯與即時檢查點記錄
依序處理每個 $WORK_DIR/batches/batch_XXX.json:
- 上下文與句子邊界規則:僅為語義連貫性讀取
context_tail;請勿將context_tail單元包含在您的輸出 JSON 中。仔細閱讀batch_glossary和每個單元的guard_hints,並保持清晰的 1:1 句子邊界,以便多句單元在[S1]..[Sn]處整齊對齊。 - 輸出結構:將
$WORK_DIR/batch_XXX_out.json撰寫為一個乾淨的 JSON 陣列(無對話式前綴,如「以下是翻譯」):
[
{
"unit_id": "U0005",
"translated_text": "摘要——差分隱私已成功擴展到量子計算與資訊處理領域……",
"rationale": "鎖定 'differential privacy' -> '差分隱私';保留否定詞 '無法' 和限定詞 '僅'。",
"uncertainty": ""
}
]
- 每個批次後立即建立檢查點:
python3 "$SKILL_DIR/scripts/merge_checkpoint.py" \
--work-dir "$WORK_DIR" \
--output-dir "$OUTPUT_DIR" \
--input-dir "$INPUT_DIR" \
--add-batch "$WORK_DIR/batch_001_out.json"
步驟 4:確定性六維審計與精準修復(最多 2 輪)
在所有批次(或預算範圍內的子集)合併後,審計 $WORK_DIR/merged_translations.json:
python3 "$SKILL_DIR/scripts/translation_verifier.py" \
--work-dir "$WORK_DIR" \
--translations "$WORK_DIR/merged_translations.json"
如果 $WORK_DIR/verification_report.json 報告 error_count > 0 或高風險的 LOGIC_NEGATION_RISK / LOGIC_QUALIFIER_RISK 警告:
- 讀取
$WORK_DIR/repair_queue.json(僅包含有問題的單元及其issues、required_terms和required_entities)。 - 將目標修復寫入
$WORK_DIR/repair_patch_r1.json(相同的 JSON 陣列結構;可選地包含"trigger_codes": [...]和"repair_reason": "...")。 - 應用修補程式並重新驗證(限制最多 2 輪修復):
python3 "$SKILL_DIR/scripts/merge_checkpoint.py" \
--work-dir "$WORK_DIR" \
--output-dir "$OUTPUT_DIR" \
--input-dir "$INPUT_DIR" \
--apply-repair "$WORK_DIR/repair_patch_r1.json"
python3 "$SKILL_DIR/scripts/translation_verifier.py" \
--work-dir "$WORK_DIR" \
--translations "$WORK_DIR/merged_translations.json"
步驟 5:最終渲染與嚴格契約驗證
生成最終的多視圖交付成果(result.json、bilingual_parallel.md、glossary.md、glossary.json、verification_report.md)並通過嚴格驗證:
python3 "$SKILL_DIR/scripts/render_deliverables.py" \
--work-dir "$WORK_DIR" \
--output-dir "$OUTPUT_DIR" \
--input-dir "$INPUT_DIR" \
--final
python3 "$SKILL_DIR/scripts/validate_output.py" \
--output-dir "$OUTPUT_DIR" \
--input-dir "$INPUT_DIR" \
--strict
確保 validate_output.py 在完成任務前以代碼 0 退出。
- 檢查額外案例交付成果:檢查
case.json/task.md以確認除了標準交付成果之外,是否明確要求任何額外的獨立輸出檔名或問答欄位;如果有,也將其撰寫並交付至$OUTPUT_DIR。
5. 異常處理手冊
| 異常 / 邊界條件 | 自動檢測訊號 | 強制措施 |
|---|---|---|
| 掃描 PDF / 缺失文字層 | dominant_route == "scanned_needs_ocr" | case_ingestor.py 首先嘗試本地 OCR;如果不可用或出現亂碼,則輸出 untranslatable_asset(manual_review_asset)。請勿編造文字;如實報告於 uncertainty 和 status。 |
| 亂碼 / 自訂字體編碼 | source_warnings 包含 potential_font_garble | 謹慎翻譯周圍上下文,在單元 uncertainty 中標記不確定的符號,並在人工複查清單中列出確切頁碼/單元。 |
| 純圖像方程式 / 表格 | 缺少數學主體或 manual_review_asset | 保留可見的方程式 ID/標題;在不編造公式的情況下註明純視覺資產的限制。 |
| 超長論文(> 25k 字) | batch_count 自動調整為 <= 18 | 正常翻譯;監控請求預算。如果預算接近極限,提前停止並執行步驟 4+5 以乾淨地產出 status="partial"。 |
| 參考文獻 / 展示方程式 | translate_policy == "preserve_source" | 除非 task.md/case.json 中明確要求,否則切勿重新翻譯展示方程式或參考文獻條目。 |
| 混合語言來源 | 中文論文中的英文術語或英文論文中的中文摘要 | 保持 glossary.json 與主要方向一致;保留 ASCII 標準縮寫(do_not_translate: true)。 |
6. 依賴項、捆綁腳本與輸出文件
- 零崩潰 Python 設計:
$SKILL_DIR/scripts/下的所有腳本僅在頂層導入 Python 3 標準函式庫。可選的 PDF/OCR 後端(PyMuPDF/fitz、pdfplumber、pytesseract、tesseract、pdftotext)在函數內部動態探查並配有優雅的回退機制。 - 捆綁資源:
scripts/case_ingestor.py+scripts/pdf_engine/(pdf_diagnose.py、layout_parser.py、table_formula_ocr.py)scripts/term_extractor.py、scripts/entity_guard.py、scripts/translation_verifier.pyscripts/merge_checkpoint.py、scripts/render_deliverables.py、scripts/validate_output.pyresources/academic_invariants.json(否定/限定/讓步詞典、SI 單位、一致性模式)templates/glossary_template.json(可重複使用的術語表結構)
- 交付至
$OUTPUT_DIR的文件:result.json— 機器可讀的承載內容,包含case_id、所有output_contract欄位(包括巢狀 JSON Schema 物件)、status、sources、uncertainty、change_records、glossary、verification_summary和bilingual_alignment(附有按單元的sentence_alignment)。bilingual_parallel.md— 按章節結構化的 1:1 平行對照文件,包含頁面/錨點標記([U0009 | p.1 | paragraph]),多句單元的句子級[S1]/[S2]對齊對,鎖定的術語,理由,以及顯式的[⚠️ 缺失文字層/模糊資產,需人工複核]標籤。glossary.md和glossary.json— 整份文檔範圍內鎖定的術語表,附有首次出現的錨點和禁止的錯誤翻譯。verification_report.md— 量化覆蓋率、術語一致性、實體忠實度、修訂歷史記錄和人工複查清單。
7. 最小端到端調用範例(僅供說明)
給定一個本地測試資料夾 /tmp/demo_case/input,其中包含 Markdown 摘要 paper.md 和 case.json:
WORK=/tmp/demo_case/work
OUT=/tmp/demo_case/output
IN=/tmp/demo_case/input
python3 "$SKILL_DIR/scripts/case_ingestor.py" --input-dir "$IN" --work-dir "$WORK"
python3 "$SKILL_DIR/scripts/term_extractor.py" extract --work-dir "$WORK"
# 根據 term_candidates.json 撰寫 $WORK/glossary.json,然後編譯並豐富:
python3 "$SKILL_DIR/scripts/term_extractor.py" compile --work-dir "$WORK" --glossary "$WORK/glossary.json"
python3 "$SKILL_DIR/scripts/entity_guard.py" --work-dir "$WORK" --enrich-batches
# 將 batch_001.json 翻譯為 $WORK/batch_001_out.json 後:
python3 "$SKILL_DIR/scripts/merge_checkpoint.py" --work-dir "$WORK" --output-dir "$OUT" --input-dir "$IN" --add-batch "$WORK/batch_001_out.json"
python3 "$SKILL_DIR/scripts/translation_verifier.py" --work-dir "$WORK" --translations "$WORK/merged_translations.json"
python3 "$SKILL_DIR/scripts/render_deliverables.py" --work-dir "$WORK" --output-dir "$OUT" --input-dir "$IN" --final
python3 "$SKILL_DIR/scripts/validate_output.py" --output-dir "$OUT" --input-dir "$IN" --strict
說明性 result.json 片段(示例結構,非基準分數):
{
"case_id": "example_case_001",
"status": "success",
"progress": {"completed_units": 6, "total_translatable_units": 6, "total_units": 8},
"sources": {"dominant_format": "markdown"},
"uncertainty": [],
"change_records": [],
"verification_summary": {"audi