Skip to main content

Urdu NLP toolkit — sentence segmentation that outperforms urduhack

Project description

urdunlp

Urdu sentence segmentation that outperforms urduhack.

pip install urdunlp

The problem

Urdu has 230 million speakers and almost no production-ready NLP tooling. The dominant open-source library, urduhack, implements sentence tokenisation — but its accuracy on real Urdu text leaves room to improve.

urdunlp is a lightweight, zero-dependency library built around a rule-based segmentation algorithm that was benchmarked against urduhack and outperformed it by ~7 percentage points on the same dataset.

Model Boundary accuracy
urduhack sentence_tokenizer 74.1%
urdunlp segment 81.5%

Quickstart

from urdunlp import segment

text = "وزیراعظم نے کہا کہ ملک ترقی کر رہا ہے۔ عوام خوشحال ہیں اور مستقبل روشن ہے۔"
sentences = segment(text)
# ['وزیراعظم نے کہا کہ ملک ترقی کر رہا ہے', 'عوام خوشحال ہیں اور مستقبل روشن ہے']

Domain adaptation

The default endword and conjunction lists were learned from a news corpus. For better accuracy on your domain (legal, literary, conversational), pass a hand-labeled reference text to learn_from_text first:

from urdunlp import segment, learn_from_text

with open("my_labeled_text.txt", encoding="utf-8") as f:
    labeled = f.read()

endwords, conjunctions = learn_from_text(labeled)
sentences = segment(my_raw_text, endwords=endwords, conjunctions=conjunctions)

Evaluating accuracy

from urdunlp import segment, boundary_accuracy

predicted = segment(raw_text)
reference = labeled_text.split("۔")

score = boundary_accuracy(predicted, reference)
print(f"Accuracy: {score:.1f}%")

For a corpus of documents:

from urdunlp import corpus_accuracy

score = corpus_accuracy(predicted_corpus, reference_corpus)

API reference

segment(text, endwords=None, conjunctions=None) → list[str]

Segment Urdu text into sentences.

  • text — raw Urdu string
  • endwords — optional set[str] of sentence-ending words (uses built-in defaults if None)
  • conjunctions — optional set[str] of continuation words (uses built-in defaults if None)

Returns a list of sentence strings, without the trailing ۔.


learn_from_text(labeled_text, base_endwords=None, base_conjunctions=None) → tuple[set, set]

Learn endwords and conjunctions from a hand-labeled text. The labeled text should use ۔ as the sentence boundary marker.

Returns (endwords, conjunctions) — pass these directly to segment.


boundary_accuracy(predicted, reference, window=3) → float

Compute segmentation accuracy using a trailing-word boundary match strategy. Returns a percentage (0–100).


corpus_accuracy(predicted_corpus, reference_corpus, window=3) → float

Mean boundary_accuracy across a list of (predicted, reference) document pairs.


How it works

The segmentation algorithm works in two passes:

  1. Primary split — divide on ۔ (the Urdu full stop), which handles the majority of sentence boundaries cleanly.

  2. Secondary rule — after a primary split, check whether the final word of each chunk is a known endword (a verb form that typically closes a clause). If the word immediately following an endword is a conjunction or another endword, treat the boundary as a continuation rather than a full stop — this handles compound sentences and subordinate clauses that urduhack mis-segments.

The endword and conjunction lists are seeded with defaults learned from a news corpus and can be extended for any domain using learn_from_text.


Development

git clone https://github.com/manhazamir/urdunlp
cd urdunlp
pip install -e ".[dev]"
pytest

Research

This library packages the algorithm from original research comparing Urdu sentence segmentation approaches. The benchmark used a hand-labeled dataset of Urdu news paragraphs; accuracy was computed using boundary-precision matching on the final three words of each sentence.

Project details


Download files

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

Source Distribution

urdu_segmentation_nlp-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

urdu_segmentation_nlp-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file urdu_segmentation_nlp-0.1.0.tar.gz.

File metadata

  • Download URL: urdu_segmentation_nlp-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for urdu_segmentation_nlp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e0f250b35ccd735d3357e7ba353e9f0bff7474544ab4c85e3006c23480164f6a
MD5 6ab002c01e36d18b775e986dfd097588
BLAKE2b-256 25c16c05369d5ef2ad9ad94d9b4b863b3cceaed25132f81207cecc25255e5ab0

See more details on using hashes here.

File details

Details for the file urdu_segmentation_nlp-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for urdu_segmentation_nlp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f56be757831d59e18c88f6b926fd160eba00b271dccab746e5984214b602f79
MD5 8a54d9a915b9ab9124d94b4f1fce020b
BLAKE2b-256 5a74a788f1966ebd118bffebfb4f814ede8507372b2d957248654806a831ff31

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