pygexml 0.1.2__py3-none-any.whl

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.
pygexml/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .page import Page
2
+
3
+ __all__ = ["Page"]
pygexml/geometry.py ADDED
@@ -0,0 +1,57 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ class GeometryError(Exception):
5
+ pass
6
+
7
+
8
+ @dataclass
9
+ class Point:
10
+ x: int
11
+ y: int
12
+
13
+ def __str__(self) -> str:
14
+ return f"{self.x},{self.y}"
15
+
16
+
17
+ @dataclass
18
+ class Box:
19
+ top_left: Point
20
+ bottom_right: Point
21
+
22
+ def __post_init__(self) -> None:
23
+ if (
24
+ self.top_left.x > self.bottom_right.x
25
+ or self.top_left.y > self.bottom_right.y
26
+ ):
27
+ raise GeometryError("Box: top left is not top left")
28
+
29
+ def width(self) -> int:
30
+ return self.bottom_right.x - self.top_left.x
31
+
32
+ def height(self) -> int:
33
+ return self.bottom_right.y - self.top_left.y
34
+
35
+ def contains(self, point: Point) -> bool:
36
+ return (
37
+ self.top_left.x <= point.x <= self.bottom_right.x
38
+ and self.top_left.y <= point.y <= self.bottom_right.y
39
+ )
40
+
41
+
42
+ @dataclass
43
+ class Polygon:
44
+ points: list[Point]
45
+
46
+ def __post_init__(self) -> None:
47
+ if len(self.points) < 1:
48
+ raise GeometryError("Polygon: points must not be empty")
49
+
50
+ def bounding_box(self) -> Box:
51
+ min_x = min(point.x for point in self.points)
52
+ max_x = max(point.x for point in self.points)
53
+ min_y = min(point.y for point in self.points)
54
+ max_y = max(point.y for point in self.points)
55
+ return Box(
56
+ top_left=Point(x=min_x, y=min_y), bottom_right=Point(x=max_x, y=max_y)
57
+ )
pygexml/page.py ADDED
@@ -0,0 +1,188 @@
1
+ from re import Pattern, compile
2
+ from warnings import warn
3
+ from dataclasses import dataclass
4
+ from typing import ClassVar
5
+ from collections.abc import Iterable
6
+ from lxml import etree
7
+ from lxml.etree import _Element as Element, QName
8
+
9
+ from .geometry import Point, Polygon, GeometryError
10
+
11
+
12
+ def find_child(element: Element, name: str) -> Element | None:
13
+ for child in element:
14
+ if QName(child).localname == name:
15
+ return child
16
+ return None
17
+
18
+
19
+ def find_children(element: Element, name: str) -> Iterable[Element]:
20
+ return (child for child in element if QName(child).localname == name)
21
+
22
+
23
+ class PageXMLError(Exception):
24
+ pass
25
+
26
+
27
+ @dataclass
28
+ class Coords:
29
+ polygon: Polygon
30
+
31
+ # Loose regex that allows for negative values that can be handled by
32
+ # our code. Context: PeroOCR sometimes produces PageXML with negative
33
+ # coordinate values.
34
+ # https://github.com/DCGM/pero-ocr/issues/84#issuecomment-3745059403
35
+ LOOSE_PATTERN: ClassVar[Pattern[str]] = compile(
36
+ r"^(-?[0-9]+,-?[0-9]+ )+(-?[0-9]+,-?[0-9]+)$"
37
+ )
38
+
39
+ # Regex from official Page-XML spec
40
+ STRICT_PATTERN: ClassVar[Pattern[str]] = compile(
41
+ r"^([0-9]+,[0-9]+ )+([0-9]+,[0-9]+)$"
42
+ )
43
+
44
+ _NOT_ENOUGH_POINTS: ClassVar[str] = "Coords: at least 2 Points are required"
45
+
46
+ def __post_init__(self) -> None:
47
+ if len(self.polygon.points) < 2:
48
+ raise PageXMLError(Coords._NOT_ENOUGH_POINTS)
49
+
50
+ def __str__(self) -> str:
51
+ return " ".join(str(p) for p in self.polygon.points)
52
+
53
+ @classmethod
54
+ def parse(cls, points_str: str) -> "Coords":
55
+
56
+ if not cls.LOOSE_PATTERN.match(points_str):
57
+ raise PageXMLError("Invalid Coords XML string")
58
+
59
+ if not cls.STRICT_PATTERN.match(points_str):
60
+ warn(
61
+ "Warning: Coords XML string does not match the PAGE XMl spec: "
62
+ + points_str
63
+ )
64
+
65
+ points: list[Point] = []
66
+ for pair_str in points_str.split(" "):
67
+ [x, y] = pair_str.split(",")
68
+ points.append(Point(x=int(x), y=int(y)))
69
+
70
+ try:
71
+ polygon = Polygon(points=points)
72
+ except GeometryError:
73
+ raise PageXMLError(cls._NOT_ENOUGH_POINTS)
74
+
75
+ return Coords(polygon=polygon)
76
+
77
+
78
+ type ID = str
79
+
80
+
81
+ @dataclass
82
+ class TextLine:
83
+ id: ID
84
+ coords: Coords
85
+ text: str
86
+
87
+ def words(self) -> Iterable[str]:
88
+ return self.text.split()
89
+
90
+ @classmethod
91
+ def from_xml(cls, element: Element) -> "TextLine":
92
+ if QName(element).localname != "TextLine":
93
+ raise PageXMLError("TextLine: wrong element given")
94
+ if "id" not in element.attrib:
95
+ raise PageXMLError("TextLine: no id found")
96
+ coords_element = find_child(element, "Coords")
97
+ if coords_element is None:
98
+ raise PageXMLError("TextLine: no Coords found")
99
+ if "points" not in coords_element.attrib:
100
+ raise PageXMLError("TextLine: Coords has no points attribute")
101
+ text_equiv = find_child(element, "TextEquiv")
102
+ text_element = (
103
+ find_child(text_equiv, "Unicode") if text_equiv is not None else None
104
+ )
105
+ if text_element is None:
106
+ raise PageXMLError("TextLine: no text found")
107
+ return TextLine(
108
+ id=str(element.attrib["id"]),
109
+ coords=Coords.parse(str(coords_element.attrib["points"])),
110
+ text=text_element.text if text_element.text is not None else "",
111
+ )
112
+
113
+
114
+ @dataclass
115
+ class TextRegion:
116
+ id: ID
117
+ coords: Coords
118
+ textlines: dict[ID, TextLine]
119
+
120
+ @classmethod
121
+ def from_xml(cls, element: Element) -> "TextRegion":
122
+ if QName(element).localname != "TextRegion":
123
+ raise PageXMLError("TextRegion: wrong element given")
124
+ if "id" not in element.attrib:
125
+ raise PageXMLError("TextRegion: no id found")
126
+ coords_element = find_child(element, "Coords")
127
+ if coords_element is None:
128
+ raise PageXMLError("TextRegion: no Coords element found")
129
+ if "points" not in coords_element.attrib:
130
+ raise PageXMLError("TextRegion: Coords has no points attribute")
131
+ text_lines = find_children(element, "TextLine")
132
+
133
+ return TextRegion(
134
+ id=str(element.attrib["id"]),
135
+ coords=Coords.parse(str(coords_element.attrib["points"])),
136
+ textlines={
137
+ tl.id: tl for tl in (TextLine.from_xml(tl) for tl in text_lines)
138
+ },
139
+ )
140
+
141
+ def lookup_textline(self, id: ID) -> TextLine | None:
142
+ return self.textlines.get(id)
143
+
144
+ def all_text(self) -> Iterable[str]:
145
+ return (tl.text for tl in self.textlines.values())
146
+
147
+ def all_words(self) -> Iterable[str]:
148
+ return (w for tl in self.textlines.values() for w in tl.words())
149
+
150
+
151
+ @dataclass
152
+ class Page:
153
+ image_filename: str
154
+ regions: dict[ID, TextRegion]
155
+
156
+ @classmethod
157
+ def from_xml(cls, element: Element) -> "Page":
158
+ if QName(element).localname != "Page":
159
+ raise PageXMLError("Page: wrong element given")
160
+
161
+ if "imageFilename" not in element.attrib:
162
+ raise PageXMLError("Page: no filename found")
163
+
164
+ regions = find_children(element, "TextRegion")
165
+
166
+ return Page(
167
+ image_filename=str(element.attrib["imageFilename"]),
168
+ regions={
169
+ tr.id: tr for tr in (TextRegion.from_xml(region) for region in regions)
170
+ },
171
+ )
172
+
173
+ @classmethod
174
+ def from_xml_string(cls, xml_str: str) -> "Page":
175
+ root = etree.fromstring(xml_str.encode("utf-8"))
176
+ page_element = find_child(root, "Page")
177
+ if page_element is None:
178
+ raise PageXMLError("Page: no page element found")
179
+ return cls.from_xml(page_element)
180
+
181
+ def lookup_region(self, id: ID) -> TextRegion | None:
182
+ return self.regions.get(id)
183
+
184
+ def all_text(self) -> Iterable[str]:
185
+ return (line for region in self.regions.values() for line in region.all_text())
186
+
187
+ def all_words(self) -> Iterable[str]:
188
+ return (word for region in self.regions.values() for word in region.all_words())
pygexml/py.typed ADDED
File without changes
pygexml/strategies.py ADDED
@@ -0,0 +1,69 @@
1
+ # mypy: disallow_untyped_defs=False
2
+ # mypy: disallow_untyped_calls=False
3
+
4
+ import string
5
+
6
+ import hypothesis.strategies as st
7
+
8
+ from pygexml.geometry import Point, Box, Polygon
9
+ from pygexml.page import Coords, Page, TextLine, TextRegion
10
+
11
+ st_points = st.builds(Point, x=st.integers(min_value=0), y=st.integers(min_value=0))
12
+
13
+
14
+ @st.composite
15
+ def st_box_points(draw):
16
+ tl = draw(st_points)
17
+ br_x = draw(st.integers(min_value=tl.x + 1))
18
+ br_y = draw(st.integers(min_value=tl.y + 1))
19
+ br = Point(x=br_x, y=br_y)
20
+ return tl, br
21
+
22
+
23
+ st_boxes = st.builds(
24
+ lambda pp: Box(top_left=pp[0], bottom_right=pp[1]), st_box_points()
25
+ )
26
+
27
+ st_polygons = st.builds(Polygon, st.lists(st_points, min_size=1))
28
+ st_polygons2 = st.builds(Polygon, st.lists(st_points, min_size=2))
29
+
30
+ st_coords = st.builds(Coords, polygon=st_polygons2)
31
+
32
+ st_coords_strings = st.builds(str, st_coords)
33
+
34
+
35
+ def st_xml_text(**kwargs):
36
+ xml_chars = (
37
+ st.characters(min_codepoint=0x20, max_codepoint=0xD7FF)
38
+ | st.characters(min_codepoint=0xE000, max_codepoint=0xFFFD)
39
+ | st.characters(min_codepoint=0x10000, max_codepoint=0x10FFFF)
40
+ | st.sampled_from(["\t", "\n"])
41
+ )
42
+ return st.text(alphabet=xml_chars, **kwargs)
43
+
44
+
45
+ def st_simple_text(**kwargs):
46
+ simple = string.ascii_letters + string.digits + " _-"
47
+ return st.text(alphabet=simple, max_size=20, **kwargs)
48
+
49
+
50
+ st_text_lines = st.builds(
51
+ TextLine, id=st_simple_text(), coords=st_coords, text=st_xml_text()
52
+ )
53
+
54
+ st_text_regions = st.builds(
55
+ TextRegion,
56
+ id=st_simple_text(),
57
+ coords=st_coords,
58
+ textlines=st.builds(
59
+ lambda lines: {l.id: l for l in lines}, st.lists(st_text_lines)
60
+ ),
61
+ )
62
+
63
+
64
+ @st.composite
65
+ def st_pages(draw):
66
+ image_filename = draw(st_simple_text())
67
+ regions = {tr.id: tr for tr in draw(st.lists(st_text_regions))}
68
+ page = Page(image_filename=image_filename, regions=regions)
69
+ return page
@@ -0,0 +1,117 @@
1
+ Metadata-Version: 2.4
2
+ Name: pygexml
3
+ Version: 0.1.2
4
+ Summary: A minimal Python wrapper around the PAGE-XML format for OCR output
5
+ Author-email: Mirko Westermeier <mirko.westermeier@uni-muenster.de>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/SCDH/pygexml
8
+ Project-URL: Repository, https://github.com/SCDH/pygexml
9
+ Project-URL: Documentation, https://scdh.github.io/pygexml
10
+ Project-URL: Issues, https://github.com/SCDH/pygexml/issues
11
+ Requires-Python: >=3.12
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: lxml
15
+ Provides-Extra: strategies
16
+ Requires-Dist: hypothesis; extra == "strategies"
17
+ Provides-Extra: dev
18
+ Requires-Dist: mypy; extra == "dev"
19
+ Requires-Dist: pyright; extra == "dev"
20
+ Requires-Dist: black; extra == "dev"
21
+ Requires-Dist: lxml-stubs; extra == "dev"
22
+ Provides-Extra: test
23
+ Requires-Dist: pytest; extra == "test"
24
+ Requires-Dist: hypothesis; extra == "test"
25
+ Provides-Extra: docs
26
+ Requires-Dist: pdoc; extra == "docs"
27
+ Dynamic: license-file
28
+
29
+ # pygexml
30
+
31
+ A minimal Python wrapper around the [PAGE-XML][page-xml] format for OCR output.
32
+
33
+ [![pygexml checks, tests and docs][workflows-badge]][workflows] [![API docs online][api-docs-badge]][api-docs]
34
+
35
+ ## Installation
36
+
37
+ ```
38
+ pip install pygexml
39
+ ```
40
+
41
+ Requires Python 3.12+.
42
+
43
+ ## Usage
44
+
45
+ ```python
46
+ from pygexml import Page
47
+
48
+ page = Page.from_xml_string(xml_string)
49
+
50
+ for line in page.all_text():
51
+ print(line)
52
+ ```
53
+
54
+ ### Data model
55
+
56
+ | Class | Import from |
57
+ |---|---|
58
+ | `Page` | `pygexml` |
59
+ | `Page`, `TextRegion`, `TextLine`, `Coords` | `pygexml.page` |
60
+ | `Point`, `Box`, `Polygon` | `pygexml.geometry` |
61
+
62
+ `Page`, `TextRegion` and `TextLine` each expose `all_text()` and `all_words()` iterators.
63
+ Lookups by ID are available via `lookup_region()` and `lookup_textline()`.
64
+
65
+ Refer to the [online API docs][api-docs] for details.
66
+
67
+ ### Hypothesis strategies
68
+
69
+ The `pygexml.strategies` module provides [Hypothesis][hypothesis] strategies for all pygexml types, ready to use in property-based tests - including downstream projects:
70
+
71
+ ```python
72
+ from hypothesis import given
73
+ from pygexml.strategies import st_pages
74
+
75
+ @given(st_pages())
76
+ def test_my_page_processing(page):
77
+ assert process(page) is not None
78
+ ```
79
+
80
+ Refer to the [`pygexml.strategies` API docs][api-docs-strategies] for details.
81
+
82
+ ## Development
83
+
84
+ ```bash
85
+ pip install ".[dev,test,docs]"
86
+
87
+ black pygexml test # format
88
+ mypy pygexml test # type check
89
+ pyright pygexml test # type check
90
+ pytest -v # tests
91
+ pdoc -o .api_docs pygexml/* # API docs
92
+ ```
93
+
94
+ CI runs on Python 3.12, 3.13 and 3.14. [API documentation][api-docs] is published to GitHub Pages on every push to `main`.
95
+
96
+ ## Contributing
97
+
98
+ [Bug reports, feature requests][gh-issues] and [pull requests][gh-prs] are welcome. Feel free to open draft pull requests early to invite discussion and collaboration.
99
+
100
+ Please note that this project has a [Code of Conduct](CODE_OF_CONDUCT.md).
101
+
102
+ ## Copyright and License
103
+
104
+ Copyright (c) 2026 [Mirko Westermeier][gh-memowe] (SCDH, University of Münster)
105
+
106
+ Released under the [MIT License](LICENSE).
107
+
108
+ [page-xml]: https://github.com/PRImA-Research-Lab/PAGE-XML
109
+ [workflows]: https://github.com/SCDH/pygexml/actions/workflows/checks_tests_docs.yml
110
+ [workflows-badge]: https://github.com/SCDH/pygexml/actions/workflows/checks_tests_docs.yml/badge.svg
111
+ [hypothesis]: https://hypothesis.readthedocs.io
112
+ [api-docs]: https://scdh.github.io/pygexml
113
+ [api-docs-strategies]: https://scdh.github.io/pygexml/pygexml/strategies.html
114
+ [api-docs-badge]: https://img.shields.io/badge/API%20docs-online-blue?logo=gitbook&logoColor=lightgrey
115
+ [gh-issues]: https://github.com/SCDH/pygexml/issues
116
+ [gh-prs]: https://github.com/SCDH/pygexml/pulls
117
+ [gh-memowe]: https://github.com/memowe
@@ -0,0 +1,10 @@
1
+ pygexml/__init__.py,sha256=fCNsaocAimKd454bOvuxmABVYzJ1SlZJ5BmY9gztXdo,43
2
+ pygexml/geometry.py,sha256=yr_hsKH52nsrPsFcki1e_I3pnu2wKNSjvoWDn13ciLs,1414
3
+ pygexml/page.py,sha256=Ivd3PjC2d2wBQcH-ijxNg0NYXen7n9fh2cMQwNffic0,6081
4
+ pygexml/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ pygexml/strategies.py,sha256=JthqvLN6o3xEHZTLe8YprHMGwAeMOrbCFDBZIKjUjCs,1889
6
+ pygexml-0.1.2.dist-info/licenses/LICENSE,sha256=-tfmDDwot09J3YfFKWMK3CRhPs_qZ9qMfTfXKk8jlE8,1061
7
+ pygexml-0.1.2.dist-info/METADATA,sha256=W6e8DRdKxMJ2RgdaNhISPkC6sTv2nkMTRSVUed1I_ew,3714
8
+ pygexml-0.1.2.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
9
+ pygexml-0.1.2.dist-info/top_level.txt,sha256=GwUE9XI9ezJT0rf_Kp-VBOJmRgo-fVWOXt0nCwxplIk,8
10
+ pygexml-0.1.2.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2026 Mirko Westermeier
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included
12
+ in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ pygexml