Skip to main content

Advanced Indonesian Natural Language Processing Library

Project description

nahiarhdNLP โ€” Indonesian NLP utilities

Lightweight utilities for Indonesian text preprocessing: cleaning, normalization, emoji conversion, spell correction, stemming, stopwords and a configurable Pipeline.

Install

pip install nahiarhdNLP

Quick usage

Import the pipeline and helpers:

from nahiarhdNLP.preprocessing import Pipeline, pipeline
from nahiarhdNLP.preprocessing import remove_html, remove_mentions, to_lowercase
  1. Pipeline with callables (function mode):
pipe = Pipeline(remove_html, remove_mentions, to_lowercase)
result = pipe.process("Hello <b>WORLD</b> @user https://example.com")
# -> HTML removed, mentions cleaned, lowered
  1. Pipeline with config dictionary (config mode):
config = {"clean_html": True, "clean_mentions": True, "remove_urls": True}
pipe = Pipeline(config)
result = pipe.process("Hello <b>World</b> @User https://EXAMPLE.com")
  1. Helper pipeline() for one-shot processing with a config:
result = pipeline("Gw lg browsing https://google.com", {"remove_urls": True, "to_lowercase": True})

Running tests (local)

Project contains unit tests under nahiarhdNLP/tests. To run them locally in the project virtualenv:

# from project root
# ensure virtualenv is active, or install pytest into your environment
python3 -m pip install -U "pytest" "pytest-cov"
python3 -m pytest -q

If you use the project .venv, run:

.venv/bin/python3 -m pip install -U pytest pytest-cov
.venv/bin/python3 -m pytest -q

Notes & troubleshooting

  • The package exposes two pipeline implementations:
    • nahiarhdNLP.preprocessing.main.Pipeline (config-only implementation) โ€” useful for config-driven workflows.
    • nahiarhdNLP.preprocessing.utils.Pipeline (function-or-config implementation) โ€” supports both passing callables and config dicts.
  • If you see import-time errors, ensure the package root is on PYTHONPATH or install the package in editable mode:
pip install -e .

If you want more examples (tokenization, stemmer, emoji conversion, or spell-correction examples), tell me which features you want documented and I'll add them to this README.

More examples

Stemmer

from nahiarhdNLP.preprocessing import Stemmer

try:
    stemmer = Stemmer()
    text = "bermain-main dengan senang"
    stemmed = stemmer.stem(text)
    print(stemmed)
    # Expected: "main main dengan senang"
except Exception as e:
    print("Stemmer error:", e)
    print("Install Sastrawi if missing: pip install Sastrawi")

EmojiConverter

from nahiarhdNLP.preprocessing import EmojiConverter

emoji = EmojiConverter()
emoji._load_data()
print(emoji.emoji_to_text_convert("๐Ÿ˜€๐Ÿ˜"))
# e.g. -> "wajah_gembira wajah_menyeringai"
print(emoji.text_to_emoji_convert("wajah_gembira"))
# e.g. -> "๐Ÿ˜€"

SpellCorrector (slang + spelling)

from nahiarhdNLP.preprocessing import SpellCorrector

spell = SpellCorrector()
print(spell.correct_word("sya"))
# e.g. -> "saya"
print(spell.correct_sentence("gw lg di rmh"))
# e.g. -> "gue lagi di rumah"

Linguistic processing

remove_stopwords, stem_text, tokenize

Word-preserving cleaning

enable_html_cleaning, enable_url_cleaning, enable_mention_cleaning, enable_hashtag_cleaning, enable_email_cleaning, enable_phone_cleaning, enable_currency_cleaning

Replacement helpers (new)

replace_email, replace_link, replace_user


### 8. ๐ŸŽ›๏ธ Preprocess Function (Backward Compatibility)

