Skip to main content

Functions to preprocess and normalize text.

Project description

clean-text Build Status PyPI PyPI - Python Version PyPI - Downloads

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

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.

NB: The package is named clean-text and not cleantext.

Usage

from cleantext import clean

clean("some input",
    fix_unicode=True,               # fix various unicode errors
    to_ascii=True,                  # transliterate to closest ASCII representation
    lower=True,                     # lowercase text
    no_line_breaks=False,           # fully strip line breaks as opposed to only normalizing them
    no_urls=False,                  # replace all URLs with a special token
    no_emails=False,                # replace all email addresses with a special token
    no_phone_numbers=False,         # replace all phone numbers with a special token
    no_numbers=False,               # replace all numbers with a special token
    no_digits=False,                # replace all digits with a special token
    no_currency_symbols=False,      # replace all currency symbols with a special token
    no_punct=False,                 # fully remove punctuation
    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"                       # set to 'de' for German special handling
)

Carefully choose the arguments that fit your task. The default parameters are listed above. Whitespace is always normalized.

You may also only use specific functions for cleaning. For this, take a look at the source code.

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. 🙃

Development

Install and 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

Acknowledgements

Built upon the work by Burton DeWilde for Textacy.

License

Apache

Sponsoring

This work was created as part of a project that was funded by the German Federal Ministry of Education and Research.

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

clean-text-0.2.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

clean_text-0.2.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file clean-text-0.2.1.tar.gz.

File metadata

  • Download URL: clean-text-0.2.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.8.4 Darwin/19.6.0

File hashes

Hashes for clean-text-0.2.1.tar.gz
Algorithm Hash digest
SHA256 15239c0a117dd574a554fd9f105ab2ed302dd2f4da12a4d39df8c24a3ebfce8d
MD5 d4d00b397e4f214e1fdf5d23d2cbca6e
BLAKE2b-256 fe03ab982633102468662e57e63bd41f4551d0ac80a69e28316e9c8c47ae7bd9

See more details on using hashes here.

File details

Details for the file clean_text-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: clean_text-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.8.4 Darwin/19.6.0

File hashes

Hashes for clean_text-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b219e9b0c8e2159f7f7fbaba56164bbc2ffc0e8aea1d73cac34bc0b1fb482081
MD5 314e79139b5618d17f5625012844fe55
BLAKE2b-256 e3a4cb7b851f1f7ae68a128482cd57ff0c4c96b64083f41ca5e9608e2a2889a5

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