Skip to main content

A Python module for preprocessing text for NLP tasks

Project description

kleantext

A Python package for preprocessing textual data for machine learning and natural language processing tasks. It includes functionality for:

  • Converting text to lowercase (optional case-sensitive mode)
  • Removing HTML tags, punctuation, numbers, and special characters
  • Handling emojis (removal or conversion to textual descriptions)
  • Handling negations
  • Removing or retaining specific patterns (hashtags, mentions, etc.)
  • Removing stopwords (with customizable stopword lists)
  • Stemming and lemmatization
  • Correcting spelling (optional)
  • Expanding contractions and slangs
  • Named Entity Recognition (NER) masking (e.g., replacing entities with placeholders)
  • Detecting and translating text to a target language
  • Profanity filtering
  • Customizable text preprocessing pipeline

Installation

Option 1: Clone or Download

  1. Clone the repository using:
    git clone https://github.com/your-username/kleantext.git
    
  2. Navigate to the project directory:
    cd kleantext
    

Option 2: Install via pip (if published)

pip install kleantext

Usage

Quick Start

from kleantext.preprocessor import TextPreprocessor

# Initialize the preprocessor with custom settings
preprocessor = TextPreprocessor(
    remove_stopwords=True,
    perform_spellcheck=True,
    use_stemming=False,
    use_lemmatization=True,
    custom_stopwords={"example", "test"},
    case_sensitive=False,
    detect_language=True,
    target_language="en"
)

# Input text
text = "This is an example! Isn't it great? Visit https://example.com for more 😊."

# Preprocess the text
clean_text = preprocessor.clean_text(text)
print(clean_text)  # Output: "this is isnt it great visit for more"

Features and Configuration

1. Case Sensitivity

Control whether the text should be converted to lowercase:

preprocessor = TextPreprocessor(case_sensitive=True)

2. Removing HTML Tags

Automatically remove HTML tags like <div> or <p>.

3. Emoji Handling

Convert emojis to text or remove them entirely:

import emoji
text = emoji.demojize("😊 Hello!")  # Output: ":blush: Hello!"

4. Stopword Removal

Remove common stopwords, with support for custom lists:

custom_stopwords = {"is", "an", "the"}
preprocessor = TextPreprocessor(custom_stopwords=custom_stopwords)

5. Slang and Contraction Expansion

Expand contractions like "can't" to "cannot":

text = "I can't go"
expanded_text = preprocessor.clean_text(text)

6. Named Entity Recognition (NER) Masking

Mask entities like names, organizations, or dates using spacy:

text = "Barack Obama was the 44th President of the USA."
masked_text = preprocessor.clean_text(text)

7. Profanity Filtering

Censor offensive words:

text = "This is a badword!"
filtered_text = preprocessor.clean_text(text)

8. Language Detection and Translation

Detect the text's language and translate it:

preprocessor = TextPreprocessor(detect_language=True, target_language="en")
text = "Bonjour tout le monde"
translated_text = preprocessor.clean_text(text)  # Output: "Hello everyone"

9. Tokenization

Tokenize text for further NLP tasks:

from nltk.tokenize import word_tokenize
tokens = word_tokenize("This is an example.")
print(tokens)  # Output: ['This', 'is', 'an', 'example', '.']

Advanced Configuration

Create a custom pipeline by enabling or disabling specific cleaning steps:

pipeline = ["lowercase", "remove_html", "remove_urls", "remove_stopwords"]
preprocessor.clean_text(text, pipeline=pipeline)

Testing

Run unit tests using:

python -m unittest discover tests

License

This project is licensed under the MIT License.


Contributing

Feel free to fork the repository, create a feature branch, and submit a pull request. Contributions are welcome!


Snippets

Full Preprocessing Example

from kleantext.preprocessor import TextPreprocessor

# Initialize with default settings
preprocessor = TextPreprocessor(remove_stopwords=True, perform_spellcheck=False)

text = "Hello!!! This is, an example. Isn't it? 😊"
clean_text = preprocessor.clean_text(text)
print(clean_text)

Profanity Filtering

preprocessor = TextPreprocessor()
text = "This is a badword!"
clean_text = preprocessor.clean_text(text)
print(clean_text)  # Output: "This is a [CENSORED]!"

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

kleantext-0.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

kleantext-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file kleantext-0.1.tar.gz.

File metadata

  • Download URL: kleantext-0.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.1

File hashes

Hashes for kleantext-0.1.tar.gz
Algorithm Hash digest
SHA256 48bdf8e3edf5602871dfdfeccc87a7aaef74c4ad5f14eed802e43e3fcb393396
MD5 d78fc53bb23058ada370cdeb6f7526b9
BLAKE2b-256 12e89411d6fbb6f1e4c08b84ad6c38ef7f0b40d38482ff5dbb59ecf964371b12

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kleantext-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.1

File hashes

Hashes for kleantext-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d230d7b396f275a4a19cb1527595dc8acef216342cdf0955cba451831ac46af0
MD5 5545111fe6a8bae7478c7c6c110d2f4f
BLAKE2b-256 02d069b7aaae81d1b78e371f8ccba27720d1d7f65180a3aa514871d0d72aac72

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