30 lines
784 B
Python
30 lines
784 B
Python
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
|
|
def normalize_search_text(value: str) -> str:
|
|
"""Normalisiert Text fuer wortbasierte Suche.
|
|
|
|
Gemeinsames Verhalten:
|
|
- lower-case
|
|
- Nicht-Alphanumerisches -> Leerzeichen
|
|
- mehrfachen Whitespace kollabieren
|
|
"""
|
|
|
|
value = (value or "").casefold()
|
|
value = re.sub(r"[^a-z0-9]+", " ", value)
|
|
value = re.sub(r"\s+", " ", value).strip()
|
|
return value
|
|
|
|
|
|
def matches_query(query: str, *, title: str) -> bool:
|
|
"""True, wenn der normalisierte Titel den normalisierten Query als ganzes Token enthaelt."""
|
|
|
|
normalized_query = normalize_search_text(query)
|
|
if not normalized_query:
|
|
return False
|
|
haystack = f" {normalize_search_text(title)} "
|
|
return f" {normalized_query} " in haystack
|
|
|