File size: 7,501 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
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
"""Recover unresolved image rows backed by a completed row inventory."""

from copy import deepcopy
import json
from typing import Any

from . import ar, image_rows
from .schema import MAX_LIST_ITEMS


def prepare(
    record: dict[str, Any], predictions: dict[str, dict[str, Any]]
) -> tuple[dict[str, Any], list[dict[str, Any]]] | None:
    if record.get("_null_recovery") or record.get("_row_omission_recovery"):
        return None
    annotations = (
        record.get("_meta", {}).get("extraction_contract", {}).get("fields", {})
    )
    tasks = {name: (name, ids, media) for name, ids, media in record["_ar_tasks"]}
    if len(tasks) != len(record["_ar_tasks"]):
        return None
    contexts, retry_tasks = [], []
    constraints = dict(record.get("_ar_constraints", {}))
    for parent, annotation in annotations.items():
        queries = annotation.get("image_row_fields")
        spec, inventory = record["fields"].get(parent, {}), predictions.get(parent, {})
        value = inventory.get("value")
        if (
            set(annotation) != {"image_row_fields"}
            or not isinstance(queries, list)
            or len(queries) != len(set(queries))
            or not queries
            or not image_rows.is_inventory(spec)
            or spec.get("knowledge")
            or not inventory.get("valid")
            or not inventory.get("complete")
            or not isinstance(value, list)
            or not value
            or len(value) > min(spec.get("max_items", MAX_LIST_ITEMS), len(queries))
            or any(
                not isinstance(row, dict)
                or set(row) != {"label"}
                or not isinstance(row["label"], str)
                or not row["label"].strip()
                for row in value
            )
            or any(child not in predictions for child in queries)
        ):
            continue
        properties = spec.get("items", {}).get("properties", {})
        text_keys = [
            key
            for key, child in properties.items()
            if child.get("type") == "text" and not child.get("knowledge")
        ]
        if not text_keys:
            continue
        labels = [row["label"] for row in value]
        proposals = [predictions[child] for child in queries]
        selected, conflicts = image_rows._select(labels, proposals, text_keys)
        if not conflicts or any(
            row is not None and (not row.get("valid") or not row.get("complete"))
            for row in selected
        ):
            continue
        children, local_constraints = [], {}
        for index in conflicts:
            child = queries[index]
            row, child_spec = predictions[child], record["fields"].get(child, {})
            row_value = row.get("value")
            task = tasks.get(child)
            native = constraints.get(child, {})
            if (
                annotations.get(child) != {"parent": parent, "index": index + 1}
                or child_spec.get("type") != "object"
                or not child_spec.get("_image_row_proposal")
                or child_spec.get("knowledge")
                or not row.get("valid")
                or not row.get("complete")
                or not isinstance(row_value, dict)
                or not row_value
                or any(cell is not None for cell in row_value.values())
                or any(row is previous for previous in selected if previous is not None)
                or task is None
                or task[2] is None
                or set(native) != {"json_schema"}
            ):
                break
            schema = json.loads(native["json_schema"])
            if schema != ar.schema(child_spec) or not set(text_keys) <= set(
                schema.get("properties", {})
            ):
                break
            # A completed inventory requires a matching textual cell; the
            # original selector remains the acceptance test for that label.
            # Full branches retain required fields with the native grammar compiler.
            branches = []
            for key in text_keys:
                branch = deepcopy(schema)
                branch["properties"][key]["type"] = "string"
                branches.append(branch)
            local_constraints[child] = {"json_schema": json.dumps({"anyOf": branches})}
            children.append(child)
        else:
            constraints.update(local_constraints)
            retry_tasks.extend(tasks[child] for child in children)
            contexts.append(
                {
                    "parent": parent,
                    "queries": queries,
                    "labels": labels,
                    "text_keys": text_keys,
                    "selected": selected,
                    "conflicts": conflicts,
                    "children": children,
                }
            )
    if not retry_tasks:
        return None
    repair = {
        **record,
        "_ar_tasks": retry_tasks,
        "_ar_constraints": constraints,
        "_ar_prefix": None,
        "_ar_group_prefixes": [],
        "_ar_media_groups": [],
        "_null_recovery": True,
        "_row_omission_recovery": True,
    }
    return repair, contexts


def merge(
    record: dict[str, Any],
    predictions: dict[str, dict[str, Any]],
    alternatives: dict[str, dict[str, Any]],
    plan: tuple[dict[str, Any], list[dict[str, Any]]],
) -> tuple[dict[str, dict[str, Any]], list[dict[str, Any]]]:
    repair, contexts = plan
    lengths = {name: len(ids) for name, ids, _ in repair["_ar_tasks"]}
    result = dict(predictions)
    evidence = []
    for context in contexts:
        candidates = {}
        for child in context["children"]:
            original, alternative = predictions[child], alternatives.get(child, {})
            account = {
                "tokens": original["tokens"] + alternative.get("tokens", 0),
                "retry_input_tokens": original.get("retry_input_tokens", 0)
                + lengths[child],
            }
            result[child] = {**original, **account}
            value = alternative.get("value")
            if (
                alternative.get("valid")
                and alternative.get("complete")
                and isinstance(value, dict)
                and any(cell is not None for cell in value.values())
            ):
                candidates[child] = {
                    **alternative,
                    **account,
                    "image_row_omission_recovered": True,
                }
        trial = {**result, **candidates}
        selected, conflicts = image_rows._select(
            context["labels"],
            [trial[name] for name in context["queries"]],
            context["text_keys"],
        )
        unchanged = all(
            previous is None or selected[index] is previous
            for index, previous in enumerate(context["selected"])
        )
        accepted = (
            not conflicts and unchanged and len(candidates) == len(context["children"])
        )
        if accepted:
            result.update(candidates)
        evidence.append(
            {
                "parent": context["parent"],
                "original_conflicts": context["conflicts"],
                "retry_children": context["children"],
                "conflicts_after_retry": conflicts,
                "previously_matched_proposals_preserved_exactly": unchanged,
                "accepted": accepted,
            }
        )
    return result, evidence