File size: 1,267 Bytes
cdc726b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""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<index>\d+))|"
                    rf"(?P<ordinal>{LABEL}|\d+(?:st|nd|rd|th|e|eme))\s+{CLIP}|"
                    rf"(?P<older>avant[- ]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])