fix: preserve unchanged paragraph compositions - #611
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
There was a problem hiding this comment.
1 issue found across 2 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="babeldoc/format/pdf/document_il/midend/il_translator.py">
<violation number="1" location="babeldoc/format/pdf/document_il/midend/il_translator.py:1000">
P3: The normalization pattern `[. 。…,]{20,}` is now duplicated a third time (it already exists at il_translator.py:1268 and twice in il_translator_llm_only.py:754/763). Keep the regex in one shared module-level constant so the unchanged-output comparison stays in sync with the pre/post normalization applied by the two callers.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| """Post-translation processing: update paragraph with translated text.""" | ||
| tracker.set_output(translated_text) | ||
| if translated_text == translate_input: | ||
| normalized_input = re.sub( |
There was a problem hiding this comment.
P3: The normalization pattern [. 。…,]{20,} is now duplicated a third time (it already exists at il_translator.py:1268 and twice in il_translator_llm_only.py:754/763). Keep the regex in one shared module-level constant so the unchanged-output comparison stays in sync with the pre/post normalization applied by the two callers.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At babeldoc/format/pdf/document_il/midend/il_translator.py, line 1000:
<comment>The normalization pattern `[. 。…,]{20,}` is now duplicated a third time (it already exists at il_translator.py:1268 and twice in il_translator_llm_only.py:754/763). Keep the regex in one shared module-level constant so the unchanged-output comparison stays in sync with the pre/post normalization applied by the two callers.</comment>
<file context>
@@ -997,7 +997,12 @@ def post_translate_paragraph(
"""Post-translation processing: update paragraph with translated text."""
tracker.set_output(translated_text)
- if translated_text == translate_input.unicode:
+ normalized_input = re.sub(
+ r"[. 。…,]{20,}",
+ ".",
</file context>
There was a problem hiding this comment.
Fixed in be8b888. The repeated-punctuation rule is now the module-level EXCESSIVE_PUNCTUATION_RE in il_translator; the unchanged-output comparison, standard translator cleanup, and both LLM-only normalization sites all use that single compiled regex. The now-unused re import was removed from il_translator_llm_only.py. The focused regression and Ruff checks both pass.
Related Issue(s)
#610
Description
post_translate_paragraphcompared the translatedstrwith theTranslateInputobject, so the unchanged-output branch could never run for a normal input. This patch compares withtranslate_input.unicodeinstead.When a translator intentionally returns the source text unchanged, BabelDOC now returns
Falsefrom post-processing and keeps the paragraph's original composition objects rather than rebuilding and re-typesetting them.A focused unit regression verifies the return value, tracker output, unicode, and composition object identity. No prompt, public API, dependency, or generated IL file changes are included.
PR Type
Contributor Checklist
Testing Instructions
Local result: 1 test passed; Ruff reported all checks passed.
Additional Notes
The downstream evidence came from a local copyrighted scientific manuscript, but this PR includes and requires no source PDF or translated output.
Summary by cubic
Preserves a paragraph’s existing composition when the translator returns the source text unchanged. Old: unchanged paragraphs were rebuilt and marked changed; New: we detect unchanged text after normalizing 20+ punctuation runs, return False from post-processing, keep the original composition, set tracker output, and mark
placeholder_full_match.translated_texttotranslate_input.unicodeafter collapsing[. 。…,]{20,}to "." using sharedEXCESSIVE_PUNCTUATION_REin bothil_translatorandil_translator_llm_onlyto unify normalization and token counts.Written for commit be8b888. Summary will update on new commits.