ZainabEman's picture
Upload 152 files
546c207 verified
Raw History Blame Contribute Delete
4.09 kB
"""OCR normalization, snapping to the known part set, and occurrence matching."""
import pytest
from spareparts import matcher
from spareparts.ocr import Detection
KNOWN = frozenset({"30642", "74012", "74506", "74639", "74401", "73290", "30638"})
def detection(text, conf=0.99, box=(100, 500, 200, 540)):
x0, y0, x1, y1 = box
return Detection(text, conf, x0, y0, x1, y1, [[x0, y0], [x1, y0], [x1, y1], [x0, y1]])
@pytest.mark.parametrize(
"raw,expected",
[
("*7.4012", "74012"), # spurious dot inside an asterisked callout
("-74506", "74506"), # leader line absorbed into the OCR box
("*30638", "30638"), # footnote marker
("30642", "30642"),
("Seite 18", "18"),
("D / PU-S-12", "12"),
],
)
def test_normalize_strips_everything_but_digits(raw, expected):
assert matcher.normalize(raw) == expected
def test_snap_exact_and_fuzzy():
assert matcher.snap("30642", KNOWN) == ("30642", "exact")
# A dropped leading digit, seen on real pages ('4639' for 74639).
assert matcher.snap("4639", KNOWN) == ("74639", "fuzzy1")
assert matcher.snap("4401", KNOWN) == ("74401", "fuzzy1")
def test_snap_refuses_to_invent_part_numbers():
assert matcher.snap("", KNOWN) is None
assert matcher.snap("12345", KNOWN) is None # two edits from anything known
assert matcher.snap("061101", KNOWN) is None # a drawing revision code
assert matcher.snap("1207", KNOWN) is None # 'Seite 12' + section '07'
def test_snap_drops_ambiguous_matches():
"""Two candidates one edit away is not a match, it is a coin toss."""
ambiguous = frozenset({"74011", "74012"})
assert matcher.snap("7401", ambiguous) is None
def test_header_and_footer_strips_are_furniture():
height = 2352
assert matcher.is_furniture(detection("PU-ELA", box=(28, 24, 140, 65)), height)
assert matcher.is_furniture(detection("Seite 18", box=(1543, 2290, 1669, 2323)), height)
assert not matcher.is_furniture(detection("30642", box=(425, 1644, 520, 1677)), height)
def test_labels_for_page_accepts_callouts_and_reports_rejects():
detections = [
detection("30642", box=(425, 1644, 520, 1677)),
detection("*7.4012", box=(1243, 190, 1351, 231)),
detection("Seite 18", box=(1543, 2290, 1669, 2323)),
detection("061101", box=(30, 1000, 140, 1040)),
]
labels, rejected = matcher.labels_for_page(
detections, KNOWN, frozenset({"30642"}), image_height=2352
)
assert [lab.part_no for lab in labels] == ["30642", "74012"]
assert labels[1].has_asterisk is True
assert labels[1].raw_text == "*7.4012" # the raw read is preserved
assert labels[0].in_page_table is True
assert labels[1].in_page_table is False # drawn but absent from this table
assert {why for _, _, why in rejected} == {"page furniture", "no part match"}
def test_fuzzy_match_needs_higher_confidence():
low = [detection("4639", conf=0.6, box=(400, 900, 500, 940))]
labels, rejected = matcher.labels_for_page(low, KNOWN, frozenset(), 2352)
assert labels == []
assert rejected[0][2] == "fuzzy below threshold"
high = [detection("4639", conf=0.95, box=(400, 900, 500, 940))]
labels, _ = matcher.labels_for_page(high, KNOWN, frozenset(), 2352)
assert labels[0].part_no == "74639"
assert labels[0].match_kind == "fuzzy1"
# A fuzzy snap is a weaker claim than a clean read.
assert labels[0].confidence < labels[0].ocr_confidence
def test_assign_instances_numbers_repeated_callouts():
labels, _ = matcher.labels_for_page(
[
detection("74506", box=(200, 100, 300, 140)),
detection("74506", box=(200, 900, 300, 940)),
detection("30642", box=(700, 500, 800, 540)),
],
KNOWN, frozenset(), 2352,
)
numbered = matcher.assign_instances(labels)
by_part = {}
for instance, label in numbered:
by_part.setdefault(label.part_no, []).append(instance)
assert by_part["74506"] == [0, 1]
assert by_part["30642"] == [0]