Skip to content

archaeo_super_prompt.dataset.normalization.intervention_date.transforms

[docs] module archaeo_super_prompt.dataset.normalization.intervention_date.transforms

  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
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""Transform functions."""

import re

from .utils import (
    Date,
    InterventionDataForDateNormalizationRowSchema,
    Precision,
)

DAY_PATTERN = r"(\d{1,2})"
MONTH_PATTERN = r"\b(gennaio|febbraio|marzo|aprile|maggio|giugno|luglio|agosto|settembre|ottobre|novembre|dicembre)\b"
YEAR_PATTERN = r"(\d{4})"


# ----- PARTICULAR PATTERNS -----


def get_day_period(
    row: InterventionDataForDateNormalizationRowSchema,
) -> Date | None:
    """Extract a precise date period from a string of 2 dates separated by an hyphen."""
    s = row.data_intervento.lower()
    pattern = (
        r"(\d{1,2})\s+([a-zA-Z]+)\s*-\s*(\d{1,2})\s+([a-zA-Z]+)\s+(\d{4})"
    )
    pattern_without_year = (
        r"(\d{1,2})\s+([a-zA-Z]+)\s*-\s*(\d{1,2})\s+([a-zA-Z]+)"
    )
    m = re.fullmatch(pattern, s)
    if m:
        day1, month1, day2, month2, year = m.groups()
        return Date(
            f"{day1}/{month1}/{year}", f"{day2}/{month2}/{year}", "day"
        )
    m = re.fullmatch(pattern_without_year, s)
    if m:
        day1, month1, day2, month2 = m.groups()
        year = row.anno
        return Date(
            f"{day1}/{month1}/{year}", f"{day2}/{month2}/{year}", "day"
        )
    return None


def get_single_day_period(
    row: InterventionDataForDateNormalizationRowSchema,
) -> Date | None:
    """Extract a single date with a day."""
    s = row.data_intervento
    pattern_without_year = r"(\d{1,2})\s+([a-zA-Z]+)"
    pattern_with_year = pattern_without_year + r"\s+(\d{4})"
    m_with_year = re.fullmatch(pattern_with_year, s)
    if m_with_year:
        day1, month1, year = m_with_year.groups()
        return Date(
            f"{day1}/{month1}/{year}", f"{day1}/{month1}/{year}", "day"
        )
    m_without_year = re.fullmatch(pattern_without_year, s)
    if m_without_year:
        day1, month1 = m_without_year.groups()
        year1 = row.anno
        return Date(
            f"{day1}/{month1}/{year1}", f"{day1}/{month1}/{year1}", "day"
        )
    return None


def get_month_period(
    row: InterventionDataForDateNormalizationRowSchema,
) -> Date | None:
    """Extract a month period from a string of 2 months separated by an hyphen."""
    s = row.data_intervento
    pattern = r"([a-zA-Z]+)\s+(\d{4})\s*-\s*([a-zA-Z]+)\s+(\d{4})"
    pattern_with_year_on_right = r"([a-zA-Z]+)\s*-\s*([a-zA-Z]+)\s+(\d{4})"
    pattern_without_year = r"([a-zA-Z]+)\s*-\s*([a-zA-Z]+)"
    m = re.fullmatch(pattern, s)
    if m:
        month1, year1, month2, year2 = m.groups()
        return Date(f"{1}/{month1}/{year1}", f"{28}/{month2}/{year2}", "month")
    m = re.fullmatch(pattern_with_year_on_right, s)
    if m:
        month1, month2, year = m.groups()
        return Date(f"{1}/{month1}/{year}", f"{28}/{month2}/{year}", "month")
    m = re.fullmatch(pattern_without_year, s)
    if m:
        month1, month2 = m.groups()
        year = row.anno
        return Date(f"{1}/{month1}/{year}", f"{28}/{month2}/{year}", "month")
    return None


def get_single_month_period(
    row: InterventionDataForDateNormalizationRowSchema,
) -> Date | None:
    """Extract a single month period."""
    s = row.data_intervento
    pattern_without_year = r"([a-zA-Z]+)"
    pattern_with_year = pattern_without_year + r"\s+(\d{4})"
    m_with_year = re.fullmatch(pattern_with_year, s)
    if m_with_year:
        month1, year1 = m_with_year.groups()
        return Date(f"{1}/{month1}/{year1}", f"{28}/{month1}/{year1}", "month")
    m_without_year = re.fullmatch(pattern_without_year, s)
    if m_without_year:
        (month1,) = m_without_year.groups()
        year1 = row.anno
        return Date(f"{1}/{month1}/{year1}", f"{28}/{month1}/{year1}", "month")
    return None


def start_year(
    row: InterventionDataForDateNormalizationRowSchema,
) -> Date | None:
    """Extract a year, meaning a year when the intervention has started."""
    s = row.data_intervento
    pattern = r"(\d{4})\s*-\s*"
    m = re.fullmatch(pattern, s)
    if not m:
        return None
    start_year = m.groups()
    final_year = row.anno
    return Date(f"{1}/{1}/{start_year}", f"{31}/{12}/{final_year}", "year")


# ----- PARTICULAR PATTERNS -----
# ----- GENERIC PATTERNS -----


def _get_d_y_m(ds: str) -> tuple[str | None, str | None, str | None]:
    ds = ds.strip().lower()
    d_m_y__pattern = rf"{DAY_PATTERN}\s+{MONTH_PATTERN}\s+{YEAR_PATTERN}"
    d_m__pattern = rf"{DAY_PATTERN}\s+{MONTH_PATTERN}"
    m_y__pattern = rf"{MONTH_PATTERN}\s+{YEAR_PATTERN}"
    y__pattern = YEAR_PATTERN
    m__pattern = MONTH_PATTERN
    d__pattern = DAY_PATTERN
    m = re.fullmatch(d_m_y__pattern, ds)
    if m:
        day, month, year = m.groups()
        return (day, month, year)
    m = re.fullmatch(d_m__pattern, ds)
    if m:
        day, month = m.groups()
        return (day, month, None)
    m = re.fullmatch(m_y__pattern, ds)
    if m:
        month, year = m.groups()
        return (None, month, year)
    m = re.fullmatch(y__pattern, ds)
    if m:
        (year,) = m.groups()
        return None, None, year
    m = re.fullmatch(m__pattern, ds)
    if m:
        (month,) = m.groups()
        return None, month, None
    m = re.fullmatch(d__pattern, ds)
    if m:
        (day,) = m.groups()
        return day, None, None
    return None, None, None


def generic_period(
    row: InterventionDataForDateNormalizationRowSchema,
) -> Date | None:
    """Process a generic period of 2 dates separated by an hyphen.

    Notes:
    If 2 dates are given, usually it is not a window of time for the start of
    the intervention but the start and the end dates of the intervention.
    """
    s = row.data_intervento
    split_pattern = r"\s*-\s*"
    splits = re.split(split_pattern, s)
    if len(splits) != 2:
        return None

    start, end = tuple(map(_get_d_y_m, splits))
    if start == (None, None, None) and end == (None, None, None):
        return None
    precision: Precision = "day"
    if start[0] is None and end[0] is None:
        if start[1] is None and end[1] is None:
            precision = "year"
        else:
            precision = "month"
    return Date(
        "/".join(
            (
                start[0] if start[0] is not None else str(1),
                start[1]
                if start[1] is not None
                else (end[1] if end[1] is not None else str(1)),
                start[2]
                if start[2] is not None
                else (end[2] if end[2] is not None else str(row.anno)),
            )
        ),
        "/".join(
            (
                end[0]
                if end[0] is not None
                else (str(31) if precision == "year" else str(28)),
                end[1] if end[1] is not None else str(12),
                end[2]
                if end[2] is not None
                else str(row.anno),
            )
        ),
        precision,
    )


def generic_single_period(
    row: InterventionDataForDateNormalizationRowSchema,
) -> Date | None:
    """Process a single period with the day, month or year precision."""
    d, m, y = _get_d_y_m(row.data_intervento)
    if (d, m, y) == (None, None, None):
        return None
    y = y if y is not None else str(row.anno)
    if m is None:
        return Date(f"{1}/{1}/{y}", f"{31}/{12}/{y}", "year")
    if d is None:
        return Date(f"{1}/{m}/{y}", f"{28}/{m}/{y}", "month")
    return Date(f"{d}/{m}/{y}", f"{d}/{m}/{y}", "day")


def precised_numeric_start_date(
    row: InterventionDataForDateNormalizationRowSchema,
) -> Date | None:
    """Process a precise date in the format day/month/year."""
    date_pattern = r"(\d{1,2})\/(\d{1,2})\/(\d{4})"
    m = re.fullmatch(date_pattern, row.data_intervento.strip())
    if m:
        d, m, y = m.groups()
        return Date(f"{d}/{m}/{y}", f"{d}/{m}/{y}", "day")
    return None


def before_day_month(
    row: InterventionDataForDateNormalizationRowSchema,
) -> Date | None:
    """Return only the most recent day before which the intervention could happen."""
    s = row.data_intervento.strip()
    pattern = r"(?:pre|[a,A]nte|prima di|prima del)\s*(.*)"
    m = re.fullmatch(pattern, s)
    if not m:
        return None
    (final_date_str,) = m.groups()
    d, m, y = _get_d_y_m(final_date_str)
    y = y if y is not None else row.anno
    precision: Precision = (
        "day" if d is not None else ("month" if m is not None else "year")
    )
    m = m if m is not None else 12
    d = d if d is not None else (31 if precision == "year" else 28)
    return Date(
        "<UNKNOWN>",
        f"{d}/{m}/{y}",
        "day",
    )


# ----- GENERIC PATTERNS -----