@louisultra/dsh-verymath 0.0.0-stage → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +133 -0
- package/LICENSE +21 -0
- package/NOTICE.md +35 -0
- package/README.md +108 -2
- package/README_EN.md +105 -0
- package/UPSTREAM.json +106 -0
- package/cordis.patch.yml +12 -0
- package/docs/capability-examples.md +581 -0
- package/docs/examples/README.md +32 -0
- package/docs/examples/bad_scan.md +10 -0
- package/docs/examples/broken-proof.lean +9 -0
- package/docs/examples/claims.md +7 -0
- package/docs/examples/knapsack.json +10 -0
- package/docs/examples/matrix.json +1 -0
- package/docs/examples/points.csv +21 -0
- package/docs/examples/qp.json +8 -0
- package/docs/examples/research-ws/project/GOALS.yaml +4 -0
- package/docs/examples/research-ws/workstreams/ws-lsa/report.md +13 -0
- package/docs/examples/research-ws/workstreams/ws-lsa/reviews/r1.json +1 -0
- package/docs/examples/research-ws/workstreams/ws-lsa/status.yaml +1 -0
- package/docs/examples/source.csv +4 -0
- package/lib/core/error.d.ts +20 -0
- package/lib/core/error.js +25 -0
- package/lib/core/gates.d.ts +42 -0
- package/lib/core/gates.js +217 -0
- package/lib/core/glossary.d.ts +101 -0
- package/lib/core/glossary.js +243 -0
- package/lib/core/graph.d.ts +118 -0
- package/lib/core/graph.js +460 -0
- package/lib/core/index.d.ts +120 -0
- package/lib/core/index.js +197 -0
- package/lib/core/paper.d.ts +163 -0
- package/lib/core/paper.js +73 -0
- package/lib/core/paths.d.ts +2 -0
- package/lib/core/paths.js +12 -0
- package/lib/core/sage.d.ts +61 -0
- package/lib/core/sage.js +212 -0
- package/lib/core/skills.d.ts +11 -0
- package/lib/core/skills.js +110 -0
- package/lib/tool/index.d.ts +21 -0
- package/lib/tool/index.js +1144 -0
- package/package.json +92 -4
- package/skills/after-ocr/SKILL.md +123 -0
- package/skills/after-ocr/references/audit-schema.md +94 -0
- package/skills/after-ocr/references/error-taxonomy.md +179 -0
- package/skills/after-ocr/scripts/merge_audit_logs.py +329 -0
- package/skills/after-ocr/scripts/scan_markdown_ocr.py +350 -0
- package/skills/after-ocr/tests/test_scripts.py +84 -0
- package/skills/discover-math-problems/SKILL.md +115 -0
- package/skills/discover-math-problems/references/artifact-contract.md +93 -0
- package/skills/discover-math-problems/references/autonomous-research-protocol.md +86 -0
- package/skills/discover-math-problems/references/conjecture-generation-protocol.md +63 -0
- package/skills/discover-math-problems/references/extremal-graph-variants.md +53 -0
- package/skills/graph-theory-paper-reading/SKILL.md +600 -0
- package/skills/invariant-computation/SKILL.md +169 -0
- package/skills/invariant-computation/examples/invariant_summary.example.json +51 -0
- package/skills/invariant-computation/manifest.yaml +46 -0
- package/skills/invariant-computation/references/INDEX.md +20 -0
- package/skills/invariant-computation/references/failure_modes.md +40 -0
- package/skills/invariant-computation/references/method_route_map.md +177 -0
- package/skills/invariant-computation/references/source_notes.md +19 -0
- package/skills/invariant-computation/references/tool_catalog.md +65 -0
- package/skills/invariant-computation/references/validation_checks.md +52 -0
- package/skills/invariant-computation/schema/invariant_summary.schema.json +260 -0
- package/skills/lean-formalization/SKILL.md +111 -0
- package/skills/lean-formalization/lean-runtime/config/env.example +5 -0
- package/skills/lean-formalization/lean-runtime/config/lean_agent.example.toml +28 -0
- package/skills/lean-formalization/lean-runtime/config/numina_runtime.example.toml +12 -0
- package/skills/lean-formalization/lean-runtime/examples/smoke/LocalLeanSmoke.lean +7 -0
- package/skills/lean-formalization/lean-runtime/prompts/complete_sorries.md +3 -0
- package/skills/lean-formalization/lean-runtime/prompts/formalize_statement.md +3 -0
- package/skills/lean-formalization/lean-runtime/prompts/prove_theorem.md +3 -0
- package/skills/lean-formalization/lean-runtime/prompts/repair_lean_file.md +3 -0
- package/skills/lean-formalization/lean-runtime/references/backend_adapter_checklist.md +36 -0
- package/skills/lean-formalization/lean-runtime/references/direct_lean_workflow.md +48 -0
- package/skills/lean-formalization/lean-runtime/references/failure_taxonomy.md +17 -0
- package/skills/lean-formalization/lean-runtime/references/interactive_orchestration.md +83 -0
- package/skills/lean-formalization/lean-runtime/references/lean_agent_capability_map.md +116 -0
- package/skills/lean-formalization/lean-runtime/references/lean_lsp_mcp_adapter.md +219 -0
- package/skills/lean-formalization/lean-runtime/references/lean_runtime_configuration.md +92 -0
- package/skills/lean-formalization/lean-runtime/references/numina_reverse_analysis.md +80 -0
- package/skills/lean-formalization/lean-runtime/references/numina_runtime.md +41 -0
- package/skills/lean-formalization/lean-runtime/references/numina_subagent_troubleshooting.md +77 -0
- package/skills/lean-formalization/lean-runtime/references/review_checklist.md +10 -0
- package/skills/lean-formalization/lean-runtime/references/specialist_agent_patterns.md +65 -0
- package/skills/lean-formalization/lean-runtime/schemas/config.schema.json +12 -0
- package/skills/lean-formalization/lean-runtime/schemas/result.schema.json +33 -0
- package/skills/lean-formalization/lean-runtime/schemas/task.schema.json +30 -0
- package/skills/lean-formalization/lean-runtime/scripts/ai4m_lean.py +222 -0
- package/skills/lean-formalization/lean-runtime/scripts/check_lean_project.py +134 -0
- package/skills/lean-formalization/lean-runtime/scripts/common.py +296 -0
- package/skills/lean-formalization/lean-runtime/scripts/configure_lean.py +222 -0
- package/skills/lean-formalization/lean-runtime/scripts/detect_sorry.py +49 -0
- package/skills/lean-formalization/lean-runtime/scripts/direct_task.py +100 -0
- package/skills/lean-formalization/lean-runtime/scripts/extract_minimal_failure.py +74 -0
- package/skills/lean-formalization/lean-runtime/scripts/numina_runtime.py +414 -0
- package/skills/lean-formalization/lean-runtime/scripts/smoke_test.py +75 -0
- package/skills/lean-formalization/lean-runtime/scripts/tool_status.py +79 -0
- package/skills/lean-formalization/lean-runtime/scripts/validate_patch.py +81 -0
- package/skills/lean-formalization/lean-runtime/scripts/verify_delivery.py +484 -0
- package/skills/lean-formalization/lean-runtime/tests/fixtures/after_bad.lean +4 -0
- package/skills/lean-formalization/lean-runtime/tests/fixtures/after_statement_changed.lean +4 -0
- package/skills/lean-formalization/lean-runtime/tests/fixtures/before.lean +4 -0
- package/skills/lean-formalization/lean-runtime/tests/fixtures/failure.lean +7 -0
- package/skills/lean-formalization/lean-runtime/tests/test_check_lean_project.py +59 -0
- package/skills/lean-formalization/lean-runtime/tests/test_cli.py +377 -0
- package/skills/lean-formalization/lean-runtime/tests/test_common_config.py +84 -0
- package/skills/lean-formalization/lean-runtime/tests/test_configure_lean.py +138 -0
- package/skills/lean-formalization/lean-runtime/tests/test_detect_sorry.py +24 -0
- package/skills/lean-formalization/lean-runtime/tests/test_direct_task.py +69 -0
- package/skills/lean-formalization/lean-runtime/tests/test_numina_runtime.py +168 -0
- package/skills/lean-formalization/lean-runtime/tests/test_validate_patch.py +29 -0
- package/skills/lean-setup/SKILL.md +99 -0
- package/skills/least-squares/README.md +85 -0
- package/skills/least-squares/SKILL.md +613 -0
- package/skills/least-squares/examples.md +380 -0
- package/skills/math-beamer/SKILL.md +325 -0
- package/skills/math-beamer/references/build-and-layout-audit.md +63 -0
- package/skills/math-beamer/references/math-domain-patterns.md +56 -0
- package/skills/math-beamer/references/template-policy.md +43 -0
- package/skills/math-beamer/references/template-source-index.md +51 -0
- package/skills/math-beamer/templates/catalog.yaml +235 -0
- package/skills/math-beamer/templates/packs/algebra-number-theory-talk/main.tex +45 -0
- package/skills/math-beamer/templates/packs/algebra-number-theory-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/algorithm-talk/main.tex +43 -0
- package/skills/math-beamer/templates/packs/algorithm-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/analysis-core-talk/main.tex +49 -0
- package/skills/math-beamer/templates/packs/analysis-core-talk/template.yaml +19 -0
- package/skills/math-beamer/templates/packs/backup-heavy-talk/main.tex +40 -0
- package/skills/math-beamer/templates/packs/backup-heavy-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/beamerposter-a0/main.tex +45 -0
- package/skills/math-beamer/templates/packs/beamerposter-a0/template.yaml +12 -0
- package/skills/math-beamer/templates/packs/combinatorics-graph-talk/main.tex +53 -0
- package/skills/math-beamer/templates/packs/combinatorics-graph-talk/template.yaml +18 -0
- package/skills/math-beamer/templates/packs/control-systems-talk/main.tex +51 -0
- package/skills/math-beamer/templates/packs/control-systems-talk/template.yaml +18 -0
- package/skills/math-beamer/templates/packs/course-lecture-zh/main.tex +43 -0
- package/skills/math-beamer/templates/packs/course-lecture-zh/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/data-experiment-results/main.tex +45 -0
- package/skills/math-beamer/templates/packs/data-experiment-results/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/dynamical-systems-talk/main.tex +42 -0
- package/skills/math-beamer/templates/packs/dynamical-systems-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/geometry-topology-talk/main.tex +43 -0
- package/skills/math-beamer/templates/packs/geometry-topology-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/information-coding-crypto-talk/main.tex +49 -0
- package/skills/math-beamer/templates/packs/information-coding-crypto-talk/template.yaml +18 -0
- package/skills/math-beamer/templates/packs/logic-foundations-category-talk/main.tex +51 -0
- package/skills/math-beamer/templates/packs/logic-foundations-category-talk/template.yaml +18 -0
- package/skills/math-beamer/templates/packs/numerical-analysis-talk/main.tex +51 -0
- package/skills/math-beamer/templates/packs/numerical-analysis-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/optimization-talk/main.tex +58 -0
- package/skills/math-beamer/templates/packs/optimization-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/pde-analysis-talk/main.tex +49 -0
- package/skills/math-beamer/templates/packs/pde-analysis-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/probability-statistics-talk/main.tex +52 -0
- package/skills/math-beamer/templates/packs/probability-statistics-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/reading-group-paper/main.tex +46 -0
- package/skills/math-beamer/templates/packs/reading-group-paper/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/seminar-minimal-en/main.tex +60 -0
- package/skills/math-beamer/templates/packs/seminar-minimal-en/template.yaml +13 -0
- package/skills/math-beamer/templates/packs/seminar-minimal-zh/main.tex +60 -0
- package/skills/math-beamer/templates/packs/seminar-minimal-zh/template.yaml +13 -0
- package/skills/math-beamer/templates/packs/theorem-proof-talk/main.tex +60 -0
- package/skills/math-beamer/templates/packs/theorem-proof-talk/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/thesis-defense/main.tex +42 -0
- package/skills/math-beamer/templates/packs/thesis-defense/template.yaml +9 -0
- package/skills/math-beamer/templates/packs/workshop-tutorial/main.tex +32 -0
- package/skills/math-beamer/templates/packs/workshop-tutorial/template.yaml +9 -0
- package/skills/math-beamer/templates/preview-gallery.md +32 -0
- package/skills/math-beamer/templates/previews/algebra-number-theory-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/algebra-number-theory-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/algorithm-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/algorithm-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/analysis-core-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/analysis-core-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/backup-heavy-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/backup-heavy-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/beamerposter-a0-content.png +0 -0
- package/skills/math-beamer/templates/previews/beamerposter-a0-cover.png +0 -0
- package/skills/math-beamer/templates/previews/combinatorics-graph-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/combinatorics-graph-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/control-systems-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/control-systems-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/course-lecture-zh-content.png +0 -0
- package/skills/math-beamer/templates/previews/course-lecture-zh-cover.png +0 -0
- package/skills/math-beamer/templates/previews/data-experiment-results-content.png +0 -0
- package/skills/math-beamer/templates/previews/data-experiment-results-cover.png +0 -0
- package/skills/math-beamer/templates/previews/dynamical-systems-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/dynamical-systems-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/geometry-topology-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/geometry-topology-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/information-coding-crypto-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/information-coding-crypto-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/logic-foundations-category-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/logic-foundations-category-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/numerical-analysis-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/numerical-analysis-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/optimization-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/optimization-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/pde-analysis-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/pde-analysis-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/probability-statistics-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/probability-statistics-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/reading-group-paper-content.png +0 -0
- package/skills/math-beamer/templates/previews/reading-group-paper-cover.png +0 -0
- package/skills/math-beamer/templates/previews/seminar-minimal-en-content.png +0 -0
- package/skills/math-beamer/templates/previews/seminar-minimal-en-cover.png +0 -0
- package/skills/math-beamer/templates/previews/seminar-minimal-zh-content.png +0 -0
- package/skills/math-beamer/templates/previews/seminar-minimal-zh-cover.png +0 -0
- package/skills/math-beamer/templates/previews/theorem-proof-talk-content.png +0 -0
- package/skills/math-beamer/templates/previews/theorem-proof-talk-cover.png +0 -0
- package/skills/math-beamer/templates/previews/thesis-defense-content.png +0 -0
- package/skills/math-beamer/templates/previews/thesis-defense-cover.png +0 -0
- package/skills/math-beamer/templates/previews/workshop-tutorial-content.png +0 -0
- package/skills/math-beamer/templates/previews/workshop-tutorial-cover.png +0 -0
- package/skills/math-beamer/templates/review-report.md +54 -0
- package/skills/math-beamer/templates/slide-source-ledger.md +22 -0
- package/skills/math-beamer/templates/source-packet.md +50 -0
- package/skills/math-glossary/SKILL.md +347 -0
- package/skills/math-glossary/references/glossary-schema.md +71 -0
- package/skills/math-glossary/references/glossary-usage-guide.md +176 -0
- package/skills/math-glossary/references/glossary-web-template.md +127 -0
- package/skills/math-glossary/references/glossary.schema.json +76 -0
- package/skills/math-glossary/references/user-manual.md +435 -0
- package/skills/math-glossary/requirements-ai.txt +3 -0
- package/skills/math-glossary/requirements.txt +3 -0
- package/skills/math-glossary/scripts/glossary_backup.py +42 -0
- package/skills/math-glossary/scripts/glossary_build.py +467 -0
- package/skills/math-glossary/scripts/glossary_enrich_examples.py +283 -0
- package/skills/math-glossary/scripts/glossary_export.py +333 -0
- package/skills/math-glossary/scripts/glossary_web.py +1265 -0
- package/skills/math-paper-reading/README.md +163 -0
- package/skills/math-paper-reading/SKILL.md +48 -0
- package/skills/math-paper-reading/agent_router.md +36 -0
- package/skills/math-paper-reading/references/proof_pattern_taxonomy.md +123 -0
- package/skills/math-paper-reading/references/quality_rubric.md +36 -0
- package/skills/math-paper-reading/skill_base.md +627 -0
- package/skills/math-paper-reading/skill_literature_search.md +866 -0
- package/skills/math-paper-reading/skill_paper_deep_read.md +1024 -0
- package/skills/math-paper-reading/skill_pathway_proof.md +718 -0
- package/skills/math-paper-reading/skill_reference_manager.md +746 -0
- package/skills/matlab-runner/SKILL.md +217 -0
- package/skills/matlab-runner/examples/README.md +33 -0
- package/skills/matlab-runner/examples/matrix_report.m +28 -0
- package/skills/matlab-runner/examples/plot_damped_sine.m +64 -0
- package/skills/matlab-runner/examples/test_vector_norm.m +12 -0
- package/skills/matlab-runner/references/client-setup.md +24 -0
- package/skills/matlab-runner/references/file-session-safety.md +86 -0
- package/skills/matlab-runner/references/numerical-safety.md +126 -0
- package/skills/matlab-runner/references/resource-safety.md +125 -0
- package/skills/mixed-integer-programming/SKILL.md +119 -0
- package/skills/mixed-integer-programming/lp-examples.md +251 -0
- package/skills/mixed-integer-programming/mip-examples-verified.md +118 -0
- package/skills/open-problem-research-pipeline/SKILL.md +76 -0
- package/skills/open-problem-research-pipeline/skill_literature_analysis.md +201 -0
- package/skills/open-problem-research-pipeline/skill_literature_proof_framework.md +206 -0
- package/skills/open-problem-research-pipeline/skill_literature_search.md +172 -0
- package/skills/open-problem-research-pipeline/skill_qskill.md +177 -0
- package/skills/osqp-solver/SKILL.md +245 -0
- package/skills/osqp-solver/references/citation.md +25 -0
- package/skills/osqp-solver/references/dual-infeasible-example.json +14 -0
- package/skills/osqp-solver/references/feasible-example.json +21 -0
- package/skills/osqp-solver/references/primal-infeasible-example.json +17 -0
- package/skills/osqp-solver/references/python-api.md +95 -0
- package/skills/osqp-solver/references/verification.md +94 -0
- package/skills/osqp-solver/scripts/check_osqp_environment.py +120 -0
- package/skills/osqp-solver/scripts/solve_qp.py +943 -0
- package/skills/paper-writing/SKILL.md +115 -0
- package/skills/paper-writing/skill_claim_evidence_ledger.md +55 -0
- package/skills/paper-writing/skill_formula_environment_and_readability.md +41 -0
- package/skills/paper-writing/skill_latex_build_and_layout_audit.md +73 -0
- package/skills/paper-writing/skill_notation_and_variable_consistency.md +42 -0
- package/skills/paper-writing/skill_paper_skeleton_and_logical_architecture.md +54 -0
- package/skills/paper-writing/skill_proof_obligation_and_assumption_audit.md +57 -0
- package/skills/paper-writing/templates/claim-evidence-ledger.md +23 -0
- package/skills/paper-writing/templates/proof-obligation-ledger.md +22 -0
- package/skills/paper-writing/templates/rapid-prototype-paper-skeleton.md +46 -0
- package/skills/paper-writing/templates/source-packet.md +37 -0
- package/skills/paper-writing/templates/submission-readiness.md +41 -0
- package/skills/proof-blueprint-review/SKILL.md +129 -0
- package/skills/proof-blueprint-review/references/agent-mediated-proof-protocol.md +57 -0
- package/skills/proof-blueprint-review/references/generation-agent.md +45 -0
- package/skills/proof-blueprint-review/references/proof-acceptance-contract.md +41 -0
- package/skills/proof-blueprint-review/references/verification-agent.md +37 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""按文件与行号合并多轮 after-ocr 审校日志。"""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import re
|
|
8
|
+
from collections import Counter, defaultdict
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
BLOCK_RE = re.compile(r"^###\s+([^\n]+)\n(.*?)(?=^#{2,3}\s|\Z)", re.MULTILINE | re.DOTALL)
|
|
13
|
+
FIELD_RE = re.compile(r"^- ([^::\n]+)[::](.*)$", re.MULTILINE)
|
|
14
|
+
LOCATION_RE = re.compile(r"`(.+?):(\d+)`")
|
|
15
|
+
LINE_ONLY_RE = re.compile(r"(?:行\s*`?(\d+)|\bL(\d+))", re.IGNORECASE)
|
|
16
|
+
PLAIN_LINE_RE = re.compile(r"\s*`?(\d+)")
|
|
17
|
+
PASS_RE = re.compile(r"-([A-Za-z0-9]+)-\d+$")
|
|
18
|
+
HEADER_MARKDOWN_RE = re.compile(r"^- (?:源 |主审 |OCR )?Markdown[::]\s*`([^`]+)`", re.MULTILINE)
|
|
19
|
+
INLINE_META_RE = re.compile(
|
|
20
|
+
r"^- 类别:`?([^`;\n]+)`?;严重度:([^;\n]+);置信度:([^;\n]+)", re.MULTILINE
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
CATEGORY_ALIASES = {
|
|
24
|
+
"TEXT_OCR": "TEXT_OCR",
|
|
25
|
+
"MATH_OCR_SYMBOL": "MATH_OCR_SYMBOL",
|
|
26
|
+
"MATH_OCR_STRUCTURE": "MATH_OCR_STRUCTURE",
|
|
27
|
+
"MATH_DELIMITER": "MATH_DELIMITER",
|
|
28
|
+
"INLINE_MATH": "INLINE_MATH",
|
|
29
|
+
"LATEX_ENVIRONMENT": "LATEX_ENVIRONMENT",
|
|
30
|
+
"MATH_SEMANTICS": "MATH_SEMANTICS",
|
|
31
|
+
"MARKDOWN_LAYOUT": "MARKDOWN_LAYOUT",
|
|
32
|
+
"HEADING_STRUCTURE": "HEADING_STRUCTURE",
|
|
33
|
+
"REFERENCE_NUMBERING": "REFERENCE_NUMBERING",
|
|
34
|
+
"IMAGE_TABLE": "IMAGE_TABLE",
|
|
35
|
+
"OCR_ARTIFACT": "OCR_ARTIFACT",
|
|
36
|
+
"UNICODE_LATEX": "UNICODE_LATEX",
|
|
37
|
+
"OCR_TEXT_IN_MATH": "INLINE_MATH",
|
|
38
|
+
"OCR_TEXT": "TEXT_OCR",
|
|
39
|
+
"UNICODE_MATH": "UNICODE_LATEX",
|
|
40
|
+
"LATEX_ARRAY": "LATEX_ENVIRONMENT",
|
|
41
|
+
"LATEX_DELIMITER": "MATH_DELIMITER",
|
|
42
|
+
"MATH_FONT": "MATH_OCR_SYMBOL",
|
|
43
|
+
"MATH_MODE": "INLINE_MATH",
|
|
44
|
+
"MATH_OCR_DELIMITER": "MATH_DELIMITER",
|
|
45
|
+
"MATH_SUBSCRIPT": "MATH_OCR_SYMBOL",
|
|
46
|
+
"OCR_MATH_SYMBOL": "MATH_OCR_SYMBOL",
|
|
47
|
+
"LATEX_TEXT": "INLINE_MATH",
|
|
48
|
+
"LATEX_FUNCTION_ARGUMENT": "MATH_OCR_STRUCTURE",
|
|
49
|
+
"LATEX_MATH_MODE": "INLINE_MATH",
|
|
50
|
+
"STRUCTURE_MISSING": "HEADING_STRUCTURE",
|
|
51
|
+
"OCR_OMISSION": "TEXT_OCR",
|
|
52
|
+
"REFERENCE_OCR": "REFERENCE_NUMBERING",
|
|
53
|
+
"OCR_REFERENCE": "REFERENCE_NUMBERING",
|
|
54
|
+
"CITATION_OCR": "REFERENCE_NUMBERING",
|
|
55
|
+
"EQUATION_NUMBERING": "REFERENCE_NUMBERING",
|
|
56
|
+
"LATEX_SUBSCRIPT": "MATH_OCR_SYMBOL",
|
|
57
|
+
"LATEX_PARENTHESIS": "MATH_DELIMITER",
|
|
58
|
+
"MATH_OCR_SEMANTIC": "MATH_SEMANTICS",
|
|
59
|
+
"LATEX_PUNCTUATION": "MATH_DELIMITER",
|
|
60
|
+
"OCR_PUNCTUATION_IN_MATH": "MATH_OCR_SYMBOL",
|
|
61
|
+
"OCR_DUPLICATE_TEXT": "OCR_ARTIFACT",
|
|
62
|
+
"LATEX_SET_BUILDER": "MATH_OCR_STRUCTURE",
|
|
63
|
+
"OCR_PUNCTUATION": "TEXT_OCR",
|
|
64
|
+
"LATEX_AMBIGUOUS": "MATH_OCR_STRUCTURE",
|
|
65
|
+
"LATEX_FORMULA": "MATH_OCR_STRUCTURE",
|
|
66
|
+
"LATEX_SUBSTACK": "MATH_OCR_STRUCTURE",
|
|
67
|
+
"OCR_GRAMMAR": "TEXT_OCR",
|
|
68
|
+
"MATH_OPERATOR": "MATH_OCR_SYMBOL",
|
|
69
|
+
"OCR_TRUNCATION": "TEXT_OCR",
|
|
70
|
+
"TEXT_LAYOUT": "MARKDOWN_LAYOUT",
|
|
71
|
+
"LATEX_FORMAT": "LATEX_ENVIRONMENT",
|
|
72
|
+
"MARKDOWN_STRUCTURE": "MARKDOWN_LAYOUT",
|
|
73
|
+
"MARKDOWN_TABLE": "IMAGE_TABLE",
|
|
74
|
+
"MARKDOWN_FORMAT": "MARKDOWN_LAYOUT",
|
|
75
|
+
"TEXT_OCR_GARBAGE": "OCR_ARTIFACT",
|
|
76
|
+
"HEADER_FOOTER_OCR": "OCR_ARTIFACT",
|
|
77
|
+
"MATH_OCR_OMISSION": "MATH_OCR_STRUCTURE",
|
|
78
|
+
"MARKDOWN_OCR": "MARKDOWN_LAYOUT",
|
|
79
|
+
"LATEX_COMMAND": "MATH_OCR_SYMBOL",
|
|
80
|
+
"FORMULA_STRUCTURE": "MATH_OCR_STRUCTURE",
|
|
81
|
+
"FORMULA_REFERENCE": "REFERENCE_NUMBERING",
|
|
82
|
+
"STRUCTURE": "HEADING_STRUCTURE",
|
|
83
|
+
"CROSS_REF": "REFERENCE_NUMBERING",
|
|
84
|
+
"MATH_TEXT_BOUNDARY": "INLINE_MATH",
|
|
85
|
+
"OCR_RESIDUE": "OCR_ARTIFACT",
|
|
86
|
+
"FRONT_MATTER": "OCR_ARTIFACT",
|
|
87
|
+
"TOC": "HEADING_STRUCTURE",
|
|
88
|
+
"OCR_TEXT": "TEXT_OCR",
|
|
89
|
+
"MATH_SYMBOL": "MATH_OCR_SYMBOL",
|
|
90
|
+
"正文 OCR": "TEXT_OCR",
|
|
91
|
+
"数学符号 OCR": "MATH_OCR_SYMBOL",
|
|
92
|
+
"正文/公式边界 OCR": "INLINE_MATH",
|
|
93
|
+
"公式文本 OCR": "INLINE_MATH",
|
|
94
|
+
"公式结构 OCR": "MATH_OCR_STRUCTURE",
|
|
95
|
+
"公式 OCR": "MATH_OCR_STRUCTURE",
|
|
96
|
+
"数学定界符 OCR": "MATH_DELIMITER",
|
|
97
|
+
"标题结构 OCR": "HEADING_STRUCTURE",
|
|
98
|
+
"练习结构 OCR": "HEADING_STRUCTURE",
|
|
99
|
+
"公式正文混排": "INLINE_MATH",
|
|
100
|
+
"正文": "TEXT_OCR",
|
|
101
|
+
"MATH_FORMULA": "MATH_OCR_STRUCTURE",
|
|
102
|
+
"MATH_NOTATION": "MATH_OCR_SYMBOL",
|
|
103
|
+
"MATH_DOMAIN": "MATH_OCR_SYMBOL",
|
|
104
|
+
"MATH_INDEX": "MATH_OCR_SYMBOL",
|
|
105
|
+
"LATEX_NOTATION": "MATH_OCR_SYMBOL",
|
|
106
|
+
"OCR_GARBLED": "OCR_ARTIFACT",
|
|
107
|
+
"MATH_RANGE": "MATH_OCR_SYMBOL",
|
|
108
|
+
"LATEX_ENV": "LATEX_ENVIRONMENT",
|
|
109
|
+
"MATH_SPACE": "MATH_DELIMITER",
|
|
110
|
+
"MATH_LIMIT": "MATH_OCR_STRUCTURE",
|
|
111
|
+
"MATH_RELATION": "MATH_OCR_SYMBOL",
|
|
112
|
+
"MATH_TEXT": "INLINE_MATH",
|
|
113
|
+
"MATH_CONSTANT": "MATH_OCR_SYMBOL",
|
|
114
|
+
"MATH_EXPONENT": "MATH_OCR_SYMBOL",
|
|
115
|
+
"INDEX": "REFERENCE_NUMBERING",
|
|
116
|
+
"OCR_SYMBOL": "MATH_OCR_SYMBOL",
|
|
117
|
+
"LIST_STRUCTURE": "MARKDOWN_LAYOUT",
|
|
118
|
+
"CROSS_REFERENCE": "REFERENCE_NUMBERING",
|
|
119
|
+
"MATH_ARGUMENT": "MATH_OCR_STRUCTURE",
|
|
120
|
+
"STRUCTURE_MARKDOWN": "MARKDOWN_LAYOUT",
|
|
121
|
+
"MATH_PUNCTUATION": "MATH_DELIMITER",
|
|
122
|
+
"LATEX_STRUCTURE": "LATEX_ENVIRONMENT",
|
|
123
|
+
"MATH_BRACKET": "MATH_DELIMITER",
|
|
124
|
+
"PUNCTUATION": "TEXT_OCR",
|
|
125
|
+
"MATH_SIGN": "MATH_OCR_SYMBOL",
|
|
126
|
+
"MATH_STATEMENT": "MATH_SEMANTICS",
|
|
127
|
+
"MATH_LOGIC": "MATH_SEMANTICS",
|
|
128
|
+
"MATH_INTERPOLATION": "MATH_SEMANTICS",
|
|
129
|
+
"MATH_ARROW": "MATH_OCR_SYMBOL",
|
|
130
|
+
"MATH_SET": "MATH_OCR_SYMBOL",
|
|
131
|
+
"VARIABLE": "MATH_OCR_SYMBOL",
|
|
132
|
+
"TEXT_LOGIC": "TEXT_OCR",
|
|
133
|
+
"MATH_NORM": "MATH_SEMANTICS",
|
|
134
|
+
"MATH_ORDER": "MATH_SEMANTICS",
|
|
135
|
+
"MATH_ESTIMATE": "MATH_SEMANTICS",
|
|
136
|
+
"DOMAIN": "MATH_OCR_SYMBOL",
|
|
137
|
+
"OCR_INDEX": "REFERENCE_NUMBERING",
|
|
138
|
+
"MATH_DEFINITION": "MATH_SEMANTICS",
|
|
139
|
+
"MATH_VARIABLE": "MATH_OCR_SYMBOL",
|
|
140
|
+
"INDEX_FORMAT": "REFERENCE_NUMBERING",
|
|
141
|
+
"INDEX_NOTATION": "REFERENCE_NUMBERING",
|
|
142
|
+
"STRUCTURE_HEADING": "HEADING_STRUCTURE",
|
|
143
|
+
"集合": "MATH_OCR_SYMBOL",
|
|
144
|
+
"引号": "TEXT_OCR",
|
|
145
|
+
"Unicode": "UNICODE_LATEX",
|
|
146
|
+
"TABLE_OR_MATRIX": "IMAGE_TABLE",
|
|
147
|
+
"LATEX_SYNTAX": "MATH_OCR_STRUCTURE",
|
|
148
|
+
"MARKDOWN_MATH": "MATH_DELIMITER",
|
|
149
|
+
"LAYOUT_DUPLICATION": "OCR_ARTIFACT",
|
|
150
|
+
"CODE_OCR": "MARKDOWN_LAYOUT",
|
|
151
|
+
"MATH_ALGORITHM_INCONSISTENCY": "MATH_SEMANTICS",
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def normalize_category(raw: str) -> str:
|
|
156
|
+
if raw in CATEGORY_ALIASES:
|
|
157
|
+
return CATEGORY_ALIASES[raw]
|
|
158
|
+
if "语义" in raw or "定义" in raw:
|
|
159
|
+
return "MATH_SEMANTICS"
|
|
160
|
+
if "定界" in raw or "公式边界" in raw:
|
|
161
|
+
return "MATH_DELIMITER"
|
|
162
|
+
if "正文粘连" in raw or "公式文本" in raw or "文本入公式" in raw:
|
|
163
|
+
return "INLINE_MATH"
|
|
164
|
+
if any(token in raw for token in ("矩阵结构", "二维", "公式结构", "数学公式", "练习公式", "公式")):
|
|
165
|
+
return "MATH_OCR_STRUCTURE"
|
|
166
|
+
if any(token in raw for token in ("下标", "数学符号", "数学函数", "数学记号", "数学字体", "算子", "几何符号", "变量")):
|
|
167
|
+
return "MATH_OCR_SYMBOL"
|
|
168
|
+
if any(token in raw for token in ("索引", "引用", "编号")):
|
|
169
|
+
return "REFERENCE_NUMBERING"
|
|
170
|
+
if "标题" in raw or "章节" in raw:
|
|
171
|
+
return "HEADING_STRUCTURE"
|
|
172
|
+
if "图" in raw or "表格" in raw:
|
|
173
|
+
return "IMAGE_TABLE"
|
|
174
|
+
if any(token in raw for token in ("残留", "页眉", "页脚", "版式", "重复", "截断")):
|
|
175
|
+
return "OCR_ARTIFACT"
|
|
176
|
+
if any(token in raw for token in ("代码", "伪代码", "列表", "段落", "格式", "结构", "HTML", "版面")):
|
|
177
|
+
return "MARKDOWN_LAYOUT"
|
|
178
|
+
if any(token in raw for token in ("正文", "标点", "断词", "字符")):
|
|
179
|
+
return "TEXT_OCR"
|
|
180
|
+
if "OCR" in raw:
|
|
181
|
+
return "TEXT_OCR"
|
|
182
|
+
return raw
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def parse_log(path: Path) -> list[dict]:
|
|
186
|
+
text = path.read_text(encoding="utf-8")
|
|
187
|
+
header_match = HEADER_MARKDOWN_RE.search(text)
|
|
188
|
+
header_markdown = header_match.group(1) if header_match else ""
|
|
189
|
+
records: list[dict] = []
|
|
190
|
+
for match in BLOCK_RE.finditer(text):
|
|
191
|
+
record_id = match.group(1).strip()
|
|
192
|
+
if "覆盖确认" in record_id or "问题记录" in record_id:
|
|
193
|
+
continue
|
|
194
|
+
body = match.group(2)
|
|
195
|
+
fields = {key.strip(): value.strip() for key, value in FIELD_RE.findall(body)}
|
|
196
|
+
inline_meta = INLINE_META_RE.search(body)
|
|
197
|
+
if inline_meta:
|
|
198
|
+
fields["类别"] = inline_meta.group(1).strip()
|
|
199
|
+
fields["严重度"] = inline_meta.group(2).strip()
|
|
200
|
+
fields["置信度"] = inline_meta.group(3).strip()
|
|
201
|
+
location_value = fields.get("位置", fields.get("行号", ""))
|
|
202
|
+
location_match = LOCATION_RE.search(location_value)
|
|
203
|
+
if location_match:
|
|
204
|
+
source_path = location_match.group(1)
|
|
205
|
+
line = int(location_match.group(2))
|
|
206
|
+
if header_markdown and not Path(source_path).is_absolute() and Path(source_path).name == Path(header_markdown).name:
|
|
207
|
+
source_path = header_markdown
|
|
208
|
+
elif header_markdown and (line_match := LINE_ONLY_RE.search(location_value)):
|
|
209
|
+
source_path = header_markdown
|
|
210
|
+
line = int(line_match.group(1) or line_match.group(2))
|
|
211
|
+
elif header_markdown and (plain_line_match := PLAIN_LINE_RE.match(location_value)):
|
|
212
|
+
source_path = header_markdown
|
|
213
|
+
line = int(plain_line_match.group(1))
|
|
214
|
+
else:
|
|
215
|
+
source_path = str(path)
|
|
216
|
+
line = 10**12 + len(records)
|
|
217
|
+
pass_match = PASS_RE.search(record_id)
|
|
218
|
+
raw_category_value = fields.get("类别", "未分类")
|
|
219
|
+
backticked_category = re.match(r"`([^`]+)`", raw_category_value)
|
|
220
|
+
raw_category_full = (
|
|
221
|
+
backticked_category.group(1)
|
|
222
|
+
if backticked_category
|
|
223
|
+
else re.split(r"[;;]", raw_category_value, maxsplit=1)[0]
|
|
224
|
+
).strip("` ")
|
|
225
|
+
raw_category = raw_category_full if raw_category_full in CATEGORY_ALIASES else re.split(r"/", raw_category_full, maxsplit=1)[0].strip()
|
|
226
|
+
records.append(
|
|
227
|
+
{
|
|
228
|
+
"id": record_id,
|
|
229
|
+
"pass": pass_match.group(1) if pass_match else path.stem,
|
|
230
|
+
"source_path": source_path,
|
|
231
|
+
"line": line,
|
|
232
|
+
"raw_category": raw_category,
|
|
233
|
+
"category": normalize_category(raw_category),
|
|
234
|
+
"severity": fields.get("严重度", fields.get("严重程度", fields.get("严重级别", "未记录"))),
|
|
235
|
+
"confidence": fields.get("置信度", "未记录"),
|
|
236
|
+
"current": fields.get("当前内容", fields.get("当前", "")),
|
|
237
|
+
"problem": fields.get("问题", fields.get("说明", "")),
|
|
238
|
+
"suggestion": fields.get("建议修复", fields.get("建议", "")),
|
|
239
|
+
"evidence": fields.get("证据", ""),
|
|
240
|
+
"log": str(path.resolve()),
|
|
241
|
+
}
|
|
242
|
+
)
|
|
243
|
+
return records
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def render(records: list[dict], inputs: list[Path]) -> str:
|
|
247
|
+
groups: dict[tuple[str, int], list[dict]] = defaultdict(list)
|
|
248
|
+
for record in records:
|
|
249
|
+
groups[(record["source_path"], record["line"])].append(record)
|
|
250
|
+
|
|
251
|
+
overlaps = 0
|
|
252
|
+
pass_only = Counter()
|
|
253
|
+
categories = Counter(record["category"] for record in records)
|
|
254
|
+
for grouped in groups.values():
|
|
255
|
+
passes = {record["pass"] for record in grouped}
|
|
256
|
+
if len(passes) > 1:
|
|
257
|
+
overlaps += 1
|
|
258
|
+
elif passes:
|
|
259
|
+
pass_only[next(iter(passes))] += 1
|
|
260
|
+
|
|
261
|
+
output = [
|
|
262
|
+
"# after-ocr 多轮日志合并",
|
|
263
|
+
"",
|
|
264
|
+
"## 摘要",
|
|
265
|
+
"",
|
|
266
|
+
f"- 输入日志:{len(inputs)}",
|
|
267
|
+
f"- 原始问题记录:{len(records)}",
|
|
268
|
+
f"- 按文件与行号归并后的位置:{len(groups)}",
|
|
269
|
+
f"- 多轮共同命中的位置:{overlaps}",
|
|
270
|
+
f"- 多轮共同命中率:{overlaps / len(groups):.1%}" if groups else "- 多轮共同命中率:0.0%",
|
|
271
|
+
f"- 单轮命中位置:{sum(pass_only.values())}",
|
|
272
|
+
]
|
|
273
|
+
for pass_name, count in sorted(pass_only.items()):
|
|
274
|
+
output.append(f" - 仅 `{pass_name}` 轮命中:{count}")
|
|
275
|
+
output.extend(["", "### 类别计数", ""])
|
|
276
|
+
for category, count in categories.most_common():
|
|
277
|
+
output.append(f"- `{category}`:{count}")
|
|
278
|
+
|
|
279
|
+
output.extend(["", "## 逐位置记录", ""])
|
|
280
|
+
for index, ((source_path, line), grouped) in enumerate(sorted(groups.items()), start=1):
|
|
281
|
+
output.extend(
|
|
282
|
+
[
|
|
283
|
+
f"### M-{index:04d}",
|
|
284
|
+
"",
|
|
285
|
+
f"- 位置:`{source_path}:{line}`",
|
|
286
|
+
f"- 命中轮次:{', '.join(sorted({record['pass'] for record in grouped}))}",
|
|
287
|
+
f"- 原始编号:{', '.join(record['id'] for record in grouped)}",
|
|
288
|
+
f"- 类别:{', '.join(sorted({record['category'] for record in grouped}))}",
|
|
289
|
+
"",
|
|
290
|
+
]
|
|
291
|
+
)
|
|
292
|
+
for record in grouped:
|
|
293
|
+
output.extend(
|
|
294
|
+
[
|
|
295
|
+
f"#### {record['id']}",
|
|
296
|
+
"",
|
|
297
|
+
f"- 严重度/置信度:{record['severity']} / {record['confidence']}",
|
|
298
|
+
f"- 当前内容:{record['current']}",
|
|
299
|
+
f"- 原始类别:{record['raw_category']}",
|
|
300
|
+
f"- 问题:{record['problem']}",
|
|
301
|
+
f"- 建议修复:{record['suggestion']}",
|
|
302
|
+
f"- 证据:{record['evidence']}",
|
|
303
|
+
"",
|
|
304
|
+
]
|
|
305
|
+
)
|
|
306
|
+
return "\n".join(output).rstrip() + "\n"
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def main() -> int:
|
|
310
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
311
|
+
parser.add_argument("logs", nargs="+", type=Path, help="两份或多份审校日志")
|
|
312
|
+
parser.add_argument("--output", type=Path, help="输出 Markdown;省略时写入标准输出")
|
|
313
|
+
args = parser.parse_args()
|
|
314
|
+
|
|
315
|
+
for path in args.logs:
|
|
316
|
+
if not path.is_file():
|
|
317
|
+
parser.error(f"日志不存在:{path}")
|
|
318
|
+
|
|
319
|
+
records = [record for path in args.logs for record in parse_log(path)]
|
|
320
|
+
rendered = render(records, args.logs)
|
|
321
|
+
if args.output:
|
|
322
|
+
args.output.write_text(rendered, encoding="utf-8")
|
|
323
|
+
else:
|
|
324
|
+
print(rendered, end="")
|
|
325
|
+
return 0
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
if __name__ == "__main__":
|
|
329
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""扫描公式密集型 OCR Markdown,输出需人工确认的候选位置。"""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import json
|
|
8
|
+
import re
|
|
9
|
+
from collections import Counter
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
RULES = [
|
|
14
|
+
(
|
|
15
|
+
"operator-glued",
|
|
16
|
+
"MATH_OCR_SYMBOL",
|
|
17
|
+
"高",
|
|
18
|
+
re.compile(
|
|
19
|
+
r"\\(?:sin|cos|tan|cot|sec|csc|arctan|arcsin|arccos|ln|log|exp)"
|
|
20
|
+
r"(?:x|y|z|t|u|v|w|F|G|H|theta|lambda|alpha|beta|gamma|phi|psi)\b"
|
|
21
|
+
),
|
|
22
|
+
"标准算子可能与变量粘连成未定义命令",
|
|
23
|
+
),
|
|
24
|
+
(
|
|
25
|
+
"spaced-operator",
|
|
26
|
+
"MATH_OCR_SYMBOL",
|
|
27
|
+
"中",
|
|
28
|
+
re.compile(r"\\(?:operatorname\*?|mathrm)\{(?:[A-Za-z]\s+){2,}[A-Za-z](?:\s+[A-Za-z])*\}"),
|
|
29
|
+
"算子或短语可能被 OCR 拆成逐字母空格",
|
|
30
|
+
),
|
|
31
|
+
(
|
|
32
|
+
"split-support",
|
|
33
|
+
"MATH_OCR_SYMBOL",
|
|
34
|
+
"高",
|
|
35
|
+
re.compile(r"\\(?:text|mathrm|operatorname)\{sup\}\s*p\b", re.IGNORECASE),
|
|
36
|
+
"支集算子 supp 可能被拆成 sup p",
|
|
37
|
+
),
|
|
38
|
+
(
|
|
39
|
+
"distribution-space-font",
|
|
40
|
+
"MATH_OCR_SYMBOL",
|
|
41
|
+
"中",
|
|
42
|
+
re.compile(r"\\mathbb\{D\}(?:\s*\^\s*\{?\\?prime\}?)?"),
|
|
43
|
+
"分布空间 D 的花体可能被识别成黑板粗体",
|
|
44
|
+
),
|
|
45
|
+
(
|
|
46
|
+
"accented-digit",
|
|
47
|
+
"MATH_OCR_SYMBOL",
|
|
48
|
+
"高",
|
|
49
|
+
re.compile(r"\\(?:tilde|hat|bar|dot|ddot)\s*\{?\d\}?"),
|
|
50
|
+
"带重音的数字通常来自变量形近误识",
|
|
51
|
+
),
|
|
52
|
+
(
|
|
53
|
+
"epsilon-index-as-digits",
|
|
54
|
+
"MATH_OCR_SYMBOL",
|
|
55
|
+
"中",
|
|
56
|
+
re.compile(r"\bK_\{?8[0Oo]\}?"),
|
|
57
|
+
"邻域或紧集下标 80/8O 可能来自 epsilon_0",
|
|
58
|
+
),
|
|
59
|
+
(
|
|
60
|
+
"plain-real-space",
|
|
61
|
+
"MATH_OCR_SYMBOL",
|
|
62
|
+
"低",
|
|
63
|
+
re.compile(r"(?<!\\mathbf\{)(?<!\\mathbb\{)(?<!\\mathrm\{)(?<![A-Za-z\\])R\s*\^\s*\{?(?:[1-9n])\}?"),
|
|
64
|
+
"实数空间 R^n 可能丢失黑板粗体;若 R 表示环则保留",
|
|
65
|
+
),
|
|
66
|
+
(
|
|
67
|
+
"support-as-complement",
|
|
68
|
+
"MATH_SEMANTICS",
|
|
69
|
+
"高",
|
|
70
|
+
re.compile(r"\\operatorname\{supp\}[^\n=]*=[^\n]*(?:\\neq|!=)\s*0[^\n]*\}\s*\^\s*\{?c\}?"),
|
|
71
|
+
"支集疑似被写成非零点集的补集,需检查闭包和补集符号",
|
|
72
|
+
),
|
|
73
|
+
(
|
|
74
|
+
"laplacian-triangle",
|
|
75
|
+
"MATH_OCR_SYMBOL",
|
|
76
|
+
"中",
|
|
77
|
+
re.compile(r"\\triangle\s*=\s*\\sum"),
|
|
78
|
+
"Laplacian 的大写 Delta 可能被识别成 triangle 命令",
|
|
79
|
+
),
|
|
80
|
+
(
|
|
81
|
+
"notin-corruption",
|
|
82
|
+
"MATH_OCR_SYMBOL",
|
|
83
|
+
"高",
|
|
84
|
+
re.compile(r"\\(?:bar|overline)\{\\in\}|\\mathrm\{[^}]*bar[^}]*e[^}]*\}"),
|
|
85
|
+
"不属于号可能被识别成带横线的属于号或字母残片",
|
|
86
|
+
),
|
|
87
|
+
(
|
|
88
|
+
"pandoc-bracket",
|
|
89
|
+
"UNICODE_LATEX",
|
|
90
|
+
"中",
|
|
91
|
+
re.compile(r"\{\[\}|\{\]\}|\\text(?:less|greater|bar)"),
|
|
92
|
+
"存在 Pandoc/HTML 转义残留",
|
|
93
|
+
),
|
|
94
|
+
(
|
|
95
|
+
"escaped-script",
|
|
96
|
+
"UNICODE_LATEX",
|
|
97
|
+
"中",
|
|
98
|
+
re.compile(r"\\_[{]|\^\{\}\{|\\\{[A-Za-z0-9]+\\_"),
|
|
99
|
+
"上下标或集合可能保留 Markdown 转义",
|
|
100
|
+
),
|
|
101
|
+
(
|
|
102
|
+
"unicode-math",
|
|
103
|
+
"UNICODE_LATEX",
|
|
104
|
+
"低",
|
|
105
|
+
re.compile(r"[∞≤≥∑∏∫∂∇∈∉→←↔±∁ℒℝℕ]"),
|
|
106
|
+
"Unicode 数学符号与 LaTeX 可能混用",
|
|
107
|
+
),
|
|
108
|
+
(
|
|
109
|
+
"weak-lp-comma-split",
|
|
110
|
+
"MATH_OCR_STRUCTURE",
|
|
111
|
+
"中",
|
|
112
|
+
re.compile(r"L\s*\^\s*\{\s*p\s*\}\s*,\s*\\infty", re.IGNORECASE),
|
|
113
|
+
"弱 L^p/Lorentz 空间的逗号疑似逃出上标花括号",
|
|
114
|
+
),
|
|
115
|
+
(
|
|
116
|
+
"split-roman",
|
|
117
|
+
"TEXT_OCR",
|
|
118
|
+
"低",
|
|
119
|
+
re.compile(r"\(i\s+i(?:\s+i)?\)", re.IGNORECASE),
|
|
120
|
+
"罗马编号可能被拆开",
|
|
121
|
+
),
|
|
122
|
+
(
|
|
123
|
+
"repeated-artifact",
|
|
124
|
+
"OCR_ARTIFACT",
|
|
125
|
+
"中",
|
|
126
|
+
re.compile(r"(?<!\d)([01Il])\1{7,}(?!\d)"),
|
|
127
|
+
"长重复字符可能来自图像、页眉或扫描噪声",
|
|
128
|
+
),
|
|
129
|
+
(
|
|
130
|
+
"heading-number-residue",
|
|
131
|
+
"HEADING_STRUCTURE",
|
|
132
|
+
"低",
|
|
133
|
+
re.compile(r"^#{1,6}\s*(?:专题|第\s*\d+\s*[章节篇]|§+\s*\d+|Chapter\s+\d+)", re.IGNORECASE),
|
|
134
|
+
"标题可能保留原书编号,需检查是否与渲染编号重复",
|
|
135
|
+
),
|
|
136
|
+
(
|
|
137
|
+
"text-symbol-in-math",
|
|
138
|
+
"INLINE_MATH",
|
|
139
|
+
"低",
|
|
140
|
+
re.compile(r"(?<!\\)\$\s*[①-⑳]+\s*\$"),
|
|
141
|
+
"圈号等正文符号可能被放入数学模式",
|
|
142
|
+
),
|
|
143
|
+
(
|
|
144
|
+
"adjacent-inline-math",
|
|
145
|
+
"MATH_DELIMITER",
|
|
146
|
+
"中",
|
|
147
|
+
re.compile(r"(?<!\$)\$(?!\$)[^$\n]{1,100}\$(?!\$)\s+(?<!\$)\$(?!\$)[^$\n]{1,100}\$(?!\$)"),
|
|
148
|
+
"由空白分隔的相邻数学片段可能属于同一公式",
|
|
149
|
+
),
|
|
150
|
+
(
|
|
151
|
+
"block-environment-in-inline-math",
|
|
152
|
+
"MATH_OCR_STRUCTURE",
|
|
153
|
+
"中",
|
|
154
|
+
re.compile(r"(?<!\$)\$(?!\$)[^$\n]*\\begin\{(?:cases|aligned|array|matrix|pmatrix|bmatrix|vmatrix)\}"),
|
|
155
|
+
"二维 LaTeX 环境可能被单美元行内数学包围",
|
|
156
|
+
),
|
|
157
|
+
(
|
|
158
|
+
"repeated-english-word",
|
|
159
|
+
"TEXT_OCR",
|
|
160
|
+
"低",
|
|
161
|
+
re.compile(r"\b([A-Za-z]{2,})\s+\1\b", re.IGNORECASE),
|
|
162
|
+
"相邻英文单词重复,可能来自 OCR 拼接",
|
|
163
|
+
),
|
|
164
|
+
]
|
|
165
|
+
|
|
166
|
+
BEGIN_RE = re.compile(r"\\begin\{([^}]+)\}")
|
|
167
|
+
END_RE = re.compile(r"\\end\{([^}]+)\}")
|
|
168
|
+
UNESCAPED_DOLLAR_RE = re.compile(r"(?<!\\)\$")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def issue(line: int, column: int, rule: str, category: str, severity: str, message: str, excerpt: str) -> dict:
|
|
172
|
+
return {
|
|
173
|
+
"line": line,
|
|
174
|
+
"column": column,
|
|
175
|
+
"rule": rule,
|
|
176
|
+
"category": category,
|
|
177
|
+
"severity": severity,
|
|
178
|
+
"message": message,
|
|
179
|
+
"excerpt": excerpt.rstrip("\n")[:240],
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def scan(path: Path) -> tuple[list[dict], dict]:
|
|
184
|
+
lines = path.read_text(encoding="utf-8").splitlines(keepends=True)
|
|
185
|
+
findings: list[dict] = []
|
|
186
|
+
env_stack: list[tuple[str, int]] = []
|
|
187
|
+
display_open_line: int | None = None
|
|
188
|
+
total_dollars = 0
|
|
189
|
+
total_left = 0
|
|
190
|
+
total_right = 0
|
|
191
|
+
|
|
192
|
+
for number, text in enumerate(lines, start=1):
|
|
193
|
+
for rule_id, category, severity, pattern, message in RULES:
|
|
194
|
+
for match in pattern.finditer(text):
|
|
195
|
+
findings.append(issue(number, match.start() + 1, rule_id, category, severity, message, text))
|
|
196
|
+
|
|
197
|
+
dollars = list(UNESCAPED_DOLLAR_RE.finditer(text))
|
|
198
|
+
total_dollars += len(dollars)
|
|
199
|
+
if len(dollars) % 2 == 1:
|
|
200
|
+
findings.append(
|
|
201
|
+
issue(number, dollars[-1].start() + 1, "odd-dollar-line", "MATH_DELIMITER", "高", "本行含奇数个未转义美元符号", text)
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
single_dollars = [
|
|
205
|
+
match
|
|
206
|
+
for match in dollars
|
|
207
|
+
if (match.start() == 0 or text[match.start() - 1] != "$")
|
|
208
|
+
and (match.end() >= len(text) or text[match.end()] != "$")
|
|
209
|
+
]
|
|
210
|
+
for opening, closing in zip(single_dollars[0::2], single_dollars[1::2]):
|
|
211
|
+
content = text[opening.end() : closing.start()]
|
|
212
|
+
if re.fullmatch(r"\s*[A-Za-z][A-Za-z' -]{3,}\s*", content):
|
|
213
|
+
findings.append(
|
|
214
|
+
issue(
|
|
215
|
+
number,
|
|
216
|
+
opening.start() + 1,
|
|
217
|
+
"plain-words-in-inline-math",
|
|
218
|
+
"INLINE_MATH",
|
|
219
|
+
"低",
|
|
220
|
+
"纯英文词或短语可能误入行内数学模式",
|
|
221
|
+
text,
|
|
222
|
+
)
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
double_count = len(re.findall(r"(?<!\\)\$\$", text))
|
|
226
|
+
if double_count % 2 == 1:
|
|
227
|
+
if display_open_line is None:
|
|
228
|
+
display_open_line = number
|
|
229
|
+
else:
|
|
230
|
+
display_open_line = None
|
|
231
|
+
|
|
232
|
+
begins = [(m.start(), "begin", m.group(1)) for m in BEGIN_RE.finditer(text)]
|
|
233
|
+
ends = [(m.start(), "end", m.group(1)) for m in END_RE.finditer(text)]
|
|
234
|
+
for column, kind, env in sorted(begins + ends):
|
|
235
|
+
if kind == "begin":
|
|
236
|
+
env_stack.append((env, number))
|
|
237
|
+
elif not env_stack:
|
|
238
|
+
findings.append(issue(number, column + 1, "orphan-end", "LATEX_ENVIRONMENT", "高", f"发现无对应 begin 的 end{{{env}}}", text))
|
|
239
|
+
else:
|
|
240
|
+
expected, begin_line = env_stack.pop()
|
|
241
|
+
if expected != env:
|
|
242
|
+
findings.append(
|
|
243
|
+
issue(
|
|
244
|
+
number,
|
|
245
|
+
column + 1,
|
|
246
|
+
"environment-mismatch",
|
|
247
|
+
"LATEX_ENVIRONMENT",
|
|
248
|
+
"高",
|
|
249
|
+
f"环境从第 {begin_line} 行 begin{{{expected}}} 开始,却由 end{{{env}}} 关闭",
|
|
250
|
+
text,
|
|
251
|
+
)
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
left_count = len(re.findall(r"\\left(?:\b|[\[\]().|{}])", text))
|
|
255
|
+
right_count = len(re.findall(r"\\right(?:\b|[\[\]().|{}])", text))
|
|
256
|
+
total_left += left_count
|
|
257
|
+
total_right += right_count
|
|
258
|
+
if left_count != right_count and (left_count or right_count):
|
|
259
|
+
findings.append(issue(number, 1, "line-left-right", "MATH_DELIMITER", "中", "本行的 \\left 与 \\right 数量不同,需结合多行公式检查", text))
|
|
260
|
+
|
|
261
|
+
joined = "".join(lines)
|
|
262
|
+
if "\\mathcal{Q}" in joined and "\\Omega" in joined:
|
|
263
|
+
first_q_line = next(number for number, text in enumerate(lines, start=1) if "\\mathcal{Q}" in text)
|
|
264
|
+
findings.append(
|
|
265
|
+
issue(
|
|
266
|
+
first_q_line,
|
|
267
|
+
lines[first_q_line - 1].find("\\mathcal{Q}") + 1,
|
|
268
|
+
"domain-symbol-inconsistency",
|
|
269
|
+
"MATH_OCR_SYMBOL",
|
|
270
|
+
"中",
|
|
271
|
+
"全文同时出现 \\mathcal{Q} 与 \\Omega;若二者指同一开集,需全局核对形近误识",
|
|
272
|
+
lines[first_q_line - 1],
|
|
273
|
+
)
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
if total_dollars % 2 == 1:
|
|
277
|
+
findings.append(issue(len(lines), 1, "global-dollar-imbalance", "MATH_DELIMITER", "阻断", "全文未转义美元符号总数为奇数", ""))
|
|
278
|
+
if display_open_line is not None:
|
|
279
|
+
findings.append(issue(display_open_line, 1, "unclosed-display-math", "MATH_DELIMITER", "阻断", "由此开始的 $$ 显示数学未闭合", lines[display_open_line - 1]))
|
|
280
|
+
for env, begin_line in env_stack:
|
|
281
|
+
findings.append(issue(begin_line, 1, "unclosed-environment", "LATEX_ENVIRONMENT", "阻断", f"begin{{{env}}} 未闭合", lines[begin_line - 1]))
|
|
282
|
+
if total_left != total_right:
|
|
283
|
+
findings.append(issue(len(lines), 1, "global-left-right", "MATH_DELIMITER", "高", f"全文 \\left={total_left},\\right={total_right}", ""))
|
|
284
|
+
|
|
285
|
+
findings.sort(key=lambda item: (item["line"], item["column"], item["rule"]))
|
|
286
|
+
summary = {
|
|
287
|
+
"path": str(path.resolve()),
|
|
288
|
+
"lines": len(lines),
|
|
289
|
+
"findings": len(findings),
|
|
290
|
+
"by_category": dict(sorted(Counter(item["category"] for item in findings).items())),
|
|
291
|
+
"by_rule": dict(sorted(Counter(item["rule"] for item in findings).items())),
|
|
292
|
+
}
|
|
293
|
+
return findings, summary
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def render_markdown(findings: list[dict], summary: dict) -> str:
|
|
297
|
+
output = [
|
|
298
|
+
"# OCR Markdown 候选扫描",
|
|
299
|
+
"",
|
|
300
|
+
f"- 文件:`{summary['path']}`",
|
|
301
|
+
f"- 总行数:{summary['lines']}",
|
|
302
|
+
f"- 目标行段:{summary['target_range']}",
|
|
303
|
+
f"- 候选数:{summary['findings']}",
|
|
304
|
+
"",
|
|
305
|
+
]
|
|
306
|
+
for item in findings:
|
|
307
|
+
output.extend(
|
|
308
|
+
[
|
|
309
|
+
f"- `{item['line']}:{item['column']}` `{item['category']}` `{item['rule']}` `{item['severity']}`:{item['message']}",
|
|
310
|
+
f" - `{item['excerpt'].replace('`', 'ˋ')}`",
|
|
311
|
+
]
|
|
312
|
+
)
|
|
313
|
+
return "\n".join(output) + "\n"
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def main() -> int:
|
|
317
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
318
|
+
parser.add_argument("input", type=Path, help="待扫描的 Markdown 绝对或相对路径")
|
|
319
|
+
parser.add_argument("--format", choices=("json", "markdown"), default="markdown")
|
|
320
|
+
parser.add_argument("--output", type=Path, help="输出文件;省略时写入标准输出")
|
|
321
|
+
parser.add_argument("--start-line", type=int, default=1, help="仅输出从该行开始的候选,默认 1")
|
|
322
|
+
parser.add_argument("--end-line", type=int, help="仅输出到该行结束的候选,默认文件末行")
|
|
323
|
+
args = parser.parse_args()
|
|
324
|
+
|
|
325
|
+
if not args.input.is_file():
|
|
326
|
+
parser.error(f"输入文件不存在:{args.input}")
|
|
327
|
+
|
|
328
|
+
findings, summary = scan(args.input)
|
|
329
|
+
end_line = args.end_line if args.end_line is not None else summary["lines"]
|
|
330
|
+
if args.start_line < 1 or end_line < args.start_line or end_line > summary["lines"]:
|
|
331
|
+
parser.error(f"无效行段:{args.start_line}-{end_line};文件总行数 {summary['lines']}")
|
|
332
|
+
findings = [item for item in findings if args.start_line <= item["line"] <= end_line]
|
|
333
|
+
summary["target_range"] = f"{args.start_line}-{end_line}"
|
|
334
|
+
summary["findings"] = len(findings)
|
|
335
|
+
summary["by_category"] = dict(sorted(Counter(item["category"] for item in findings).items()))
|
|
336
|
+
summary["by_rule"] = dict(sorted(Counter(item["rule"] for item in findings).items()))
|
|
337
|
+
if args.format == "json":
|
|
338
|
+
rendered = json.dumps({"summary": summary, "findings": findings}, ensure_ascii=False, indent=2) + "\n"
|
|
339
|
+
else:
|
|
340
|
+
rendered = render_markdown(findings, summary)
|
|
341
|
+
|
|
342
|
+
if args.output:
|
|
343
|
+
args.output.write_text(rendered, encoding="utf-8")
|
|
344
|
+
else:
|
|
345
|
+
print(rendered, end="")
|
|
346
|
+
return 0
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
if __name__ == "__main__":
|
|
350
|
+
raise SystemExit(main())
|