Skip to main content

🇱🇰 sinhala-toolkit

The missing developer toolkit for Sinhala language processing

සිංහල භාෂා සැකසීම සඳහා නවීන developer toolkit එක License: MIT Packagist Version PyPI Version PHP Version Python Version Sinhala NLP PRs Welcome GitHub Stars

English · සිංහල · Quick Start · API Reference · Contributing


Why this exists

There are academic NLP papers on Sinhala. There are scattered university datasets. But if you're a developer who wants to just use a Sinhala tokenizer, cleaner, or stopword filter in your PHP or Python project today — you had nothing.

Until now.

sinhala-toolkit is a production-ready, zero-dependency Sinhala NLP toolkit for PHP (gosdrkht/sinhala-toolkit) and Python (sinkit). Built for the Sri Lankan dev community, by a Sri Lankan developer.


Features

Feature Status
📋 Stopwords list (140+ curated words, categorised) ✅ Ready (PHP & Python)
🧹 Text cleaner (Unicode, ZWJ, whitespace, noise) ✅ Ready (PHP & Python)
✂️ Sentence tokenizer (danda, mixed punctuation) ✅ Ready (PHP & Python)
🔤 Word tokenizer (Sinhala, English, mixed) ✅ Ready (PHP & Python)
🔍 Token type classifier (sinhala/english/number/mixed) ✅ Ready (PHP & Python)
🧬 Grapheme cluster splitter (ZWJ conjuncts) ✅ Ready (PHP & Python)
📊 Word frequency counter ✅ Ready (PHP & Python)
🔡 Script detector (sinhala/english/mixed/unknown) ✅ Ready (PHP & Python)
😊 Sentiment analyser ✅ Ready (Python)
💬 Singlish transliterator 🔲 Coming soon
🚫 Profanity filter 🔲 Coming soon

Quick Start

PHP Installation

Option 1 — Composer (Recommended)

composer require gosdrkht/sinhala-toolkit

Option 2 — Manual (shared hosting friendly)

Download and drop the php/src/ and data/ folders into your project:

require_once 'php/src/TextCleaner.php';
require_once 'php/src/Tokenizer.php';

use SinhalaToolkit\TextCleaner;
use SinhalaToolkit\Tokenizer;

Python Installation (sinkit)

pip install sinkit
from sinkit import TextCleaner, Tokenizer, SentimentAnalyser

cleaner = TextCleaner()
print(cleaner.clean("  මම   සහ  ඔබ  ගෙදර  ගියා  ", remove_stopwords=True))
# "ගෙදර ගියා"

tokenizer = Tokenizer()
print(tokenizer.tokenize("ශ්‍රී ලංකාව ලස්සන රටකි"))
# ['ශ්‍රී', 'ලංකාව', 'ලස්සන', 'රටකි']

sa = SentimentAnalyser()
print(sa.analyse("මෙය ඉතා හොඳ චිත්‍රපටයකි")["label"])
# "positive"

Usage Examples

Text Cleaner

use SinhalaToolkit\TextCleaner;

$cleaner = new TextCleaner('/path/to/data/stopwords.json');

// Full clean pipeline
$raw  = "  මම   සහ  ඔබ   හොඳ  ගුරුවරයා  ළඟ  ඉගෙන  ගත්තා  ";
$text = $cleaner->clean($raw, [
    'remove_stopwords' => true,
]);
// Output: "හොඳ ගුරුවරයා ඉගෙන ගත්තා"

// Remove English and numbers from mixed text
$mixed = "මෙය good idea එකක් වේ 2024 දී";
$clean = $cleaner->clean($mixed, [
    'remove_english' => true,
    'remove_numbers' => true,
]);
// Output: "මෙය එකක් වේ"

// Detect script
$cleaner->detectScript("ආයුබෝවන්");          // "sinhala"
$cleaner->detectScript("Hello World");        // "english"
$cleaner->detectScript("Hello ආයුබෝවන්");   // "mixed"

// Check stopwords
$cleaner->isStopword("මම");      // true
$cleaner->isStopword("ගුරු");    // false
$cleaner->isStopword("සඳහා");    // true

Tokenizer

use SinhalaToolkit\Tokenizer;

$tokenizer = new Tokenizer();

// --- Sentence tokenization ---

$text = "ඔහු ගෙදර ගියා। ඇය පාසලට ගියාය। ළමයි ක්‍රීඩා කළා।";
$sentences = $tokenizer->splitSentences($text);
// [
//   "ඔහු ගෙදර ගියා",
//   "ඇය පාසලට ගියාය",
//   "ළමයි ක්‍රීඩා කළා"
// ]

// Works with mixed-language text too
$mixed = "What is your name? ඔබේ නම කුමක්ද?";
$sentences = $tokenizer->splitSentences($mixed);
// ["What is your name", "ඔබේ නම කුමක්ද"]


