text stringclasses 3
values |
|---|
transformers>=4.35.0 |
tqdm>=4.65.0 |
polars>=0.19.0 |
Shamela BM25 Index (SQLite FTS5)
BM25 full-text search index for Maktabati/shamela-vectors, covering the complete Al-Maktaba Al-Shamela corpus — 8,589 classical Islamic books.
Designed as the lexical retrieval component of a hybrid search pipeline: BM25 (this repo) + Dense Embeddings (shamela-vectors) → RRF Fusion
Contents
| File | Description |
|---|---|
bm25_shamela.db |
SQLite FTS5 database (~3GB) |
build_bm25_index.py |
Script to rebuild the index from scratch |
requirements.txt |
Python dependencies |
hybrid_search.py(RRF fusion pipeline) is maintained separately and links both this index andshamela-vectors.
Why BM25 + Dense?
Dense embeddings (semantic search) excel at conceptual queries but miss exact terminology. Classical Islamic texts present a specific challenge: users searching in Modern Standard Arabic often miss Classical Arabic variants.
| Query | Dense only | BM25 only | Hybrid |
|---|---|---|---|
| فائدة بنكية (bank interest) | finds semantic matches | misses ربا if not exact | finds both |
| حكم الغرر (ruling on uncertainty) | good | good | best |
| سند specific narrator name | poor | excellent | excellent |
Arabic Preprocessing
The index applies the following normalization before indexing (consistent with shamela-vectors):
# Diacritics removed
# أإآٱ → ا
# ة → ه
# ى → ي
This ensures robust recall across orthographic variants common in classical Arabic manuscripts.
Corpus Stats
| Metric | Value |
|---|---|
| Books | 8,589 |
| Categories | 40 |
| Chunks indexed | ~4M |
| Chunk size | 512 tokens |
| Overlap | 50 tokens |
| Source | AuthenticIlm/Shamela4_Full_DB |
Schema
Each FTS5 row contains:
text_norm → normalized Arabic text (indexed, searchable)
point_id → UUID matching Maktabati/shamela-vectors exactly
book_id → Shamela book ID
title → Arabic book title
author → Arabic author name
page → Page reference (OpenITI-compatible format, e.g. V02P045)
category_ar → Arabic category name
The point_id field is a deterministic UUID computed identically to shamela-vectors, enabling direct cross-index lookup without a join table.
Usage
Option A — Download pre-built index
from huggingface_hub import hf_hub_download
path = hf_hub_download(
repo_id="Maktabati/shamela-bm25",
filename="bm25_shamela.db",
repo_type="dataset"
)
Option B — Build from scratch
# Requires: AuthenticIlm/Shamela4_Full_DB downloaded locally
pip install -r requirements.txt
python build_bm25_index.py --root /path/to/shamela_hf
Basic BM25 search
import sqlite3
def bm25_search(query: str, db_path: str, limit: int = 20):
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
rows = conn.execute("""
SELECT point_id, title, author, page, book_id
FROM chunks
WHERE text_norm MATCH ?
ORDER BY rank
LIMIT ?
""", (query, limit)).fetchall()
conn.close()
return [dict(r) for r in rows]
results = bm25_search("حكم الصلاة بغير وضوء", "bm25_shamela.db")
Hybrid Search
For full hybrid retrieval (BM25 + Dense + RRF), use this index together with Maktabati/shamela-vectors:
Query
├── BM25 → SQLite FTS5 (this repo) → Top-150
├── Dense → Qdrant / shamela-vectors → Top-150
└── RRF Fusion → Top-10
RRF score: 1/(k + rank_bm25) + 1/(k + rank_dense) with k=60
Related
- Maktabati/shamela-vectors — Dense embeddings (Qdrant, multilingual-e5-base)
- AuthenticIlm/Shamela4_Full_DB — Source corpus
License
Apache 2.0. See source corpus license for text content rights.
- Downloads last month
- 7