102 lines
3.5 KiB
Python
102 lines
3.5 KiB
Python
"""Tests fuer Timeline, Kontextfenster und Zeitstempel."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from sjaudiosearch.search.context import build_context, build_timeline
|
|
|
|
|
|
def _segments(*specs):
|
|
return [
|
|
{
|
|
"id": index + 1,
|
|
"start_time": start,
|
|
"end_time": end,
|
|
"text": text,
|
|
"confidence": 0.9,
|
|
}
|
|
for index, (text, start, end) in enumerate(specs)
|
|
]
|
|
|
|
|
|
def test_context_contains_fifty_words_before_and_after():
|
|
prefix = " ".join(f"wort{i}" for i in range(80))
|
|
suffix = " ".join(f"danach{i}" for i in range(80))
|
|
text = f"{prefix} der schlüssel liegt im alten haus {suffix}"
|
|
timeline = build_timeline(1, _segments((text, 0.0, 60.0)))
|
|
|
|
start_index = len(prefix.split())
|
|
context = build_context(timeline, start_index, start_index + 6, context_words=50)
|
|
|
|
assert len(context.before.split()) == 50
|
|
assert len(context.after.split()) == 50
|
|
assert context.before.split()[-1] == "wort79"
|
|
assert context.after.split()[0] == "danach0"
|
|
assert context.match.split()[0] == "der"
|
|
assert "haus" in context.match
|
|
|
|
|
|
def test_context_spans_segment_boundaries_without_loss():
|
|
first = "Der Professor betrat den Raum und sah sich suchend um"
|
|
second = "der Schlüssel lag im alten Haus auf dem Tisch"
|
|
timeline = build_timeline(1, _segments((first, 5.0, 10.0), (second, 10.0, 15.0)))
|
|
|
|
assert len(timeline.tokens) == len(first.split()) + len(second.split())
|
|
start_index = len(first.split())
|
|
context = build_context(timeline, start_index, start_index + 4, context_words=50)
|
|
|
|
assert context.before.endswith("suchend um")
|
|
assert context.match.startswith("der Schlüssel")
|
|
assert context.start_time == pytest.approx(10.0, abs=1.5)
|
|
|
|
|
|
def test_timestamps_use_word_precision_when_available():
|
|
texts = ["Eins zwei drei vier fünf"]
|
|
segments = _segments((texts[0], 0.0, 5.0))
|
|
words = {
|
|
1: [
|
|
{"word": "Eins", "start_time": 10.0, "end_time": 10.5},
|
|
{"word": "zwei", "start_time": 10.5, "end_time": 11.0},
|
|
{"word": "drei", "start_time": 11.0, "end_time": 11.5},
|
|
{"word": "vier", "start_time": 11.5, "end_time": 12.0},
|
|
{"word": "fünf", "start_time": 12.0, "end_time": 12.5},
|
|
]
|
|
}
|
|
timeline = build_timeline(1, segments, words)
|
|
context = build_context(timeline, 2, 4, context_words=10)
|
|
|
|
assert context.precision == "word"
|
|
assert context.start_time == pytest.approx(11.0)
|
|
assert context.end_time == pytest.approx(12.0)
|
|
assert context.match == "drei vier"
|
|
|
|
|
|
def test_timestamps_fall_back_to_segment_precision():
|
|
timeline = build_timeline(1, _segments(("Eins zwei drei vier", 30.0, 34.0)))
|
|
context = build_context(timeline, 0, 2, context_words=5)
|
|
|
|
assert context.precision == "segment"
|
|
assert 30.0 <= context.start_time <= 32.0
|
|
assert context.end_time <= 34.0
|
|
|
|
|
|
def test_context_block_formatting():
|
|
timeline = build_timeline(1, _segments(("vorher der Schlüssel nachher", 0.0, 5.0)))
|
|
context = build_context(timeline, 1, 2, context_words=10)
|
|
block = context.as_block()
|
|
|
|
assert block.startswith('"\u2026vorher')
|
|
assert "der Schlüssel" in block
|
|
assert block.endswith('nachher\u2026"')
|
|
|
|
alone = build_context(timeline, 0, 4, context_words=0)
|
|
assert alone.as_block() == '"vorher der Schlüssel nachher"'
|
|
|
|
|
|
def test_empty_timeline_is_safe():
|
|
timeline = build_timeline(1, [])
|
|
context = build_context(timeline, 0, 1, 50)
|
|
assert context.match == ""
|
|
assert context.start_time == 0.0
|