--- name: parse_rebel_output kind: function lang: py domain: datascience version: "1.0.0" purity: pure signature: "def parse_rebel_output(decoded_text: str) -> list[dict]" description: "Parser puro del wire format de REBEL / mREBEL. Convierte la cadena decoded por el tokenizer (con skip_special_tokens=False) a una lista de triplets tipados {head, head_type, type, tail, tail_type}. Nunca lanza excepcion." tags: [rebel, mrebel, relation-extraction, nlp, parser, knowledge-graph, datascience, python] uses_functions: [] uses_types: [] returns: [] returns_optional: false error_type: "" imports: [] params: - name: decoded_text desc: "cadena raw producida por tokenizer.decode(..., skip_special_tokens=False) — incluye tokens especiales como , , , , tp_XX, etc." output: "lista de dicts con claves head (str), head_type (str), type (str), tail (str), tail_type (str). Lista vacia si no hay triplets completos o el input es vacio." tested: true tests: - "string vacio retorna lista vacia" - "un triplet completo retorna un dict con campos correctos" - "dos triplets retorna dos dicts" - "triplet incompleto sin cierre no rompe" - "tokens angulares desconocidos no lanzan excepcion" test_file_path: "python/functions/datascience/tests/test_parse_rebel_output.py" file_path: "python/functions/datascience/parse_rebel_output.py" notes: | Funcion pura. Adapta el parser oficial del README de Babelscape/rebel al estilo del registry. Compatible con mREBEL (prefijo tp_XX, lang token __es__, __en__) y REBEL (sin prefijo de idioma). El formato wire incluye para separar triplets y tokens para cerrar spans de head/tail. El estado de la maquina es: t=leyendo head, s=leyendo tail, o=leyendo relacion. --- ## Ejemplo ```python from python.functions.datascience.parse_rebel_output import parse_rebel_output decoded = "tp_XX Pablo Isla Inditex employer" triplets = parse_rebel_output(decoded) # [{'head': 'Pablo Isla', 'head_type': 'per', 'type': 'employer', # 'tail': 'Inditex', 'tail_type': 'org'}] ``` ## Formato wire REBEL / mREBEL ``` tp_XX HEAD_TOKENS TAIL_TOKENS RELATION_TOKENS ... ``` - `` — marca el inicio de un nuevo triplet (y cierra el anterior). - `` — cierra el span del head y abre el span del tail. - `` — cierra el span del tail y abre el span de la relacion. - El ultimo triplet se cierra con `` (ya eliminado antes del split). ## Notas - No valida ni filtra los `head_type`/`tail_type` — los devuelve tal cual emite el modelo. - Compatible con cualquier variante seq2seq que use el mismo wire format (Babelscape/rebel, Babelscape/mrebel-large, Babelscape/mrebel-base). - Para usar el output en el grafo, pasar por `align_relations_to_entities` que resuelve head/tail a nombres canonicos del conjunto de entidades conocido.