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]!"

Usage Examples

1. Converting Text to Lowercase (Optional Case-Sensitive Mode)

preprocessor = TextPreprocessor(case_sensitive=True)
text = "Hello WORLD!"
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "Hello WORLD!"

preprocessor = TextPreprocessor(case_sensitive=False)
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "hello world"

2. Removing HTML Tags, Punctuation, Numbers, and Special Characters

text = "This is a <b>bold</b> statement! Price: $100."
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "this is a bold statement price"

3. Handling Emojis (Removal or Conversion to Textual Descriptions)

text = "I love Python! 😍🔥"
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "i love python :heart_eyes: :fire:"

4. Handling Negations

text = "I don't like this movie."
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "i do not like this movie"

5. Removing or Retaining Specific Patterns (Hashtags, Mentions, etc.)

text = "Follow @user and check #MachineLearning!"
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "follow user and check machinelearning"

6. Removing Stopwords (With Customizable Stopword Lists)

preprocessor = TextPreprocessor(custom_stopwords={"example", "test"})
text = "This is an example test showing stopword removal."
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "this is an showing stopword removal"

7. Stemming and Lemmatization

With Stemming:

preprocessor = TextPreprocessor(use_stemming=True, use_lemmatization=False)
text = "running flies better than walking"
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "run fli better than walk"

With Lemmatization:

preprocessor = TextPreprocessor(use_stemming=False, use_lemmatization=True)
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "running fly better than walking"

8. Correcting Spelling (Optional)

preprocessor = TextPreprocessor(perform_spellcheck=True)
text = "Ths is a tst sentnce."
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "This is a test sentence"

9. Expanding Contractions and Slang Handling

from contractions import fix

text = "I'm gonna go, but I can't wait!"
text = fix(text)  
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "i am going to go but i cannot wait"

10. Named Entity Recognition (NER) Masking

text = "John Doe lives in New York."
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "[PERSON] lives in [LOCATION]"

11. Detecting and Translating Text to a Target Language

preprocessor = TextPreprocessor(detect_language=True, target_language="en")
text = "Bonjour! Comment ça va?"
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "Hello! How are you?"

12. Profanity Filtering

text = "This is a f***ing great product!"
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "this is a **** great product"

13. Customizable Text Preprocessing Pipeline

preprocessor = TextPreprocessor(
    remove_stopwords=True,
    perform_spellcheck=True,
    use_stemming=False,
    use_lemmatization=True,
    case_sensitive=False,
    detect_language=True,
    target_language="en"
)

text = "Ths is an amazng movi!! 😍🔥 <b>100%</b> recommended!"
cleaned_text = preprocessor.clean_text(text)
print(cleaned_text)  # Output: "this is an amazing movie heart_eyes fire recommended"

Conclusion

KleanText is a robust and flexible text preprocessing library designed to clean and normalize text efficiently. You can customize the pipeline to fit your specific NLP needs. 🚀

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.4.tar.gz (5.7 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.4-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kleantext-0.1.4.tar.gz
Algorithm Hash digest
SHA256 0e0a8a550ae23f483052bed27cc54f419e0aa39fa5ce4f6b22d7fd6e6c043815
MD5 af86600e355b695dc731ca1193bf5e51
BLAKE2b-256 33c684a498ee8864dd62adb7d87e488fb935b66e7b83fb437c937dd07a0e363a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for kleantext-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 63869b23915494f7883b8e88315bd1b175bb365973d5d2a4b849b77d5658f0d3
MD5 2a21d6b7a17c4d31e731c06511ce5f28
BLAKE2b-256 a48c85dbb1e2bb007e68c639e942fde6458b6c322c2e175aef56578cfe3ffce5

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