| 164 | |
| 165 | |
| 166 | def _god_node_article( |
| 167 | G: nx.Graph, |
| 168 | nid: str, |
| 169 | labels: dict[int, str], |
| 170 | node_community: dict[str, int] | None = None, |
| 171 | community_slugs: dict[int, str] | None = None, |
| 172 | god_slugs: dict[str, str] | None = None, |
| 173 | ) -> str: |
| 174 | community_slugs = community_slugs or {} |
| 175 | god_slugs = god_slugs or {} |
| 176 | d = G.nodes[nid] |
| 177 | node_label = d.get("label", nid) |
| 178 | src = d.get("source_file", "") |
| 179 | cid = (node_community or {}).get(nid) |
| 180 | community_name = labels.get(cid, f"Community {cid}") if cid is not None else None |
| 181 | |
| 182 | lines: list[str] = [] |
| 183 | lines += [f"# {node_label}", ""] |
| 184 | lines += [f"> God node · {G.degree(nid)} connections · `{src}`", ""] |
| 185 | |
| 186 | if cid is not None and community_name: |
| 187 | lines += [f"**Community:** {_md_link_to(community_name, community_slugs.get(cid))}", ""] |
| 188 | |
| 189 | # Group neighbors by relation type |
| 190 | by_relation: dict[str, list[str]] = {} |
| 191 | directed = isinstance(G, nx.DiGraph) |
| 192 | connections = ( |
| 193 | (target if source == nid else source, source != nid, data) |
| 194 | for source, target, data in _incident_edges(G, [nid]) |
| 195 | ) |
| 196 | for neighbor, incoming, ed in sorted( |
| 197 | connections, key=lambda item: G.degree(item[0]), reverse=True |
| 198 | ): |
| 199 | neighbor_label = G.nodes[neighbor].get("label", neighbor) |
| 200 | rel = ed.get("relation", "related") |
| 201 | conf = ed.get("confidence", "") |
| 202 | conf_str = f" `{conf}`" if conf else "" |
| 203 | # On a directed graph an incident edge can point either way. Mark the |
| 204 | # ones where this node is the target, so a directional relation like |
| 205 | # `calls` is not read backwards: an unmarked entry is outgoing (this |
| 206 | # node -> neighbor), an `(incoming)` entry is neighbor -> this node |
| 207 | # (#5369 follow-up). |
| 208 | dir_str = " _(incoming)_" if directed and incoming else "" |
| 209 | by_relation.setdefault(rel, []).append( |
| 210 | f"{_md_link_to(neighbor_label, god_slugs.get(neighbor))}{conf_str}{dir_str}" |
| 211 | ) |
| 212 | |
| 213 | lines += ["## Connections by Relation", ""] |
| 214 | for rel, targets in sorted(by_relation.items()): |
| 215 | lines.append(f"### {rel}") |
| 216 | for t in targets[:20]: |
| 217 | lines.append(f"- {t}") |
| 218 | # The cap keeps god-node articles readable, but silently dropping the |
| 219 | # tail made the body disagree with the header's degree count with |
| 220 | # nothing telling the reader anything was cut (#3127). Entries are |
| 221 | # degree-sorted, so what is hidden is the low-degree tail. |
| 222 | if len(targets) > 20: |
| 223 | lines.append( |