```python
from nahiarhdNLP.preprocessing import preprocess

# Preprocessing dengan parameter eksplisit
result = preprocess(
    "Halooo @user!!! 123 ๐Ÿ˜€",
    remove_emoji=True,
    remove_mentions=True,
    remove_numbers=True,
    remove_punctuation=True,
    replace_repeated_chars=True,
    to_lowercase=True,
    replace_spell_corrector=False,
)
print(result)
# Output: "haloo !! 123"

9. ๐Ÿ“Š Dataset Loader

from nahiarhdNLP.datasets import DatasetLoader

loader = DatasetLoader()

# Load stopwords dari CSV lokal
stopwords = loader.load_stopwords_dataset()
print(f"Jumlah stopwords: {len(stopwords)}")

# Load slang dictionary dari CSV lokal
slang_dict = loader.load_slang_dataset()
print(f"Jumlah slang: {len(slang_dict)}")

# Load emoji dictionary dari CSV lokal
emoji_dict = loader.load_emoji_dataset()
print(f"Jumlah emoji: {len(emoji_dict)}")

# Load wordlist dari JSON lokal
wordlist = loader.load_wordlist_dataset()
print(f"Jumlah kata: {len(wordlist)}")

Catatan: Semua dataset (stopword, slang, emoji, wordlist) di-load langsung dari file CSV/JSON di folder nahiarhdNLP/datasets/. Tidak ada proses download dari external source.

๐Ÿ”ฅ Demo Script

Untuk melihat semua fitur library bekerja:

python -m nahiarhdNLP.demo

Demo ini menunjukkan:

  • โœ… Semua fungsi individual utility
  • โœ… Penggunaan class-based approach
  • โœ… Pipeline system (functions & config)
  • โœ… Advanced pipeline features
  • โœ… Handling error dan troubleshooting

๐Ÿšจ Error Handling

try:
    from nahiarhdNLP.preprocessing import SpellCorrector
    spell = SpellCorrector()
    result = spell.correct_sentence("test")
except ImportError:
    print("Package nahiarhdNLP belum terinstall")
    print("Install dengan: pip install nahiarhdNLP")
except Exception as e:
    print(f"Error: {e}")

๐Ÿ’ก Tips Penggunaan

  1. Untuk preprocessing simple: Gunakan Pipeline(function1, function2, ...) - langsung pass functions!
  2. Untuk kontrol detail: Gunakan Pipeline(config_dict) atau preprocess() dengan parameter boolean
  3. Untuk kontrol penuh: Gunakan kelas individual (TextCleaner, SpellCorrector, dll)
  4. Untuk spell correction + slang: Gunakan SpellCorrector yang menggabungkan kedua fitur
  5. Untuk menghapus emoji: Gunakan remove_emoji() atau set remove_emoji=True di Pipeline/preprocess
  6. Untuk stemming: Install Sastrawi terlebih dahulu: pip install Sastrawi
  7. Untuk load dataset: Gunakan DatasetLoader dari nahiarhdNLP.datasets
  8. Untuk inisialisasi kelas: Panggil _load_data() untuk kelas yang memerlukan dataset
  9. Pipeline design: Pipeline(remove_url, to_lowercase) lebih jelas daripada config dictionary
  10. Function chaining: Pipeline bisa dipanggil seperti function dengan pipeline("text")
  11. Demo testing: Jalankan python -m nahiarhdNLP.demo untuk melihat semua fitur bekerja

โšก Performance & Dataset

nahiarhdNLP menggunakan dataset lokal yang sudah disediakan:

  • Stopwords: File stop_word.csv (788 kata)
  • Slang Dictionary: File slang.csv (15,675 pasangan)
  • Emoji Mapping: File emoji.csv (3,530 emoji)
  • Wordlist: File wordlist.json (kamus kata Indonesia)
  • KBBI Dictionary: File kata_dasar_kbbi.csv (28,527 kata)
  • Kamus Tambahan: File kamus.txt (30,871 kata)

Semua dataset tersimpan di folder nahiarhdNLP/datasets/ dan diakses melalui DatasetLoader.

๐Ÿ“ฆ Dependencies

Package ini membutuhkan:

  • pandas - untuk load dan proses dataset CSV/JSON
  • Sastrawi - untuk stemming (opsional)
  • rich - untuk output formatting di demo (opsional)

๐Ÿ”ง Struktur Modul

nahiarhdNLP/
โ”œโ”€โ”€ datasets/
โ”‚   โ”œโ”€โ”€ loaders.py          # DatasetLoader class
โ”‚   โ”œโ”€โ”€ emoji.csv           # Dataset emoji (3,530 entries)
โ”‚   โ”œโ”€โ”€ slang.csv           # Dataset slang (15,675 entries)
โ”‚   โ”œโ”€โ”€ stop_word.csv       # Dataset stopwords (788 entries)
โ”‚   โ”œโ”€โ”€ wordlist.json       # Dataset wordlist
โ”‚   โ”œโ”€โ”€ kata_dasar_kbbi.csv # Dataset KBBI (28,527 entries)
โ”‚   โ””โ”€โ”€ kamus.txt           # Dataset kamus tambahan (30,871 entries)
โ”œโ”€โ”€ preprocessing/
โ”‚   โ”œโ”€โ”€ cleaning/
โ”‚   โ”‚   โ”œโ”€โ”€ text_cleaner.py     # TextCleaner class (complete removal)
โ”‚   โ”‚   โ””โ”€โ”€ text_cleaner_word.py # Word-preserving TextCleaner class
โ”‚   โ”œโ”€โ”€ linguistic/
โ”‚   โ”‚   โ”œโ”€โ”€ stemmer.py      # Stemmer class
โ”‚   โ”‚   โ””โ”€โ”€ stopwords.py    # StopwordRemover class
โ”‚   โ”œโ”€โ”€ normalization/
โ”‚   โ”‚   โ”œโ”€โ”€ emoji.py        # EmojiConverter class
โ”‚   โ”‚   โ””โ”€โ”€ spell_corrector.py # SpellCorrector class
โ”‚   โ”œโ”€โ”€ tokenization/
โ”‚   โ”‚   โ””โ”€โ”€ tokenizer.py    # Tokenizer class
โ”‚   โ””โ”€โ”€ utils.py            # Fungsi utility individual & Pipeline
โ””โ”€โ”€ demo.py                 # File demo penggunaan

๐Ÿ†• Changelog Versi 1.4.0

  • ๐Ÿš€ [FITUR BARU] Menambahkan remove_emoji() function untuk menghapus emoji dari teks
  • โœ… [BARU] TextCleaner sekarang memiliki method clean_emoji() untuk menghapus emoji
  • โœ… [BARU] Pipeline mendukung "remove_emoji" config untuk emoji removal
  • โœ… [BARU] Preprocess function mendukung parameter remove_emoji=True/False
  • โœ… [PERBAIKAN] Demo script diperbarui dengan contoh emoji removal
  • โœ… [PERBAIKAN] Dokumentasi lengkap untuk fitur emoji removal
  • ๐Ÿš€ [MAJOR] Pipeline sekarang mendukung 2 mode: Functions dan Config Dictionary
  • โœ… [BARU] Pipeline dengan functions: Pipeline(remove_url, to_lowercase)
  • โœ… [BARU] Pipeline dengan config: Pipeline({"remove_url": True, "to_lowercase": True})
  • โœ… [BARU] Advanced pipeline features: get_config(), get_enabled_steps(), update_config()
  • โœ… [PERBAIKAN] Fungsi pipeline(text, config) sekarang bekerja dengan config dictionary
  • โœ… [PERBAIKAN] TextCleaner sekarang punya method clean_html() yang benar
  • โœ… [PERBAIKAN] SpellCorrector demo diperbaiki dengan proper instantiation
  • โœ… [PERBAIKAN] Demo script berjalan sempurna tanpa error
  • โœ… [PERBAIKAN] Dokumentasi yang akurat dan sesuai implementasi
  • โœ… [PERBAIKAN] Function names yang konsisten: replace_spell_corrector, replace_repeated_chars
  • โœ… [PERBAIKAN] Backward compatibility dengan preprocess() function
  • โœ… Menggabungkan spell correction dan slang normalization dalam SpellCorrector
  • โœ… Semua dataset menggunakan file lokal (CSV/JSON)
  • โœ… Struktur yang lebih terorganisir dengan pemisahan kelas dan fungsi
  • โœ… Penambahan DatasetLoader untuk manajemen dataset terpusat
  • โœ… Dataset lengkap dengan 6 file berbeda (emoji, slang, stopwords, wordlist, KBBI, kamus)

๐Ÿ†• Changelog Versi 1.4.11 (Latest)

  • ๐Ÿš€ [FITUR BARU] Enable Functions untuk pembersihan dengan mempertahankan konten
  • โœ… [BARU] enable_html_cleaning() - Membersihkan HTML tags
  • โœ… [BARU] enable_url_cleaning() - Membersihkan URL
  • โœ… [BARU] enable_mention_cleaning() - Membersihkan mentions (@user)
  • โœ… [BARU] enable_hashtag_cleaning() - Membersihkan hashtags (#tag)
  • โœ… [BARU] enable_email_cleaning() - Membersihkan email
  • โœ… [BARU] enable_phone_cleaning() - Membersihkan nomor telepon
  • โœ… [BARU] enable_currency_cleaning() - Membersihkan mata uang
  • โœ… [BARU] Demo script diperbarui dengan contoh lengkap untuk semua fitur baru
  • โœ… [BARU] Dokumentasi README lengkap dengan contoh penggunaan fitur baru
  • โœ… [PERBAIKAN] Integrasi penuh dengan sistem Pipeline dan preprocess functions
  • โœ… [PERBAIKAN] Backward compatibility dengan semua fitur existing
  • โœ… [PERBAIKAN] Package structure yang konsisten dan terorganisir

๐Ÿ› Troubleshooting

Error saat import dataset:

# Pastikan memanggil _load_data() untuk kelas yang memerlukan dataset
stopword = StopwordRemover()
stopword._load_data()  # Penting!

Error Sastrawi tidak ditemukan:

pip install Sastrawi

Error pandas tidak ditemukan:

pip install pandas

Testing semua fitur:

python -m nahiarhdNLP.demo

๐Ÿ“„ License

MIT License

๐Ÿ‘จโ€๐Ÿ’ป Author

Raihan Hidayatullah Djunaedi raihanhd.dev@gmail.com


Untuk contoh penggunaan lengkap, lihat file demo.py di repository ini atau jalankan python -m nahiarhdNLP.demo.

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

nahiarhdnlp-1.4.11.tar.gz (782.2 kB view details)

Uploaded Source

Built Distribution

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

nahiarhdnlp-1.4.11-py3-none-any.whl (786.5 kB view details)

Uploaded Python 3

File details

Details for the file nahiarhdnlp-1.4.11.tar.gz.

File metadata

  • Download URL: nahiarhdnlp-1.4.11.tar.gz
  • Upload date:
  • Size: 782.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for nahiarhdnlp-1.4.11.tar.gz
Algorithm Hash digest
SHA256 ee4433472d7a1d5fceb15d48b335b9b121341095c7920f1a178fd1bff0c0fc7d
MD5 e78923df5398f82b1e07f88e0fa4918b
BLAKE2b-256 5d8789d24a366db0476740c4125c4b8af02573757cbedeaeeae5bcaa69573f36

See more details on using hashes here.

File details

Details for the file nahiarhdnlp-1.4.11-py3-none-any.whl.

File metadata

  • Download URL: nahiarhdnlp-1.4.11-py3-none-any.whl
  • Upload date:
  • Size: 786.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for nahiarhdnlp-1.4.11-py3-none-any.whl
Algorithm Hash digest
SHA256 9f48ab4555211ffb08ef7eb90a368affe6ccd6d0130de8761ea3bdad0785667d
MD5 a88c5c3d69fa6d7fbdd31698d421c2c3
BLAKE2b-256 1968658be7b35b8e6222251d464076220e0c1aac07c2917dcba40a6193dd2831

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