abinazebinoy commited on
Commit
63af5ed
·
1 Parent(s): b30a41a

feat(processing): add canonical_id.py -- SHA-256 ID generation

Browse files

Single source of truth for stable entity IDs, matching
make_id() in graph/loader.py for full compatibility.
Helper functions for politician, company, contract, NGO ID generation.
Pure ASCII.

Files changed (1) hide show
  1. processing/canonical_id.py +46 -0
processing/canonical_id.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ BharatGraph - Canonical ID Generation
3
+ Single source of truth for deterministic entity IDs.
4
+ Every node in the graph derives its ID from its most stable
5
+ real-world identifier (CIN, constituency+name, etc.).
6
+ Identical to make_id() in graph/loader.py for full compatibility.
7
+ Pure ASCII.
8
+ """
9
+ import hashlib
10
+ import re
11
+
12
+
13
+ def canonical_id(*parts) -> str:
14
+ """
15
+ 20-char SHA-256-derived ID from canonical entity properties.
16
+ Same algorithm as make_id() in loader.py for full compatibility.
17
+
18
+ Examples:
19
+ canonical_id("narendra modi", "gujarat") -> stable ID
20
+ canonical_id("L51100GJ1993PLC019067") -> company by CIN
21
+ canonical_id("adani enterprises", "gujarat") -> same every run
22
+ """
23
+ combined = "|".join(str(p).lower().strip() for p in parts)
24
+ return hashlib.sha256(combined.encode()).hexdigest()[:20]
25
+
26
+
27
+ def canonical_id_for_politician(name: str, state: str,
28
+ election: str = "") -> str:
29
+ return canonical_id(name, state, election)
30
+
31
+
32
+ def canonical_id_for_company(cin: str = "", name: str = "",
33
+ state: str = "") -> str:
34
+ if cin:
35
+ return canonical_id(cin)
36
+ return canonical_id(name, state)
37
+
38
+
39
+ def canonical_id_for_contract(order_id: str) -> str:
40
+ return canonical_id(order_id)
41
+
42
+
43
+ def canonical_id_for_ngo(darpan_id: str = "", name: str = "") -> str:
44
+ if darpan_id:
45
+ return canonical_id("ngo", darpan_id)
46
+ return canonical_id("ngo", name)