Spaces:
Sleeping
Sleeping
Update app.py
Browse files59 unmodified lines6061626364636465666768697069717273747576757778798081828384858687888990919293949596979882838485991001011021031041051061071081091101111121138990919293949511411511611711811912012112212312497989910010110210310410510610712512612712812913013113213313413513613713813914011111214114214314414514614759 unmodified lines
def find_state_city_from_parts(parts, text_lower): """ Improved: prefer extracting city from the SAME part that contains pincode or state. Returns (city, state). 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 = None city = None
# 1) find explicit state token anywhere in text # 1) explicit state token anywhere in text for s_lc, s in zip(STATES_LC, STATES): if s_lc in text_lower: state = s break
# 2) find part that contains pincode (best case) pin = extract_pincode(text_lower)
# 2) If we have state AND pin try to capture "CITY STATE PIN" pattern from whole text first if state and pin: try: # match something like "<city> <state> 121008" - capture the <city> part pattern = 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(), state except re.error: pass
# 3) If pin exists, find the fragment that contains the pin and derive city from it if pin: for i, p in enumerate(parts): if pin in p: # strip pin and state from this fragment 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_candidate break # split into words and keep only alphabetic tokens, skipping keywords/numbers tokens = [t for t in re.split(r"[^A-Za-z]+", cleaned) if t.strip()] # remove obvious keywords and numerics tokens = [t for t in tokens if t and not re.search(r"\d", t) and t.lower() not in set([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 and not any(pref in prev_clean.lower() for pref in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS): city = prev_clean.title() break if city: return city, state
# 3) if a state was found but city not yet, try to extract token before state in the entire text if prev_clean: # If previous fragment contains words like 'sector' or 'behind' it's not a city if not any(pref in prev_clean.lower() for pref in COMMON_LANDMARK_PREFIXES + COMMON_FLAT_WORDS + ROAD_TOKENS): # take cleaned prev as city return prev_clean.title(), state break # we checked the pin fragment; stop looping
# 4) If state exists but no pin capture succeeded: take token(s) immediately before state in text if state: try: m2 = re.search(re.escape(state.lower()), text_lower) 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 m2: before = text_lower[: m2.start()].strip() # take last comma-separated/dash-separated segment before state 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 + ROAD_TOKENS): return candidate.title(), state except 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 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(), state if last_clean: # if last_clean contains digits or keywords, it's unlikely a city if 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(), state
return None, state
app.py+60-28