Spaces:
Sleeping

File size: 2,306 Bytes
c65f83c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from rapidfuzz.fuzz import token_set_ratio
from rapidfuzz.distance import JaroWinkler
import re

# Preprocess and normalize Indian address
def preprocess_address(address):
    address = str(address).lower()

    # Split alphanumeric combinations like 45vastrapur → 45 vastrapur
    address = re.sub(r"(\d+)([a-zA-Z]+)", r"\1 \2", address)
    address = re.sub(r"([a-zA-Z]+)(\d+)", r"\1 \2", address)

    # Replace common Indian address terms
    replacements = {
        r"\brd\b": "road",
        r"\bst\b": "street",
        r"\bopp\b": "opposite",
        r"\bnear\b": "",
        r"\bg\.k\.\b": "greater kailash",
        r"\bno\b\.?": "",
        r"\bsec\b": "sector",
        r"[\.,\-]": " ",  # punctuation → space
    }

    for pattern, repl in replacements.items():
        address = re.sub(pattern, repl, address)

    address = re.sub(r"\s+", " ", address).strip()
    return address

# Main comparison logic
def match_addresses(input_address, reference_address):
    if not input_address or not reference_address:
        return "Please enter both addresses.", "", "", "", "", ""

    clean_input = preprocess_address(input_address)
    clean_reference = preprocess_address(reference_address)

    jaro = round(JaroWinkler.normalized_similarity(clean_input, clean_reference) * 100, 2)
    token = token_set_ratio(clean_input, clean_reference)
    final = round((jaro + token) / 2, 2)

    return f"{final}%", clean_input, clean_reference, f"{jaro}%", f"{token}%", "✔ Match Calculated"

# Gradio Interface
demo = gr.Interface(
    fn=match_addresses,
    inputs=[
        gr.Textbox(label="Input Address", placeholder="e.g., 45 Vastrapur Vastrapur"),
        gr.Textbox(label="Reference Address", placeholder="e.g., 45vastrapur Vastrapur")
    ],
    outputs=[
        gr.Textbox(label="Final Match Score (%)"),
        gr.Textbox(label="Preprocessed Input"),
        gr.Textbox(label="Preprocessed Reference"),
        gr.Textbox(label="Jaro-Winkler Score (%)"),
        gr.Textbox(label="Token Set Ratio (%)"),
        gr.Textbox(label="Status")
    ],
    title="Indian Address Matcher",
    description="Compares two Indian addresses using preprocessing + Jaro-Winkler + Token Set Ratio. Handles numeric-locality combinations like '45vastrapur'."
)

demo.launch()