Skip to main content

Simple, Pythonic text processing. Sentiment analysis, POS tagging, noun phrase parsing, and more.

Project description

Requirements

  • Python >= 2.7, but not Python 3 (yet)

Installation

Just run:

$ pip install textblob && python download_corpora.py

This installs textblob and downloads the necessary NLTK models.

Best to see that everything is working by running:

$ nosetests

Usage

Simple.

Create a TextBlob

from text.blob import TextBlob

zen = """Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""

blob = TextBlob(zen) # Create a new TextBlob

Part-of-speech and noun phrase tagging

blob.pos_tags         # [('beautiful', 'JJ'), ('is', 'VBZ'), ('better', 'RBR'),
                      # ('than', 'IN'), ('ugly', 'RB'), ...]

blob.noun_phrases     # ['beautiful', 'explicit', 'simple', 'complex', 'flat',
                      # 'sparse', 'readability', 'special cases',
                      # 'practicality beats purity', 'errors', 'unless',
                      # 'obvious way','dutch', 'right now', 'bad idea',
                      # 'good idea', 'namespaces', 'great idea']

Sentiment analysis

The sentiment property returns a tuple of the form (polarity, subjectivity) where polarity ranges from -1.0 to 1.0 and subjectivity ranges from 0.0 to 1.0.

blob.sentiment        # (0.20, 0.58)

Get word and noun phrase frequencies

blob.word_counts['special']   # 2 (not case-sensitive by default)
blob.words.count('special')   # Same thing
blob.words.count('special', case_sensitive=True)  # 1

blob.noun_phrases.count('great idea')  # 1

TextBlobs are like Python strings!

blob[0:19]            # TextBlob("Beautiful is better")
blob.upper()          # TextBlob("BEAUTIFUL IS BETTER THAN UGLY...")
blob.find("purity")   # 293

blob1 = TextBlob('apples')
blob2 = TextBlob('bananas')
blob1 < blob2            # True
blob1 + ' and ' + blob2  # TextBlob('apples and bananas')

Get start and end indices of sentences

This can be useful for sentence highlighting, for example.

for sentence in blob.sentences:
    print(sentence)  # Beautiful is better than ugly
    print("---- Starts at index {}, Ends at index {}"\
                .format(sentence.start_index, sentence.end_index))  # 0, 30

Get a serialized version of the blob (a list of dicts)

blob.serialized       # [{'end_index': 30,
                     # 'noun_phrases': ['beautiful'],
                     # 'raw_sentence': 'Beautiful is better than ugly.',
                     # 'start_index': 0,
                     # 'stripped_sentence': 'beautiful is better than ugly'},

Testing

Run $ nosetests to run all tests.

Project details


Release history Release notifications

History Node

0.15.1

History Node

0.15.0

History Node

0.14.0

History Node

0.13.1

History Node

0.13.0

History Node

0.12.0

History Node

0.11.1

History Node

0.11.0

History Node

0.10.0

History Node

0.9.1

History Node

0.9.0

History Node

0.8.4

History Node

0.8.3

History Node

0.8.2

History Node

0.8.1

History Node

0.8.0

History Node

0.7.1

History Node

0.7.0

History Node

0.6.3

History Node

0.6.2

History Node

0.6.1

History Node

0.6.0

History Node

0.5.3

History Node

0.5.2

History Node

0.5.1

History Node

0.5.0

History Node

0.4.0

History Node

0.3.10

History Node

0.3.9

History Node

0.3.8

History Node

0.3.7

History Node

0.3.6

History Node

0.3.5

History Node

0.3.4

History Node

0.3.3

History Node

0.3.2

History Node

0.3.1

History Node

0.3.0

History Node

0.2.6

History Node

0.2.5

History Node

0.2.4

History Node

0.2.3

History Node

0.2.1

History Node

0.2.0

History Node

0.1.36

History Node

0.1.35

History Node

0.1.34

History Node

0.1.33

This version
History Node

0.1.32

History Node

0.1.31

History Node

0.1.3

History Node

0.1.2

History Node

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
textblob-0.1.32.tar.gz (30.6 kB) Copy SHA256 hash SHA256 Source None Jul 1, 2013

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page