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
- Clone the repository using:
git clone https://github.com/your-username/kleantext.git
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kleantext-0.1.1.tar.gz.
File metadata
- Download URL: kleantext-0.1.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59beadd6bd450c42b5a852a978c35cc633cc3280511014556e01b649233017e7
|
|
| MD5 |
30fd0847a8532f78532dc34e32055947
|
|
| BLAKE2b-256 |
5719999d64aec002c56034f412691952d2ebe060e5f1b10f33283ef760df4fcf
|
File details
Details for the file kleantext-0.1.1-py3-none-any.whl.
File metadata
- Download URL: kleantext-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c378fe5451a9a0787d30291f36963c28b83e6a621b82d0780e5e3dad50965cb8
|
|
| MD5 |
b6f9c537a8d395587a0d404851b716dd
|
|
| BLAKE2b-256 |
e1283df48c7396f8a2af2afa75a47626264c190b1e1409a3a26f456699b2ec79
|