File size: 10,874 Bytes
ccc58b9
 
 
 
 
2f92340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa87992
2f92340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa87992
2f92340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# ARCH-2 FIX: This is entity_resolver v1 -- DEPRECATED.
# Use processing/entity_resolver_v2.py (EntityResolverV2) instead.
# This file is kept only for backward compatibility. Do not edit.
# The pipeline already imports EntityResolverV2 via the alias:
#   from processing.entity_resolver_v2 import EntityResolver
"""
BharatGraph - Entity Resolver
Matches the SAME person or company appearing under different names
across scrapers.

The core problem in Indian data:
  MyNeta says:  "Rahul Kumar"
  MCA says:     "RAHUL KUMAR"
  GeM says:     "R. Kumar"
  PIB says:     "Shri Rahul Kumar"

Are they the same person? This module figures that out.

Method: fuzzy string matching (no ML needed for Phase 2).
Phase 4 (AI) will upgrade this with graph embeddings.
"""

import re
import json
import os
from datetime import datetime
from loguru import logger
from processing.cleaner import NameCleaner


class EntityResolver:
    """
    Resolves duplicate entities across scraped datasets.

    Approach:
    1. Clean all names first (using NameCleaner)
    2. Build a token set for each name
    3. Compare token overlap ratios
    4. Score >= threshold -> same entity -> merge into canonical record

    Example:
        "Rahul Kumar"  -> tokens: {"rahul", "kumar"}
        "R. Kumar"     -> tokens: {"r", "kumar"}
        overlap = 1/2 = 0.5  -> probably same (threshold 0.6 = no match)

        "Rahul Kumar"  -> tokens: {"rahul", "kumar"}
        "Kumar Rahul"  -> tokens: {"kumar", "rahul"}
        overlap = 2/2 = 1.0  -> definite match
    """

    def __init__(self, threshold: float = 0.7):
        """
        threshold: minimum token overlap ratio to consider a match.
        0.7 means 70% of tokens must match.
        Higher = stricter (fewer false positives).
        Lower = looser (catches more variations but more false positives).
        """
        self.threshold = threshold
        self.cleaner   = NameCleaner()
        logger.info(f"[Resolver] Initialized with threshold={threshold}")

    def _tokenize(self, name: str) -> set:
        """
        Convert a name to a set of lowercase tokens.
        Strips single-char initials that can vary (R. vs Rahul).
        """
        if not name:
            return set()
        # Lowercase, remove punctuation except spaces
        name = name.lower()
        name = re.sub(r"[^\w\s]", " ", name)
        tokens = set(name.split())
        # Remove very short tokens (initials like "r", "k")
        # but keep them if the name is mostly initials
        long_tokens = {t for t in tokens if len(t) > 1}
        return long_tokens if long_tokens else tokens

    def similarity_score(self, name1: str, name2: str) -> float:
        """
        Calculate similarity between two names.
        Returns float between 0.0 (no match) and 1.0 (exact match).

        Uses Jaccard similarity on token sets:
        score = |intersection| / |union|
        """
        t1 = self._tokenize(name1)
        t2 = self._tokenize(name2)

        if not t1 or not t2:
            return 0.0

        intersection = t1 & t2
        union        = t1 | t2

        return len(intersection) / len(union)

    def is_same_entity(self, name1: str, name2: str) -> bool:
        """
        Returns True if two names likely refer to the same person/company.
        """
        # First try exact match (after cleaning)
        c1 = self.cleaner.clean_person_name(name1)
        c2 = self.cleaner.clean_person_name(name2)
        if c1.lower() == c2.lower():
            return True

        # Then try fuzzy match
        score = self.similarity_score(c1, c2)
        return score >= self.threshold

    def find_matches(self, name: str, candidates: list,
                     name_field: str = "name") -> list:
        """
        Find all records in candidates that match the given name.

        Args:
            name: the name to search for
            candidates: list of dicts with a name field
            name_field: which dict key contains the name

        Returns:
            list of (record, score) tuples, sorted by score desc
        """
        name_clean = self.cleaner.clean_person_name(name)
        matches = []

        for record in candidates:
            candidate_name = record.get(name_field, "")
            candidate_clean = self.cleaner.clean_person_name(candidate_name)

            score = self.similarity_score(name_clean, candidate_clean)
            if score >= self.threshold:
                matches.append((record, score))

        # Sort by score descending
        matches.sort(key=lambda x: x[1], reverse=True)
        return matches

    def resolve_dataset(self, records: list,
                        name_field: str = "name") -> list:
        """
        Takes a list of records and groups duplicates together.
        Returns list of canonical records, each with a 'duplicates' key.

        This is the main method for deduplication within one dataset.
        """
        if not records:
            return []

        resolved   = []
        used_indices = set()

        for i, record in enumerate(records):
            if i in used_indices:
                continue

            canonical = dict(record)
            canonical["duplicates"]   = []
            canonical["_resolved"]    = True
            canonical["_resolve_ids"] = [i]

            name_i = record.get(name_field, "")

            # Find duplicates
            for j, other in enumerate(records):
                if j <= i or j in used_indices:
                    continue
                name_j = other.get(name_field, "")
                score  = self.similarity_score(
                    self.cleaner.clean_person_name(name_i),
                    self.cleaner.clean_person_name(name_j)
                )
                if score >= self.threshold:
                    canonical["duplicates"].append({
                        "record": other,
                        "score":  round(score, 3),
                    })
                    canonical["_resolve_ids"].append(j)
                    used_indices.add(j)

            used_indices.add(i)
            resolved.append(canonical)

        logger.info(
            f"[Resolver] {len(records)} records -> "
            f"{len(resolved)} unique entities "
            f"({len(records) - len(resolved)} duplicates merged)"
        )
        return resolved

    def cross_dataset_match(self,
                             dataset_a: list, dataset_b: list,
                             name_field_a: str = "name",
                             name_field_b: str = "name") -> list:
        """
        Find matching entities ACROSS two different datasets.
        e.g. match politicians (MyNeta) against company directors (MCA)

        Returns list of match dicts:
        {
          "record_a": {...},
          "record_b": {...},
          "score": 0.85,
          "match_type": "cross_dataset"
        }

        This is the KEY function for finding politician-company links.
        """
        matches = []

        for rec_a in dataset_a:
            name_a = self.cleaner.clean_person_name(
                rec_a.get(name_field_a, "")
            )
            if not name_a:
                continue

            for rec_b in dataset_b:
                name_b = self.cleaner.clean_person_name(
                    rec_b.get(name_field_b, "")
                )
                if not name_b:
                    continue

                score = self.similarity_score(name_a, name_b)
                if score >= self.threshold:
                    matches.append({
                        "record_a":   rec_a,
                        "record_b":   rec_b,
                        "name_a":     name_a,
                        "name_b":     name_b,
                        "score":      round(score, 3),
                        "match_type": "cross_dataset",
                        "matched_at": datetime.now().isoformat(),
                    })

        logger.info(
            f"[Resolver] Cross-dataset: {len(dataset_a)} x {len(dataset_b)} "
            f"-> {len(matches)} matches found"
        )
        return matches

    def save_matches(self, matches: list, filepath: str):
        """Save match results to JSON."""
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        with open(filepath, "w", encoding="utf-8") as f:
            json.dump(matches, f, indent=2, ensure_ascii=False)
        logger.success(f"[Resolver] Saved {len(matches)} matches to {filepath}")


