from __future__ import annotations from dataclasses import dataclass import networkx as nx from edkb_system.contracts import Block, Context @dataclass class Node: id: str text: str page: int kind: str bbox: tuple[float, float, float, float] def _is_decision(text: str) -> bool: kws = ["判定", "判断", "是否", "if", "check"] s = text.lower() return any(k in s for k in kws) def _dist(a: Node, b: Node) -> float: ax = (a.bbox[0] + a.bbox[2]) / 2.0 ay = (a.bbox[1] + a.bbox[3]) / 2.0 bx = (b.bbox[0] + b.bbox[2]) / 2.0 by = (b.bbox[1] + b.bbox[3]) / 2.0 return ((ax - bx) ** 2 + (ay - by) ** 2) ** 0.5 def run(input_data: list[Block], context: Context) -> dict: blocks = [ b for b in input_data if b.type in {"textbox", "shape", "diamond", "arrow_label", "paragraph"} and b.text.strip() ] by_page: dict[int, list[Block]] = {} for b in blocks: by_page.setdefault(b.page, []).append(b) page_markdowns: list[str] = [] debug: list[dict] = [] for page in sorted(by_page): cands = by_page[page] nodes: list[Node] = [] labels: list[Node] = [] for b in cands: text = b.text.strip() if b.type == "arrow_label" or text.upper() in { "NG", "OK", "Y", "N", "YES", "NO", }: labels.append(Node(b.id, text, b.page, "label", b.bbox)) continue kind = "decision" if (_is_decision(text) or b.type == "diamond") else "step" nodes.append(Node(b.id, text, b.page, kind, b.bbox)) nodes = sorted( nodes, key=lambda n: ( (n.bbox[1] + n.bbox[3]) / 2.0, (n.bbox[0] + n.bbox[2]) / 2.0, ), ) if not nodes: continue g = nx.DiGraph() for n in nodes: g.add_node(n.id, node=n) for i, src in enumerate(nodes[:-1]): dst = nodes[i + 1] g.add_edge(src.id, dst.id, label=None) for dec in [n for n in nodes if n.kind == "decision"]: right_steps = [ n for n in nodes if ((n.bbox[0] + n.bbox[2]) / 2.0) > ((dec.bbox[0] + dec.bbox[2]) / 2.0) ] if right_steps: target = min(right_steps, key=lambda x: _dist(dec, x)) label_node = None right_labels = [ l for l in labels if ((l.bbox[0] + l.bbox[2]) / 2.0) > ((dec.bbox[0] + dec.bbox[2]) / 2.0) ] if right_labels: label_node = min(right_labels, key=lambda x: _dist(dec, x)) g.add_edge( dec.id, target.id, label=(label_node.text if label_node else "分支") ) ordered = nodes idx_map = {n.id: i + 1 for i, n in enumerate(ordered)} cycles = list(nx.simple_cycles(g)) cyc_nodes = {nid for cyc in cycles for nid in cyc} lines = [f"## Page {page} Procedure"] for n in ordered: title = n.text if n.kind == "decision" and not title.startswith("判定"): title = f"判定:{title}" lines.append(f"{idx_map[n.id]}. {title}") if n.kind == "decision": for _, dst, data in g.out_edges(n.id, data=True): if data.get("label") is None: continue label = data.get("label") or "分支" if dst in cyc_nodes: lines.append(f"- {label} -> 返回到步骤{idx_map.get(dst, '?')}") else: dst_text = g.nodes[dst]["node"].text lines.append(f"- {label} -> {dst_text}") page_markdowns.append("\n".join(lines)) debug.append( { "page": page, "nodes": [ {"id": n.id, "text": n.text, "kind": n.kind, "bbox": list(n.bbox)} for n in nodes ], "edges": [ {"src": s, "dst": d, "label": data.get("label")} for s, d, data in g.edges(data=True) ], } ) context.flows_detected = len(page_markdowns) return {"flow_markdowns": page_markdowns, "debug": debug}