From 4475ebb04b4f0cf589d3760db480b11edf0cc360 Mon Sep 17 00:00:00 2001 From: rafa Date: Tue, 11 Aug 2026 21:57:37 -0400 Subject: [PATCH] =?UTF-8?q?Dar=20contexto=20al=20traducir=20t=C3=ADtulos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: rafa Signed-off-by: rafa --- scripts/translate_post.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/scripts/translate_post.py b/scripts/translate_post.py index 4fe17b6..1d6c24a 100644 --- a/scripts/translate_post.py +++ b/scripts/translate_post.py @@ -176,7 +176,7 @@ def _system_prompt(lang: str) -> str: ) -def translate_text(text: str, lang: str, *, is_title: bool = False) -> str: +def translate_text(text: str, lang: str, *, is_title: bool = False, context: str = "") -> str: text = text.strip() if not text: return "" @@ -185,6 +185,13 @@ def translate_text(text: str, lang: str, *, is_title: bool = False) -> str: return out user = f"<<>>{text}<<>>" if is_title: + context_block = "" + if context: + context_block = ( + "\nBODY CONTEXT (for meaning only; do not translate or include it):\n" + f"<<>>{context}<<>>\n" + ) + user = context_block + user kind = "el TÍTULO" task = ( f"Traduce {kind} que va entre las marcas.\n" @@ -192,6 +199,7 @@ def translate_text(text: str, lang: str, *, is_title: bool = False) -> str: f"No lo dejes en inglés salvo que el original ya sea un nombre propio o una marca.\n" f"Responde solo con el título traducido entre las marcas:\n{user}" ) + task += "\nSi es un modismo, interpreta su sentido segun el contexto, no lo calques." else: kind = "el texto" task = f"Traduce {kind} que va entre las marcas:\n{user}" @@ -226,6 +234,13 @@ def translate_html(html: str, lang: str) -> str: # ── Datos / estado ─────────────────────────────────────────────────────────── +def title_context(html: str) -> str: + """First paragraph gives title idioms enough meaning without bloating the prompt.""" + first = re.search(r"]*>.*?

", html, flags=re.IGNORECASE | re.DOTALL) + context = first.group(0) if first else html + return re.sub(r"\s+", " ", context).strip()[:2000] + + def read_post(post_id: int) -> dict: return json.loads(php_helper("read", str(post_id))) @@ -290,8 +305,8 @@ def process_post(post_id: int, langs: list[str], status: str, force: bool, state log(f" {lang}: --force, eliminada traducción previa #{existing}") try: t0 = time.time() - title = translate_text(src["title"], lang, is_title=True) content = translate_html(src["content"], lang) + title = translate_text(src["title"], lang, is_title=True, context=title_context(src["content"])) new_id = create_translation(post_id, lang, title, content, status) dt = time.time() - t0 log(f" {lang}: creado #{new_id} ({dt:.0f}s) → «{title[:50]}»") -- 2.52.0