Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/test_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from textractor.entities.word import Word
from textractor.data.constants import TextTypes
from textractor.entities.bbox import BoundingBox
from textractor.utils.text_utils import linearize_children
from textractor.visualizers.entitylist import EntityList

class TestLine(unittest.TestCase):
Expand Down Expand Up @@ -85,3 +86,25 @@ def test_set_page_id(self):
def test_repr(self):
"""Test case setter for the repr function"""
self.assertEqual(self.line.__repr__(), "TEST WORDS ADDED")


def test_line_dedup_is_deterministic(self):
"""Check that the return value is deterministic.

5 lines sharing one bbox, rendered 5 times — empirically
<1-in-5000 chance of matching outputs without determinism.
"""
outputs = []
for _ in range(5):
bbox = BoundingBox(0.1, 0.1, 0.05, 0.02)
lines = [Line(f"L{i}", bbox) for i in range(5)]
for i, line in enumerate(lines):
w = Word(f"W{i}", bbox, text=f"word{i}")
w.line = line
line._children = [w]
words = [line._children[0] for line in lines]
text, _ = linearize_children(words)
outputs.append(text)

for text in outputs[1:]:
self.assertEqual(text, outputs[0])
2 changes: 1 addition & 1 deletion textractor/utils/text_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def linearize_children(
"""

words_in_elements = [e for e in elements if isinstance(e, Word)]
lines = set([w.line for w in words_in_elements])
lines = list(dict.fromkeys(w.line for w in words_in_elements))
new_lines = []
for line in lines:
if line is None:
Expand Down