Compare commits
4
Commits
20e9be32db
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56a7c2b416 | ||
|
|
f48c95d0df | ||
|
|
d5f7202bf3 | ||
|
|
00ac418796 |
No files matched your search
+1981
-6
File diff suppressed because it is too large.
Load diff
@@ -1,5 +1,3 @@
|
|||||||
beautifulsoup4>=4.12
|
beautifulsoup4>=4.12
|
||||||
deep-translator>=1.11
|
deep-translator>=1.11
|
||||||
lxml>=5.0
|
lxml>=5.0
|
||||||
requests>=2.30
|
|
||||||
urllib3>=2.0
|
|
||||||
+35
-2
@@ -32,6 +32,12 @@ MAX_CHUNK = 4000
|
|||||||
DELAY = 0.3
|
DELAY = 0.3
|
||||||
SENTENCE_RE = re.compile(r'(?<=[.!?])\s+')
|
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__))
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
SOURCE_DIR = os.path.join(BASE_DIR, 'original')
|
SOURCE_DIR = os.path.join(BASE_DIR, 'original')
|
||||||
TRANSLATED_DIR = os.path.join(BASE_DIR, 'translated')
|
TRANSLATED_DIR = os.path.join(BASE_DIR, 'translated')
|
||||||
@@ -41,11 +47,38 @@ def log(msg):
|
|||||||
print(msg, flush=True)
|
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):
|
def translate_text(text):
|
||||||
"""Translate using deep_translator."""
|
"""Translate using deep_translator, preserving chapter numbers."""
|
||||||
|
clean_text, protected = protect_numerals(text)
|
||||||
try:
|
try:
|
||||||
translator = GoogleTranslator(source='en', target='id')
|
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
|
return result if result else None
|
||||||
except Exception:
|
except Exception:
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in new issue
Block a user