File size: 9,408 Bytes
32e4c72
b6a6194
9b4cf19
32e4c72
 
9b4cf19
32e4c72
 
 
 
 
 
 
b6a6194
32e4c72
 
 
 
 
 
 
9b4cf19
 
32e4c72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc7d9a
 
 
c379b57
32e4c72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6a6194
32e4c72
b6a6194
32e4c72
 
 
 
 
 
 
 
 
 
 
b6a6194
5eb3a63
09a324c
 
 
 
9b4cf19
 
 
09a324c
 
 
 
 
32e4c72
b6a6194
 
 
 
 
9b4cf19
 
 
 
09a324c
9b4cf19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5820bde
 
 
09a324c
5820bde
 
 
 
 
 
 
 
 
 
 
 
9b4cf19
 
 
09a324c
9b4cf19
 
09a324c
9b4cf19
 
 
 
 
 
 
 
 
 
 
 
32e4c72
9b4cf19
 
09a324c
9b4cf19
5820bde
 
 
 
 
 
 
 
 
 
 
 
9b4cf19
32e4c72
 
 
 
 
 
 
5eb3a63
32e4c72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b4cf19
 
 
 
 
 
32e4c72
 
 
 
5eb3a63
9b4cf19
32e4c72
 
 
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
import pymupdf
from scripts.models import RegulatoryChange
from scripts.utility_functions import color_mapping, get_best_fuzzy_match


def add_infos_to_pdf_agentic(doc, changes, successful_annotations, extraction_method="Landing AI", nlp_preprocessing=True):
    type_counts = {
        "addition": 0,
        "modification": 0,
        "deletion": 0,
        "unspecified": 0,
    }
    for change in changes:
        change_type = change.type if hasattr(change, "type") else "unspecified"
        if change_type in type_counts:
            type_counts[change_type] += 1
        else:
            type_counts["unspecified"] += 1

    summary_text = (
        "Regulatory Summary:\n"
        f"- Extraction Method: {extraction_method}\n"
        f"- Nlp preprocessing: {'yes' if nlp_preprocessing else 'no'}\n"
        f"- Total Changes: {len(changes)}, Successful Annotations: {successful_annotations}\n"
        f"- Additions: {type_counts.get('addition', 0)}\n"
        f"- Deletions: {type_counts.get('deletion', 0)}\n"
        f"- Modifications: {type_counts.get('modification', 0)}\n"
    )
    page = doc.load_page(0)
    rect = pymupdf.Rect(10, 10, 550, 150)
    page.insert_textbox(
        rect,
        summary_text,
        fontsize=9,
        fontname="helv",
        align=pymupdf.TEXT_ALIGN_LEFT,
        color=(0, 0, 0.7),
        overlay=True,
    )

    metadata = doc.metadata
    metadata["title"] = "Annotated " + (
        metadata["title"] if metadata["title"] else "PDF"
    )
    metadata["author"] = "Fortiss ReguLens" + (
        " & " + metadata["author"] if metadata["author"] else ""
    )
    metadata["subject"] = "Annotated PDF with regulatory changes"
    metadata["keywords"] = "regulatory, changes, annotations, pdf"
    doc.set_metadata(metadata)


def add_failed_annotations_to_pdf_agentic(doc, failed_annotations):
    """
    Doc is edited in place.
    Adds failed annotations to the end of the PDF document.

    :param doc: The PyMuPDF document object.
    :type doc: pymupdf.Document
    :param failed_annotations: The failed annotations to be added.
    :type failed_annotations: array
    """
    if not failed_annotations:
        return
    page = doc.new_page(pno=-1)
    annotation_str = "Failed Annotations:\n"
    for failed_annotation in failed_annotations:
        text = failed_annotation["change"].text
        change_type = failed_annotation["change"].type
        change_str = failed_annotation["change"].category
        page_num = failed_annotation["page"]
        annotation_str += (
            f"Page {page_num}: {text} ({change_type}) Change: {change_str}\n"
        )

    rect = pymupdf.Rect(20, 20, 580, 822)
    page.insert_textbox(
        rect,
        annotation_str,
        fontsize=9,
        fontname="helv",
        align=pymupdf.TEXT_ALIGN_LEFT,
        color=(0, 0, 0.7),
    )


