forked from Deen-Bridge/dnb-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadhkar.py
More file actions
50 lines (41 loc) · 1.7 KB
/
Copy pathadhkar.py
File metadata and controls
50 lines (41 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import json
from pathlib import Path
from typing import Any
DATA_PATH = Path(__file__).parent / "data" / "adhkar.json"
class AdhkarEntry(dict):
"""Simple typed container for a single adhkar/dua entry."""
class AdhkarCorpus:
def __init__(self, data_file: Path = DATA_PATH):
self.data_file = data_file
self.entries: list[dict[str, Any]] = []
self._load_corpus()
def _load_corpus(self) -> None:
if not self.data_file.exists():
self.entries = []
return
with open(self.data_file, encoding="utf-8") as handle:
payload = json.load(handle)
self.entries = payload.get("entries", [])
def search(self, *, category: str | None = None, query: str | None = None) -> list[dict[str, Any]]:
matches: list[dict[str, Any]] = []
normalized_query = (query or "").strip().casefold()
for entry in self.entries:
category_matches = True
if category:
category_matches = str(entry.get("category", "")).casefold() == category.casefold()
text_matches = True
if normalized_query:
haystack = " ".join(
[
str(entry.get("arabic", "")),
str(entry.get("transliteration", "")),
str(entry.get("translation", "")),
str(entry.get("category", "")),
str(entry.get("source", "")),
]
).casefold()
text_matches = normalized_query in haystack
if category_matches and text_matches:
matches.append(entry)
return matches
corpus = AdhkarCorpus()