Skip to main content

Urdu NLP toolkit — sentence segmentation that outperforms urduhack

Project description

urdunlp

Urdu sentence segmentation that outperforms urduhack.

pip install urdu-segmentation-nlp

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/urdu-segmentation-nlp
cd urdunlp
pip install pytest
python -m pytest tests/ -v

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.1.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.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: urdu_segmentation_nlp-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 37e32098802ed7e7be806b6b319986edce05b271ffe7ae7f05385a1b620bfc40
MD5 ec505287ecd62c2b739259ecce17f0ed
BLAKE2b-256 128aaf22a459e9d400cd9d82f910e61329d236965bbf0521f8e384f552abe8ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for urdu_segmentation_nlp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3ca5f0c7181e9c6a0d01d6c62460c03c1a5dd4280715370363b85609afdc7abf
MD5 220c7bf77f6e65d1d258ae717e10e437
BLAKE2b-256 6ebaf161109a3ac85ccdf508e7a493e4f9b4b9d2b0beb79a740c3600e31c0f3a

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