Spaces:
Sleeping
Sleeping
Update app.py
Browse files498 unmodified lines4995005015025025035045051 unmodified line507508509510510511512513512513514515516517518519520519520521521523524525526527528529530522523524525526527528529532530531532533534537535536537538539540543544541542546547548543544550551545546547548554549550551552553498 unmodified lines s = s.replace("β", "'").replace("β", "'").replace("β", '"').replace("β", '"') s = s.lower() s = re.sub(r"[^a-z0-9' ]+", " ", s) s = s.replace("'", "") # <-- NEW: drop apostrophes to match ASR "isn t" s = s.replace("'", "") # drop apostrophes β "isn't" β "isnt" s = re.sub(r"\s+", " ", s).strip() return s
1 unmodified line """ Find the first likely reply immediately after the cue(s) that contain the phrase. Heuristics: - match normalized phrase in 1β3-cue windows - match normalized phrase in 1β3-cue windows (include last window) - skip noises ([music], [applause], empty) - prefer the first short, declarative cue (<= 30 chars, no '?', no ':') - if present in the next few cues, return single-word replies like 'Extremely' - prefer a single-word/short reply; whitelist common single-word answers - scan a few more cues to catch delayed replies """ target = self._norm(phrase) n = len(captions) norm_caps = [self._norm(x) for x in captions]
# Candidates to favor as one-word answers single_word_whitelist = {"extremely", "yes", "no", "indeed", "absolutely", "correct", "exactly"} # Search single cue and 2-3 cue windows n = len(captions) norm_caps = [self._norm(x) for x in captions]
# helper to check if a cue looks like a clean short reply def is_clean_short(s: str) -> bool: s0 = s.strip(' "\'') if not s0 or len(s0) > 30: return False if any(ch in s0 for ch in "?:"): return False return True def clean_text(raw: str) -> str: # strip outer quotes/hyphens/spaces, drop trailing punctuation stripped = re.sub(r'^[\s\'"ββ\-]+|[\s\'"ββ]+$', '', raw.strip()) return re.sub(r'[.!?]+$', '', stripped).strip()
def is_single_word(s: str) -> bool: return re.fullmatch(r"[A-Za-z]+", s) is not None # Search the quoted phrase across up to 3 adjacent cues # Search the quoted phrase across up to 3 adjacent cues (include final window) for win in (1, 2, 3): for i in range(n - win + 1): window = " ".join(norm_caps[i:i+win]) if target and target in window: # scan forward up to ~10 cues for the best reply # scan forward up to ~12 cues for best reply best = None for j in range(i + win, min(i + win + 12, n)): raw = captions[j].strip() if not raw: continue # skip bracketed noises if re.match(r"^\[[^\]]+\]$", raw): if re.match(r"^\[[^\]]+\]$", raw): # [music], [applause] continue # strip trailing punctuation for return stripped = re.sub(r'[ \'"ββ]+$', '', re.sub(r'^[ \'"ββ]+', '', raw)) trimmed = re.sub(r'[.!?]+$', '', stripped).strip() trimmed = clean_text(raw) # prefer whitelisted single-word replies if seen if self._norm(trimmed) in single_word_whitelist: # 1) Prefer whitelisted single words like "Extremely" if self._norm(trimmed) in single_word_whitelist and is_single_word(trimmed): return trimmed if is_clean_short(trimmed) and best is None: # 2) Otherwise, keep the first short declarative as a fallback if best is None and len(trimmed) <= 30 and ("?" not in trimmed and ":" not in trimmed): best = trimmed if best:app.py+21-25