robotframework-doctestlibrary 0.31.0__tar.gz → 0.32.0.dev2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- robotframework_doctestlibrary-0.32.0.dev2/.gitignore +82 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/Ai/__init__.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/Ai/prompts.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/Ai/renderers.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/Ai/responses.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/DocumentRepresentation.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/Downloader.py +0 -0
- robotframework_doctestlibrary-0.32.0.dev2/DocTest/HeaderFooterDetector.py +230 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/IgnoreAreaManager.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/Ocr.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/PdfStructureComparator.py +229 -3
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/PdfStructureModels.py +82 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/PdfTest.py +163 -29
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/PrintJob.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/PrintJobTests.py +0 -0
- robotframework_doctestlibrary-0.32.0.dev2/DocTest/StructureReportBuilder.py +558 -0
- robotframework_doctestlibrary-0.32.0.dev2/DocTest/TextNormalization.py +117 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/VisualTest.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/__init__.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/config.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/llm/__init__.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/llm/client.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/llm/config.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/llm/exceptions.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/llm/prompts.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/llm/types.py +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/LICENSE +0 -0
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/PKG-INFO +23 -39
- {robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/README.md +0 -0
- robotframework_doctestlibrary-0.32.0.dev2/pyproject.toml +61 -0
- robotframework_doctestlibrary-0.31.0/DocTest/TextNormalization.py +0 -54
- robotframework_doctestlibrary-0.31.0/pyproject.toml +0 -65
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Python artifacts
|
|
2
|
+
*.pyc
|
|
3
|
+
*.pyo
|
|
4
|
+
*.pyd
|
|
5
|
+
.Python
|
|
6
|
+
__pycache__/
|
|
7
|
+
|
|
8
|
+
# Virtual environments
|
|
9
|
+
.venv*/
|
|
10
|
+
env/
|
|
11
|
+
venv/
|
|
12
|
+
ENV/
|
|
13
|
+
env.bak/
|
|
14
|
+
venv.bak/
|
|
15
|
+
.env
|
|
16
|
+
|
|
17
|
+
# Distribution / packaging
|
|
18
|
+
dist/
|
|
19
|
+
build/
|
|
20
|
+
*.egg-info/
|
|
21
|
+
.eggs/
|
|
22
|
+
develop-eggs/
|
|
23
|
+
downloads/
|
|
24
|
+
eggs/
|
|
25
|
+
lib/
|
|
26
|
+
lib64/
|
|
27
|
+
parts/
|
|
28
|
+
sdist/
|
|
29
|
+
var/
|
|
30
|
+
wheels/
|
|
31
|
+
share/python-wheels/
|
|
32
|
+
.installed.cfg
|
|
33
|
+
*.egg
|
|
34
|
+
MANIFEST
|
|
35
|
+
|
|
36
|
+
# Testing
|
|
37
|
+
.pytest_cache/
|
|
38
|
+
.tox/
|
|
39
|
+
.nox/
|
|
40
|
+
.cache
|
|
41
|
+
nosetests.xml
|
|
42
|
+
coverage.xml
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
*.cover
|
|
46
|
+
.hypothesis
|
|
47
|
+
|
|
48
|
+
# Logs and reports
|
|
49
|
+
logs/
|
|
50
|
+
log.html
|
|
51
|
+
output.xml
|
|
52
|
+
report.html
|
|
53
|
+
*.log
|
|
54
|
+
pip-log.txt
|
|
55
|
+
pip-delete-this-directory.txt
|
|
56
|
+
|
|
57
|
+
# Project specific
|
|
58
|
+
screenshots/
|
|
59
|
+
results/
|
|
60
|
+
robotframework_doctestlibrary.egg-info/
|
|
61
|
+
|
|
62
|
+
# IDE
|
|
63
|
+
.idea/
|
|
64
|
+
.vscode/
|
|
65
|
+
.mypy_cache
|
|
66
|
+
|
|
67
|
+
# OS files
|
|
68
|
+
.DS_Store
|
|
69
|
+
Thumbs.db
|
|
70
|
+
*.swp
|
|
71
|
+
*.swo
|
|
72
|
+
*~
|
|
73
|
+
|
|
74
|
+
# Version control
|
|
75
|
+
.git
|
|
76
|
+
|
|
77
|
+
# Internal docs
|
|
78
|
+
release*.md
|
|
79
|
+
RELEASE*.md
|
|
80
|
+
|
|
81
|
+
# Models and datasets
|
|
82
|
+
*.pb
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"""Repetition-based header/footer detection for PDF structure comparison.
|
|
2
|
+
|
|
3
|
+
Scans configurable vertical regions at the top/bottom of each page, identifies
|
|
4
|
+
text lines that repeat across multiple pages (with digit normalization for page
|
|
5
|
+
numbers), and removes them from the DocumentStructure before comparison.
|
|
6
|
+
|
|
7
|
+
This module is a pure-function domain service with no side effects, no Robot
|
|
8
|
+
Framework dependency, and no I/O.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import re
|
|
14
|
+
from collections import defaultdict
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
from typing import Dict, FrozenSet, List, Set
|
|
17
|
+
|
|
18
|
+
from DocTest.PdfStructureModels import (
|
|
19
|
+
DocumentStructure,
|
|
20
|
+
PageStructure,
|
|
21
|
+
TextBlock,
|
|
22
|
+
TextLine,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"HeaderFooterConfig",
|
|
27
|
+
"DetectionResult",
|
|
28
|
+
"detect_repeating_headers_footers",
|
|
29
|
+
"strip_detected_headers_footers",
|
|
30
|
+
"filter_headers_footers",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
_DIGIT_RUN_RE = re.compile(r"\d+")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class HeaderFooterConfig:
|
|
38
|
+
"""Configuration for repetition-based header/footer detection."""
|
|
39
|
+
|
|
40
|
+
header_scan_height: float = 0.0
|
|
41
|
+
footer_scan_height: float = 0.0
|
|
42
|
+
repeat_threshold: int = 2
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def enabled(self) -> bool:
|
|
46
|
+
"""Return True if at least one scan region is configured."""
|
|
47
|
+
return self.header_scan_height > 0 or self.footer_scan_height > 0
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass(frozen=True)
|
|
51
|
+
class DetectionResult:
|
|
52
|
+
"""Immutable record of which normalized keys were detected as headers/footers."""
|
|
53
|
+
|
|
54
|
+
header_keys: FrozenSet[str]
|
|
55
|
+
footer_keys: FrozenSet[str]
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def has_detections(self) -> bool:
|
|
59
|
+
return bool(self.header_keys or self.footer_keys)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _normalize_for_grouping(text: str) -> str:
|
|
63
|
+
"""Replace all digit runs with '#' so page-number variants group together.
|
|
64
|
+
|
|
65
|
+
Examples:
|
|
66
|
+
"Page 1 of 5" -> "Page # of #"
|
|
67
|
+
"ACME Corp" -> "ACME Corp" (no digits, unchanged)
|
|
68
|
+
"- 3 -" -> "- # -"
|
|
69
|
+
"""
|
|
70
|
+
return _DIGIT_RUN_RE.sub("#", text)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def detect_repeating_headers_footers(
|
|
74
|
+
structure: DocumentStructure,
|
|
75
|
+
config: HeaderFooterConfig,
|
|
76
|
+
) -> DetectionResult:
|
|
77
|
+
"""Scan a DocumentStructure and identify text that repeats across pages
|
|
78
|
+
in the header/footer regions.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
structure: The document to scan.
|
|
82
|
+
config: Detection parameters (scan heights and threshold).
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
A DetectionResult containing the normalized keys of detected
|
|
86
|
+
header and footer lines.
|
|
87
|
+
"""
|
|
88
|
+
if not config.enabled:
|
|
89
|
+
return DetectionResult(header_keys=frozenset(), footer_keys=frozenset())
|
|
90
|
+
|
|
91
|
+
header_candidates: Dict[str, Set[int]] = defaultdict(set)
|
|
92
|
+
footer_candidates: Dict[str, Set[int]] = defaultdict(set)
|
|
93
|
+
|
|
94
|
+
for page in structure.pages:
|
|
95
|
+
footer_boundary = page.height - config.footer_scan_height
|
|
96
|
+
|
|
97
|
+
for block in page.blocks:
|
|
98
|
+
for line in block.lines:
|
|
99
|
+
text = line.text or ""
|
|
100
|
+
if not text:
|
|
101
|
+
continue
|
|
102
|
+
key = _normalize_for_grouping(text)
|
|
103
|
+
|
|
104
|
+
# Check header region
|
|
105
|
+
if config.header_scan_height > 0 and line.bbox[1] < config.header_scan_height:
|
|
106
|
+
header_candidates[key].add(page.page_number)
|
|
107
|
+
|
|
108
|
+
# Check footer region
|
|
109
|
+
if config.footer_scan_height > 0 and line.bbox[3] > footer_boundary:
|
|
110
|
+
footer_candidates[key].add(page.page_number)
|
|
111
|
+
|
|
112
|
+
detected_header_keys = frozenset(
|
|
113
|
+
key for key, pages in header_candidates.items() if len(pages) >= config.repeat_threshold
|
|
114
|
+
)
|
|
115
|
+
detected_footer_keys = frozenset(
|
|
116
|
+
key for key, pages in footer_candidates.items() if len(pages) >= config.repeat_threshold
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
return DetectionResult(
|
|
120
|
+
header_keys=detected_header_keys,
|
|
121
|
+
footer_keys=detected_footer_keys,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def strip_detected_headers_footers(
|
|
126
|
+
structure: DocumentStructure,
|
|
127
|
+
detection: DetectionResult,
|
|
128
|
+
config: HeaderFooterConfig,
|
|
129
|
+
) -> DocumentStructure:
|
|
130
|
+
"""Remove detected header/footer lines from a DocumentStructure.
|
|
131
|
+
|
|
132
|
+
Only lines that (a) match a detected normalized key AND (b) fall within
|
|
133
|
+
the configured scan region are removed. Body lines with identical text
|
|
134
|
+
are preserved.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
structure: The document to filter.
|
|
138
|
+
detection: The detection result from detect_repeating_headers_footers().
|
|
139
|
+
config: The same config used for detection (needed for region bounds).
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
A new DocumentStructure with header/footer lines removed.
|
|
143
|
+
"""
|
|
144
|
+
if not detection.has_detections:
|
|
145
|
+
return structure
|
|
146
|
+
|
|
147
|
+
filtered_pages: List[PageStructure] = []
|
|
148
|
+
|
|
149
|
+
for page in structure.pages:
|
|
150
|
+
footer_boundary = page.height - config.footer_scan_height
|
|
151
|
+
new_blocks: List[TextBlock] = []
|
|
152
|
+
next_line_index = 0
|
|
153
|
+
|
|
154
|
+
for block in page.blocks:
|
|
155
|
+
new_lines: List[TextLine] = []
|
|
156
|
+
|
|
157
|
+
for line in block.lines:
|
|
158
|
+
text = line.text or ""
|
|
159
|
+
key = _normalize_for_grouping(text)
|
|
160
|
+
|
|
161
|
+
# Remove if line is in header region AND matches a detected header key
|
|
162
|
+
if (
|
|
163
|
+
config.header_scan_height > 0
|
|
164
|
+
and line.bbox[1] < config.header_scan_height
|
|
165
|
+
and key in detection.header_keys
|
|
166
|
+
):
|
|
167
|
+
continue
|
|
168
|
+
|
|
169
|
+
# Remove if line is in footer region AND matches a detected footer key
|
|
170
|
+
if (
|
|
171
|
+
config.footer_scan_height > 0
|
|
172
|
+
and line.bbox[3] > footer_boundary
|
|
173
|
+
and key in detection.footer_keys
|
|
174
|
+
):
|
|
175
|
+
continue
|
|
176
|
+
|
|
177
|
+
new_lines.append(
|
|
178
|
+
TextLine(
|
|
179
|
+
index=next_line_index,
|
|
180
|
+
text=text,
|
|
181
|
+
bbox=line.bbox,
|
|
182
|
+
fonts=set(line.fonts),
|
|
183
|
+
spans=list(line.spans),
|
|
184
|
+
)
|
|
185
|
+
)
|
|
186
|
+
next_line_index += 1
|
|
187
|
+
|
|
188
|
+
if new_lines:
|
|
189
|
+
new_blocks.append(
|
|
190
|
+
TextBlock(
|
|
191
|
+
index=block.index,
|
|
192
|
+
bbox=block.bbox,
|
|
193
|
+
lines=new_lines,
|
|
194
|
+
)
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
filtered_pages.append(
|
|
198
|
+
PageStructure(
|
|
199
|
+
page_number=page.page_number,
|
|
200
|
+
width=page.width,
|
|
201
|
+
height=page.height,
|
|
202
|
+
blocks=new_blocks,
|
|
203
|
+
)
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
return DocumentStructure(pages=filtered_pages, config=structure.config)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def filter_headers_footers(
|
|
210
|
+
structure: DocumentStructure,
|
|
211
|
+
config: HeaderFooterConfig,
|
|
212
|
+
) -> DocumentStructure:
|
|
213
|
+
"""Convenience function: detect and strip in one call.
|
|
214
|
+
|
|
215
|
+
Equivalent to:
|
|
216
|
+
detection = detect_repeating_headers_footers(structure, config)
|
|
217
|
+
return strip_detected_headers_footers(structure, detection, config)
|
|
218
|
+
|
|
219
|
+
Args:
|
|
220
|
+
structure: The document to process.
|
|
221
|
+
config: Detection parameters.
|
|
222
|
+
|
|
223
|
+
Returns:
|
|
224
|
+
A new DocumentStructure with detected repeating headers/footers removed.
|
|
225
|
+
If config.enabled is False, returns the input unchanged.
|
|
226
|
+
"""
|
|
227
|
+
if not config.enabled:
|
|
228
|
+
return structure
|
|
229
|
+
detection = detect_repeating_headers_footers(structure, config)
|
|
230
|
+
return strip_detected_headers_footers(structure, detection, config)
|
|
File without changes
|
{robotframework_doctestlibrary-0.31.0 → robotframework_doctestlibrary-0.32.0.dev2}/DocTest/Ocr.py
RENAMED
|
File without changes
|
|
@@ -4,16 +4,18 @@ import difflib
|
|
|
4
4
|
from dataclasses import dataclass, field
|
|
5
5
|
from typing import Dict, Iterable, List, Optional, Sequence, Tuple
|
|
6
6
|
|
|
7
|
-
from DocTest.PdfStructureModels import DocumentStructure, PageStructure, TextLine
|
|
7
|
+
from DocTest.PdfStructureModels import DocumentStructure, PageStructure, TextLine, WordToken
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
__all__ = [
|
|
11
11
|
"StructureTolerance",
|
|
12
12
|
"LineDifference",
|
|
13
13
|
"DocumentTextDifference",
|
|
14
|
+
"DocumentWordDifference",
|
|
14
15
|
"StructureComparisonResult",
|
|
15
16
|
"compare_document_structures",
|
|
16
17
|
"compare_document_text_only",
|
|
18
|
+
"compare_document_words",
|
|
17
19
|
]
|
|
18
20
|
|
|
19
21
|
|
|
@@ -54,6 +56,20 @@ class DocumentTextDifference:
|
|
|
54
56
|
cand_index: Optional[int] = None
|
|
55
57
|
|
|
56
58
|
|
|
59
|
+
@dataclass
|
|
60
|
+
class DocumentWordDifference:
|
|
61
|
+
"""Details about word-level content mismatch in page-agnostic comparison."""
|
|
62
|
+
|
|
63
|
+
diff_type: str # "missing_words", "extra_words", "word_mismatch"
|
|
64
|
+
message: str
|
|
65
|
+
ref_words: Optional[List[str]] = None
|
|
66
|
+
cand_words: Optional[List[str]] = None
|
|
67
|
+
ref_start_index: Optional[int] = None
|
|
68
|
+
ref_end_index: Optional[int] = None
|
|
69
|
+
cand_start_index: Optional[int] = None
|
|
70
|
+
cand_end_index: Optional[int] = None
|
|
71
|
+
|
|
72
|
+
|
|
57
73
|
@dataclass
|
|
58
74
|
class StructureComparisonResult:
|
|
59
75
|
"""Aggregate differences found during structure comparison."""
|
|
@@ -61,6 +77,7 @@ class StructureComparisonResult:
|
|
|
61
77
|
passed: bool = True
|
|
62
78
|
page_differences: Dict[int, List[LineDifference]] = field(default_factory=dict)
|
|
63
79
|
document_differences: List[DocumentTextDifference] = field(default_factory=list)
|
|
80
|
+
word_differences: List[DocumentWordDifference] = field(default_factory=list)
|
|
64
81
|
summary: List[str] = field(default_factory=list)
|
|
65
82
|
|
|
66
83
|
def add_difference(self, diff: LineDifference):
|
|
@@ -72,13 +89,18 @@ class StructureComparisonResult:
|
|
|
72
89
|
self.passed = False
|
|
73
90
|
self.document_differences.append(diff)
|
|
74
91
|
|
|
92
|
+
def add_word_difference(self, diff: DocumentWordDifference):
|
|
93
|
+
"""Add a document-level word difference."""
|
|
94
|
+
self.passed = False
|
|
95
|
+
self.word_differences.append(diff)
|
|
96
|
+
|
|
75
97
|
def extend_summary(self, message: str):
|
|
76
98
|
self.summary.append(message)
|
|
77
99
|
|
|
78
100
|
def difference_count(self) -> int:
|
|
79
|
-
"""Return total count of all differences (page-level and
|
|
101
|
+
"""Return total count of all differences (page-level, document-level, and word-level)."""
|
|
80
102
|
page_diff_count = sum(len(diffs) for diffs in self.page_differences.values())
|
|
81
|
-
return page_diff_count + len(self.document_differences)
|
|
103
|
+
return page_diff_count + len(self.document_differences) + len(self.word_differences)
|
|
82
104
|
|
|
83
105
|
|
|
84
106
|
def compare_document_structures(
|
|
@@ -229,6 +251,210 @@ def compare_document_text_only(
|
|
|
229
251
|
return result
|
|
230
252
|
|
|
231
253
|
|
|
254
|
+
def _compare_words_unordered(
|
|
255
|
+
ref_words: List[str],
|
|
256
|
+
ref_originals: List[str],
|
|
257
|
+
cand_words: List[str],
|
|
258
|
+
cand_originals: List[str],
|
|
259
|
+
) -> StructureComparisonResult:
|
|
260
|
+
"""Compare words using bag-of-words (Counter-based) comparison.
|
|
261
|
+
|
|
262
|
+
This mode ignores word order entirely and only checks that both documents
|
|
263
|
+
contain the same words with the same frequencies. It is useful when text
|
|
264
|
+
reflows across pages cause identical content to appear in different order.
|
|
265
|
+
|
|
266
|
+
Excess words in the reference are reported as ``missing_words``, excess
|
|
267
|
+
words in the candidate as ``extra_words``.
|
|
268
|
+
"""
|
|
269
|
+
from collections import Counter
|
|
270
|
+
|
|
271
|
+
result = StructureComparisonResult()
|
|
272
|
+
|
|
273
|
+
ref_counts = Counter(ref_words)
|
|
274
|
+
cand_counts = Counter(cand_words)
|
|
275
|
+
|
|
276
|
+
# Words that appear more in reference than candidate (missing from candidate)
|
|
277
|
+
ref_excess = ref_counts - cand_counts
|
|
278
|
+
# Words that appear more in candidate than reference (extra in candidate)
|
|
279
|
+
cand_excess = cand_counts - ref_counts
|
|
280
|
+
|
|
281
|
+
# Build original-text lists for reporting by scanning the original arrays
|
|
282
|
+
# and picking up excess instances
|
|
283
|
+
if ref_excess:
|
|
284
|
+
remaining = dict(ref_excess)
|
|
285
|
+
excess_originals: List[str] = []
|
|
286
|
+
for norm_word, orig_word in zip(ref_words, ref_originals):
|
|
287
|
+
if remaining.get(norm_word, 0) > 0:
|
|
288
|
+
excess_originals.append(orig_word)
|
|
289
|
+
remaining[norm_word] -= 1
|
|
290
|
+
if excess_originals:
|
|
291
|
+
preview = " ".join(excess_originals[:10])
|
|
292
|
+
if len(excess_originals) > 10:
|
|
293
|
+
preview += f" ... (+{len(excess_originals) - 10} more)"
|
|
294
|
+
result.add_word_difference(
|
|
295
|
+
DocumentWordDifference(
|
|
296
|
+
diff_type="missing_words",
|
|
297
|
+
message=f"Words in reference not found in candidate (unordered): '{_truncate_text(preview, 120)}'",
|
|
298
|
+
ref_words=excess_originals,
|
|
299
|
+
ref_start_index=0,
|
|
300
|
+
ref_end_index=len(excess_originals),
|
|
301
|
+
)
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
if cand_excess:
|
|
305
|
+
remaining_cand = dict(cand_excess)
|
|
306
|
+
cand_excess_originals: List[str] = []
|
|
307
|
+
for norm_word, orig_word in zip(cand_words, cand_originals):
|
|
308
|
+
if remaining_cand.get(norm_word, 0) > 0:
|
|
309
|
+
cand_excess_originals.append(orig_word)
|
|
310
|
+
remaining_cand[norm_word] -= 1
|
|
311
|
+
if cand_excess_originals:
|
|
312
|
+
preview = " ".join(cand_excess_originals[:10])
|
|
313
|
+
if len(cand_excess_originals) > 10:
|
|
314
|
+
preview += f" ... (+{len(cand_excess_originals) - 10} more)"
|
|
315
|
+
result.add_word_difference(
|
|
316
|
+
DocumentWordDifference(
|
|
317
|
+
diff_type="extra_words",
|
|
318
|
+
message=f"Extra words in candidate not found in reference (unordered): '{_truncate_text(preview, 120)}'",
|
|
319
|
+
cand_words=cand_excess_originals,
|
|
320
|
+
cand_start_index=0,
|
|
321
|
+
cand_end_index=len(cand_excess_originals),
|
|
322
|
+
)
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
return result
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def compare_document_words(
|
|
329
|
+
reference: DocumentStructure,
|
|
330
|
+
candidate: DocumentStructure,
|
|
331
|
+
*,
|
|
332
|
+
case_sensitive: bool = True,
|
|
333
|
+
normalize_ligatures: bool = False,
|
|
334
|
+
normalize_word_boundaries: bool = False,
|
|
335
|
+
compare_order: str = "ordered",
|
|
336
|
+
) -> StructureComparisonResult:
|
|
337
|
+
"""Compare document text at the word level, ignoring line and page boundaries.
|
|
338
|
+
|
|
339
|
+
Flattens all text into word tokens and uses SequenceMatcher to detect
|
|
340
|
+
insertions, deletions, and replacements at word granularity. Contiguous
|
|
341
|
+
diff opcodes of the same type are grouped into single difference records
|
|
342
|
+
for cleaner reporting.
|
|
343
|
+
|
|
344
|
+
Args:
|
|
345
|
+
reference: The reference document structure.
|
|
346
|
+
candidate: The candidate document structure to compare.
|
|
347
|
+
case_sensitive: Whether word comparison is case-sensitive.
|
|
348
|
+
normalize_ligatures: When True, replace known typographic ligatures
|
|
349
|
+
with their ASCII equivalents in each word before comparison.
|
|
350
|
+
normalize_word_boundaries: When True, merge tokens that were split
|
|
351
|
+
across line boundaries by connector characters (``/``, ``-``, ``\\``).
|
|
352
|
+
compare_order: Comparison strategy. ``"ordered"`` (default) uses
|
|
353
|
+
SequenceMatcher for sequence-sensitive comparison. ``"unordered"``
|
|
354
|
+
uses Counter-based bag-of-words comparison that ignores word order,
|
|
355
|
+
useful when text reflows across pages.
|
|
356
|
+
|
|
357
|
+
Returns:
|
|
358
|
+
A StructureComparisonResult with document-level word differences.
|
|
359
|
+
"""
|
|
360
|
+
from DocTest.PdfStructureModels import flatten_document_words
|
|
361
|
+
|
|
362
|
+
result = StructureComparisonResult()
|
|
363
|
+
|
|
364
|
+
ref_words, ref_tokens = flatten_document_words(
|
|
365
|
+
reference,
|
|
366
|
+
normalize_word_boundaries=normalize_word_boundaries,
|
|
367
|
+
normalize_ligatures_in_words=normalize_ligatures,
|
|
368
|
+
)
|
|
369
|
+
cand_words, cand_tokens = flatten_document_words(
|
|
370
|
+
candidate,
|
|
371
|
+
normalize_word_boundaries=normalize_word_boundaries,
|
|
372
|
+
normalize_ligatures_in_words=normalize_ligatures,
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
# Preserve originals for reporting before potential case normalization
|
|
376
|
+
ref_originals = list(ref_words)
|
|
377
|
+
cand_originals = list(cand_words)
|
|
378
|
+
|
|
379
|
+
if not case_sensitive:
|
|
380
|
+
ref_words = [w.lower() for w in ref_words]
|
|
381
|
+
cand_words = [w.lower() for w in cand_words]
|
|
382
|
+
|
|
383
|
+
if compare_order == "unordered":
|
|
384
|
+
return _compare_words_unordered(ref_words, ref_originals, cand_words, cand_originals)
|
|
385
|
+
|
|
386
|
+
matcher = difflib.SequenceMatcher(a=ref_words, b=cand_words, autojunk=False)
|
|
387
|
+
|
|
388
|
+
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
|
|
389
|
+
if tag == "equal":
|
|
390
|
+
continue
|
|
391
|
+
|
|
392
|
+
ref_slice = ref_originals[i1:i2] if i1 < i2 else None
|
|
393
|
+
cand_slice = cand_originals[j1:j2] if j1 < j2 else None
|
|
394
|
+
|
|
395
|
+
if tag == "replace":
|
|
396
|
+
ref_preview = " ".join(ref_slice) if ref_slice else ""
|
|
397
|
+
cand_preview = " ".join(cand_slice) if cand_slice else ""
|
|
398
|
+
message = (
|
|
399
|
+
f"Word mismatch at positions {i1}-{i2 - 1}: "
|
|
400
|
+
f"reference='{_truncate_text(ref_preview, 80)}', "
|
|
401
|
+
f"candidate='{_truncate_text(cand_preview, 80)}'"
|
|
402
|
+
)
|
|
403
|
+
result.add_word_difference(
|
|
404
|
+
DocumentWordDifference(
|
|
405
|
+
diff_type="word_mismatch",
|
|
406
|
+
message=message,
|
|
407
|
+
ref_words=ref_slice,
|
|
408
|
+
cand_words=cand_slice,
|
|
409
|
+
ref_start_index=i1,
|
|
410
|
+
ref_end_index=i2,
|
|
411
|
+
cand_start_index=j1,
|
|
412
|
+
cand_end_index=j2,
|
|
413
|
+
)
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
elif tag == "delete":
|
|
417
|
+
ref_preview = " ".join(ref_slice) if ref_slice else ""
|
|
418
|
+
message = (
|
|
419
|
+
f"Words missing in candidate at positions {i1}-{i2 - 1}: "
|
|
420
|
+
f"'{_truncate_text(ref_preview, 80)}'"
|
|
421
|
+
)
|
|
422
|
+
result.add_word_difference(
|
|
423
|
+
DocumentWordDifference(
|
|
424
|
+
diff_type="missing_words",
|
|
425
|
+
message=message,
|
|
426
|
+
ref_words=ref_slice,
|
|
427
|
+
ref_start_index=i1,
|
|
428
|
+
ref_end_index=i2,
|
|
429
|
+
)
|
|
430
|
+
)
|
|
431
|
+
|
|
432
|
+
elif tag == "insert":
|
|
433
|
+
cand_preview = " ".join(cand_slice) if cand_slice else ""
|
|
434
|
+
message = (
|
|
435
|
+
f"Extra words in candidate at positions {j1}-{j2 - 1}: "
|
|
436
|
+
f"'{_truncate_text(cand_preview, 80)}'"
|
|
437
|
+
)
|
|
438
|
+
result.add_word_difference(
|
|
439
|
+
DocumentWordDifference(
|
|
440
|
+
diff_type="extra_words",
|
|
441
|
+
message=message,
|
|
442
|
+
cand_words=cand_slice,
|
|
443
|
+
cand_start_index=j1,
|
|
444
|
+
cand_end_index=j2,
|
|
445
|
+
)
|
|
446
|
+
)
|
|
447
|
+
|
|
448
|
+
return result
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def _truncate_text(text: str, max_length: int) -> str:
|
|
452
|
+
"""Truncate text with ellipsis if it exceeds max_length."""
|
|
453
|
+
if len(text) <= max_length:
|
|
454
|
+
return text
|
|
455
|
+
return text[: max_length - 3] + "..."
|
|
456
|
+
|
|
457
|
+
|
|
232
458
|
def _compare_page(
|
|
233
459
|
ref_page: PageStructure,
|
|
234
460
|
cand_page: PageStructure,
|