forked from Deen-Bridge/dnb-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvocabulary.py
More file actions
290 lines (217 loc) · 9.58 KB
/
Copy pathvocabulary.py
File metadata and controls
290 lines (217 loc) · 9.58 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""Quranic Vocabulary Analysis and Learning System (#168)
Provides root-word extraction, frequency statistics, morphological grouping,
search by root or meaning, and example-verse retrieval for Quranic Arabic.
"""
import json
import logging
import re
from collections import defaultdict
from pathlib import Path
from typing import Any
from fastapi import APIRouter
from pydantic import BaseModel, Field
from corpus import corpus
logger = logging.getLogger(__name__)
router = APIRouter(tags=["vocabulary"])
# ---------------------------------------------------------------------------
# Data loading
# ---------------------------------------------------------------------------
DATA_DIR = Path(__file__).parent / "data"
VOCAB_FILE = DATA_DIR / "quran_vocabulary.json"
def _load_vocabulary_data() -> dict[str, Any]:
if VOCAB_FILE.exists():
with open(VOCAB_FILE, encoding="utf-8") as f:
return json.load(f)
return {}
VOCAB_DATA: dict[str, Any] = _load_vocabulary_data()
# ---------------------------------------------------------------------------
# Arabic root extraction (light stemmer)
# ---------------------------------------------------------------------------
# Common Arabic prefixes/suffixes stripped during light stemming
_PREFIXES = ["ال", "و", "ف", "ب", "ل", "ك", "س"]
_SUFFIXES = ["ه", "ها", "هم", "هن", "هما", "ون", "ين", "ان", "ات", "ية", "ي"]
_TASHKEEL = re.compile(r"[\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED]")
def strip_tashkeel(word: str) -> str:
return _TASHKEEL.sub("", word)
def extract_root(word: str) -> str:
"""Light Arabic root extraction: strip tashkeel, common prefixes/suffixes, and
reduce consonant clusters to approximate the trilateral/quadrilateral root.
This is a heuristic — for production, pair with a morphological analyser.
"""
w = strip_tashkeel(word).strip()
# strip common prefixes
for p in sorted(_PREFIXES, key=len, reverse=True):
if len(w) > 3 and w.startswith(p):
candidate = w[len(p) :]
if len(candidate) >= 2:
w = candidate
break
# strip common suffixes
for s in sorted(_SUFFIXES, key=len, reverse=True):
if len(w) > 3 and w.endswith(s):
candidate = w[: -len(s)]
if len(candidate) >= 2:
w = candidate
break
return w
# ---------------------------------------------------------------------------
# Vocabulary DB helpers
# ---------------------------------------------------------------------------
def get_all_entries() -> list[dict[str, Any]]:
return VOCAB_DATA.get("entries", [])
def find_by_root(root: str) -> list[dict[str, Any]]:
root = strip_tashkeel(root).strip()
return [e for e in get_all_entries() if extract_root(e.get("word", "")) == root]
def find_by_meaning(query: str) -> list[dict[str, Any]]:
q = query.lower()
return [e for e in get_all_entries() if q in e.get("meaning", "").lower()]
def find_by_word(word: str) -> dict[str, Any] | None:
w = strip_tashkeel(word).strip()
for e in get_all_entries():
if strip_tashkeel(e.get("word", "")) == w:
return e
return None
# ---------------------------------------------------------------------------
# Frequency analysis
# ---------------------------------------------------------------------------
def _count_word_in_corpus(word: str) -> int:
"""Count occurrences of a word across all ayat."""
w = strip_tashkeel(word)
count = 0
for _key, ayah_data in corpus.ayat.items():
text = strip_tashkeel(ayah_data.get("text", ""))
count += text.count(w)
return count
def compute_frequencies() -> list[dict[str, Any]]:
"""Return vocabulary entries enriched with corpus frequency counts."""
results = []
for entry in get_all_entries():
freq = _count_word_in_corpus(entry.get("word", ""))
results.append({**entry, "frequency": freq})
return sorted(results, key=lambda e: -e["frequency"])
# ---------------------------------------------------------------------------
# Root family grouping
# ---------------------------------------------------------------------------
def group_by_root() -> dict[str, list[dict[str, Any]]]:
"""Group vocabulary entries by their extracted root."""
groups: dict[str, list[dict[str, Any]]] = defaultdict(list)
for entry in get_all_entries():
root = extract_root(entry.get("word", ""))
groups[root].append(entry)
return dict(groups)
# ---------------------------------------------------------------------------
# Example verse retrieval
# ---------------------------------------------------------------------------
def get_example_verses(word: str, limit: int = 5) -> list[dict[str, Any]]:
"""Return up to *limit* ayat that contain *word*."""
w = strip_tashkeel(word)
results: list[dict[str, Any]] = []
for key, ayah_data in corpus.ayat.items():
text = ayah_data.get("text", "")
if w in strip_tashkeel(text):
parts = key.split(":")
results.append({
"surah": int(parts[0]),
"ayah": int(parts[1]),
"text": text,
})
if len(results) >= limit:
break
return results
# ---------------------------------------------------------------------------
# Response models
# ---------------------------------------------------------------------------
class VocabularyEntry(BaseModel):
word: str = Field(..., description="Quranic Arabic word")
root: str = Field(default="", description="Extracted root")
meaning: str = Field(default="", description="English meaning")
frequency: int = Field(default=0, description="Occurrence count in Quran")
verses: list[dict[str, Any]] = Field(default_factory=list, description="Example verses")
class RootFamily(BaseModel):
root: str
words: list[dict[str, Any]]
class VocabularySearchRequest(BaseModel):
query: str = Field(..., min_length=1, description="Root, word, or English meaning to search")
search_type: str = Field(default="auto", description="'root', 'meaning', 'word', or 'auto'")
class VocabularySearchResponse(BaseModel):
results: list[dict[str, Any]]
total: int
class FrequencyResponse(BaseModel):
entries: list[dict[str, Any]]
total: int
class RootFamilyResponse(BaseModel):
families: dict[str, list[dict[str, Any]]]
total_roots: int
class ExampleVersesResponse(BaseModel):
word: str
verses: list[dict[str, Any]]
# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
@router.get("/vocabulary/search", response_model=VocabularySearchResponse)
async def search_vocabulary(
query: str = "",
search_type: str = "auto",
) -> VocabularySearchResponse:
"""Search vocabulary by root, word form, or English meaning.
Use ``search_type=root|word|meaning`` or ``auto`` (default) which tries
all three and merges results.
"""
results: list[dict[str, Any]] = []
if search_type == "root":
results = find_by_root(query)
elif search_type == "meaning":
results = find_by_meaning(query)
elif search_type == "word":
match = find_by_word(query)
results = [match] if match else []
else:
# auto
by_root = find_by_root(query)
by_meaning = find_by_meaning(query)
by_word = find_by_word(query)
seen: set[str] = set()
for entry in by_root + by_meaning:
w = entry.get("word", "")
if w not in seen:
seen.add(w)
results.append(entry)
if by_word and by_word.get("word") not in seen:
results.append(by_word)
return VocabularySearchResponse(results=results, total=len(results))
@router.get("/vocabulary/frequencies", response_model=FrequencyResponse)
async def get_frequencies() -> FrequencyResponse:
"""Return all vocabulary entries sorted by Quranic frequency."""
entries = compute_frequencies()
return FrequencyResponse(entries=entries, total=len(entries))
@router.get("/vocabulary/roots", response_model=RootFamilyResponse)
async def get_root_families() -> RootFamilyResponse:
"""Group vocabulary by their Arabic root."""
families = group_by_root()
return RootFamilyResponse(families=families, total_roots=len(families))
@router.get("/vocabulary/verse-examples", response_model=ExampleVersesResponse)
async def verse_examples(word: str = "", limit: int = 5) -> ExampleVersesResponse:
"""Retrieve example ayat containing the given word."""
verses = get_example_verses(word, limit=limit)
return ExampleVersesResponse(word=word, verses=verses)
@router.get("/vocabulary/word/{word}", response_model=VocabularyEntry)
async def get_word_detail(word: str) -> VocabularyEntry:
"""Get full details for a single word including root, meaning, and examples."""
entry = find_by_word(word)
if not entry:
from errors import APIException
raise APIException(
status_code=404,
detail=f"Word '{word}' not found in vocabulary database.",
hint="Try a different spelling or search by root/meaning using /vocabulary/search.",
)
root = extract_root(entry.get("word", ""))
freq = _count_word_in_corpus(entry.get("word", ""))
verses = get_example_verses(entry.get("word", ""), limit=5)
return VocabularyEntry(
word=entry.get("word", word),
root=root,
meaning=entry.get("meaning", ""),
frequency=freq,
verses=verses,
)