clean-text
User-generated content on the Web and in social media is often dirty. Preprocess your scraped data with clean-text
to create a normalized text representation. For instance, turn this corrupted input:
A bunch of \\u2018new\\u2019 references, including [Moana](https://en.wikipedia.org/wiki/Moana_%282016_film%29).
»Yóù àré rïght <3!«
into this clean output:
A bunch of 'new' references, including [moana](<URL>).
"you are right <3!"
clean-text
uses ftfy, unidecode and numerous hand-crafted rules, i.e., RegEx.
Installation
To install the GPL-licensed package unidecode alongside:
pip install clean-text[gpl]
You may want to abstain from GPL:
pip install clean-text
NB: This package is named clean-text
and not cleantext
.
If unidecode is not available, clean-text
will resort to Python's unicodedata.normalize for transliteration.
Transliteration to closest ASCII symbols involes manually mappings, i.e., ê
to e
.
unidecode
's mapping is superiour but unicodedata's are sufficent.
However, you may want to disable this feature altogether depending on your data and use case.
To make it clear: There are inconsistencies between processing text with or without unidecode
.
Usage
from cleantext import clean
clean("some input",
fix_unicode=True,
to_ascii=True,
lower=True,
no_line_breaks=False,
no_urls=False,
no_emails=False,
no_phone_numbers=False,
no_numbers=False,
no_digits=False,
no_currency_symbols=False,
no_punct=False,
replace_with_punct="",
replace_with_url="<URL>",
replace_with_email="<EMAIL>",
replace_with_phone_number="<PHONE>",
replace_with_number="<NUMBER>",
replace_with_digit="0",
replace_with_currency_symbol="<CUR>",
lang="en"
)
Carefully choose the arguments that fit your task. The default parameters are listed above.
You may also only use specific functions for cleaning. For this, take a look at the source code.
Supported languages
So far, only English and German are fully supported.
It should work for the majority of western languages.
If you need some special handling for your language, feel free to contribute. 🙃
Using clean-text
with scikit-learn
There is also scikit-learn compatible API to use in your pipelines.
All of the parameters above work here as well.
pip install clean-text[gpl,sklearn]
pip install clean-text[sklearn]
from cleantext.sklearn import CleanTransformer
cleaner = CleanTransformer(no_punct=False, lower=False)
cleaner.transform(['Happily clean your text!', 'Another Input'])
Development
Use poetry.
Contributing
If you have a question, found a bug or want to propose a new feature, have a look at the issues page.
Pull requests are especially welcomed when they fix bugs or improve the code quality.
If you don't like the output of clean-text
, consider adding a test with your specific input and desired output.
Related Work
Generic text cleaning packages
Full-blown NLP libraries with some text cleaning
Remove or replace strings
Detect dates
Clean massive Common Crawl data
Acknowledgements
Built upon the work by Burton DeWilde for Textacy.
License
Apache