pygexml 0.1.2__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.
- pygexml-0.1.2/LICENSE +20 -0
- pygexml-0.1.2/PKG-INFO +117 -0
- pygexml-0.1.2/README.md +89 -0
- pygexml-0.1.2/pygexml/__init__.py +3 -0
- pygexml-0.1.2/pygexml/geometry.py +57 -0
- pygexml-0.1.2/pygexml/page.py +188 -0
- pygexml-0.1.2/pygexml/py.typed +0 -0
- pygexml-0.1.2/pygexml/strategies.py +69 -0
- pygexml-0.1.2/pygexml.egg-info/PKG-INFO +117 -0
- pygexml-0.1.2/pygexml.egg-info/SOURCES.txt +15 -0
- pygexml-0.1.2/pygexml.egg-info/dependency_links.txt +1 -0
- pygexml-0.1.2/pygexml.egg-info/requires.txt +17 -0
- pygexml-0.1.2/pygexml.egg-info/top_level.txt +1 -0
- pygexml-0.1.2/pyproject.toml +38 -0
- pygexml-0.1.2/setup.cfg +4 -0
- pygexml-0.1.2/test/test_geometry.py +114 -0
- pygexml-0.1.2/test/test_page.py +400 -0
pygexml-0.1.2/LICENSE
ADDED
|
@@ -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.
|
pygexml-0.1.2/PKG-INFO
ADDED
|
@@ -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
|
pygexml-0.1.2/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# pygexml
|
|
2
|
+
|
|
3
|
+
A minimal Python wrapper around the [PAGE-XML][page-xml] format for OCR output.
|
|
4
|
+
|
|
5
|
+
[![pygexml checks, tests and docs][workflows-badge]][workflows] [![API docs online][api-docs-badge]][api-docs]
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
pip install pygexml
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires Python 3.12+.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from pygexml import Page
|
|
19
|
+
|
|
20
|
+
page = Page.from_xml_string(xml_string)
|
|
21
|
+
|
|
22
|
+
for line in page.all_text():
|
|
23
|
+
print(line)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Data model
|
|
27
|
+
|
|
28
|
+
| Class | Import from |
|
|
29
|
+
|---|---|
|
|
30
|
+
| `Page` | `pygexml` |
|
|
31
|
+
| `Page`, `TextRegion`, `TextLine`, `Coords` | `pygexml.page` |
|
|
32
|
+
| `Point`, `Box`, `Polygon` | `pygexml.geometry` |
|
|
33
|
+
|
|
34
|
+
`Page`, `TextRegion` and `TextLine` each expose `all_text()` and `all_words()` iterators.
|
|
35
|
+
Lookups by ID are available via `lookup_region()` and `lookup_textline()`.
|
|
36
|
+
|
|
37
|
+
Refer to the [online API docs][api-docs] for details.
|
|
38
|
+
|
|
39
|
+
### Hypothesis strategies
|
|
40
|
+
|
|
41
|
+
The `pygexml.strategies` module provides [Hypothesis][hypothesis] strategies for all pygexml types, ready to use in property-based tests - including downstream projects:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from hypothesis import given
|
|
45
|
+
from pygexml.strategies import st_pages
|
|
46
|
+
|
|
47
|
+
@given(st_pages())
|
|
48
|
+
def test_my_page_processing(page):
|
|
49
|
+
assert process(page) is not None
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Refer to the [`pygexml.strategies` API docs][api-docs-strategies] for details.
|
|
53
|
+
|
|
54
|
+
## Development
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install ".[dev,test,docs]"
|
|
58
|
+
|
|
59
|
+
black pygexml test # format
|
|
60
|
+
mypy pygexml test # type check
|
|
61
|
+
pyright pygexml test # type check
|
|
62
|
+
pytest -v # tests
|
|
63
|
+
pdoc -o .api_docs pygexml/* # API docs
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
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`.
|
|
67
|
+
|
|
68
|
+
## Contributing
|
|
69
|
+
|
|
70
|
+
[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.
|
|
71
|
+
|
|
72
|
+
Please note that this project has a [Code of Conduct](CODE_OF_CONDUCT.md).
|
|
73
|
+
|
|
74
|
+
## Copyright and License
|
|
75
|
+
|
|
76
|
+
Copyright (c) 2026 [Mirko Westermeier][gh-memowe] (SCDH, University of Münster)
|
|
77
|
+
|
|
78
|
+
Released under the [MIT License](LICENSE).
|
|
79
|
+
|
|
80
|
+
[page-xml]: https://github.com/PRImA-Research-Lab/PAGE-XML
|
|
81
|
+
[workflows]: https://github.com/SCDH/pygexml/actions/workflows/checks_tests_docs.yml
|
|
82
|
+
[workflows-badge]: https://github.com/SCDH/pygexml/actions/workflows/checks_tests_docs.yml/badge.svg
|
|
83
|
+
[hypothesis]: https://hypothesis.readthedocs.io
|
|
84
|
+
[api-docs]: https://scdh.github.io/pygexml
|
|
85
|
+
[api-docs-strategies]: https://scdh.github.io/pygexml/pygexml/strategies.html
|
|
86
|
+
[api-docs-badge]: https://img.shields.io/badge/API%20docs-online-blue?logo=gitbook&logoColor=lightgrey
|
|
87
|
+
[gh-issues]: https://github.com/SCDH/pygexml/issues
|
|
88
|
+
[gh-prs]: https://github.com/SCDH/pygexml/pulls
|
|
89
|
+
[gh-memowe]: https://github.com/memowe
|
|
@@ -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
|
+
)
|
|
@@ -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())
|
|
File without changes
|
|
@@ -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,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
pygexml/__init__.py
|
|
5
|
+
pygexml/geometry.py
|
|
6
|
+
pygexml/page.py
|
|
7
|
+
pygexml/py.typed
|
|
8
|
+
pygexml/strategies.py
|
|
9
|
+
pygexml.egg-info/PKG-INFO
|
|
10
|
+
pygexml.egg-info/SOURCES.txt
|
|
11
|
+
pygexml.egg-info/dependency_links.txt
|
|
12
|
+
pygexml.egg-info/requires.txt
|
|
13
|
+
pygexml.egg-info/top_level.txt
|
|
14
|
+
test/test_geometry.py
|
|
15
|
+
test/test_page.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pygexml
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pygexml"
|
|
7
|
+
version = "0.1.2"
|
|
8
|
+
description = "A minimal Python wrapper around the PAGE-XML format for OCR output"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Mirko Westermeier", email = "mirko.westermeier@uni-muenster.de" },
|
|
14
|
+
]
|
|
15
|
+
dependencies = [
|
|
16
|
+
"lxml",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://github.com/SCDH/pygexml"
|
|
21
|
+
Repository = "https://github.com/SCDH/pygexml"
|
|
22
|
+
Documentation = "https://scdh.github.io/pygexml"
|
|
23
|
+
Issues = "https://github.com/SCDH/pygexml/issues"
|
|
24
|
+
|
|
25
|
+
[tool.setuptools.package-data]
|
|
26
|
+
pygexml = ["py.typed"]
|
|
27
|
+
|
|
28
|
+
[project.optional-dependencies]
|
|
29
|
+
strategies = ["hypothesis"]
|
|
30
|
+
dev = ["mypy", "pyright", "black", "lxml-stubs"]
|
|
31
|
+
test = ["pytest", "hypothesis"]
|
|
32
|
+
docs = ["pdoc"]
|
|
33
|
+
|
|
34
|
+
[tool.setuptools]
|
|
35
|
+
packages = ["pygexml"]
|
|
36
|
+
|
|
37
|
+
[tool.mypy]
|
|
38
|
+
strict = true
|
pygexml-0.1.2/setup.cfg
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from hypothesis import given, strategies as st
|
|
3
|
+
|
|
4
|
+
from pygexml.strategies import *
|
|
5
|
+
from pygexml.geometry import *
|
|
6
|
+
|
|
7
|
+
############## Tests for Point #####################
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@given(st.integers(min_value=0), st.integers(min_value=0))
|
|
11
|
+
def test_point_construction(x: int, y: int) -> None:
|
|
12
|
+
point = Point(x=x, y=y)
|
|
13
|
+
assert point.x == x
|
|
14
|
+
assert point.y == y
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@given(st.integers(min_value=0), st.integers(min_value=0))
|
|
18
|
+
def test_point_stringification(x: int, y: int) -> None:
|
|
19
|
+
point = Point(x=x, y=y)
|
|
20
|
+
assert str(point) == f"{x},{y}"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
############## Tests for Box ####################
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_box_simple_example() -> None:
|
|
27
|
+
p17 = Point(17, 17)
|
|
28
|
+
p42 = Point(42, 42)
|
|
29
|
+
box = Box(top_left=p17, bottom_right=p42)
|
|
30
|
+
assert box.top_left == p17
|
|
31
|
+
assert box.bottom_right == p42
|
|
32
|
+
assert box.width() == 25
|
|
33
|
+
assert box.height() == 25
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@given(st_box_points())
|
|
37
|
+
def test_box_construction(pp: tuple[Point, Point]) -> None:
|
|
38
|
+
tl, br = pp
|
|
39
|
+
box = Box(top_left=tl, bottom_right=br)
|
|
40
|
+
assert box.top_left == tl
|
|
41
|
+
assert box.bottom_right == br
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@given(st_box_points())
|
|
45
|
+
def test_box_construction_exception(pp: tuple[Point, Point]) -> None:
|
|
46
|
+
tl, br = pp
|
|
47
|
+
with pytest.raises(Exception, match="top left is not top left"):
|
|
48
|
+
Box(top_left=br, bottom_right=tl) # flipped points!
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@given(st_boxes)
|
|
52
|
+
def test_box_width(box: Box) -> None:
|
|
53
|
+
assert box.width() == abs(box.bottom_right.x - box.top_left.x)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@given(st_boxes)
|
|
57
|
+
def test_box_height(box: Box) -> None:
|
|
58
|
+
assert box.height() == abs(box.bottom_right.y - box.top_left.y)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_box_simple_contains() -> None:
|
|
62
|
+
box = Box(Point(17, 17), Point(42, 42))
|
|
63
|
+
assert box.contains(Point(23, 37))
|
|
64
|
+
assert not box.contains(Point(23, 666))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@given(st_points, st_boxes)
|
|
68
|
+
def test_box_containment(point: Point, box: Box) -> None:
|
|
69
|
+
assert box.contains(point) == (
|
|
70
|
+
box.top_left.x <= point.x <= box.bottom_right.x
|
|
71
|
+
and box.top_left.y <= point.y <= box.bottom_right.y
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
############## Tests for Polygon ####################
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_polygon_simple_example() -> None:
|
|
79
|
+
p17 = Point(17, 17)
|
|
80
|
+
p42 = Point(42, 42)
|
|
81
|
+
polygon = Polygon(points=[p17, p42])
|
|
82
|
+
assert polygon.points == [p17, p42]
|
|
83
|
+
assert polygon.bounding_box().top_left == p17
|
|
84
|
+
assert polygon.bounding_box().bottom_right == p42
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@given(st.lists(st_points, min_size=1))
|
|
88
|
+
def test_polygon_construction(points: list[Point]) -> None:
|
|
89
|
+
polygon = Polygon(points=points)
|
|
90
|
+
assert polygon.points == points
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_polygon_construction_without_points() -> None:
|
|
94
|
+
with pytest.raises(GeometryError, match="Polygon: points must not be empty"):
|
|
95
|
+
Polygon(points=[])
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@given(st_polygons)
|
|
99
|
+
def test_polygon_bounding_box_corners(polygon: Polygon) -> None:
|
|
100
|
+
points = polygon.points
|
|
101
|
+
min_x = min(point.x for point in points)
|
|
102
|
+
max_x = max(point.x for point in points)
|
|
103
|
+
min_y = min(point.y for point in points)
|
|
104
|
+
max_y = max(point.y for point in points)
|
|
105
|
+
bounding_box = polygon.bounding_box()
|
|
106
|
+
assert bounding_box.top_left == Point(x=min_x, y=min_y)
|
|
107
|
+
assert bounding_box.bottom_right == Point(x=max_x, y=max_y)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@given(st_polygons)
|
|
111
|
+
def test_polygon_bounding_box_contains(polygon: Polygon) -> None:
|
|
112
|
+
bounding_box = polygon.bounding_box()
|
|
113
|
+
for point in polygon.points:
|
|
114
|
+
assert bounding_box.contains(point)
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from hypothesis import given, assume
|
|
3
|
+
import hypothesis.strategies as st
|
|
4
|
+
from lxml import etree
|
|
5
|
+
|
|
6
|
+
from pygexml.strategies import *
|
|
7
|
+
from pygexml.geometry import Point, Polygon
|
|
8
|
+
from pygexml.page import Coords, ID, TextLine, TextRegion, Page
|
|
9
|
+
|
|
10
|
+
############## Tests for Coords ####################
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_coords_example() -> None:
|
|
14
|
+
polygon = Polygon(points=[Point(1, 2), Point(3, 4)])
|
|
15
|
+
coords = Coords(polygon=polygon)
|
|
16
|
+
assert coords.polygon == polygon
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_coords_with_not_enough_points() -> None:
|
|
20
|
+
with pytest.raises(Exception, match="at least 2 Points"):
|
|
21
|
+
Coords(polygon=Polygon(points=[Point(17, 42)]))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_coords_stringification() -> None:
|
|
25
|
+
coords = Coords(polygon=Polygon(points=[Point(1, 2), Point(17, 42)]))
|
|
26
|
+
assert str(coords) == "1,2 17,42"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_coords_parse_example() -> None:
|
|
30
|
+
coords = Coords.parse("1,2 17,42")
|
|
31
|
+
assert coords.polygon.points == [Point(1, 2), Point(17, 42)]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_coords_parse_invalid_inputs() -> None:
|
|
35
|
+
with pytest.raises(Exception, match="Invalid Coords XML string"):
|
|
36
|
+
Coords.parse("17,42 666,42 SCDH, yay!")
|
|
37
|
+
with pytest.raises(Exception, match="Invalid Coords XML string"):
|
|
38
|
+
Coords.parse("")
|
|
39
|
+
with pytest.raises(Exception, match="Invalid Coords XML string"):
|
|
40
|
+
Coords.parse("17,42")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_coords_allow_for_simple_negative_coordinates() -> None:
|
|
44
|
+
with pytest.warns(
|
|
45
|
+
UserWarning, match=r".*does not match the PAGE XMl spec: 1,-2 -3,4"
|
|
46
|
+
):
|
|
47
|
+
coords = Coords.parse("1,-2 -3,4")
|
|
48
|
+
assert coords.polygon.points == [Point(1, -2), Point(-3, 4)]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@given(st_coords)
|
|
52
|
+
def test_coords_parse_arbitrary(coords: Coords) -> None:
|
|
53
|
+
assert Coords.parse(str(coords)) == coords
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@given(st_coords_strings)
|
|
57
|
+
def test_coords_stringify_arbitrary(coords_str: str) -> None:
|
|
58
|
+
coords_object = Coords.parse(coords_str)
|
|
59
|
+
assert str(coords_object) == coords_str
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
############## Tests for TextLines ####################
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_textline_simple_parsing_example() -> None:
|
|
66
|
+
tl = TextLine.from_xml(etree.fromstring("""
|
|
67
|
+
<TextLine id="tl-id">
|
|
68
|
+
<Coords points="17,42 1,2"/>
|
|
69
|
+
<TextEquiv>
|
|
70
|
+
<Unicode>tl-text</Unicode>
|
|
71
|
+
</TextEquiv>
|
|
72
|
+
</TextLine>
|
|
73
|
+
"""))
|
|
74
|
+
assert tl.id == "tl-id"
|
|
75
|
+
assert tl.coords == Coords.parse("17,42 1,2")
|
|
76
|
+
assert tl.text == "tl-text"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_textline_wrong_element() -> None:
|
|
80
|
+
with pytest.raises(Exception, match="wrong element given"):
|
|
81
|
+
TextLine.from_xml(etree.fromstring("<WRONG>!!!</WRONG>"))
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_textline_no_id() -> None:
|
|
85
|
+
xml = etree.fromstring("<TextLine></TextLine>")
|
|
86
|
+
with pytest.raises(Exception, match="no id found"):
|
|
87
|
+
TextLine.from_xml(xml)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_textline_no_coords() -> None:
|
|
91
|
+
xml = etree.fromstring("""
|
|
92
|
+
<TextLine id="tl-id">
|
|
93
|
+
<TextEquiv>
|
|
94
|
+
<Unicode>tl-text</Unicode>
|
|
95
|
+
</TextEquiv>
|
|
96
|
+
</TextLine>
|
|
97
|
+
""")
|
|
98
|
+
with pytest.raises(Exception, match="no Coords found"):
|
|
99
|
+
TextLine.from_xml(xml)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def test_textline_no_text() -> None:
|
|
103
|
+
xml = etree.fromstring("""
|
|
104
|
+
<TextLine id="tl-id">
|
|
105
|
+
<Coords points="0,0 10,0 10,10 0,10"/>
|
|
106
|
+
<TextEquiv>
|
|
107
|
+
</TextEquiv>
|
|
108
|
+
</TextLine>
|
|
109
|
+
""")
|
|
110
|
+
with pytest.raises(Exception, match="no text found"):
|
|
111
|
+
TextLine.from_xml(xml)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_textline_empty_text() -> None:
|
|
115
|
+
xml = etree.fromstring("""
|
|
116
|
+
<TextLine id="tl-id">
|
|
117
|
+
<Coords points="1,2 3,4"/>
|
|
118
|
+
<TextEquiv>
|
|
119
|
+
<Unicode/>
|
|
120
|
+
</TextEquiv>
|
|
121
|
+
</TextLine>
|
|
122
|
+
""")
|
|
123
|
+
assert TextLine.from_xml(xml).text == ""
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def test_textline_simple_words() -> None:
|
|
127
|
+
tl = TextLine("", Coords.parse("1,2 3,4"), "foo bar baz ")
|
|
128
|
+
assert tl.words() == ["foo", "bar", "baz"]
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@given(st_text_lines)
|
|
132
|
+
def test_textline_words(tl: TextLine) -> None:
|
|
133
|
+
assert tl.words() == tl.text.split()
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
####### Tests for TextRegion ###############
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def test_textregion_simple_parsing_example() -> None:
|
|
140
|
+
tr = TextRegion.from_xml(etree.fromstring("""
|
|
141
|
+
<TextRegion id="tr-id">
|
|
142
|
+
<Coords points="1,2 8,9"/>
|
|
143
|
+
<TextLine id="tl-1">
|
|
144
|
+
<Coords points="17,42 1,2"/>
|
|
145
|
+
<TextEquiv>
|
|
146
|
+
<Unicode>bla</Unicode>
|
|
147
|
+
</TextEquiv>
|
|
148
|
+
</TextLine>
|
|
149
|
+
|
|
150
|
+
<TextLine id="tl-2">
|
|
151
|
+
<Coords points="20,38 2,2"/>
|
|
152
|
+
<TextEquiv>
|
|
153
|
+
<Unicode>blub</Unicode>
|
|
154
|
+
</TextEquiv>
|
|
155
|
+
</TextLine>
|
|
156
|
+
</TextRegion>
|
|
157
|
+
"""))
|
|
158
|
+
assert tr.id == "tr-id"
|
|
159
|
+
assert tr.coords == Coords.parse("1,2 8,9")
|
|
160
|
+
assert tr.textlines == {
|
|
161
|
+
"tl-1": TextLine(
|
|
162
|
+
id="tl-1",
|
|
163
|
+
coords=Coords.parse("17,42 1,2"),
|
|
164
|
+
text="bla",
|
|
165
|
+
),
|
|
166
|
+
"tl-2": TextLine(
|
|
167
|
+
id="tl-2",
|
|
168
|
+
coords=Coords.parse("20,38 2,2"),
|
|
169
|
+
text="blub",
|
|
170
|
+
),
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def test_textregion_wrong_element() -> None:
|
|
175
|
+
with pytest.raises(Exception, match="wrong element given"):
|
|
176
|
+
TextRegion.from_xml(etree.fromstring("<WRONG>!!!</WRONG>"))
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def test_textregion_no_id() -> None:
|
|
180
|
+
xml = etree.fromstring("<TextRegion></TextRegion>")
|
|
181
|
+
with pytest.raises(Exception, match="no id found"):
|
|
182
|
+
TextRegion.from_xml(xml)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def test_textregion_no_coords() -> None:
|
|
186
|
+
xml = etree.fromstring("""
|
|
187
|
+
<TextRegion id="tr-id">
|
|
188
|
+
<TextLine id="tl-1">
|
|
189
|
+
<TextEquiv>
|
|
190
|
+
<Unicode>bla</Unicode>
|
|
191
|
+
</TextEquiv>
|
|
192
|
+
</TextLine>
|
|
193
|
+
</TextRegion>
|
|
194
|
+
""")
|
|
195
|
+
with pytest.raises(Exception, match="no Coords element found"):
|
|
196
|
+
TextRegion.from_xml(xml)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
@given(st_text_lines, st_text_regions)
|
|
200
|
+
def test_textregion_line_lookup(line: TextLine, region: TextRegion) -> None:
|
|
201
|
+
assume(not line.id in region.textlines)
|
|
202
|
+
region.textlines[line.id] = line
|
|
203
|
+
assert region.lookup_textline(line.id) == line
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@given(st.text(), st_text_regions)
|
|
207
|
+
def test_textregion_line_lookup_not_found(id: ID, region: TextRegion) -> None:
|
|
208
|
+
assume(not id in region.textlines)
|
|
209
|
+
assert region.lookup_textline(id) is None
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def test_textregion_all_text_and_words() -> None:
|
|
213
|
+
tr = TextRegion(
|
|
214
|
+
id="a",
|
|
215
|
+
coords=Coords.parse("1,2 3,4"),
|
|
216
|
+
textlines={
|
|
217
|
+
"b": TextLine(id="b", coords=Coords.parse("2,3 4,5"), text="foo"),
|
|
218
|
+
"c": TextLine(id="c", coords=Coords.parse("2,3 4,5"), text="bar baz "),
|
|
219
|
+
},
|
|
220
|
+
)
|
|
221
|
+
assert list(tr.all_text()) == ["foo", "bar baz "]
|
|
222
|
+
assert list(tr.all_words()) == ["foo", "bar", "baz"]
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
@given(st_text_regions)
|
|
226
|
+
def test_textregion_all_arbitrary_text_and_words(region: TextRegion) -> None:
|
|
227
|
+
assert list(region.all_text()) == [tl.text for tl in region.textlines.values()]
|
|
228
|
+
assert list(region.all_words()) == [
|
|
229
|
+
w for tl in region.textlines.values() for w in tl.words()
|
|
230
|
+
]
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
############### Tests for Page ####################
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def test_create_page_from_element() -> None:
|
|
237
|
+
pa = Page.from_xml(etree.fromstring("""
|
|
238
|
+
<Page imageFilename="7895328.jpg" imageWidth="4279" imageHeight="5315">
|
|
239
|
+
<TextRegion id="tr-1">
|
|
240
|
+
<Coords points="1,2 8,9"/>
|
|
241
|
+
<TextLine id="tl-1">
|
|
242
|
+
<Coords points="17,42 1,2"/>
|
|
243
|
+
<TextEquiv>
|
|
244
|
+
<Unicode>bla</Unicode>
|
|
245
|
+
</TextEquiv>
|
|
246
|
+
</TextLine>
|
|
247
|
+
<TextLine id="tl-2">
|
|
248
|
+
<Coords points="20,38 2,2"/>
|
|
249
|
+
<TextEquiv>
|
|
250
|
+
<Unicode>blub</Unicode>
|
|
251
|
+
</TextEquiv>
|
|
252
|
+
</TextLine>
|
|
253
|
+
</TextRegion>
|
|
254
|
+
|
|
255
|
+
<TextRegion id="tr-2">
|
|
256
|
+
<Coords points="1,2 8,9"/>
|
|
257
|
+
<TextLine id="tl-1">
|
|
258
|
+
<Coords points="17,42 1,2"/>
|
|
259
|
+
<TextEquiv>
|
|
260
|
+
<Unicode>bla</Unicode>
|
|
261
|
+
</TextEquiv>
|
|
262
|
+
</TextLine>
|
|
263
|
+
<TextLine id="tl-2">
|
|
264
|
+
<Coords points="20,38 2,2"/>
|
|
265
|
+
<TextEquiv>
|
|
266
|
+
<Unicode>blub</Unicode>
|
|
267
|
+
</TextEquiv>
|
|
268
|
+
</TextLine>
|
|
269
|
+
</TextRegion>
|
|
270
|
+
</Page>
|
|
271
|
+
"""))
|
|
272
|
+
|
|
273
|
+
assert pa.image_filename == "7895328.jpg"
|
|
274
|
+
assert pa.regions == {
|
|
275
|
+
"tr-1": TextRegion(
|
|
276
|
+
id="tr-1",
|
|
277
|
+
coords=Coords.parse("1,2 8,9"),
|
|
278
|
+
textlines={
|
|
279
|
+
"tl-1": TextLine(
|
|
280
|
+
id="tl-1",
|
|
281
|
+
coords=Coords.parse("17,42 1,2"),
|
|
282
|
+
text="bla",
|
|
283
|
+
),
|
|
284
|
+
"tl-2": TextLine(
|
|
285
|
+
id="tl-2",
|
|
286
|
+
coords=Coords.parse("20,38 2,2"),
|
|
287
|
+
text="blub",
|
|
288
|
+
),
|
|
289
|
+
},
|
|
290
|
+
),
|
|
291
|
+
"tr-2": TextRegion(
|
|
292
|
+
id="tr-2",
|
|
293
|
+
coords=Coords.parse("1,2 8,9"),
|
|
294
|
+
textlines={
|
|
295
|
+
"tl-1": TextLine(
|
|
296
|
+
id="tl-1",
|
|
297
|
+
coords=Coords.parse("17,42 1,2"),
|
|
298
|
+
text="bla",
|
|
299
|
+
),
|
|
300
|
+
"tl-2": TextLine(
|
|
301
|
+
id="tl-2",
|
|
302
|
+
coords=Coords.parse("20,38 2,2"),
|
|
303
|
+
text="blub",
|
|
304
|
+
),
|
|
305
|
+
},
|
|
306
|
+
),
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def test_page_wrong_element() -> None:
|
|
311
|
+
with pytest.raises(Exception, match="wrong element given"):
|
|
312
|
+
Page.from_xml(etree.fromstring("<WRONG>!!!</WRONG>"))
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def test_page_no_filename() -> None:
|
|
316
|
+
xml = "<Page></Page>"
|
|
317
|
+
with pytest.raises(Exception, match="no filename found"):
|
|
318
|
+
Page.from_xml(etree.fromstring(xml))
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def test_page_from_string() -> None:
|
|
322
|
+
pa = Page.from_xml_string("""<?xml version='1.0' encoding='utf-8'?>
|
|
323
|
+
<PcGts xmlns="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15/pagecontent.xsd">
|
|
324
|
+
<Metadata>
|
|
325
|
+
<Creator>God</Creator>
|
|
326
|
+
<Created>Sonntag</Created>
|
|
327
|
+
</Metadata>
|
|
328
|
+
<Page imageFilename="a.jpg" imageWidth="4217" imageHeight="1742">
|
|
329
|
+
<TextRegion id="b">
|
|
330
|
+
<Coords points="1,2 3,4"/>
|
|
331
|
+
<TextLine id="c" index="0" custom="heights_v2:[91.0,32.1]">
|
|
332
|
+
<Coords points="5,6 7,8"/>
|
|
333
|
+
<Baseline points="2008,360 2208,352"/>
|
|
334
|
+
<TextEquiv conf="0.932">
|
|
335
|
+
<Unicode>d</Unicode>
|
|
336
|
+
</TextEquiv>
|
|
337
|
+
</TextLine>
|
|
338
|
+
</TextRegion>
|
|
339
|
+
</Page>
|
|
340
|
+
</PcGts>
|
|
341
|
+
""") # use default PageXML namespace
|
|
342
|
+
assert pa.image_filename == "a.jpg"
|
|
343
|
+
assert pa.regions == {
|
|
344
|
+
"b": TextRegion(
|
|
345
|
+
id="b",
|
|
346
|
+
coords=Coords.parse("1,2 3,4"),
|
|
347
|
+
textlines={"c": TextLine(id="c", coords=Coords.parse("5,6 7,8"), text="d")},
|
|
348
|
+
)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
@given(st_text_regions, st_pages())
|
|
353
|
+
def test_page_region_lookup(region: TextRegion, page: Page) -> None:
|
|
354
|
+
assume(region.id not in page.regions)
|
|
355
|
+
page.regions[region.id] = region
|
|
356
|
+
assert page.lookup_region(region.id) == region
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
@given(st.text(), st_pages())
|
|
360
|
+
def test_page_region_lookup_not_found(id: str, page: Page) -> None:
|
|
361
|
+
assume(id not in page.regions)
|
|
362
|
+
assert page.lookup_region(id) is None
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def test_page_all_text_and_words() -> None:
|
|
366
|
+
pa = Page(
|
|
367
|
+
image_filename="a",
|
|
368
|
+
regions={
|
|
369
|
+
"a": TextRegion(
|
|
370
|
+
id="a",
|
|
371
|
+
coords=Coords.parse("1,2 3,4"),
|
|
372
|
+
textlines={
|
|
373
|
+
"b": TextLine(id="b", coords=Coords.parse("2,3 4,5"), text="foo"),
|
|
374
|
+
"c": TextLine(id="c", coords=Coords.parse("2,3 4,5"), text="bar"),
|
|
375
|
+
},
|
|
376
|
+
),
|
|
377
|
+
"c": TextRegion(
|
|
378
|
+
id="c",
|
|
379
|
+
coords=Coords.parse("1,2 3,4"),
|
|
380
|
+
textlines={
|
|
381
|
+
"b": TextLine(id="b", coords=Coords.parse("2,3 4,5"), text="bla"),
|
|
382
|
+
"c": TextLine(
|
|
383
|
+
id="c", coords=Coords.parse("2,3 4,5"), text="blub 42"
|
|
384
|
+
),
|
|
385
|
+
},
|
|
386
|
+
),
|
|
387
|
+
},
|
|
388
|
+
)
|
|
389
|
+
assert list(pa.all_text()) == ["foo", "bar", "bla", "blub 42"]
|
|
390
|
+
assert list(pa.all_words()) == ["foo", "bar", "bla", "blub", "42"]
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
@given(st_pages())
|
|
394
|
+
def test_page_all_arbitrary_text_and_words(page: Page) -> None:
|
|
395
|
+
assert list(page.all_text()) == [
|
|
396
|
+
t for r in page.regions.values() for t in r.all_text()
|
|
397
|
+
]
|
|
398
|
+
assert list(page.all_words()) == [
|
|
399
|
+
w for r in page.regions.values() for w in r.all_words()
|
|
400
|
+
]
|