Compare commits

...
2 Commits
Author SHA1 Message Date
proitlab 56a7c2b416 Update Session 2026-07-25 09:51:32 +07:00
proitlab f48c95d0df Fix chapter translation error 2026-07-25 09:51:07 +07:00
2 changed files with 1440 additions and 8 deletions

No files matched your search

+1405 -6
View File
File diff suppressed because it is too large. Load diff
+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