Update app.py
Browse files
app.py
CHANGED
|
@@ -43,23 +43,23 @@ lang_dict = {
|
|
| 43 |
lang_list = list(lang_dict.keys())
|
| 44 |
|
| 45 |
def split_html_content(text):
|
| 46 |
-
"""Separa etiquetas HTML y
|
| 47 |
-
# Expresión regular
|
| 48 |
-
pattern = r'(<[^>]
|
| 49 |
parts = []
|
| 50 |
|
| 51 |
for match in re.finditer(pattern, text, re.DOTALL):
|
| 52 |
-
if match.group(1): # Elemento HTML completo
|
| 53 |
html_tag = match.group(1)
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
content = content_match.group(1).strip()
|
| 57 |
opening = html_tag[:content_match.start(1) + 1]
|
| 58 |
closing = html_tag[content_match.end(1):]
|
| 59 |
-
parts.append(('
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
parts.append(('html_close', closing))
|
| 63 |
else:
|
| 64 |
parts.append(('html', html_tag))
|
| 65 |
elif match.group(2): # Texto fuera de etiquetas
|
|
@@ -83,28 +83,36 @@ def split_text(text, limit=4000):
|
|
| 83 |
return chunks
|
| 84 |
|
| 85 |
def translate_text(source_lang, target_lang, text):
|
| 86 |
-
"""Traduce contenido manteniendo etiquetas HTML"""
|
| 87 |
source_code = lang_dict[source_lang]
|
| 88 |
target_code = lang_dict[target_lang]
|
| 89 |
|
| 90 |
# Separar HTML y contenido
|
| 91 |
parts = split_html_content(text)
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# Traducir solo las partes de texto
|
| 94 |
translated_parts = []
|
| 95 |
for part_type, content in parts:
|
| 96 |
-
if part_type == 'text' and content:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
else:
|
| 104 |
# Mantener etiquetas HTML sin cambios
|
| 105 |
translated_parts.append(content)
|
| 106 |
|
| 107 |
-
# Unir las partes sin añadir espacios adicionales innecesarios
|
| 108 |
return ''.join(translated_parts)
|
| 109 |
|
| 110 |
def main(Texto, source_lang, target_lang):
|
|
|
|
| 43 |
lang_list = list(lang_dict.keys())
|
| 44 |
|
| 45 |
def split_html_content(text):
|
| 46 |
+
"""Separa etiquetas HTML completas y texto traducible"""
|
| 47 |
+
# Expresión regular para capturar etiquetas HTML completas y texto fuera de ellas
|
| 48 |
+
pattern = r'(<[^>]+(?:>.*?</[^>]+>|>))|([^<]+)'
|
| 49 |
parts = []
|
| 50 |
|
| 51 |
for match in re.finditer(pattern, text, re.DOTALL):
|
| 52 |
+
if match.group(1): # Elemento HTML completo (incluye etiquetas y contenido interno)
|
| 53 |
html_tag = match.group(1)
|
| 54 |
+
# Si tiene contenido interno, separarlo
|
| 55 |
+
content_match = re.search(r'>(.*?)</', html_tag, re.DOTALL)
|
| 56 |
+
if content_match and content_match.group(1).strip():
|
| 57 |
content = content_match.group(1).strip()
|
| 58 |
opening = html_tag[:content_match.start(1) + 1]
|
| 59 |
closing = html_tag[content_match.end(1):]
|
| 60 |
+
parts.append(('html', opening))
|
| 61 |
+
parts.append(('text', content))
|
| 62 |
+
parts.append(('html', closing))
|
|
|
|
| 63 |
else:
|
| 64 |
parts.append(('html', html_tag))
|
| 65 |
elif match.group(2): # Texto fuera de etiquetas
|
|
|
|
| 83 |
return chunks
|
| 84 |
|
| 85 |
def translate_text(source_lang, target_lang, text):
|
| 86 |
+
"""Traduce contenido manteniendo etiquetas HTML intactas"""
|
| 87 |
source_code = lang_dict[source_lang]
|
| 88 |
target_code = lang_dict[target_lang]
|
| 89 |
|
| 90 |
# Separar HTML y contenido
|
| 91 |
parts = split_html_content(text)
|
| 92 |
|
| 93 |
+
# Lista de términos que no deben traducirse (nombres propios, marcas, etc.)
|
| 94 |
+
preserve_terms = {'DGM Services', 'Alexandru George Bratosin', 'X2596200v', 'info@dgm-services.com',
|
| 95 |
+
'https://dgm-services.com/', 'Calle Federico García Lorca', '722 17 99 13'}
|
| 96 |
+
|
| 97 |
# Traducir solo las partes de texto
|
| 98 |
translated_parts = []
|
| 99 |
for part_type, content in parts:
|
| 100 |
+
if part_type == 'text' and content:
|
| 101 |
+
# Verificar si el contenido está en la lista de términos a preservar
|
| 102 |
+
if content in preserve_terms:
|
| 103 |
+
translated_parts.append(content)
|
| 104 |
+
else:
|
| 105 |
+
# Dividir en fragmentos si es necesario
|
| 106 |
+
chunks = split_text(content)
|
| 107 |
+
translated_chunks = [
|
| 108 |
+
translate(chunk, target_code, source_code)
|
| 109 |
+
for chunk in chunks
|
| 110 |
+
]
|
| 111 |
+
translated_parts.append(''.join(translated_chunks))
|
| 112 |
else:
|
| 113 |
# Mantener etiquetas HTML sin cambios
|
| 114 |
translated_parts.append(content)
|
| 115 |
|
|
|
|
| 116 |
return ''.join(translated_parts)
|
| 117 |
|
| 118 |
def main(Texto, source_lang, target_lang):
|