youssefreda9 commited on
Commit
f5c89a7
·
1 Parent(s): e73811f

fix: use full verse text instead of window words (fixes missing الر and edge words)

Browse files
Files changed (1) hide show
  1. quran.py +15 -33
quran.py CHANGED
@@ -310,45 +310,28 @@ def search_bayan(query_text: str,
310
  """Arabic-Indic for uthmani, Latin for translations"""
311
  return to_arabic_nums(n) if lang_code == "uthmani" else str(n)
312
 
313
- # بناء matched_segment مع رقم كل آية
314
- aya_words: dict[tuple, list] = {}
315
- for w in matched_words:
316
- key = (w["sura_num"], w["aya_num"])
317
- if key not in aya_words:
318
- aya_words[key] = []
319
- aya_words[key].append(w["uthmani"] if lang_code == "uthmani" else w["target_text"])
320
-
321
- if lang_code == "uthmani":
322
- seg_parts = [
323
- " ".join(words) + f" ({fmt_num(a_num)})"
324
- for (_, a_num), words in aya_words.items()
325
- ]
326
- matched_segment = " ".join(seg_parts)
327
- else:
328
- # للترجمات: نص الآية كامل + رقمها (بدون تكرار)
329
- seen_texts, seg_parts = set(), []
330
- for (_, a_num), words in aya_words.items():
331
- txt = words[0] # target_text مكرر لكل كلمة، نأخذ الأول
332
- if txt not in seen_texts:
333
- seen_texts.add(txt)
334
- seg_parts.append(f"{txt} ({fmt_num(a_num)})")
335
- matched_segment = " ".join(seg_parts)
336
-
337
- # الآيات المشمولة — مرتبة بالترتيب
338
  involved: dict[tuple, dict] = {}
339
  for w in matched_words:
340
  key = (w["sura_num"], w["aya_num"])
341
  if key not in involved:
342
- involved[key] = {"sura_name": w["sura_name"], "target_text": w["target_text"]}
 
 
 
 
343
 
344
  ayah_nums = [a_num for (_, a_num) in involved]
345
  sura_name = next(iter(involved.values()))["sura_name"]
346
 
347
- verse_body_parts = []
 
 
 
348
  for (s_num, a_num), data in involved.items():
349
- verse_body_parts.append(f"{data['target_text']} ({fmt_num(a_num)})")
350
 
351
- combined_body = " ".join(verse_body_parts)
352
 
353
  # بناء المرجع: نطاق (من-إلى) بدل سرد كل الأرقام
354
  if len(ayah_nums) == 1:
@@ -358,12 +341,11 @@ def search_bayan(query_text: str,
358
  last = fmt_num(ayah_nums[-1])
359
  ref = f"{sura_name}: {first}-{last}"
360
 
361
- full_verse_formatted = f"({combined_body}) 【{ref}】"
362
- matched_segment = f"({matched_segment}) 【{ref}】"
363
 
364
  is_exact = best_score >= 0.999
365
  return {
366
- "matched_segment": matched_segment,
367
- "full_verse": full_verse_formatted,
368
 
369
  }
 
310
  """Arabic-Indic for uthmani, Latin for translations"""
311
  return to_arabic_nums(n) if lang_code == "uthmani" else str(n)
312
 
313
+ # الآيات المشمولة مرتبة بالترتيب (من الـ matched window)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  involved: dict[tuple, dict] = {}
315
  for w in matched_words:
316
  key = (w["sura_num"], w["aya_num"])
317
  if key not in involved:
318
+ involved[key] = {
319
+ "sura_name": w["sura_name"],
320
+ "target_text": w["target_text"],
321
+ "uthmani": w.get("uthmani", ""),
322
+ }
323
 
324
  ayah_nums = [a_num for (_, a_num) in involved]
325
  sura_name = next(iter(involved.values()))["sura_name"]
326
 
327
+ # ── بناء النص الكامل من الآيات الكاملة (مش من الـ window بس) ──
328
+ # نجيب الآيات الكاملة من DB عبر involved
329
+ # target_text = النص الكامل للآية (uthmani أو الترجمة)
330
+ verse_parts = []
331
  for (s_num, a_num), data in involved.items():
332
+ verse_parts.append(f"{data['target_text']} ({fmt_num(a_num)})")
333
 
334
+ combined_body = " ".join(verse_parts)
335
 
336
  # بناء المرجع: نطاق (من-إلى) بدل سرد كل الأرقام
337
  if len(ayah_nums) == 1:
 
341
  last = fmt_num(ayah_nums[-1])
342
  ref = f"{sura_name}: {first}-{last}"
343
 
344
+ result_text = f"({combined_body}) 【{ref}】"
 
345
 
346
  is_exact = best_score >= 0.999
347
  return {
348
+ "matched_segment": result_text,
349
+ "full_verse": result_text,
350
 
351
  }