Spaces:
Sleeping
Sleeping
Update app.py
Browse files59 unmodified lines6061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411559 unmodified linesdef find_state_city_from_parts(parts, text_lower):"""Improved: prefer extracting city from the SAME part that contains pincode or state.Returns (city, state)."""state = Nonecity = None# 1) find explicit state token anywhere in textfor s_lc, s in zip(STATES_LC, STATES):if s_lc in text_lower:state = sbreak# 2) find part that contains pincode (best case)pin = extract_pincode(text_lower)if pin:for i, p in enumerate(parts):if pin in p:cleaned = _strip_pin_state_city_from_fragment(p, STATES_LC)if cleaned:city_candidate = cleaned.title()if not re.search(r"\b(sector|flat|house|behind|near|opp|opposite)\b", city_candidate.lower()):city = city_candidatebreakif i - 1 >= 0:prev = parts[i - 1].strip()prev_clean = _strip_pin_state_city_from_fragment(prev, STATES_LC)if prev_clean and not any(pref in prev_clean.lower() for pref in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS):city = prev_clean.title()breakif city:return city, state# 3) if a state was found but city not yet, try to extract token before state in the entire textif state:match = re.search(re.escape(state.lower()), text_lower)if match:before = text_lower[: match.start()].strip()cand_parts = [c.strip() for c in re.split(r",|;| - |—|-", before) if c.strip()]if cand_parts:candidate = cand_parts[-1]candidate = _strip_pin_state_city_from_fragment(candidate, STATES_LC)if candidate and not any(pref in candidate.lower() for pref in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS):return candidate.title(), state# 4) fallback: last part if it looks like a city (and not a landmark/sector)if parts:last = parts[-1]last_clean = _strip_pin_state_city_from_fragment(last, STATES_LC)if last_clean and len(last_clean.split()) <= 4 and not any(pref in last_clean.lower() for pref in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS):return last_clean.title(), statereturn None, state59 unmodified lines6061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614759 unmodified linesdef find_state_city_from_parts(parts, text_lower):"""Improved and more robust city/state extraction:- prefer extracting city from the SAME part that contains pincode or state- if state present try regex 'city <state> <pincode>' first- when using pin-fragment, pick alphabetic tokens and skip keywords like sector/flat/near"""state = Nonecity = None# 1) explicit state token anywhere in textfor s_lc, s in zip(STATES_LC, STATES):if s_lc in text_lower:state = sbreakpin = extract_pincode(text_lower)# 2) If we have state AND pin try to capture "CITY STATE PIN" pattern from whole text firstif state and pin:try:# match something like "<city> <state> 121008" - capture the <city> partpattern = re.compile(r"([A-Za-z\s]{2,}?)\s+" + re.escape(state.lower()) + r"(?:\s+|,|-)\s*" + re.escape(pin))m = pattern.search(text_lower)if m:cand = m.group(1).strip()if cand and not any(k in cand for k in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS):return cand.title(), stateexcept re.error:pass# 3) If pin exists, find the fragment that contains the pin and derive city from itif pin:for i, p in enumerate(parts):if pin in p:# strip pin and state from this fragmentcleaned = _strip_pin_state_city_from_fragment(p, STATES_LC)if cleaned:# split into words and keep only alphabetic tokens, skipping keywords/numberstokens = [t for t in re.split(r"[^A-Za-z]+", cleaned) if t.strip()]# remove obvious keywords and numericstokens = [t for t in tokens if t and not re.search(r"\d", t) and t.lower() not inset([kw.strip().lower() for kw in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS + ROAD_TOKENS])]if tokens:# prefer the longest contiguous sequence of tokens as city (handles "New Delhi")city_candidate = " ".join(tokens).strip()if city_candidate:return city_candidate.title(), state# fallback: previous part (but only if it looks city-like)if i - 1 >= 0:prev = parts[i - 1].strip()prev_clean = _strip_pin_state_city_from_fragment(prev, STATES_LC)if prev_clean:# If previous fragment contains words like 'sector' or 'behind' it's not a cityif not any(pref in prev_clean.lower() for pref in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS + ROAD_TOKENS):# take cleaned prev as cityreturn prev_clean.title(), statebreak # we checked the pin fragment; stop looping# 4) If state exists but no pin capture succeeded: take token(s) immediately before state in textif state:try:m2 = re.search(re.escape(state.lower()), text_lower)if m2:before = text_lower[: m2.start()].strip()# take last comma-separated/dash-separated segment before statecand_parts = [c.strip() for c in re.split(r",|;| - |—|-", before) if c.strip()]if cand_parts:candidate = cand_parts[-1]candidate = _strip_pin_state_city_from_fragment(candidate, STATES_LC)if candidate and not any(pref in candidate.lower() for pref in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS + ROAD_TOKENS):return candidate.title(), stateexcept re.error:pass# 5) Final fallback: last part if it looks like a city (not a landmark or sector)if parts:last = parts[-1]last_clean = _strip_pin_state_city_from_fragment(last, STATES_LC)if last_clean:# if last_clean contains digits or keywords, it's unlikely a cityif not re.search(r"\d", last_clean) and not any(pref in last_clean.lower() for pref in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS + ROAD_TOKENS):return last_clean.title(), statereturn None, state
app.py+60-28