"""Recognize explicit edit requests for a numbered or older segment.""" import re import unicodedata EDIT = r"\b(?:refais|refaire|refilme|corrige|modifie|retouche|reprends|remplace|redo|re-?shoot|edit|replace|change|regenerate|remake)\s+" ARTICLE = r"(?:(?:moi|le|la|les|the|my|mon|ce|l)\s+)*" CLIP = r"(?:segments?|portions?|clips?|plans?|shots?)" ORDINALS = {"first":1,"premier":1,"premiere":1,"second":2,"seconde":2,"deuxieme":2,"third":3,"troisieme":3} LABEL = "|".join(ORDINALS) TARGET = re.compile(EDIT + ARTICLE + rf"(?:(?:{CLIP}\s+(?:(?:numero|number|no)\s*)?#?\s*(?P\d+))|" rf"(?P{LABEL}|\d+(?:st|nd|rd|th|e|eme))\s+{CLIP}|" rf"(?Pavant[- ]dernier[e]?|penultimate|second[- ]to[- ]last)\s+{CLIP})\b") def explicit_target(text, latest): normalized = unicodedata.normalize("NFKD",text.lower()).encode("ascii","ignore").decode().replace("'"," ") normalized = normalized.replace("lavant","l avant") match = TARGET.search(normalized) if not match: return None if match["older"]: return latest-1 if match["index"]: return int(match["index"]) word = match["ordinal"] return ORDINALS[word] if word in ORDINALS else int(re.match(r"\d+",word)[0])