Skip to main content

All in one text processor and cleaner.

Project description

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.
  • Very smartly 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:

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_options = {
    # If true, contractions will be expanded (it's -> it is)
    # This takes a long time. Especially the first time you run it
    "expand_contractions": False,

    # if true removes text in brackets
    # if false the brackets will be removed, but text inside will remain
    "strip_text_in_brackets": False,

    # if false replaces hyphen with space (george-louis -> george louis).
    # if true just replaces hyphen (george-louis -> georgelouis)
    "combine_concatenations": False,  
}
cleaner = Cleaner(cleaner_options)

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 is not found
python3
>> import nltk
>> nltk.download('wordnet')

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.

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

aiotext-1.0.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

aiotext-1.0.2-py2.py3-none-any.whl (9.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file aiotext-1.0.2.tar.gz.

File metadata

  • Download URL: aiotext-1.0.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2

File hashes

Hashes for aiotext-1.0.2.tar.gz
Algorithm Hash digest
SHA256 5c51b535d8904c14709af250680b024fa62a1e696249cc1f8005d4e8d7effda3
MD5 ec9fc4dc6ef7762f74d1ce1b4f0aa62c
BLAKE2b-256 0157b974b3fd24ef7432949b51a0761ab8fff69497993d6b75d7aa69c1cecd38

See more details on using hashes here.

File details

Details for the file aiotext-1.0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: aiotext-1.0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2

File hashes

Hashes for aiotext-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 02bff566a8452fd380279325f66da9ec98260ee5e19d0d82b48cf639d5d6dd11
MD5 f2195ae3371b765a5ffba0da2253263b
BLAKE2b-256 504155d8f1c3560d59173ec3f7955b618be78a441cc626e84b6551036e66a195

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