Spaces:
Running
Running
| """In-memory literature index.""" | |
| from __future__ import annotations | |
| from app.knowledge.guideline_fragments import GUIDELINE_SNIPPETS | |
| def search_literature(query: str, top_k: int = 5) -> list[dict[str, str]]: | |
| q = query.lower() | |
| scored: list[tuple[int, dict[str, str]]] = [] | |
| for snippet in GUIDELINE_SNIPPETS: | |
| hay = f"{snippet['topic']} {snippet['text']}".lower() | |
| score = sum(1 for token in q.split() if token in hay) | |
| scored.append((score, snippet)) | |
| scored.sort(key=lambda x: x[0], reverse=True) | |
| return [item for score, item in scored[:top_k] if score > 0] or GUIDELINE_SNIPPETS[: min(top_k, len(GUIDELINE_SNIPPETS))] | |