import gradio as gr from rapidfuzz.fuzz import token_sort_ratio from rapidfuzz.distance import JaroWinkler from metaphone import doublemetaphone import re # Common Indian first names and surnames (expandable) common_first_names = { "sunil", "rahul", "amit", "raj", "sachin", "vijay", "anil", "arun", "deepak", "pradeep", "ravi", "manoj" } common_surnames = { "kumar", "sharma", "verma", "patel", "reddy", "mehta", "gupta", "nair", "rao", "iyer", "yadav", "singh" } # Smart splitter to separate concatenated Indian names def smart_split_name(name): name = name.lower() name = re.sub(r"[^a-z]", "", name) # Remove non-alphabetic characters for i in range(3, len(name) - 3): first = name[:i] second = name[i:] if first in common_first_names and second in common_surnames: return f"{first} {second}" return name # Return original if no valid split # Auto-expand if name has no space def auto_expand_indian_name(name): if " " not in name: return smart_split_name(name) return name # Normalize name for consistent comparison def normalize_name(name): name = name.lower() name = re.sub(r"[^a-z\s]", "", name) name = re.sub(r"\s+", " ", name).strip() return name # Generate phonetic code using Double Metaphone def get_phonetic_code(name): tokens=name.split() codes= [] for token in tokens: code=doublemetaphone(token)[0] if code: codes.append(code) return " ".join(codes) r"""dm = DoubleMetaphone() codes = [dm.phonetics(token)[0] for token in name.split()] return " ".join(filter(None, codes)) """ # Match names with all 3 logics def match_names(name1, name2): if not name1 or not name2: return "Please enter both names.", "", "", "", "", "", "" # Auto-expand concatenated names expanded1 = auto_expand_indian_name(name1) expanded2 = auto_expand_indian_name(name2) # Normalize names norm1 = normalize_name(expanded1) norm2 = normalize_name(expanded2) # Similarity scores jaro_score = round(JaroWinkler.normalized_similarity(norm1, norm2) * 100, 2) token_score = token_sort_ratio(norm1, norm2) phon1 = get_phonetic_code(norm1) phon2 = get_phonetic_code(norm2) phon_score = round(JaroWinkler.normalized_similarity(phon1, phon2) * 100, 2) # Final average score final_score = round((jaro_score + token_score + phon_score) / 3, 2) return ( f"{final_score}%", expanded1, expanded2, f"{jaro_score}%", f"{token_score}%", f"{phon_score}%", "✔ Match calculated" ) # Gradio interface demo = gr.Interface( fn=match_names, inputs=[ gr.Textbox(label="Input Name", placeholder="e.g., Sunil Kumar"), gr.Textbox(label="Reference Name", placeholder="e.g., SunilKumar") ], outputs=[ gr.Textbox(label="Final Match Score (%)"), gr.Textbox(label="Expanded Input Name"), gr.Textbox(label="Expanded Reference Name"), gr.Textbox(label="Jaro-Winkler (%)"), gr.Textbox(label="Token Sort Ratio (%)"), gr.Textbox(label="Phonetic Similarity (%)"), gr.Textbox(label="Status") ], title="🔠 Indian Name Matcher", description="Smart matching of Indian names using typo similarity, word order, and phonetic comparison. Now supports splitting concatenated names like 'SunilKumar'." ) demo.launch()