def agentic_pdf_annotator(changes: list[RegulatoryChange], file_bytes, extraction_method="Landing AI", nlp_preprocessing=True):
    changes = [
        c for c in changes if c.confirmed and c.validated
    ]
    if not changes:
        return ""
    successful_annotations = 0
    failed_annotations = []
    try:
        doc = pymupdf.open(stream=file_bytes, filetype="pdf")
    except Exception as e:
        return ""

    # Sort by length of relevant_text in descending order to avoid overlapping highlights
    changes = sorted(changes, key=lambda c: -len(c.text))
    annotated_areas = {}
    
    # OPTIMIZATION: Pre-cache all pages and their text content
    page_cache = {}
    page_text_cache = {}
    full_text = ""
    for page_num in range(len(doc)):
        page = doc[page_num]
        page_cache[page_num] = page
        page_text = page.get_text()
        page_text_cache[page_num] = page_text
        full_text += page_text
    
    for change in changes:
        page_num = int(change.grounding[0].page)
        text = change.text
        change_type = change.type
        change_str = change.category
        comment = change.context
        if page_num < 0 or page_num >= len(doc):
            results = []
            for pnr in range(len(doc)):  # search all pages
                annotated_areas.setdefault(f"{pnr}", [])
                page = page_cache[pnr]  # Use cached page
                text_instances = page.search_for(text)
                for inst in text_instances:
                    page_num = pnr# remove?
                    results.append({"page": pnr, "bbox": inst})
                results = list(
                    filter(
                        lambda result: not any(
                            result["bbox"].intersects(area)
                            for area in annotated_areas[f"{result['page']}"]
                        ),
                        results,
                    )
                )
            if not results:
                best_match = get_best_fuzzy_match(full_text, change)
                if best_match and len(best_match) > 0:
                    print("found best fuzzy match: ", best_match)
                    for page_num in range(len(doc)):  # search all pages
                        page = page_cache[page_num]  # Use cached page
                        text_instances = page.search_for(best_match)
                        for inst in text_instances:
                            results.append({"page": page_num, "bbox": inst})
                    # we only want the results that do not overlap with already annotated areas
                    results = list(
                        filter(
                            lambda result: not any(
                                result["bbox"].intersects(area)
                                for area in annotated_areas[f"{result['page']}"]
                            ),
                            results,
                        )
                    )
            if results:  # "flattenning" the results
                page_num = results[0]["page"]
                doc_page = page_cache[page_num]  # Use cached page
                results = [r["bbox"] for r in results if r["page"] == page_num]
        else:
            doc_page = page_cache[page_num]  # Use cached page
            annotated_areas.setdefault(f"{page_num}", [])
            # Search for the relevant text on the page
            results = doc_page.search_for(text)
            # we only want the results that do not overlap with already annotated areas
            results = list(
                filter(
                    lambda result: not any(
                        result.intersects(area)
                        for area in annotated_areas[f"{page_num}"]
                    ),
                    results,
                )
            )
            if not results:
                best_match = get_best_fuzzy_match(
                    page_text_cache[page_num], change  # Use cached text
                )
                if best_match and len(best_match) > 0:
                    results = doc_page.search_for(best_match)
                    print("found best fuzzy match: ", best_match)
                    # we only want the results that do not overlap with already annotated areas
                    results = list(
                        filter(
                            lambda result: not any(
                                result.intersects(area)
                                for area in annotated_areas[f"{page_num}"]
                            ),
                            results,
                        )
                    )
        if not results:
            print(f"No non-overlapping match found on page {page_num} for: '{text}'")
            failed_annotations.append({"change": change, "page": page_num})
            continue

        color = color_mapping.get(change_type, (1, 1, 0))

        annotated_areas[f"{page_num}"].append(results[0])
        highlight = doc_page.add_highlight_annot(results[0])
        highlight.set_colors({"stroke": color})
        highlight.set_info(
            info={
                "title": "Comment",
                "content": f"{change_type} - {change_str}\n{comment}",
                "name": change_type,
            }
        )
        highlight.update()
        successful_annotations += 1

        # if the resulting rects contain anything other than our search text we know it is a multiline highlight because for each line
        # we will have a new result rect. We need to check if the text in the rect is not equal to our search text but is inside of it
        for result in results[1:]:
            resulttext = doc_page.get_textbox(result)
            if (
                (resulttext.strip() != text.strip())
                & (resulttext.strip() in text.strip())
                # & (
                #     not any(
                #         result.intersects(area)
                #         for area in annotated_areas[f"{page_num}"]
                #     )
                # )
            ):
                highlight = doc_page.add_highlight_annot(result)
                highlight.set_colors({"stroke": color})
                highlight.update()
                annotated_areas[f"{page_num}"].append(result)
    add_infos_to_pdf_agentic(doc, changes, successful_annotations, extraction_method, nlp_preprocessing)
    add_failed_annotations_to_pdf_agentic(doc, failed_annotations)
    result_bytes = doc.tobytes()
    return result_bytes