Dataset Viewer
Auto-converted to Parquet Duplicate
text
stringclasses
3 values
transformers>=4.35.0
tqdm>=4.65.0
polars>=0.19.0

Shamela BM25 Index

BM25 full-text search index for the Al-Maktaba Al-Shamela digital library, built on SQLite FTS5. Part of the maktabati.ai Islamic RAG pipeline.

Designed to pair with Maktabati/shamela-vectors for hybrid retrieval (BM25 + Dense + RRF fusion).


Statistics

Books 8,589
Categories 40
Book chunks 12,331,995
Quran verses (standalone) 6,236
Total rows 12,338,231

Chunking: 512 tokens, 50-token overlap (tokenizer: intfloat/multilingual-e5-base).
Build time: ~2 hours on a modern CPU.


Files

File Description
bm25_shamela.db.zst.aabm25_shamela.db.zst.am SQLite FTS5 database, zstd-compressed and split into 500 MB parts — 6.4 GB download, 23 GB uncompressed
build_bm25_index.py Builds the book index — resume-capable, CPU-only
add_quran_bm25.py Appends the 6,236 Quran verses to an existing DB
requirements.txt Python dependencies

Download & Reconstruct

Command line:

# Download all parts (needs huggingface_hub >= 0.20)
pip install huggingface_hub
huggingface-cli download Maktabati/shamela-bm25 --repo-type dataset --local-dir .

# Reassemble and decompress (needs zstd)
cat bm25_shamela.db.zst.* | zstd -d -o bm25_shamela.db

Python:

from huggingface_hub import snapshot_download
import subprocess, pathlib

local = snapshot_download("Maktabati/shamela-bm25", repo_type="dataset")
parts = sorted(pathlib.Path(local).glob("bm25_shamela.db.zst.*"))
with open("bm25_shamela.db", "wb") as out:
    subprocess.run(["zstd", "-d", "--stdout"] + [str(p) for p in parts], stdout=out)

Install zstd: sudo apt install zstd / brew install zstd


Schema

CREATE VIRTUAL TABLE chunks USING fts5(
    text_norm,                -- normalized Arabic text (FTS5-indexed)
    point_id   UNINDEXED,     -- UUID → matches Qdrant point IDs in shamela-vectors
    book_id    UNINDEXED,     -- numeric Shamela book ID
    title      UNINDEXED,     -- book title (or surah name for Quran)
    author     UNINDEXED,     -- author name
    page       UNINDEXED,     -- page ref: "V02P045" for books, "2:255" for Quran
    category_ar UNINDEXED,    -- Arabic category name
    tokenize = 'unicode61'
);

The point_id field is a deterministic UUID (SHA-256 of book_id#page#chunk_no) that directly matches the Qdrant point IDs in shamela-vectors — no separate join table needed for hybrid fusion.


Arabic Normalization

Applied to text_norm at index time and to queries at search time (must be symmetric):

  • Remove all diacritics: harakat, tanwin, shadda, sukun, Quranic signs (Unicode ranges \u064B–\u065F, \u0610–\u061A, \u06D6–\u06ED, …)
  • أإآٱ → ا
  • ة → ه
  • ى → ي
  • Collapse whitespace

Usage

Standalone BM25

import sqlite3, re

def normalize_arabic(text: str) -> str:
    text = re.sub(
        r'[\u0610-\u061A\u064B-\u065F\u0670'
        r'\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED]',
        '', text)
    text = re.sub(r'[أإآٱ]', 'ا', text)
    text = text.replace('ة', 'ه').replace('ى', 'ي')
    return re.sub(r'\s+', ' ', text).strip()

conn  = sqlite3.connect("bm25_shamela.db")
query = normalize_arabic("ما حكم الصلاة بغير وضوء")

# Quote tokens → no FTS5 syntax errors; OR → higher recall, BM25 ranks by IDF
tokens    = [w for w in query.split() if w]
fts_query = " OR ".join('"' + w.replace('"', '""') + '"' for w in tokens)

rows = conn.execute(
    "SELECT point_id, title, author, page "
    "FROM chunks WHERE text_norm MATCH ? ORDER BY rank LIMIT 20",
    (fts_query,)
).fetchall()

Hybrid Search (BM25 + Dense + RRF)

See hybrid_search.py in Maktabati/shamela-vectors.

RRF formula: score(d) = Σ 1 / (k + rank_i), k = 60.
BM25 candidates: 150 · Dense candidates: 150 · Final top-k: configurable.


Rebuild from Scratch

# 1. Build book index (resume-capable — safe to restart)
python build_bm25_index.py

# 2. Append standalone Quran verses (runs in seconds, idempotent)
python add_quran_bm25.py

The Quran step is separate because Shamela stores Quran verses outside the book directory structure (_meta/quran_verses.jsonl). Always run both steps.


Relation to shamela-vectors

shamela-bm25 shamela-vectors
Search type Lexical (BM25) Semantic (Dense)
Backend SQLite FTS5 Qdrant
Model intfloat/multilingual-e5-base
Quran included ✓ (via add_quran_bm25.py)
UUID scheme SHA-256(book_id#page#chunk_no) identical

License

Text content from Al-Maktaba Al-Shamela — please respect their terms of use. Index structure and scripts: CC BY-SA 4.0.

Downloads last month
187