Arabic Hadith Segmentation BERT (Sanad & Matn Parser)

This model is a fine-tuned version of AraBERT (aubmindlab/bert-base-arabertv02) optimized for structural token classification in classical Islamic texts. Its primary task is sequence labelingโ€”specifically identifying and drawing the boundary between the Sanad (ุณู†ุฏ - the chain of narrators) and the Matn (ู…ุชู† - the actual prophetic saying or text) within a raw, unsegmented Hadith string.

Model Description

Classical Arabic prophetic texts lack native punctuation marks or structural delimiters to explicitly isolate who narrated a saying from the saying itself. This model treats boundary segmentation as a Named Entity Recognition (NER) / Token Classification task using custom-mapped IOB tags.

Given a sequence of words, the model classifies each token into one of the following category IDs:

  • 0: B-SANAD (Beginning of the Narrator Chain)
  • 1: I-SANAD (Inside the Narrator Chain)
  • 2: B-MATN (Beginning of the Core Saying)
  • 3: I-MATN (Inside the Core Saying)

Intended Uses & Limitations

How to Use

You can easily download and use this model directly in your Python projects using the Hugging Face transformers library.

import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification

# 1. Load model and tokenizer directly from the Hub
model_name = "YOUR_HF_USERNAME/hadith-segmentation-bert"  # <-- Update with your path
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForTokenClassification.from_pretrained(model_name)

# Set model to evaluation mode
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
model.eval()

# ID mapping dict matching model configuration
id2label = {0: "B-SANAD", 1: "I-SANAD", 2: "B-MATN", 3: "I-MATN"}

# 2. Input your raw, unsegmented Hadith text
raw_hadith = 'ุญุฏุซู†ุง ุงู„ุญู…ูŠุฏูŠ ุนุจุฏ ุงู„ู„ู‡ ุจู† ุงู„ุฒุจูŠุฑ ุŒ ู‚ุงู„ : ุญุฏุซู†ุง ุณููŠุงู† ุŒ ู‚ุงู„ : ุญุฏุซู†ุง ูŠุญูŠู‰ ุจู† ุณุนูŠุฏ ุงู„ุฃู†ุตุงุฑูŠ ุŒ ู‚ุงู„ : ุฃุฎุจุฑู†ูŠ ู…ุญู…ุฏ ุจู† ุฅุจุฑุงู‡ูŠู… ุงู„ุชูŠู…ูŠ ุŒ ุฃู†ู‡ ุณู…ุน ุนู„ู‚ู…ุฉ ุจู† ูˆู‚ุงุต ุงู„ู„ูŠุซูŠ ุŒ ูŠู‚ูˆู„ : ุณู…ุนุช ุนู…ุฑ ุจู† ุงู„ุฎุทุงุจ ุฑุถูŠ ุงู„ู„ู‡ ุนู†ู‡ ุนู„ู‰ ุงู„ู…ู†ุจุฑุŒ ู‚ุงู„ : ุณู…ุนุช ุฑุณูˆู„ ุงู„ู„ู‡ ุตู„ู‰ ุงู„ู„ู‡ ุนู„ูŠู‡ ูˆุณู„ู…ุŒ ูŠู‚ูˆู„ : " ุฅู†ู…ุง ุงู„ุฃุนู…ุงู„ ุจุงู„ู†ูŠุงุชุŒ ูˆุฅู†ู…ุง ู„ูƒู„ ุงู…ุฑุฆ ู…ุง ู†ูˆู‰ุŒ ูู…ู† ูƒุงู†ุช ู‡ุฌุฑุชู‡ ุฅู„ู‰ ุฏู†ูŠุง ูŠุตูŠุจู‡ุง ุฃูˆ ุฅู„ู‰ ุงู…ุฑุฃุฉ ูŠู†ูƒุญู‡ุงุŒ ูู‡ุฌุฑุชู‡ ุฅู„ู‰ ู…ุง ู‡ุงุฌุฑ ุฅู„ูŠู‡'

# Tokenize raw text string
inputs = tokenizer(raw_hadith, return_tensors="pt", truncation=True, max_length=512)
inputs = {k: v.to(device) for k, v in inputs.items()}

# 3. Predict Token Categories
with torch.no_grad():
    outputs = model(**inputs)

predictions = torch.argmax(outputs.logits, dim=-1)[0].cpu().tolist()
input_tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])

# 4. Extract and Group Tokens based on Predicted Labels
sanad_tokens = []
matn_tokens = []

for token, pred_id in zip(input_tokens, predictions):
    if token in ["[CLS]", "[SEP]", "[PAD]"]:
        continue
    
    label = id2label.get(pred_id, "O")
    
    if "SANAD" in label:
        sanad_tokens.append(token)
    elif "MATN" in label:
        matn_tokens.append(token)

# Reconstruct clean component strings
final_sanad = tokenizer.convert_tokens_to_string(sanad_tokens)
final_matn = tokenizer.convert_tokens_to_string(matn_tokens)

print("--- Extracted Components ---")
print(f"SANAD: {final_sanad.strip()}\n")
print(f"MATN: {final_matn.strip()}")

Limitations & Biases

  • Vocalization (Harakat): Text performance might fluctuate slightly depending on whether your dataset utilizes full diacritics or completely normalized text. For extreme edge cases, it is recommended to apply text normalization (such as stripping excess tashkeel) prior to inference.
  • Length Constraints: The model is capped at a maximum sequence sequence context length of 512 subword tokens due to BERT base limitations.

Training Data & Methodology

  • Base Pretrained Architecture: aubmindlab/bert-base-arabertv02
  • Task: Token Classification (NER style Sequence Labeling)
  • Optimization Framework: Hugging Face Trainer API compiled with DataCollatorForTokenClassification for safe subword token label padding (ignore_index=-100).
  • Hyperparameters:
  • Learning Rate: 2e-5
  • Weight Decay: 0.01
  • Batch Size: 16
  • Training Epochs: 3

Technical Specifications & Requirements

To set up the development space or fine-tune this model further locally, ensure you have the following packages updated:

pip install torch transformers datasets accelerate
Downloads last month
103
Safetensors
Model size
0.1B params
Tensor type
F32
ยท
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Model tree for SHK4K/hadith-segmentation-bert

Finetuned
(4037)
this model