In @wraplines.py , is there any issue in the logic of the function wrap? When there is a…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
In @wrap_lines.py , is there any issue in the logic of the function
wrap? When there is aforbidden_endspunctuation then immediately a English word, can it prevent the punctuation from appearing before a line break? --- Content from referenced files --- Content from @wrap_lines.py: #!/usr/bin/env python3import json import os import unicodedata
from opencc import OpenCC from tqdm import tqdm
s2t = OpenCC("s2t") t2jp = OpenCC("t2jp")
def s2jp(s: str) -> str: char_map = { "嗯": "恩", "篡": "竄", "牀": "床", "裏": "里", "瀨": "瀬", "彥": "彦", }
codes = s2t.convert(s) s = t2jp.convert(s) for k, v in char_map.items(): s = s.replace(k, v) return sdef get_char_width(char: str) -> int: """ Returns 2 for East Asian wide characters (Chinese, etc.), and 1 for others (English, ASCII). """ if unicodedata.east_asian_width(char) in ("F", "W", "A"): return 2 return 1
def wrap(text: str, max_cn_chars: int = 33) -> list[str]: """ Wraps text preventing specific punctuations (single or multiple) from appearing before or after a line break. Also preventing line breaks in the middle of English words or numbers. """ limit = max_cn_chars * 2 lines = [] current_line = "" current_width = 0
code Assistant
I will verify the issue by creating a reproduction script
reproduce_issue.pythat tests thewrapfunction with a specific case where aforbidden_endscharacter is followed by an English word that forces a line break.Assistant
I will run the reproduction script to confirm the issue.
Assistant
I will read
wrap_lines.pyto capture the exact context needed for applying the fix using thereplacetool.Assistant
I will apply the fix to
wrap_lines.pyby adding the backtracking logic to the ASCII word handling block, ensuring forbidden characters are moved to the next line when wrapping.Assistant
I will verify the fix by running the reproduction script again.
Assistant
I will remove the reproduction script
reproduce_issue.pynow that the fix has been verified.Assistant
Yes, there was an issue in the
wrapfunction.The logic for handling ASCII words (English words/numbers) was missing the "backtracking" check that exists for normal characters. Specifically, when an English word forced a line break because it didn't fit, the function immediately wrapped the line without checking if the previous line ended with a
forbidden_endscharacter (like(,[, etc.).This meant that if you had text like
abc (Wordand the wrap happened right beforeWord, the result would be:textabc ( Wordleaving the opening parenthesis at the end of the line.
I have fixed
wrap_lines.pyby duplicating the backtracking logic into the ASCII word handling block. It now correctly moves the forbidden character to the next line:textabc (WordI verified this fix with a reproduction script.
- User
Assistant
I will run
test_refactor.pyto ensure the new implementation behaves identically to the fixed original version.Assistant
The refactored version matches the behavior of the original (fixed) version. Now I can confidently replace the content of
wrap_lines.pywith the simplified version.I will read
wrap_lines.pycompletely to ensure I replace the file content correctly, preserving imports and themainfunction (which I didn't verify but looks independent). Wait, I already read most of it. I need to make sure I don't deletemain. Thewrapfunction is defined, thenmainuses it. I will verify the structure again.The
mainfunction callswrap(zh_msg). The defaultmax_cn_charsis 33. Themainfunction does:pythonzh_msg = s2jp(zh_msg) lines = wrap(zh_msg) for i in range(len(lines) - 1): lines[i] += "\r\n"So I just need to replace the
wrapfunction and add the helpers. I will also clean up theforbidden_...definitions by moving them to module level constants as planned.Assistant
I will replace the
wrapfunction and the hardcoded sets with the new helper functions and the cleanerwrapimplementation.Assistant
I will clean up the temporary test file.
Assistant
I have simplified and optimized the
wrapfunction inwrap_lines.py.The changes include:
- Helper Functions: Extracted
is_ascii_alnum,get_token(forward scanning), andpop_token(backward scanning) to handle text processing units (words vs characters) consistently. - Unified Logic: The main loop now tokenizes the text and processes it uniformly, removing the duplicated logic for "normal characters" vs "ASCII words".
- Backtracking Extraction: The backtracking logic (moving forbidden characters to the next line) is now centralized and uses
pop_tokento correctly handle moving whole words or single characters. - Module Constants: Moved
forbidden_endsandforbidden_startsto module-level constantsFORBIDDEN_ENDSandFORBIDDEN_STARTSto avoid re-creation on every function call.
I verified the refactored code against the original (fixed) logic using a test script covering various edge cases, ensuring no regression.
- Helper Functions: Extracted