Repository navigation
feat(contexto): trozos de componentes con su frase y líneas con su continuación #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,24 @@ def unidades(texto: str, literal: bool = False) -> list[str]: | |
| return [p for p in candidatos if es_traducible(p) and not parece_clave(p)] | ||
|
|
||
|
|
||
| #: Hasta cuántos caracteres de la frase entera de un componente JSON van en la pista de cada trozo. | ||
| LARGO_FRASE = 160 | ||
|
|
||
|
|
||
| def piezas_de(texto: str, pista: str, literal: bool = False) -> list[Pieza]: | ||
| """Las piezas traducibles de un texto, con su pista. Un componente JSON con varios trozos de estilo distinto | ||
| («Use the », «Wrench» en dorado, « to rotate») se traduce por trozos para no perder los estilos; cada uno lleva la | ||
| frase entera como contexto, para que encaje en ella.""" | ||
| partes = unidades(texto, literal) | ||
| trozos = json_texto.extraer(texto, literal) or [] | ||
| if len(partes) < 2 or len(trozos) < 2: | ||
| return [Pieza(u, pista) for u in partes] | ||
| frase = " ".join("".join(trozos).split()) | ||
| frase = frase if len(frase) <= LARGO_FRASE else frase[: LARGO_FRASE - 1] + "…" | ||
| extra = f"trozo de la frase «{frase}»" | ||
| return [Pieza(u, f"{pista} · {extra}" if pista else extra) for u in partes] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an FTB Quests title or book Useful? React with 👍 / 👎. |
||
|
|
||
|
|
||
| def aplicar(texto: str, traducciones: Mapping[str, str], literal: bool = False) -> str: | ||
| if not texto.strip() or parece_clave(texto): | ||
| return texto | ||
|
|
@@ -103,7 +121,9 @@ def restaurar(self) -> list[Path]: ... | |
|
|
||
| #: Cuántos caracteres de un texto vecino o de un título se mandan como contexto. | ||
| LARGO_VECINO = 100 | ||
| MARCAS_VECINDAD = ("dentro de «", "viene después de «") | ||
| MARCAS_VECINDAD = ("dentro de «", "viene después de «", "sigue con «") | ||
| #: Una línea que termina así cierra su frase; si no, la frase sigue en la línea siguiente. | ||
| _FIN_DE_FRASE = (".", "!", "?", ":", "…", '"', "»", ")", "]") | ||
|
|
||
|
|
||
| def _recortar(texto: str) -> str: | ||
|
|
@@ -119,7 +139,7 @@ def con_vecindad(piezas: Sequence[Pieza], es_titulo: Callable[[Pieza], bool]) -> | |
| """ | ||
| titulo = anterior = "" | ||
| salida: list[Pieza] = [] | ||
| for p in piezas: | ||
| for i, p in enumerate(piezas): | ||
| if es_titulo(p): | ||
| titulo, anterior = _recortar(p.texto), "" | ||
| salida.append(p) | ||
|
|
@@ -129,11 +149,20 @@ def con_vecindad(piezas: Sequence[Pieza], es_titulo: Callable[[Pieza], bool]) -> | |
| partes.append(f"dentro de «{titulo}»") | ||
| if anterior: | ||
| partes.append(f"viene después de «{anterior}»") | ||
| # Una línea que no cierra su frase («Right-click the block») sigue en la siguiente: se manda su comienzo | ||
| siguiente = piezas[i + 1] if i + 1 < len(piezas) else None | ||
| if siguiente is not None and not es_titulo(siguiente) and not _cierra_frase(p.texto): | ||
| partes.append(f"sigue con «{_recortar(siguiente.texto)}»") | ||
| salida.append(Pieza(p.texto, " · ".join(partes), p.ambito)) | ||
| anterior = _recortar(p.texto) | ||
| return salida | ||
|
|
||
|
|
||
| def _cierra_frase(texto: str) -> bool: | ||
| limpio = re.sub(r"(?:[§&][0-9a-fk-or]|\$\([^)]*\))+$", "", texto.strip(), flags=re.I).rstrip() | ||
| return not limpio or limpio.endswith(_FIN_DE_FRASE) | ||
|
|
||
|
|
||
| def quitar_vecindad(pista: str) -> str: | ||
| """La pista sin lo que agrega `con_vecindad` (para medir cuánto aporta).""" | ||
| return " · ".join(parte for parte in pista.split(" · ") if not parte.startswith(MARCAS_VECINDAD)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """Trozos de un componente JSON con su frase entera, y líneas que siguen en la siguiente con su continuación.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from traductor_es.modulos.base import Pieza, con_vecindad, piezas_de, quitar_vecindad | ||
|
|
||
| _COMPONENTE = '["", {"text": "Use the "}, {"text": "Wrench", "color": "gold"}, {"text": " to rotate blocks."}]' | ||
|
|
||
|
|
||
| def test_cada_trozo_lleva_la_frase_entera() -> None: | ||
| piezas = piezas_de(_COMPONENTE, "misión: description") | ||
| assert [p.texto for p in piezas] == ["Use the ", "Wrench", " to rotate blocks."] | ||
| assert all(p.pista == "misión: description · trozo de la frase «Use the Wrench to rotate blocks.»" for p in piezas) | ||
|
|
||
|
|
||
| def test_un_texto_normal_o_un_componente_de_un_trozo_no_cambia() -> None: | ||
| assert piezas_de("Craft a Wrench", "misión: title") == [Pieza("Craft a Wrench", "misión: title")] | ||
| assert piezas_de('{"text": "Hola"}', "x") == [Pieza("Hola", "x")] | ||
|
|
||
|
|
||
| def test_una_linea_que_no_cierra_su_frase_lleva_la_siguiente() -> None: | ||
| piezas = [ | ||
| Pieza("The Basics", "misión: title"), | ||
| Pieza("Right-click the block with", "misión: description"), | ||
| Pieza("the &6Wrench&r to rotate it.", "misión: description"), | ||
| Pieza("Then sneak.", "misión: description"), | ||
| ] | ||
| salida = con_vecindad(piezas, lambda p: p.pista.endswith("title")) | ||
| assert "sigue con «the &6Wrench&r to rotate it.»" in salida[1].pista | ||
| assert "sigue con" not in salida[2].pista # termina en punto: la frase cierra ahí | ||
| assert "sigue con" not in salida[3].pista # es la última | ||
| assert quitar_vecindad(salida[1].pista) == "misión: description" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a component has multiple visible text segments and a
hoverEvent.show_text,json_texto.extraeralso returns the tooltip text, so this concatenation treats it as part of the visible phrase (for example, combining a label with its tooltip). That unrelated phrase is then added as context to each chunk and can mislead their translations; build the context from the same rendered text run or keep hover text separate.Useful? React with 👍 / 👎.