Fix chapter translation error

This commit is contained in:
proitlab committed 2026-07-25 09:51:07 +07:00
1 parent d5f7202bf3
commit f48c95d0df
1 file changed
+35 -2
+35 -2
View File
@@ -32,6 +32,12 @@ MAX_CHUNK = 4000
DELAY = 0.3
SENTENCE_RE = re.compile(r'(?<=[.!?])\s+')
# Patterns to protect from translation
ROMAN_NUMERAL = re.compile(r'\b([IVXLCDM]+)\b', re.IGNORECASE)
CHAPTER_PREFIX = re.compile(r'(Chapter\s+)([IVXLCDM]+|\d+)', re.IGNORECASE)
ROMAN_STANDALONE = re.compile(r'^\s*([IVXLCDM]+)[.\s]*$', re.IGNORECASE)
PLACEHOLDER_RE = re.compile(r'xxKEEP(\d+)xx')
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
SOURCE_DIR = os.path.join(BASE_DIR, 'original')
TRANSLATED_DIR = os.path.join(BASE_DIR, 'translated')
@@ -41,11 +47,38 @@ def log(msg):
print(msg, flush=True)
def protect_numerals(text):
"""Replace chapter numbers / Roman numerals with placeholders."""
protected = []
def _store(val):
protected.append(val)
return f'xxKEEP{len(protected)-1}xx'
# Protect "Chapter X" - only replace the numeral, keep "Chapter" for translation
text = CHAPTER_PREFIX.sub(lambda m: m.group(1) + _store(m.group(2)), text)
# Protect standalone Roman numerals (chapter headings)
text = ROMAN_STANDALONE.sub(lambda m: _store(m.group(1)), text)
# Protect remaining Roman numerals in text
text = ROMAN_NUMERAL.sub(lambda m: _store(m.group(1)), text)
return text, protected
def restore_numerals(text, protected):
"""Restore original numerals from placeholders."""
def _restore(m):
idx = int(m.group(1))
return protected[idx] if idx < len(protected) else m.group(0)
return PLACEHOLDER_RE.sub(_restore, text)
def translate_text(text):
"""Translate using deep_translator."""
"""Translate using deep_translator, preserving chapter numbers."""
clean_text, protected = protect_numerals(text)
try:
translator = GoogleTranslator(source='en', target='id')
result = translator.translate(text)
result = translator.translate(clean_text)
if result and protected:
result = restore_numerals(result, protected)
return result if result else None
except Exception:
return None