// --- Word tokenization ---

$tokens = $tokenizer->tokenize("ශ්‍රී ලංකාව ලස්සන රටකි");
// ["ශ්‍රී", "ලංකාව", "ලස්සන", "රටකි"]


// --- Tokenize with type metadata ---

$typed = $tokenizer->tokenizeWithTypes("ලංකා cricket team 2024 දී ජය ගත්තා");
// [
//   ["token" => "ලංකා",   "type" => "sinhala"],
//   ["token" => "cricket", "type" => "english"],
//   ["token" => "team",    "type" => "english"],
//   ["token" => "2024",    "type" => "number"],
//   ["token" => "දී",     "type" => "sinhala"],
//   ["token" => "ජය",     "type" => "sinhala"],
//   ["token" => "ගත්තා",  "type" => "sinhala"],
// ]


// --- Filter by type ---

$text = "Ceylon Ledger මගින් ඔබේ business 2024 සිට manage කරන්න";

$tokenizer->tokenizeByType($text, 'sinhala');
// ["මගින්", "ඔබේ", "සිට", "කරන්න"]

$tokenizer->tokenizeByType($text, 'english');
// ["Ceylon", "Ledger", "business", "manage"]


// --- Word frequency ---

$text = "ශ්‍රී ලංකාව ලස්සන රටකි ශ්‍රී ලංකාව හොඳ රටකි ශ්‍රී ලංකාව";
$freq = $tokenizer->frequency($text);
// ["ශ්‍රී" => 3, "ලංකාව" => 3, "රටකි" => 2, ...]


// --- Grapheme clusters (ZWJ conjuncts) ---

// ශ්‍රී contains a ZWJ conjunct consonant — kept intact as one cluster
$clusters = $tokenizer->graphemes("ශ්‍රී");
// ["ශ්‍රී"]  ← treated as a single grapheme unit

Real-World Use Cases

Search & filtering — Strip stopwords before indexing Sinhala content so "ගෙදර ගෙදරට ගෙදරයි" all match "ගෙදර"

Content moderation — Detect script type to route Sinhala vs English vs mixed comments to the right classifier

SMS / WhatsApp bots — Clean noisy user input before passing to your logic

AI pipelines — Pre-process Sinhala text before sending to Claude API or any LLM

News aggregators — Sentence-split articles, count word frequency, extract keywords


Data Files

data/stopwords.json

120+ curated Sinhala stopwords organised into categories:

pronouns       → මම, ඔබ, ඔහු, ඇය, ඔවුන් ...
conjunctions   → හා, සහ, හෝ, නමුත් ...
prepositions   → ඉහත, පහත, සඳහා, ගැන ...
particles      → ද, නේ, යි, ම ...
aux_verbs      → ඇත, නැත, වේ, හැකි ...
question_words → මොකද, ඇයි, කවදා ...
adverbs        → ඉතා, බොහෝ, දැන් ...

The flat_list key gives you a ready-to-use array of all words:

$data      = json_decode(file_get_contents('data/stopwords.json'), true);
$stopwords = $data['flat_list'];

API Reference

TextCleaner

$cleaner = new TextCleaner(?string $stopwordsPath = null);
Method Parameters Returns Description
clean() string $text, array $options string Full pipeline
normaliseUnicode() string $text string NFC normalisation
removeInvisibleChars() string $text, bool $keepZwj string Strip ZWJ/ZWNJ/BOM
normaliseWhitespace() string $text string Collapse spaces
removePunctuation() string $text string Strip punctuation
removeEnglish() string $text string Strip ASCII letters
removeNumbers() string $text string Strip digits
removeStopwords() string $text string Filter stopwords
detectScript() string $text string sinhala/english/mixed/unknown
isPureSinhala() string $text bool Check purity
isStopword() string $word bool Single word check
getStopwords() — array Full stopword list

clean() options:

[
    'remove_stopwords'   => false,  // Remove stopwords
    'remove_punctuation' => false,  // Remove punctuation
    'remove_english'     => false,  // Remove English characters
    'remove_numbers'     => false,  // Remove digits
    'keep_zwj'           => true,   // Preserve ZWJ (Sinhala conjuncts)
]

Tokenizer

$tokenizer = new Tokenizer();
Method Parameters Returns Description
splitSentences() string $text, bool $keepBoundary array Split into sentences
tokenize() string $text, bool $lowercase array Word tokens
tokenizeWithTypes() string $text array Tokens with type metadata
tokenizeByType() string $text, string $type array Filter by type
graphemes() string $text array Grapheme clusters
classifyToken() string $token string sinhala/english/number/mixed
frequency() string $text, array $stopwords array Word frequency map
countTokens() string $text int Token count
countSentences() string $text int Sentence count

