File size: 1,291 Bytes
ae97c5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations
import re
from typing import Any

COPY_NAME_INSTRUCTION = " Include attached digits and trailing numeric segments that are part of the printed name."
_ROLE = re.compile(
    "\\b(?:seller|merchant|vendor|customer|bill[- ]?to|recipient|payee|payer|guest|organization|business|company|legal)\\b",
    re.IGNORECASE,
)
_OTHER_VALUE = re.compile(
    "\\b(?:address|number|identifier|code|date|row|label|amount)\\b", re.IGNORECASE
)


def prepare(
    fields: dict[str, dict[str, Any]], *, has_media: bool = False
) -> dict[str, dict[str, Any]]:
    if not has_media:
        return fields
    result = dict(fields)
    for name, spec in fields.items():
        if spec.get("type") != "text" or spec.get("knowledge"):
            continue
        instructions = spec.get("instructions", "")
        if not isinstance(instructions, str):
            continue
        requested = re.split("[.\\n]", instructions, maxsplit=1)[0]
        if (
            re.search("\\bname\\b", requested, re.IGNORECASE)
            and _ROLE.search(requested)
            and (not _OTHER_VALUE.search(requested))
        ):
            result[name] = {
                **spec,
                "instructions": instructions + COPY_NAME_INSTRUCTION,
            }
    return result