# -- Run directly to test ---------------------------------
if __name__ == "__main__":
    print("=" * 55)
    print("BharatGraph - Entity Resolver Test")
    print("=" * 55)

    resolver = EntityResolver(threshold=0.6)

    print("\n[1] Similarity scores:")
    pairs = [
        ("Rahul Kumar",       "RAHUL KUMAR"),
        ("Rahul Kumar",       "Kumar Rahul"),
        ("R. Kumar",          "Rahul Kumar"),
        ("Priya Sharma",      "Priya Devi"),
        ("Sample Infra Ltd",  "SAMPLE INFRASTRUCTURE PVT LTD"),
        ("John Smith",        "Jane Smith"),
        ("Narendra Modi",     "N. Modi"),
    ]
    for a, b in pairs:
        score = resolver.similarity_score(a, b)
        match = "? MATCH" if score >= 0.6 else "? no match"
        print(f"  {match} ({score:.2f})  '{a}' vs '{b}'")

    print("\n[2] Deduplication within dataset:")
    politicians = [
        {"name": "RAHUL KUMAR",       "party": "Party A", "state": "TN"},
        {"name": "Rahul Kumar",        "party": "Party A", "state": "Tamil Nadu"},
        {"name": "Priya Sharma",       "party": "Party B", "state": "MH"},
        {"name": "PRIYA SHARMA",       "party": "Party B", "state": "Maharashtra"},
        {"name": "Suresh Patel",       "party": "Party C", "state": "GJ"},
    ]
    resolved = resolver.resolve_dataset(politicians, name_field="name")
    print(f"  Input: {len(politicians)} records")
    print(f"  Output: {len(resolved)} unique entities")
    for r in resolved:
        dupes = len(r["duplicates"])
        print(f"  -> '{r['name']}'" + (f" (merged {dupes} duplicate)" if dupes else ""))

    print("\n[3] Cross-dataset matching (politicians vs company directors):")
    directors = [
        {"director_name": "Rahul Kumar",  "company": "Sample Infra Pvt Ltd"},
        {"director_name": "Priya Sharma", "company": "ABC Trading Ltd"},
        {"director_name": "Amit Shah",    "company": "XYZ Holdings Ltd"},
    ]
    matches = resolver.cross_dataset_match(
        politicians, directors,
        name_field_a="name",
        name_field_b="director_name"
    )
    print(f"  Found {len(matches)} cross-dataset matches:")
    for m in matches:
        print(f"  -> '{m['name_a']}' (politician) matches '{m['name_b']}' (director) "
              f"score={m['score']}")
        print(f"     Company: {m['record_b']['company']}")

    print("\nDone!")