115 lines
3.5 KiB
Python
115 lines
3.5 KiB
Python
import pytest
|
|
|
|
from audita.corrections import apply_corrections
|
|
from audita.errors import AuditaValidationError
|
|
from audita.schemas import CorrectionCandidate, parse_transcript_json
|
|
|
|
|
|
def _transcript():
|
|
return parse_transcript_json(
|
|
"""
|
|
[
|
|
{"speaker": "Eric", "start": 10.0, "end": 11.0, "text": "I ask Chontia."},
|
|
{"speaker": "Mike", "start": 0.0, "end": 1.0, "text": "Then Lyra."}
|
|
]
|
|
"""
|
|
)
|
|
|
|
|
|
def test_apply_corrections_uses_threshold_and_sorts_chronologically():
|
|
transcript = _transcript()
|
|
corrections = [
|
|
CorrectionCandidate(
|
|
segment_id=0,
|
|
original_text="I ask Chontia.",
|
|
corrected_text="I ask Chauntea.",
|
|
confidence=0.8,
|
|
)
|
|
]
|
|
|
|
result = apply_corrections(transcript, corrections, confidence_threshold=0.8)
|
|
|
|
assert [segment.speaker for segment in result.transcript] == ["Mike", "Eric"]
|
|
assert result.transcript[1].text == "I ask Chauntea."
|
|
assert result.skipped == []
|
|
|
|
|
|
def test_apply_corrections_ignores_below_threshold():
|
|
transcript = _transcript()
|
|
corrections = [
|
|
CorrectionCandidate(
|
|
segment_id=0,
|
|
original_text="I ask Chontia.",
|
|
corrected_text="I ask Chauntea.",
|
|
confidence=0.79,
|
|
)
|
|
]
|
|
|
|
result = apply_corrections(transcript, corrections, confidence_threshold=0.8)
|
|
|
|
assert result.transcript[1].text == "I ask Chontia."
|
|
assert result.skipped == []
|
|
|
|
|
|
def test_apply_corrections_skips_duplicate_targets():
|
|
transcript = _transcript()
|
|
first = CorrectionCandidate(
|
|
segment_id=0,
|
|
original_text="I ask Chontia.",
|
|
corrected_text="I ask Chauntea.",
|
|
confidence=0.8,
|
|
)
|
|
second = CorrectionCandidate(
|
|
segment_id=0,
|
|
original_text="I ask Chontia.",
|
|
corrected_text="I ask Something Else.",
|
|
confidence=0.9,
|
|
)
|
|
|
|
result = apply_corrections(transcript, [first, second], confidence_threshold=0.8)
|
|
|
|
assert result.transcript[1].text == "I ask Chauntea."
|
|
assert len(result.skipped) == 1
|
|
assert result.skipped[0].segment_id == 0
|
|
assert "duplicate" in result.skipped[0].reason
|
|
|
|
|
|
def test_apply_corrections_skips_mismatched_original_text():
|
|
transcript = _transcript()
|
|
correction = CorrectionCandidate(
|
|
segment_id=0,
|
|
original_text="Different text.",
|
|
corrected_text="I ask Chauntea.",
|
|
confidence=0.8,
|
|
)
|
|
|
|
result = apply_corrections(transcript, [correction], confidence_threshold=0.8)
|
|
|
|
assert result.transcript[1].text == "I ask Chontia."
|
|
assert len(result.skipped) == 1
|
|
assert result.skipped[0].segment_id == 0
|
|
assert result.skipped[0].actual_text == "I ask Chontia."
|
|
assert "original_text" in result.skipped[0].reason
|
|
|
|
|
|
def test_apply_corrections_skips_missing_segment_id():
|
|
transcript = _transcript()
|
|
correction = CorrectionCandidate(
|
|
segment_id=99,
|
|
original_text="Missing.",
|
|
corrected_text="Still missing.",
|
|
confidence=0.8,
|
|
)
|
|
|
|
result = apply_corrections(transcript, [correction], confidence_threshold=0.8)
|
|
|
|
assert [segment.text for segment in result.transcript] == ["Then Lyra.", "I ask Chontia."]
|
|
assert len(result.skipped) == 1
|
|
assert result.skipped[0].segment_id == 99
|
|
assert "does not exist" in result.skipped[0].reason
|
|
|
|
|
|
def test_apply_corrections_rejects_invalid_threshold():
|
|
with pytest.raises(AuditaValidationError):
|
|
apply_corrections(_transcript(), [], confidence_threshold=1.1)
|