Spaces:
Sleeping
Sleeping
File size: 10,654 Bytes
1cbec06 | 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | """Tests for the ALTO XML serializer."""
from __future__ import annotations
from lxml import etree
from src.app.domain.models import (
AltoReadiness,
CanonicalDocument,
EvidenceType,
Geometry,
GeometryStatus,
Hyphenation,
Page,
PageXmlReadiness,
Provenance,
ReadinessLevel,
Source,
TextLine,
TextRegion,
Word,
)
from src.app.domain.models.status import BlockRole, InputType
from src.app.serializers.alto_xml import ALTO_NS, serialize_alto, serialize_alto_to_string
def _prov(ref: str = "$.test") -> Provenance:
return Provenance(
provider="test",
adapter="test.v1",
source_ref=ref,
evidence_type=EvidenceType.PROVIDER_NATIVE,
)
def _geo(x: float = 100, y: float = 200, w: float = 300, h: float = 50) -> Geometry:
return Geometry(bbox=(x, y, w, h), status=GeometryStatus.EXACT)
def _simple_doc() -> CanonicalDocument:
"""A simple one-page document with one block, one line, two words."""
return CanonicalDocument(
document_id="doc_alto_test",
source=Source(input_type=InputType.IMAGE, filename="test.png"),
pages=[
Page(
id="p1",
page_index=0,
width=2480,
height=3508,
alto_readiness=AltoReadiness(level=ReadinessLevel.FULL),
page_readiness=PageXmlReadiness(level=ReadinessLevel.FULL),
reading_order=["tb1"],
text_regions=[
TextRegion(
id="tb1",
role=BlockRole.BODY,
geometry=_geo(100, 200, 1200, 900),
lang="fra",
provenance=_prov(),
lines=[
TextLine(
id="tl1",
geometry=_geo(110, 220, 1100, 42),
lang="fra",
provenance=_prov(),
words=[
Word(
id="w1",
text="Bonjour",
geometry=_geo(110, 220, 90, 40),
lang="fra",
confidence=0.96,
provenance=_prov(),
),
Word(
id="w2",
text="monde",
geometry=_geo(220, 220, 80, 40),
lang="fra",
confidence=0.94,
provenance=_prov(),
),
],
),
],
),
],
),
],
)
class TestAltoSerialization:
def test_produces_valid_xml(self) -> None:
doc = _simple_doc()
xml_bytes = serialize_alto(doc)
# Should parse without error
root = etree.fromstring(xml_bytes)
assert root.tag == f"{{{ALTO_NS}}}alto"
def test_has_description(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
desc = root.find(f"{{{ALTO_NS}}}Description")
assert desc is not None
def test_has_layout(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
layout = root.find(f"{{{ALTO_NS}}}Layout")
assert layout is not None
def test_page_attributes(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
page = root.find(f".//{{{ALTO_NS}}}Page")
assert page is not None
assert page.get("ID") == "p1"
assert page.get("WIDTH") == "2480"
assert page.get("HEIGHT") == "3508"
def test_text_block_exists(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
tb = root.find(f".//{{{ALTO_NS}}}TextBlock")
assert tb is not None
assert tb.get("ID") == "tb1"
def test_text_block_bbox(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
tb = root.find(f".//{{{ALTO_NS}}}TextBlock")
assert tb.get("HPOS") == "100"
assert tb.get("VPOS") == "200"
assert tb.get("WIDTH") == "1200"
assert tb.get("HEIGHT") == "900"
def test_text_line_exists(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
tl = root.find(f".//{{{ALTO_NS}}}TextLine")
assert tl is not None
assert tl.get("ID") == "tl1"
def test_strings_exist(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
strings = root.findall(f".//{{{ALTO_NS}}}String")
assert len(strings) == 2
def test_string_content(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
strings = root.findall(f".//{{{ALTO_NS}}}String")
assert strings[0].get("CONTENT") == "Bonjour"
assert strings[1].get("CONTENT") == "monde"
def test_string_bbox(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
s = root.findall(f".//{{{ALTO_NS}}}String")[0]
assert s.get("HPOS") == "110"
assert s.get("VPOS") == "220"
assert s.get("WIDTH") == "90"
assert s.get("HEIGHT") == "40"
def test_string_confidence(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
s = root.findall(f".//{{{ALTO_NS}}}String")[0]
assert s.get("WC") == "0.96"
def test_string_lang(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
tb = root.find(f".//{{{ALTO_NS}}}TextBlock")
assert tb.get("LANG") == "fra"
def test_sp_between_words(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
tl = root.find(f".//{{{ALTO_NS}}}TextLine")
children = list(tl)
# Should be: String, SP, String
tags = [c.tag.split("}")[-1] for c in children]
assert tags == ["String", "SP", "String"]
def test_measurement_unit(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
mu = root.find(f".//{{{ALTO_NS}}}MeasurementUnit")
assert mu is not None
assert mu.text == "pixel"
def test_filename_in_description(self) -> None:
doc = _simple_doc()
root = etree.fromstring(serialize_alto(doc))
fn = root.find(f".//{{{ALTO_NS}}}fileName")
assert fn is not None
assert fn.text == "test.png"
def test_string_output(self) -> None:
doc = _simple_doc()
xml_str = serialize_alto_to_string(doc)
assert '<?xml version=' in xml_str
assert "Bonjour" in xml_str
assert "monde" in xml_str
class TestAltoHyphenation:
def test_hyphenated_words(self) -> None:
doc = CanonicalDocument(
document_id="doc_hyph",
source=Source(input_type=InputType.IMAGE),
pages=[
Page(
id="p1",
page_index=0,
width=1000,
height=1000,
alto_readiness=AltoReadiness(level=ReadinessLevel.FULL),
page_readiness=PageXmlReadiness(level=ReadinessLevel.FULL),
text_regions=[
TextRegion(
id="tb1",
geometry=_geo(0, 0, 1000, 500),
provenance=_prov(),
lines=[
TextLine(
id="tl1",
geometry=_geo(0, 0, 500, 40),
provenance=_prov(),
words=[
Word(
id="w1",
text="patri-",
geometry=_geo(0, 0, 80, 30),
provenance=_prov(),
hyphenation=Hyphenation(
is_hyphenated=True,
part=1,
full_form="patrimoine",
),
),
],
),
TextLine(
id="tl2",
geometry=_geo(0, 50, 500, 40),
provenance=_prov(),
words=[
Word(
id="w2",
text="moine",
geometry=_geo(0, 50, 70, 30),
provenance=_prov(),
hyphenation=Hyphenation(
is_hyphenated=True,
part=2,
full_form="patrimoine",
),
),
],
),
],
),
],
),
],
)
root = etree.fromstring(serialize_alto(doc))
strings = root.findall(f".//{{{ALTO_NS}}}String")
assert len(strings) == 2
s1 = strings[0]
assert s1.get("CONTENT") == "patri-"
assert s1.get("SUBS_TYPE") == "HypPart1"
assert s1.get("SUBS_CONTENT") == "patrimoine"
s2 = strings[1]
assert s2.get("CONTENT") == "moine"
assert s2.get("SUBS_TYPE") == "HypPart2"
assert s2.get("SUBS_CONTENT") == "patrimoine"
|