Spaces:
Sleeping
Sleeping
Update app.py
Browse files266 unmodified lines26726826927027127227332 unmodified lines306307308309310311266 unmodified linesif plot:out['plot'] = plot# Landmark (pass city/state to allow trimming)landmark = extract_landmark(parts, text_lower, city=out.get('city'), state=out.get('state'))if landmark:out['landmark'] = landmark32 unmodified linesbreakout['building_street'] = building# Clean up: ensure landmark/building/locality do not accidentally contain city/state/pincode textdef _clean_field(val):if not val:266 unmodified lines26726826927027127227332 unmodified lines306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352266 unmodified linesif plot:out['plot'] = plot# Landmark (pass city/state to allow trimming)landmark = extract_landmark(parts, text_lower, city=out.get('city'), state=out.get('state'))if landmark:out['landmark'] = landmark32 unmodified linesbreakout['building_street'] = building# ===== New robust city extraction and cleanup =====# If city not set, try to derive it from pincode fragment (cleaned)if not out.get('city'):# find pincode fragmentpin = out.get('pincode')if pin:for i, p in enumerate(parts):if pin in p:cleaned = _strip_pin_state_city_from_fragment(p, STATES_LC)# extract alphabetic tokens (skip keywords & numbers) and pick last plausible token(s)tokens = [t for t in re.findall(r"[A-Za-z]+(?:\s+[A-Za-z]+)*", cleaned) if t.strip()]# tokens may contain landmark+city; prefer last token with >1 char and not a keywordfor tok in reversed(tokens):tok_clean = tok.strip()low = tok_clean.lower()if len(tok_clean) > 2 and not any(k in low for k in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS + ROAD_TOKENS):out['city'] = tok_clean.title()breakbreak# If city was detected but appears inside landmark/locality/building, remove it from those fieldsif out.get('city'):city_token = out['city']# compile pattern oncecity_pat = re.compile(rf"\b{re.escape(city_token)}\b", flags=re.IGNORECASE)def _remove_city_from_field(val):if not val:return None# remove city token and any trailing separators/spacescleaned = city_pat.sub("", val).strip(" ,-")cleaned = re.sub(r"\s{2,}", " ", cleaned).strip()return cleaned.title() if cleaned else None# Update fields (landmark should keep its prefix like "Behind ..." if present)if out.get('landmark'):out['landmark'] = _remove_city_from_field(out['landmark'])if out.get('locality'):out['locality'] = _remove_city_from_field(out['locality'])if out.get('building_street'):out['building_street'] = _remove_city_from_field(out['building_street'])# Clean up: ensure landmark/building/locality do not accidentally contain city/state/pincode textdef _clean_field(val):if not val:
app.py+42-1