Skip to main content

Urdu text evaluation & benchmarking library with configurable orthographic normalization (WER, CER, chrF, BLEU).

Project description

urdu-text-eval

PyPI Version License: MIT Python Version

urdu-text-eval is a Python benchmarking library for Urdu text evaluation (OCR, ASR, transliteration, machine translation, LLM text generation, and text processing).

It computes standard NLP metrics (WER, CER, chrF, BLEU, Exact Match, Character Similarity) with configurable Urdu orthographic and taxonomy normalization (handling Arabic lookalike codepoints, zabar/zeer/pesh diacritics, punctuation, numbers, and presentation forms).


Key Features

  • Standard Metrics: Word Error Rate (WER), Character Error Rate (CER), Word/Char Accuracy, chrF, BLEU, Exact Match, and Levenshtein Character Similarity.
  • Urdu Taxonomy Normalization: Standardizes Arabic variants (كک, يی, هہ), presentation forms (, , ), and combined characters (ا+ٓآ).
  • Fine-Grained Diacritic Controls: Selective removal of اعراب (Zabar َ, Zeer ِ, Pesh ُ, Tanween ً ٌ ٍ, Shadda ّ, Sukun ْ, Maddah, and Hamza marks).
  • Flexible Input Formats: Evaluates simple list-of-dicts [{"actual": "...", "pred": "..."}] with support for common key aliases (ref, target, gold, Urdu, hyp, prediction, output).
  • Dual Reporting: Always reports both taxonomy-normalized (primary) and raw surface metrics side-by-side.

Installation

Install via pip:

pip install urdu-text-eval

(Dependencies: jiwer, sacrebleu, rapidfuzz)


Quick Start

from urdu_text_eval import evaluate

pairs = [
    {"actual": "کیا یہ ہے؟", "pred": "كيا يہ ہے؟"},
    {"actual": "آہ جو دل سے", "pred": "آه جو دل سے"},
]

result = evaluate(pairs)

print(result["summary"])
print(f"WER: {result['wer']:.4f} | CER: {result['cer']:.4f} | chrF: {result['chrf']:.4f}")

Output Structure

Calling evaluate(pairs, per_sample=True) returns a dictionary containing:

Key Type Description
wer float Primary Word Error Rate (0.0 = perfect match)
cer float Primary Character Error Rate
word_accuracy float 1.0 - wer
char_accuracy float 1.0 - cer
normalized_exact_match float Ratio of exact matches after normalization
chrf float Character n-gram F-score (sacrebleu)
bleu float Corpus BLEU score
mean_char_similarity float Mean normalized Levenshtein similarity
raw_wer / raw_cer float Metrics calculated on raw strings without normalization
raw_exact_match float Exact match ratio on raw strings
summary str Pre-formatted, printable evaluation report
samples list (Optional, if per_sample=True) List of dicts with per-row scores and normalized strings

Normalization & Customization

Urdu text often varies in orthography (e.g. Arabic vs. Urdu keyboards, presence of diacritics/اعراب, punctuation). You can control normalization behavior precisely:

1. Master On/Off

# Default: Normalization ON (Fair orthographic evaluation)
result = evaluate(pairs)

# Raw evaluation (No normalization applied)
result = evaluate(pairs, normalize=False)

2. Convenience Overrides

You can pass boolean flags directly to evaluate():

result = evaluate(
    pairs,
    taxonomy=True,            # Convert Arabic codepoints (ك/ي/ه) to Urdu (ک/ی/ہ)
    remove_diacritics=True,   # Remove all اعراب (zabar, zeer, pesh, tanween, etc.)
    remove_zabar=True,        # Remove zabar (َ) only
    remove_zeer=True,         # Remove zeer (ِ) only
    remove_pesh=True,         # Remove pesh (ُ) only
    remove_punctuation=True,  # Remove Urdu (؛،؟۔٪) and ASCII punctuation
    remove_digits=False,      # Remove ASCII and Urdu digits
)

3. Using NormalizeConfig or Presets

For reusability across benchmarks, configure a NormalizeConfig:

from urdu_text_eval import evaluate, NormalizeConfig

# Presets
cfg_default = NormalizeConfig.default()          # Taxonomy + diacritics + whitespace
cfg_raw     = NormalizeConfig.none()             # Raw string comparison
cfg_full    = NormalizeConfig.full()             # Everything on (including punctuation & digit stripping)
cfg_diac    = NormalizeConfig.diacritics_only()  # Only remove اعراب
cfg_punct   = NormalizeConfig.punctuation_only() # Only remove punctuation
cfg_tax     = NormalizeConfig.taxonomy_only()    # Only taxonomy mapping

# Custom Configuration
cfg = NormalizeConfig(
    enabled=True,
    taxonomy=True,             # Map Arabic/presentation characters
    combine_characters=True,   # Combine characters (ا + ٓ → آ)
    remove_zabar=True,         # Remove zabar
    remove_zeer=True,          # Remove zeer
    remove_pesh=True,          # Remove pesh
    remove_tanween=True,       # Remove tanween
    remove_shadda=True,        # Remove shadda
    remove_sukun=True,         # Remove sukun
    remove_punctuation=False,  # Keep punctuation
    remove_digits=False,       # Keep digits
)

result = evaluate(pairs, config=cfg)

Standalone Normalizer Function

You can also use the Urdu normalizer directly on individual text strings:

from urdu_text_eval import normalize_urdu, NormalizeConfig

# Default normalization
clean_text = normalize_urdu("كيا يہ ہے؟")
# Output: "کیا یہ ہے؟"

# Remove diacritics (zabar/zeer/pesh) only
clean_text = normalize_urdu("شیرِ پنجاب", remove_diacritics_all=True)
# Output: "شیر پنجاب"

# Punctuation removal
clean_text = normalize_urdu("سلام، دنیا!", config=NormalizeConfig.punctuation_only())
# Output: "سلام دنیا"

API Summary

from urdu_text_eval import (
    evaluate,           # Core benchmark function: evaluate([{"actual": "...", "pred": "..."}])
    NormalizeConfig,    # Configuration dataclass & presets for text normalization
    normalize_urdu,     # Single-string Urdu normalization function
    compute_metrics,    # Lower-level function: compute_metrics(references, hypotheses)
    format_metrics,     # Formats metrics dict into a human-readable text summary
    per_sample_errors,  # Returns list of per-item error breakdown dicts
)

License

Distributed under the MIT License.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

urdu_text_eval-1.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

urdu_text_eval-1.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file urdu_text_eval-1.0.tar.gz.

File metadata

  • Download URL: urdu_text_eval-1.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.9

File hashes

Hashes for urdu_text_eval-1.0.tar.gz
Algorithm Hash digest
SHA256 d35b098484a2190f846322fdbb256ef531df90fd54d362f2c6baeed68039b92d
MD5 ee7166de1d40e088553f19ef06948902
BLAKE2b-256 4f02eceea82c06e0d21cb860f013be7df3dcb018cda65760c3ae2b06af585a30

See more details on using hashes here.

File details

Details for the file urdu_text_eval-1.0-py3-none-any.whl.

File metadata

  • Download URL: urdu_text_eval-1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.9

File hashes

Hashes for urdu_text_eval-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ecad094b6848da6cdc1d5003d74db0de7294150692e1a187179b9dd23910a318
MD5 9c075eb98818a921763ca20cc64f5ef2
BLAKE2b-256 8397a81f521edaf8cdf08950b8d21c037a02feaba35ece83d43c16c7f47a7220

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page