Skip to main content

All-in-one Text Cleaner

This package was created to speed up the process of cleaning text for natural language processing and machine learning. The package does the following:

  • Converts all text to lowercase
  • Expands contractions using pycontractions trained on the glove-twitter-100 word2vec training set (optional)
  • Removes text in brackets. Matches "()","[]", or "{}" (optional)
  • Combines concatenations (turns "georgetown-louisville" into "georgetown louisville" or "georgetownlousivelle"). Matches all types of hyphens.
  • Splits sentences on punctuation using algorithm defined in this stackoverflow post.
  • Tokenizes sentences.
  • Lemmatizes tokens using NLTK WordNetLemmatizer and a lookup table between Penn Bank tags and Word Net.

Installation

$ pip3 install aiotext
$ pip3 install git+https://github.com/EricWiener/pycontractions

Please note that pycontractions is specified as a dependency and will download from PyPi and work, but the branch I linked to above has multiple improvements.

Usage:

Options:

Option Default Description
expand_contractions True If true, contractions will be expanded (it's -> it is). This takes a long time. Especially the first time you run it.
strip_text_in_brackets False If true removes text in brackets. If false the brackets will be removed, but text inside will remain.
combine_concatenations False If false replaces hyphen with space (george-louis -> george louis). If true just removes hyphen (george-louis -> georgelouis).
w2v_path None Path to word2vec binary.
api_key "word2vec-google-news-300" w2v_path will be given preference over api_key. If no valid binary is found at the path, the api will download the key specified. If no key is specified, the Google News vector will be used.
from aiotext import Cleaner

text = "Call me Ishmael. Some years ago—never mind how long precisely—having "
text += "little or no money in my purse, and nothing particular to interest me "
text += "on shore, I thought I would sail about a little and see the watery part "
text += "of the world. It is a way I have of driving off the spleen and "
text += "regulating the circulation."

# Initialize cleaner
cleaner = Cleaner(expand_contractions=True)

assert cleaner.clean(text) == [
['call', 'me', 'ishmael'],
['some', 'year', 'ago', 'never', 'mind', 'how', 'long', 'precisely', 'have', 'little', 'or', 'no', 'money', 'in', 'my', 'purse', 'and', 'nothing', 'particular',
    'to', 'interest', 'me', 'on', 'shore', 'i', 'think', 'i', 'would', 'sail', 'about', 'a', 'little', 'and', 'see', 'the', 'watery', 'part', 'of', 'the', 'world'],
['it', 'be', 'a', 'way', 'i', 'have', 'of', 'drive', 'off',
    'the', 'spleen', 'and', 'regulate', 'the', 'circulation'],
]

Notes

  • Please note you might have to manually quit and reattempt to run the program the first time you run it if it gets stuck after downloading the contractions dataset.
  • Wordnet is used to lemmatize based on the parts of speech given by Penn Bank. Since Wordnet is limited in the number of options (eg. no pronouns), some words will not be processed. This is done to preserve the root word. For instance, "us" Wordnet will convert "us" to "u". In order to avoid this, "us" will not be passed into the lemmatizer.
  • You may need to run the following if wordnet or punkt is not found
python3
>> import nltk
>> nltk.download('wordnet')
>> nltk.download('punkt')

Change log

  • 1.0.0: Initial release
  • 1.0.1: Corrected handling of sentences without punctuation and brackets
  • 1.0.2: Added modified contraction expander download. Also made changes to solve issue with NLTK lemmatizer.
  • 1.0.3: Added options for specifying word2vec model to use for contraction expansion
  • 1.0.4: Minor syntax error
  • 1.0.5: Changed passing of arguments, updated README, improved tokenization, and changed order of parsing to tag POS before cleaning text.

Metadata

Release files for aiotext 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiotext 1.0.6
File Size Uploaded Provenance Actions
aiotext-1.0.6.tar.gz 7.7 kB Without provenance Details for aiotext-1.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aiotext 1.0.6
File Interpreter ABI Platform Actions
aiotext-1.0.6-py2.py3-none-any.whl Python 2, Python 3 none any Details for aiotext-1.0.6-py2.py3-none-any.whl

Total release size: 17.1 kB

Release files / aiotext-1.0.6.tar.gz

Download URL aiotext-1.0.6.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c650d54f3261ea8d522b169768ace8ae47c5679f0cd073c25dc03c51e5cc51fd
BLAKE2b-256 checksum
How to use checksums
863917be2687b8604c5aecbd137f39716880a9c7639160f4364d45a12233e7df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

Release files / aiotext-1.0.6-py2.py3-none-any.whl

Download URL aiotext-1.0.6-py2.py3-none-any.whl
Size 9.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0126d382913c419e166a5d184c5d4b8465f533c5090c996bf91ed4e776667146
BLAKE2b-256 checksum
How to use checksums
e8e4d463a3cc81e5687a34e2b80e15daff52fbee53293c1b2c5cf525e937cbcc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3

Security for aiotext 1.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page