File size: 1,421 Bytes
651b674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
33
34
35
36
37
"""Recover unambiguous future deadlines near the source of an agreement."""

from collections import defaultdict
from datetime import datetime
import re


_TODAY = re.compile(r"\b(?:сегодня|сегодняшн\w*|прямо\s+сейчас)\b", re.I)


def future_dates_by_turn(candidates, anchor):
    by_turn = defaultdict(set)
    for candidate in candidates:
        if candidate["turn"] < 0 or candidate["value"] is None:
            continue
        value = datetime.fromisoformat(candidate["value"])
        if value.date() > anchor.date():
            by_turn[candidate["turn"]].add(value)
    return by_turn


def agreement_deadline(future_dates, turns, source, anchor, start, end):
    """Return a deadline when a same-day fallback has one nearby alternative."""
    if start.date() != anchor.date() or _TODAY.search(turns[source]["text"]):
        return start
    nearby = future_dates.get(source, set())
    if not nearby:
        nearby = future_dates.get(source - 1, set()) | future_dates.get(source + 1, set())
    if len({value.date() for value in nearby}) != 1:
        return start
    explicit_times = {value for value in nearby if value.hour or value.minute}
    if len(explicit_times) == 1:
        deadline = next(iter(explicit_times))
    else:
        deadline = min(nearby).replace(hour=0, minute=0, second=0, microsecond=0)
    return deadline if end is None or deadline <= end else start