GodsDevProject commited on
Commit
b1ec16b
·
verified ·
1 Parent(s): df1d12f

Update entity_graph.py

Browse files
Files changed (1) hide show
  1. entity_graph.py +11 -9
entity_graph.py CHANGED
@@ -1,17 +1,19 @@
1
  import networkx as nx
2
  from typing import List, Dict
3
- import json
4
 
5
  def build_entity_graph(docs: List[Dict]) -> Dict:
6
  G = nx.Graph()
 
7
  for d in docs:
8
- G.add_node(d["agency"])
9
- for word in d["title"].split():
10
- if word.isupper():
11
- G.add_edge(d["agency"], word)
 
 
 
12
 
13
- data = {
14
- "nodes": [{"id": n} for n in G.nodes],
15
  "links": [{"source": u, "target": v} for u, v in G.edges]
16
- }
17
- return data
 
1
  import networkx as nx
2
  from typing import List, Dict
 
3
 
4
  def build_entity_graph(docs: List[Dict]) -> Dict:
5
  G = nx.Graph()
6
+
7
  for d in docs:
8
+ agency = d.get("agency", "Unknown")
9
+ G.add_node(agency, group="agency")
10
+
11
+ for token in d.get("content", "").split():
12
+ if token.isupper() and len(token) > 2:
13
+ G.add_node(token, group="entity")
14
+ G.add_edge(agency, token)
15
 
16
+ return {
17
+ "nodes": [{"id": n, "group": G.nodes[n]["group"]} for n in G.nodes],
18
  "links": [{"source": u, "target": v} for u, v in G.edges]
19
+ }