Skip to content

unaiverse.uai.templates

One table per language feeds three parts of the package: (1) the instruction that a model reads in place of a form (alt), (2) the readable projection of an answer (draft) and (3) the words and date shapes that the tolerant parser accepts (coerce). Because one table feeds all three, the renderings and the parsing cannot drift apart. The strings are part of the contract and the shared test vectors pin them. A new language needs an entry of TEMPLATES with the same keys. The corrective prompts of fill have their own private table. That table falls back to English for an unknown language. Import both names from unaiverse.uai. The protocol is described in the package page.

TEMPLATES module-attribute

TEMPLATES = {'it': {'progress': lambda step, total: f'Passo {num_to_text(step)} di {num_to_text(total)}. ', 'intro': "Rispondi con righe 'campo: valore'. Campi: ", 'types': {'text': 'testo', 'textarea': 'testo lungo', 'number': 'numero', 'integer': 'numero intero', 'bool': 'sì o no', 'date': 'data AAAA-MM-GG'}, 'select_one': lambda labels: 'una tra: ' + ', '.join(labels), 'select_many': lambda lo, hi, labels: f'da {num_to_text(lo)} a {num_to_text(hi)} tra: ' + ', '.join(labels), 'select_some': lambda labels: 'una o più tra: ' + ', '.join(labels), 'unit': lambda u: f'in {u}', 'between': lambda a, b: f'tra {num_to_text(a)} e {num_to_text(b)}', 'at_least': lambda a: f'almeno {num_to_text(a)}', 'at_most': lambda b: f'al massimo {num_to_text(b)}', 'max_length': lambda n: f'massimo {num_to_text(n)} caratteri', 'format': {'email': 'formato email', 'url': 'formato url', 'tel': 'formato telefono'}, 'required': 'obbligatorio', 'optional': 'facoltativo', 'yes': 'Sì', 'no': 'No', 'date': lambda y, m, d: f'{d}/{m}/{y}', 'true_words': ('sì', 'si', 'vero', 'yes', 'true', '1'), 'false_words': ('no', 'falso', 'false', '0'), 'date_shapes': ((_IT_DATE_RE, ('d', 'm', 'y')),)}, 'en': {'progress': lambda step, total: f'Step {num_to_text(step)} of {num_to_text(total)}. ', 'intro': "Reply with lines 'field: value'. Fields: ", 'types': {'text': 'text', 'textarea': 'long text', 'number': 'number', 'integer': 'integer', 'bool': 'yes or no', 'date': 'date YYYY-MM-DD'}, 'select_one': lambda labels: 'one of: ' + ', '.join(labels), 'select_many': lambda lo, hi, labels: f'{num_to_text(lo)} to {num_to_text(hi)} of: ' + ', '.join(labels), 'select_some': lambda labels: 'one or more of: ' + ', '.join(labels), 'unit': lambda u: f'in {u}', 'between': lambda a, b: f'between {num_to_text(a)} and {num_to_text(b)}', 'at_least': lambda a: f'at least {num_to_text(a)}', 'at_most': lambda b: f'at most {num_to_text(b)}', 'max_length': lambda n: f'at most {num_to_text(n)} characters', 'format': {'email': 'format email', 'url': 'format url', 'tel': 'format phone'}, 'required': 'required', 'optional': 'optional', 'yes': 'Yes', 'no': 'No', 'date': lambda y, m, d: f'{m}/{d}/{y}', 'true_words': ('yes', 'true', '1', 'y'), 'false_words': ('no', 'false', '0', 'n'), 'date_shapes': ((_EN_DATE_RE, ('m', 'd', 'y')),)}}

A dict from a primary language tag ("it" or "en") to a table. Each table is a dict with the keys below. Several values are functions that take numbers and write them through num_to_text.

Key Kind English Italian
progress (step, total) -> str Step 2 of 3. Passo 2 di 3.
intro str Reply with lines 'field: value'. Fields: Rispondi con righe 'campo: valore'. Campi:
types dict text, long text, number, integer, yes or no, date YYYY-MM-DD testo, testo lungo, numero, numero intero, sì o no, data AAAA-MM-GG
select_one (labels) -> str one of: A, B una tra: A, B
select_many (lo, hi, labels) -> str 1 to 3 of: A, B da 1 a 3 tra: A, B
select_some (labels) -> str one or more of: A, B una o più tra: A, B
unit (u) -> str in cm in cm
between (a, b) -> str between 50 and 250 tra 50 e 250
at_least (a) -> str at least 50 almeno 50
at_most (b) -> str at most 250 al massimo 250
max_length (n) -> str at most 80 characters massimo 80 caratteri
format dict format email, format url, format phone formato email, formato url, formato telefono
required, optional str required, optional obbligatorio, facoltativo
yes, no str Yes, No Sì, No
date (y, m, d) -> str 04/03/2024 (month first) 03/04/2024 (day first)
true_words tuple yes, true, 1, y sì, si, vero, yes, true, 1
false_words tuple no, false, 0, n no, falso, false, 0
date_shapes tuple M/D/YYYY or M-D-YYYY D/M/YYYY or D-M-YYYY

Both examples of the date row are 3 April 2024. The parser compares true_words and false_words with the lower-cased trimmed answer. An English form therefore refuses sì as a bool answer (issue type) and an Italian form refuses n. Each entry of date_shapes is a pair of a compiled pattern and the meaning of its three groups. Day and month take one or two digits and the year four digits. The parser always tries ISO YYYY-MM-DD first.

template_for

template_for(lang) -> tuple[str, dict]
Source code in unaiverse/uai/templates.py
def template_for(lang) -> tuple[str, dict]:
    """Resolves a language tag (or anything else) to a template table, falling back to English."""
    primary = str(lang or "").lower().split("-")[0]
    if primary and primary in TEMPLATES:
        return primary, TEMPLATES[primary]
    return "en", TEMPLATES["en"]

Resolves a language tag to its table. An unknown tag falls back to English.

Argument Type Default Meaning
lang any required A language tag such as "it", "IT", "it-IT" or "en-GB". The function lower-cases it and keeps the part before the first -. None, an empty string and any tag whose primary part is not in TEMPLATES select English.

It returns a tuple (primary, table) where primary is "it" or "en" and table is the matching entry of TEMPLATES.

template_for("it-IT")[0]   # 'it'
template_for("pt-BR")[0]   # 'en'
template_for(None)[0]      # 'en'
template_for("it_IT")[0]   # 'en' (only "-" separates the subtags)