Token types: sinhala · english · number · mixed · punctuation · unknown


Project Structure

sinhala-toolkit/
├── php/src/
│   ├── TextCleaner.php       # PHP text cleaning & script detection
│   ├── Tokenizer.php         # PHP sentence & word tokenization
│   ├── SentimentAnalyser.php # PHP rule-based sentiment analyser
│   └── ProfanityFilter.php   # PHP profanity filter with severity
├── python/sinkit/
│   ├── cleaner.py            # Python text cleaning & script detection
│   ├── tokenizer.py          # Python sentence & word tokenization
│   ├── stopwords.py          # Python stopwords loader
│   ├── sentiment.py          # Python rule-based sentiment analyser
│   └── profanity.py          # Python profanity filter with severity
├── data/
│   ├── stopwords.json        # 140+ curated Sinhala stopwords
│   ├── common_words.json     # Common Sinhala words dataset
│   └── profanity.json        # Sinhala profanity dataset
├── tests/
│   ├── test_cleaner.php
│   ├── test_tokenizer.php
│   ├── test_sentiment.php
│   ├── test_profanity.php
│   └── test_python.py
├── composer.json
├── setup.py
├── pyproject.toml
├── CONTRIBUTING.md
├── LICENSE
└── README.md

Roadmap

  • composer require support (gosdrkht/sinhala-toolkit on Packagist)
  • Python package (pip install sinkit on PyPI)
  • Sinhala sentiment analyser (PHP & Python)
  • Profanity / bad word filter (PHP & Python)
  • Language / script detector (Sinhala vs Tamil vs English)
  • Singlish transliterator (ආයුබෝවන් ↔ aayubowan)
  • Keyword extractor (TF-IDF)
  • REST API (hosted)

Contributing

This project needs your help! Especially:

  • 🔤 More stopwords — open data/stopwords.json and add
  • 🐛 Bug reports — especially edge cases with Sinhala Unicode
  • 🧪 More test cases — real-world Sinhala text samples
  • 📖 Sinhala documentation translation

See CONTRIBUTING.md for guidelines.


සිංහල

sinhala-toolkit යනු PHP සහ Python developers ලා සඳහා නිර්මාණය කරන ලද, නිෂ්පාදන-සූදානම් සිංහල NLP toolkit එකකි.

ස්ථාපනය

# PHP (Composer)
composer require gosdrkht/sinhala-toolkit

# Python (PyPI)
pip install sinkit

සරල නිදර්ශනයක්

$cleaner   = new SinhalaToolkit\TextCleaner('data/stopwords.json');
$tokenizer = new SinhalaToolkit\Tokenizer();

// පෙළ පිරිසිදු කිරීම
$clean = $cleaner->clean("මම සහ ඔබ හොඳ ගුරුවරයා ළඟ ඉගෙන ගත්තා", [
    'remove_stopwords' => true
]);
// ප්‍රතිඵලය: "හොඳ ගුරුවරයා ඉගෙන ගත්තා"

// වාක්‍ය කැපීම
$sentences = $tokenizer->splitSentences("ඔහු ගෙදර ගියා। ඇය පාසලට ගියාය।");
// ["ඔහු ගෙදර ගියා", "ඇය පාසලට ගියාය"]

License

MIT © 2025 Trishan Sri Viduranga


Built for the Sri Lankan developer community ශ්‍රී ලාංකික developer ප්‍රජාව සඳහා නිර්මාණය කරන ලදී ⭐ If this helped you, please star the repo! 🙏

Metadata

Release files for sinkit 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sinkit 1.1.0
File Size Uploaded Provenance Actions
sinkit-1.1.0.tar.gz 29.3 kB With provenance Details for sinkit-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sinkit 1.1.0
File Interpreter ABI Platform Actions
sinkit-1.1.0-py3-none-any.whl Python 3 none any Details for sinkit-1.1.0-py3-none-any.whl

Total release size: 56.5 kB

Release files / sinkit-1.1.0.tar.gz

Download URL sinkit-1.1.0.tar.gz
Size 29.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ec5feb156b1d3b02871d34105a6f7fb269a8c2e6d27b35d8e216da6129545ac6
BLAKE2b-256 checksum
How to use checksums
0dc9476c5a79ca23fe5b5ade7e5820575d461c5e74df759cccb7c4a6d59b41cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Release files / sinkit-1.1.0-py3-none-any.whl

Download URL sinkit-1.1.0-py3-none-any.whl
Size 27.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d55f410c02e9246c96757572d1fafdba661bd4a0f1d651c8c0fbd40545563089
BLAKE2b-256 checksum
How to use checksums
e8333425cde73ad0aa0c11959ed9b2fc4aa63d0e36cbbe2792af63808c9e112b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Security for sinkit 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sinkit-1.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page