Initial commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
"""Bewertung und Sortierung von Suchtreffern.
|
||||
|
||||
Der Score ist eine Aehnlichkeitsbewertung (0-100) und ausdruecklich keine
|
||||
statistische Wahrscheinlichkeit.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
from .engine import SearchHit
|
||||
|
||||
|
||||
def clamp_score(score: float) -> float:
|
||||
return max(0.0, min(100.0, float(score)))
|
||||
|
||||
|
||||
def dedupe_overlapping(hits: list["SearchHit"], gap_tokens: int = 3) -> list["SearchHit"]:
|
||||
"""Entfernt Treffer, die denselben Textabschnitt mehrfach abdecken."""
|
||||
if not hits:
|
||||
return []
|
||||
|
||||
ordered = sorted(hits, key=lambda hit: (-hit.score, hit.start_token))
|
||||
kept: list["SearchHit"] = []
|
||||
for hit in ordered:
|
||||
overlapping = False
|
||||
for existing in kept:
|
||||
if existing.audio_id != hit.audio_id:
|
||||
continue
|
||||
same_region = (
|
||||
abs(existing.start_token - hit.start_token) <= gap_tokens
|
||||
or (hit.start_token < existing.end_token and existing.start_token < hit.end_token)
|
||||
)
|
||||
if same_region:
|
||||
overlapping = True
|
||||
break
|
||||
if not overlapping:
|
||||
kept.append(hit)
|
||||
return kept
|
||||
|
||||
|
||||
def rank_hits(hits: list["SearchHit"], limit: int) -> list["SearchHit"]:
|
||||
"""Sortiert Treffer nach Relevanz und begrenzt die Anzahl."""
|
||||
ordered = sorted(
|
||||
hits,
|
||||
key=lambda hit: (
|
||||
-hit.score,
|
||||
hit.filename.lower(),
|
||||
hit.start_time,
|
||||
),
|
||||
)
|
||||
return ordered[: max(0, int(limit))]
|
||||
Reference in New Issue
Block a user