## File: docs/advanced_usage.rst
.. _advanced:
Advanced Usage: Overriding Models and the Blobber Class
=======================================================
TextBlob allows you to specify which algorithms you want to use under the hood of its simple API.
Sentiment Analyzers
-------------------
New in version `0.5.0`.
The ``textblob.sentiments`` module contains two sentiment analysis implementations, ``PatternAnalyzer`` (based on the pattern_ library) and ``NaiveBayesAnalyzer`` (an NLTK_ classifier trained on a movie reviews corpus).
The default implementation is ``PatternAnalyzer``, but you can override the analyzer by passing another implementation into a TextBlob's constructor.
For instance, the ``NaiveBayesAnalyzer`` returns its result as a namedtuple of the form: ``Sentiment(classification, p_pos, p_neg)``.
::
>>> from textblob import TextBlob
>>> from textblob.sentiments import NaiveBayesAnalyzer
>>> blob = TextBlob("I love this library", analyzer=NaiveBayesAnalyzer())
>>> blob.sentiment
Sentiment(classification='pos', p_pos=0.7996209910191279, p_neg=0.2003790089808724)
Tokenizers
----------
New in version `0.4.0`.
The ``words`` and ``sentences`` properties are helpers that use the ``textblob.tokenizers.WordTokenizer`` and ``textblob.tokenizers.SentenceTokenizer`` classes, respectively.
You can use other tokenizers, such as those provided by NLTK, by passing them into the ``TextBlob`` constructor then accessing the ``tokens`` property.
.. doctest::
>>> from textblob import TextBlob
>>> from nltk.tokenize import TabTokenizer
>>> tokenizer = TabTokenizer()
>>> blob = TextBlob("This is\ta rather tabby\tblob.", tokenizer=tokenizer)
>>> blob.tokens
WordList(['This is', 'a rather tabby', 'blob.'])
You can also use the ``tokenize([tokenizer])`` method.
.. doctest::
>>> from textblob import TextBlob
>>> from nltk.tokenize import BlanklineTokenizer
>>> tokenizer = BlanklineTokenizer()
>>> blob = TextBlob("A token\n\nof appreciation")
>>> blob.tokenize(tokenizer)
WordList(['A token', 'of appreciation'])
Noun Phrase Chunkers
--------------------
TextBlob currently has two noun phrases chunker implementations,
``textblob.np_extractors.FastNPExtractor`` (default, based on Shlomi Babluki's implementation from
`this blog post `_)
and ``textblob.np_extractors.ConllExtractor``, which uses the CoNLL 2000 corpus to train a tagger.
You can change the chunker implementation (or even use your own) by explicitly passing an instance of a noun phrase extractor to a TextBlob's constructor.
.. doctest::
>>> from textblob import TextBlob
>>> from textblob.np_extractors import ConllExtractor
>>> extractor = ConllExtractor()
>>> blob = TextBlob("Python is a high-level programming language.", np_extractor=extractor)
>>> blob.noun_phrases
WordList(['python', 'high-level programming language'])
POS Taggers
-----------
TextBlob currently has two POS tagger implementations, located in ``textblob.taggers``. The default is the ``PatternTagger`` which uses the same implementation as the pattern_ library.
The second implementation is ``NLTKTagger`` which uses NLTK_'s TreeBank tagger. *Numpy is required to use the NLTKTagger*.
Similar to the tokenizers and noun phrase chunkers, you can explicitly specify which POS tagger to use by passing a tagger instance to the constructor.
::
>>> from textblob import TextBlob
>>> from textblob.taggers import NLTKTagger
>>> nltk_tagger = NLTKTagger()
>>> blob = TextBlob("Tag! You're It!", pos_tagger=nltk_tagger)
>>> blob.pos_tags
[(Word('Tag'), u'NN'), (Word('You'), u'PRP'), (Word('''), u'VBZ'), (Word('re'), u'NN'), (Word('It')
, u'PRP')]
.. _pattern: http://www.clips.ua.ac.be/pattern
.. _NLTK: http://nltk.org/
Parsers
-------
New in version `0.6.0`.
Parser implementations can also be passed to the TextBlob constructor.
::
>>> from textblob import TextBlob
>>> from textblob.parsers import PatternParser
>>> blob = TextBlob("Parsing is fun.", parser=PatternParser())
>>> blob.parse()
'Parsing/VBG/B-VP/O is/VBZ/I-VP/O fun/VBG/I-VP/O ././O/O'
Blobber: A TextBlob Factory
---------------------------
New in `0.4.0`.
It can be tedious to repeatedly pass taggers, NP extractors, sentiment analyzers, classifiers, and tokenizers to multiple TextBlobs. To keep your code `DRY `_, you can use the ``Blobber`` class to create TextBlobs that share the same models.
First, instantiate a ``Blobber`` with the tagger, NP extractor, sentiment analyzer, classifier, and/or tokenizer of your choice.
.. doctest::
>>> from textblob import Blobber
>>> from textblob.taggers import NLTKTagger
>>> tb = Blobber(pos_tagger=NLTKTagger())
You can now create new TextBlobs like so:
.. doctest::
>>> blob1 = tb("This is a blob.")
>>> blob2 = tb("This is another blob.")
>>> blob1.pos_tagger is blob2.pos_tagger
True
---
## File: docs/api_reference.rst
.. _api:
API Reference
=============
Blob Classes
------------
.. automodule:: textblob.blob
:members:
:inherited-members:
.. _api_base_classes:
Base Classes
------------
.. automodule:: textblob.base
:members:
Tokenizers
----------
.. automodule:: textblob.tokenizers
:members:
:inherited-members:
POS Taggers
-----------
.. automodule:: textblob.en.taggers
:members:
:inherited-members:
Noun Phrase Extractors
----------------------
.. automodule:: textblob.en.np_extractors
:members: BaseNPExtractor, ConllExtractor, FastNPExtractor
:inherited-members:
Sentiment Analyzers
-------------------
.. automodule:: textblob.en.sentiments
:members:
:inherited-members:
Parsers
-------
.. automodule:: textblob.en.parsers
:members:
:inherited-members:
.. _api_classifiers:
Classifiers
-----------
.. automodule:: textblob.classifiers
:members:
:inherited-members:
Blobber
-------
.. autoclass:: textblob.blob.Blobber
:members:
:special-members:
:exclude-members: __weakref__
File Formats
------------
.. automodule:: textblob.formats
:members:
:inherited-members:
Wordnet
-------
.. automodule:: textblob.wordnet
:members:
Exceptions
----------
.. module:: textblob.exceptions
.. autoexception:: textblob.exceptions.TextBlobError
.. autoexception:: textblob.exceptions.MissingCorpusError
.. autoexception:: textblob.exceptions.DeprecationError
.. autoexception:: textblob.exceptions.TranslatorError
.. autoexception:: textblob.exceptions.NotTranslated
.. autoexception:: textblob.exceptions.FormatError
---
## File: docs/classifiers.rst
.. _classifiers:
Tutorial: Building a Text Classification System
***********************************************
The ``textblob.classifiers`` module makes it simple to create custom classifiers.
As an example, let's create a custom sentiment analyzer.
Loading Data and Creating a Classifier
======================================
First we'll create some training and test data.
.. doctest::
>>> train = [
... ("I love this sandwich.", "pos"),
... ("this is an amazing place!", "pos"),
... ("I feel very good about these beers.", "pos"),
... ("this is my best work.", "pos"),
... ("what an awesome view", "pos"),
... ("I do not like this restaurant", "neg"),
... ("I am tired of this stuff.", "neg"),
... ("I can't deal with this", "neg"),
... ("he is my sworn enemy!", "neg"),
... ("my boss is horrible.", "neg"),
... ]
>>> test = [
... ("the beer was good.", "pos"),
... ("I do not enjoy my job", "neg"),
... ("I ain't feeling dandy today.", "neg"),
... ("I feel amazing!", "pos"),
... ("Gary is a friend of mine.", "pos"),
... ("I can't believe I'm doing this.", "neg"),
... ]
Now we'll create a Naive Bayes classifier, passing the training data into the constructor.
.. doctest::
>>> from textblob.classifiers import NaiveBayesClassifier
>>> cl = NaiveBayesClassifier(train)
.. _data_files:
Loading Data from Files
-----------------------
You can also load data from common file formats including CSV, JSON, and TSV.
CSV files should be formatted like so:
::
I love this sandwich.,pos
This is an amazing place!,pos
I do not like this restaurant,neg
JSON files should be formatted like so:
::
[
{"text": "I love this sandwich.", "label": "pos"},
{"text": "This is an amazing place!", "label": "pos"},
{"text": "I do not like this restaurant", "label": "neg"}
]
You can then pass the opened file into the constructor.
::
>>> with open('train.json', 'r') as fp:
... cl = NaiveBayesClassifier(fp, format="json")
Classifying Text
================
Call the ``classify(text)`` method to use the classifier.
.. doctest::
>>> cl.classify("This is an amazing library!")
'pos'
You can get the label probability distribution with the ``prob_classify(text)`` method.
.. doctest::
>>> prob_dist = cl.prob_classify("This one's a doozy.")
>>> prob_dist.max()
'pos'
>>> round(prob_dist.prob("pos"), 2)
0.63
>>> round(prob_dist.prob("neg"), 2)
0.37
Classifying TextBlobs
=====================
Another way to classify text is to pass a classifier into the constructor of ``TextBlob`` and call its ``classify()`` method.
.. doctest::
>>> from textblob import TextBlob
>>> blob = TextBlob("The beer is good. But the hangover is horrible.", classifier=cl)
>>> blob.classify()
'pos'
The advantage of this approach is that you can classify sentences within a ``TextBlob``.
.. doctest::
>>> for s in blob.sentences:
... print(s)
... print(s.classify())
...
The beer is good.
pos
But the hangover is horrible.
neg
Evaluating Classifiers
======================
To compute the accuracy on our test set, use the ``accuracy(test_data)`` method.
.. doctest::
>>> cl.accuracy(test)
0.8333333333333334
.. note::
You can also pass in a file object into the ``accuracy`` method. The file can be in any of the formats listed in the :ref:`Loading Data ` section.
Use the ``show_informative_features()`` method to display a listing of the most informative features.
.. doctest::
>>> cl.show_informative_features(5) # doctest: +SKIP
Most Informative Features
contains(my) = True neg : pos = 1.7 : 1.0
contains(an) = False neg : pos = 1.6 : 1.0
contains(I) = True neg : pos = 1.4 : 1.0
contains(I) = False pos : neg = 1.4 : 1.0
contains(my) = False pos : neg = 1.3 : 1.0
Updating Classifiers with New Data
==================================
Use the ``update(new_data)`` method to update a classifier with new training data.
.. doctest::
>>> new_data = [
... ("She is my best friend.", "pos"),
... ("I'm happy to have a new friend.", "pos"),
... ("Stay thirsty, my friend.", "pos"),
... ("He ain't from around here.", "neg"),
... ]
>>> cl.update(new_data)
True
>>> cl.accuracy(test)
1.0
Feature Extractors
==================
By default, the ``NaiveBayesClassifier`` uses a simple feature extractor that indicates which words in the training set are contained in a document.
For example, the sentence *"I feel happy"* might have the features ``contains(happy): True`` or ``contains(angry): False``.
You can override this feature extractor by writing your own. A feature extractor is simply a function with ``document`` (the text to extract features from) as the first argument. The function may include a second argument, ``train_set`` (the training dataset), if necessary.
The function should return a dictionary of features for ``document``.
For example, let's create a feature extractor that just uses the first and last words of a document as its features.
.. doctest::
>>> def end_word_extractor(document):
... tokens = document.split()
... first_word, last_word = tokens[0], tokens[-1]
... feats = {}
... feats["first({0})".format(first_word)] = True
... feats["last({0})".format(last_word)] = False
... return feats
...
>>> features = end_word_extractor("I feel happy")
>>> assert features == {"last(happy)": False, "first(I)": True}
We can then use the feature extractor in a classifier by passing it as the second argument of the constructor.
.. doctest::
>>> cl2 = NaiveBayesClassifier(test, feature_extractor=end_word_extractor)
>>> blob = TextBlob("I'm excited to try my new classifier.", classifier=cl2)
>>> blob.classify()
'pos'
Next Steps
==========
Be sure to check out the :ref:`API Reference ` for the :ref:`classifiers module `.
Want to try different POS taggers or noun phrase chunkers with TextBlobs? Check out the :ref:`Advanced Usage ` guide.
---
## File: docs/extensions.rst
.. _extensions:
**********
Extensions
**********
TextBlob supports adding custom models and new languages through "extensions".
Extensions can be installed from the PyPI. ::
$ pip install textblob-name
where "name" is the name of the package.
Available extensions
====================
Languages
---------
* `textblob-fr `_: French
* `textblob-de `_: German
Part-of-speech Taggers
----------------------
* `textblob-aptagger `_: A fast and accurate tagger based on the Averaged Perceptron.
.. admonition:: Interested in creating an extension?
See the :ref:`Contributing guide `.
---
## File: docs/index.rst
.. textblob documentation master file, created by
sphinx-quickstart on Mon Aug 5 01:41:33 2013.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
TextBlob: Simplified Text Processing
====================================
Release v\ |version|. (:ref:`Changelog`)
*TextBlob* is a Python library for processing textual data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, and more.
.. code-block:: python
from textblob import TextBlob
text = """
The titular threat of The Blob has always struck me as the ultimate movie
monster: an insatiably hungry, amoeba-like mass able to penetrate
virtually any safeguard, capable of--as a doomed doctor chillingly
describes it--"assimilating flesh on contact.
Snide comparisons to gelatin be damned, it's a concept with the most
devastating of potential consequences, not unlike the grey goo scenario
proposed by technological theorists fearful of
artificial intelligence run rampant.
"""
blob = TextBlob(text)
blob.tags # [('The', 'DT'), ('titular', 'JJ'),
# ('threat', 'NN'), ('of', 'IN'), ...]
blob.noun_phrases # WordList(['titular threat', 'blob',
# 'ultimate movie monster',
# 'amoeba-like mass', ...])
for sentence in blob.sentences:
print(sentence.sentiment.polarity)
# 0.060
# -0.341
TextBlob stands on the giant shoulders of `NLTK`_ and `pattern`_, and plays nicely with both.
Features
--------
- Noun phrase extraction
- Part-of-speech tagging
- Sentiment analysis
- Classification (Naive Bayes, Decision Tree)
- Tokenization (splitting text into words and sentences)
- Word and phrase frequencies
- Parsing
- `n`-grams
- Word inflection (pluralization and singularization) and lemmatization
- Spelling correction
- Add new models or languages through extensions
- WordNet integration
Get it now
----------
::
$ pip install -U textblob
$ python -m textblob.download_corpora
Ready to dive in? Go on to the :ref:`Quickstart guide `.
Guide
=====
.. toctree::
:maxdepth: 2
license
install
quickstart
classifiers
advanced_usage
extensions
api_reference
Project info
============
.. toctree::
:maxdepth: 1
changelog
authors
contributing
.. _NLTK: http://www.nltk.org
.. _pattern: https://github.com/clips/pattern
---
## File: docs/install.rst
.. _install:
Installation
============
Installing/Upgrading From the PyPI
----------------------------------
::
$ pip install -U textblob
$ python -m textblob.download_corpora
This will install TextBlob and download the necessary NLTK corpora. If you need to change the default download directory set the ``NLTK_DATA`` environment variable.
.. admonition:: Downloading the minimum corpora
If you only intend to use TextBlob's default models (no model overrides), you can pass the ``lite`` argument. This downloads only those corpora needed for basic functionality.
::
$ python -m textblob.download_corpora lite
With conda
----------
TextBlob is also available as a `conda `_ package. To install with ``conda``, run ::
$ conda install -c conda-forge textblob
$ python -m textblob.download_corpora
From Source
-----------
TextBlob is actively developed on Github_.
You can clone the public repo: ::
$ git clone https://github.com/sloria/TextBlob.git
Or download one of the following:
* tarball_
* zipball_
Once you have the source, you can install it into your site-packages with ::
$ python setup.py install
.. _Github: https://github.com/sloria/TextBlob
.. _tarball: https://github.com/sloria/TextBlob/tarball/master
.. _zipball: https://github.com/sloria/TextBlob/zipball/master
Get the bleeding edge version
-----------------------------
To get the latest development version of TextBlob, run
::
$ pip install -U git+https://github.com/sloria/TextBlob.git@dev
Migrating from older versions (<=0.7.1)
---------------------------------------
As of TextBlob 0.8.0, TextBlob's core package was renamed to ``textblob``, whereas earlier versions used a package called ``text``. Therefore, migrating to newer versions should be as simple as rewriting your imports, like so:
New:
::
from textblob import TextBlob, Word, Blobber
from textblob.classifiers import NaiveBayesClassifier
from textblob.taggers import NLTKTagger
Old:
::
from text.blob import TextBlob, Word, Blobber
from text.classifiers import NaiveBayesClassifier
from text.taggers import NLTKTagger
Dependencies
++++++++++++
TextBlob depends on NLTK 3. NLTK will be installed automatically when you run ``pip install textblob``.
Some features, such as the maximum entropy classifier, require `numpy`_, but it is not required for basic usage.
.. _numpy: http://www.numpy.org/
.. _NLTK: http://nltk.org/
---
## File: docs/quickstart.rst
.. _quickstart:
Tutorial: Quickstart
====================
.. module:: textblob.blob
TextBlob aims to provide access to common text-processing operations through a familiar interface. You can treat :class:`TextBlob ` objects as if they were Python strings that learned how to do Natural Language Processing.
Create a TextBlob
-----------------
First, the import.
.. doctest::
>>> from textblob import TextBlob
Let's create our first :class:`TextBlob `.
.. doctest::
>>> wiki = TextBlob("Python is a high-level, general-purpose programming language.")
Part-of-speech Tagging
----------------------
Part-of-speech tags can be accessed through the :meth:`tags ` property.
.. doctest::
>>> wiki.tags
[('Python', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('high-level', 'JJ'), ('general-purpose', 'JJ'), ('programming', 'NN'), ('language', 'NN')]
Noun Phrase Extraction
----------------------
Similarly, noun phrases are accessed through the :meth:`noun_phrases ` property.
.. doctest::
>>> wiki.noun_phrases
WordList(['python'])
Sentiment Analysis
------------------
The :meth:`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.
.. doctest::
>>> testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!")
>>> testimonial.sentiment
Sentiment(polarity=0.39166666666666666, subjectivity=0.4357142857142857)
>>> testimonial.sentiment.polarity
0.39166666666666666
Tokenization
------------
You can break TextBlobs into words or sentences.
.. doctest::
>>> zen = TextBlob(
... "Beautiful is better than ugly. "
... "Explicit is better than implicit. "
... "Simple is better than complex."
... )
>>> zen.words
WordList(['Beautiful', 'is', 'better', 'than', 'ugly', 'Explicit', 'is', 'better', 'than', 'implicit', 'Simple', 'is', 'better', 'than', 'complex'])
>>> zen.sentences
[Sentence("Beautiful is better than ugly."), Sentence("Explicit is better than implicit."), Sentence("Simple is better than complex.")]
:class:`Sentence ` objects have the same properties and methods as TextBlobs.
::
>>> for sentence in zen.sentences:
... print(sentence.sentiment)
For more advanced tokenization, see the :ref:`Advanced Usage ` guide.
Words Inflection and Lemmatization
----------------------------------
Each word in :meth:`TextBlob.words ` or :meth:`Sentence.words ` is a :class:`Word `
object (a subclass of ``unicode``) with useful methods, e.g. for word inflection.
.. doctest::
>>> sentence = TextBlob("Use 4 spaces per indentation level.")
>>> sentence.words
WordList(['Use', '4', 'spaces', 'per', 'indentation', 'level'])
>>> sentence.words[2].singularize()
'space'
>>> sentence.words[-1].pluralize()
'levels'
Words can be lemmatized by calling the :meth:`lemmatize ` method.
.. doctest::
>>> from textblob import Word
>>> w = Word("octopi")
>>> w.lemmatize()
'octopus'
>>> w = Word("went")
>>> w.lemmatize("v") # Pass in WordNet part of speech (verb)
'go'
WordNet Integration
-------------------
You can access the synsets for a :class:`Word ` via the :meth:`synsets ` property or the :meth:`get_synsets ` method, optionally passing in a part of speech.
.. doctest::
>>> from textblob import Word
>>> from textblob.wordnet import VERB
>>> word = Word("octopus")
>>> word.synsets
[Synset('octopus.n.01'), Synset('octopus.n.02')]
>>> Word("hack").get_synsets(pos=VERB)
[Synset('chop.v.05'), Synset('hack.v.02'), Synset('hack.v.03'), Synset('hack.v.04'), Synset('hack.v.05'), Synset('hack.v.06'), Synset('hack.v.07'), Synset('hack.v.08')]
You can access the definitions for each synset via the :meth:`definitions ` property or the :meth:`define() ` method, which can also take an optional part-of-speech argument.
.. doctest::
>>> Word("octopus").definitions
['tentacles of octopus prepared as food', 'bottom-living cephalopod having a soft oval body with eight long tentacles']
You can also create synsets directly.
.. doctest::
>>> from textblob.wordnet import Synset
>>> octopus = Synset("octopus.n.02")
>>> shrimp = Synset("shrimp.n.03")
>>> octopus.path_similarity(shrimp)
0.1111111111111111
For more information on the WordNet API, see the NLTK documentation on the `Wordnet Interface `_.
WordLists
---------
A :class:`WordList ` is just a Python list with additional methods.
.. doctest::
>>> animals = TextBlob("cat dog octopus")
>>> animals.words
WordList(['cat', 'dog', 'octopus'])
>>> animals.words.pluralize()
WordList(['cats', 'dogs', 'octopodes'])
Spelling Correction
-------------------
Use the :meth:`correct() ` method to attempt spelling correction.
.. doctest::
>>> b = TextBlob("I havv goood speling!")
>>> print(b.correct())
I have good spelling!
:class:`Word ` objects have a :meth:`spellcheck() Word.spellcheck` method that returns a list of ``(word, confidence)`` tuples with spelling suggestions.
.. doctest::
>>> from textblob import Word
>>> w = Word("falibility")
>>> w.spellcheck()
[('fallibility', 1.0)]
Spelling correction is based on Peter Norvig's "How to Write a Spelling Corrector"[#]_ as implemented in the pattern library. It is about 70% accurate [#]_.
Get Word and Noun Phrase Frequencies
------------------------------------
There are two ways to get the frequency of a word or noun phrase in a :class:`TextBlob `.
The first is through the ``word_counts`` dictionary. ::
>>> monty = TextBlob("We are no longer the Knights who say Ni. "
... "We are now the Knights who say Ekki ekki ekki PTANG.")
>>> monty.word_counts['ekki']
3
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. ::
>>> monty.words.count('ekki')
3
You can specify whether or not the search should be case-sensitive (default is ``False``). ::
>>> monty.words.count('ekki', case_sensitive=True)
2
Each of these methods can also be used with noun phrases. ::
>>> wiki.noun_phrases.count('python')
1
Parsing
-------
Use the :meth:`parse() ` method to parse the text.
.. doctest::
>>> b = TextBlob("And now for something completely different.")
>>> print(b.parse())
And/CC/O/O now/RB/B-ADVP/O for/IN/B-PP/B-PNP something/NN/B-NP/I-PNP completely/RB/B-ADJP/O different/JJ/I-ADJP/O ././O/O
By default, TextBlob uses pattern's parser [#]_.
TextBlobs Are Like Python Strings!
----------------------------------
You can use Python's substring syntax.
.. doctest::
>>> zen[0:19]
TextBlob("Beautiful is better")
You can use common string methods.
.. doctest::
>>> zen.upper()
TextBlob("BEAUTIFUL IS BETTER THAN UGLY. EXPLICIT IS BETTER THAN IMPLICIT. SIMPLE IS BETTER THAN COMPLEX.")
>>> zen.find("Simple")
65
You can make comparisons between TextBlobs and strings.
.. doctest::
>>> apple_blob = TextBlob("apples")
>>> banana_blob = TextBlob("bananas")
>>> apple_blob < banana_blob
True
>>> apple_blob == "apples"
True
You can concatenate and interpolate TextBlobs and strings.
.. doctest::
>>> apple_blob + " and " + banana_blob
TextBlob("apples and bananas")
>>> "{0} and {1}".format(apple_blob, banana_blob)
'apples and bananas'
`n`-grams
---------
The :class:`TextBlob.ngrams() ` method returns a list of tuples of `n` successive words.
.. doctest::
>>> blob = TextBlob("Now is better than never.")
>>> blob.ngrams(n=3)
[WordList(['Now', 'is', 'better']), WordList(['is', 'better', 'than']), WordList(['better', 'than', 'never'])]
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 :class:`TextBlob `.
.. doctest::
>>> for s in zen.sentences:
... print(s)
... print("---- Starts at index {}, Ends at index {}".format(s.start, s.end))
...
Beautiful is better than ugly.
---- Starts at index 0, Ends at index 30
Explicit is better than implicit.
---- Starts at index 31, Ends at index 64
Simple is better than complex.
---- Starts at index 65, Ends at index 95
Next Steps
++++++++++
Want to build your own text classification system? Check out the :ref:`Classifiers Tutorial `.
Want to use a different POS tagger or noun phrase chunker implementation? Check out the :ref:`Advanced Usage ` guide.
.. [#] http://norvig.com/spell-correct.html
.. [#] http://www.clips.ua.ac.be/pages/pattern-en#spelling
.. [#] http://www.clips.ua.ac.be/pages/pattern-en#parser