File size: 586 Bytes
c68e070
ae6ce40
 
c68e070
 
b1ec16b
c68e070
b1ec16b
 
 
 
 
 
 
c68e070
b1ec16b
 
c68e070
b1ec16b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import networkx as nx
from typing import List, Dict

def build_entity_graph(docs: List[Dict]) -> Dict:
    G = nx.Graph()

    for d in docs:
        agency = d.get("agency", "Unknown")
        G.add_node(agency, group="agency")

        for token in d.get("content", "").split():
            if token.isupper() and len(token) > 2:
                G.add_node(token, group="entity")
                G.add_edge(agency, token)

    return {
        "nodes": [{"id": n, "group": G.nodes[n]["group"]} for n in G.nodes],
        "links": [{"source": u, "target": v} for u, v in G.edges]
    }