Tutorial: Quickstart
TextBlob aims to provide access to common text-processing operations through a familiar interface. You can treat
TextBlob
objects as if they were Python strings that learned how to do Natural Language Processing.Sentiment Analysis
The
sentiment
property returns a namedtuple of the form Sentiment(polarity,subjectivity)
. The polarity score is a float within the range [-1.0, 1.0]. The subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.Tokenization
You can break TextBlobs into words or sentences.
Sentence
objects have the same properties and methods as TextBlobs.
For more advanced tokenization, see the Advanced Usage guide.
Words Inflection and Lemmatization
Each word in
TextBlob.words
or Sentence.words
is a Word
object (a subclass of unicode
) with useful methods, e.g. for word inflection.
Words can be lemmatized by calling the
lemmatize
method.WordNet Integration
You can access the synsets for a
Word
via the synsets
property or the get_synsets
method, optionally passing in a part of speech.
You can access the definitions for each synset via the
definitions
property or the define()
method, which can also take an optional part-of-speech argument.
You can also create synsets directly.
For more information on the WordNet API, see the NLTK documentation on the Wordnet Interface.
WordLists
A
WordList
is just a Python list with additional methods.Spelling Correction
Use the
correct()
method to attempt spelling correction.Word
objects have a spellcheck() Word.spellcheck()
method that returns a list of (word, confidence)
tuples with spelling suggestions.Get Word and Noun Phrase Frequencies
There are two ways to get the frequency of a word or noun phrase in a
TextBlob
.
The first is through the
word_counts
dictionary.
If you access the frequencies this way, the search will not be case sensitive, and words that are not found will have a frequency of 0.
The second way is to use the
count()
method.
You can specify whether or not the search should be case-sensitive (default is
False
).
Each of these methods can also be used with noun phrases.
Translation and Language Detection
New in version
0.5.0
.
TextBlobs can be translated between languages.
If no source language is specified, TextBlob will attempt to detect the language. You can specify the source language explicitly, like so. Raises
TranslatorError
if the TextBlob cannot be translated into the requested language or NotTranslated
if the translated result is the same as the input string.
You can also attempt to detect a TextBlob’s language using
TextBlob.detect_language()
.
As a reference, language codes can be found here.
Language translation and detection is powered by the Google Translate API.
TextBlobs Are Like Python Strings!
You can use Python’s substring syntax.
You can use common string methods.
You can make comparisons between TextBlobs and strings.
You can concatenate and interpolate TextBlobs and strings.
Get Start and End Indices of Sentences
Use
sentence.start
and sentence.end
to get the indices where a sentence starts and ends within a TextBlob
.