Skip to main content

A comprehensive Python package for preprocessing Swahili (Kiswahili) text data. Provides functions for text normalization, stopword removal, slang normalization, typo correction, and tokenization. Includes curated datasets of Swahili stopwords, slang terms, and common typos. Designed for NLP tasks on Swahili text corpora.

Project description

swahiliclean (Python)

Python port of the Swahili text preprocessing toolkit for NLP on Kiswahili text.

Quick Start

Installation

pip install -e .

For development with tests:

pip install -e .[test]
python3 -m pytest

Basic Usage

from swahiliclean import preprocess_sw, tokenize_sw_words

# Full preprocessing pipeline
text = "manzi poa sana 2na tufanye kazi na sisi"
cleaned = preprocess_sw(text)
print(cleaned)

# Tokenization
tokens = tokenize_sw_words(text)
print(tokens)  # ["manzi", "poa", "sana", ...]

Batch Processing

All functions support batch processing with lists:

sentences = [
    "manzi poa sana 2na tufanye kazi na sisi",
    "Habari za asubuhi? Nimekuja kwa ajili ya kazi.",
    "Na sisi tunaenda kwa shule leo."
]

# Process all at once
processed = preprocess_sw(sentences)
for original, cleaned in zip(sentences, processed):
    print(f"{original} -> {cleaned}")

Features

  • Text Normalization: Lowercase, trim, collapse whitespace
  • Stopword Removal: Remove common Swahili stopwords (~258 words)
  • Slang Normalization: Replace slang with standard forms (~190 mappings)
  • Typo Correction: Fix common typos (~422 corrections)
  • Tokenization: Simple word tokenization
  • Email & URL Removal: Detect and remove email addresses and URLs
  • Text Statistics: Count and analyze stopwords, slang, typos with percentages
  • Full Pipeline: Combine all steps with configurable options

Available Functions

Core Functions

  • normalize_sw_text() - Text normalization
  • remove_sw_stopwords() - Stopword removal
  • normalize_sw_slang() - Slang normalization
  • correct_sw_typos() - Typo correction
  • tokenize_sw_words() - Word tokenization
  • preprocess_sw() - Full preprocessing pipeline

Email & URL Removal

  • remove_emails() - Remove email addresses
  • remove_urls() - Remove URLs
  • remove_emails_and_urls() - Remove both emails and URLs

Text Statistics

  • count_sw_words() - Count words in text
  • count_sw_stopwords() - Count stopwords found
  • count_sw_slang() - Count slang terms found
  • count_sw_typos() - Count typos found
  • get_text_statistics() - Get comprehensive statistics
  • get_cleaning_statistics() - Compare before/after cleaning

Custom Data

You can provide your own stopwords, typos, or slang mappings:

from swahiliclean import preprocess_sw

# Custom stopwords
result = preprocess_sw(
    "na sisi tunaenda yangu",
    custom_stopwords=["yangu", "yako"]
)

# Custom typos
result = preprocess_sw(
    "kwahiyo tunaenda",
    custom_typos=[("kwahiyo", "kwa hiyo")]
)

# Custom slang
result = preprocess_sw(
    "hii kazi ni mzito",
    custom_slang=[("mzito", "mzito sana")]
)

Configuration Options

All functions support extensive configuration:

  • Case sensitivity: Control case-sensitive matching
  • Word boundaries: Toggle word boundary matching for stopwords
  • Case preservation: Preserve original case when needed
  • Whitespace handling: Preserve newlines/tabs or normalize them
  • Custom patterns: Use custom regex patterns for tokenization
  • Step control: Enable/disable individual pipeline steps

Documentation

For complete documentation including:

  • Full parameter reference for all functions
  • Detailed examples and use cases
  • Edge cases and behavior notes
  • Advanced configuration options

See DOCUMENTATION.md for the complete manual.

Data

Bundled data files (CSV format):

  • swahiliclean/data/Stopwords.csv - ~258 stopwords
  • swahiliclean/data/Slangs.csv - ~190 slang mappings
  • swahiliclean/data/Typos.csv - ~422 typo corrections

Examples

Step-by-Step Processing

from swahiliclean import (
    normalize_sw_text,
    remove_sw_stopwords,
    correct_sw_typos,
    normalize_sw_slang
)

text = "manzi poa sana 2na tufanye kazi na sisi"

# Step 1: Normalize
normalized = normalize_sw_text(text)

# Step 2: Correct typos
corrected = correct_sw_typos(normalized)

# Step 3: Normalize slang
slang_normalized = normalize_sw_slang(corrected)

# Step 4: Remove stopwords
final = remove_sw_stopwords(slang_normalized)

Advanced Configuration

# Preserve case and newlines
result = preprocess_sw(
    "Habari YAKO\nleo",
    lowercase=False,
    preserve_newlines=True,
    remove_stop=False
)

# Case-sensitive processing
result = preprocess_sw(
    "Na sisi",
    stopwords_case_sensitive=True,
    slang_case_sensitive=True
)

# Remove emails and URLs
result = preprocess_sw(
    "Contact test@example.com or visit https://site.com",
    remove_emails_flag=True,
    remove_urls_flag=True
)

Text Statistics

from swahiliclean import get_text_statistics, get_cleaning_statistics, preprocess_sw

# Get statistics before cleaning
text = "na sisi tunaenda kwa shule"
stats = get_text_statistics(text)
print(f"Stopwords: {stats['stopwords_count']} ({stats['stopwords_percentage']}%)")
print(f"Total detected: {stats['total_detected']} ({stats['total_percentage']}%)")

# Compare before and after
before = "na sisi tunaenda kwa shule"
after = preprocess_sw(before)
cleaning_stats = get_cleaning_statistics(before, after)
print(f"Words before: {cleaning_stats['words_before']}")
print(f"Words after: {cleaning_stats['words_after']}")
print(f"Words removed: {cleaning_stats['words_removed']} ({cleaning_stats['words_removed_percentage']}%)")

Testing

Run the test suite:

python3 -m pytest

Quick batch testing:

python3 tests/batch_demo.py

Authors

License

MIT License (see LICENSE).

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

swahiliclean-0.1.0.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

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

swahiliclean-0.1.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for swahiliclean-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3abec76d9193e17da202c5d9214fceb30bfe5ba1bf109798317abcad4dfc0058
MD5 2a67316be1a56d9491cf25a79ec5cd8b
BLAKE2b-256 a0b18b12ea14d52a1187b6eb09e594a7dc4ebac021a6f740afc5c93405101fe8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swahiliclean-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for swahiliclean-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 417758139979ef16633279ea5f10e119b0936537b6b2d1be2a697156b31955b5
MD5 a52f6318adc1a6c1c76a75a2b15fd976
BLAKE2b-256 dc0140ff1f135236081a1cce5520140547cefe895c4a38fa81003975e9706bef

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