avatarNuno Bispo

Summary

This text provides a tutorial on using the Python library TextBlob for sentiment analysis and text summarization.

Abstract

The tutorial explains how to use the Python library TextBlob for natural language processing tasks such as sentiment analysis and text summarization. TextBlob is a simple-to-use library that can perform common NLP tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more. The tutorial demonstrates how to install TextBlob and use it for sentiment analysis to analyze reviews and extract nouns from an article for text summarization. The tutorial also provides code examples and a summary of the main features of TextBlob.

Opinions

  • TextBlob is a powerful and simple-to-use library for natural language processing tasks.
  • TextBlob can be used for sentiment analysis to determine the overall sentiment of a piece of text.
  • TextBlob can be used for text summarization by extracting nouns from an article.
  • TextBlob has a wide range of features, including noun phrase extraction, sentiment analysis, classification, tokenization, word and phrase frequencies, word inflection, and spelling correction.
  • TextBlob can be installed using pip and requires an additional download of library components.
  • The tutorial provides code examples and a summary of the main features of TextBlob.
  • The tutorial is part of a series called Python Shorts, which aims to provide quick and simple solutions for different needs of Python projects and to improve knowledge.

Python Shorts — NLP Sentiment Analysis and Text Summary

What if you could do sentiment analysis and text summaries with Python and NLP without the need of expensive APIs from Amazon or Google? It is possible with TextBlob.

Photo by Agence Olloweb on Unsplash

Welcome to Python Shorts.

This tutorial series will provide you with quick and simple solutions for different needs of your Python projects and to improve your knowledge.

What is TextBlob ?

TextBlob is a Python library for processing textual data which can perform common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.

Some of it’s features include:

  • Noun phrase extraction
  • Sentiment analysis
  • Classification (Naive Bayes, Decision Tree)
  • Tokenization (splitting text into words and sentences)
  • Word and phrase frequencies
  • Word inflection (pluralization and singularization) and lemmatization
  • Spelling correction

That’s a lot of features for a very simple to use library.

For this tutorial we are going to use the sentiment analysis to analyze a couple of reviews and noun phrase extraction to summarize an article.

Installing TextBlob

To install the TextBlob we just need to run the normal pip install commands and an extra download for library components:

$ pip install textblob
$ python -m textblob.download_corpora

Using TextBlob for Sentiment Analysis

Using TextBlob for sentiment analysis is very simple.

All that is needed is to feed out TextBlob with each of our reviews and perform the sentiment analysis.

This will return a named tuple of polarity and subjectivity score.

The polarity score is a float within the range [-1.0, 1.0]. This will give us our general positive or negative sentiment.

So anything above 1.0 is a positive feeling and anything below 0 is a negative feeling.

The subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.

Let’s check out our simple code:

A quick code walk-trough:

  • Line 3 we define our reviews list
  • Line 14 we go trough each review
  • Line 15 we perform the sentiment analysis with TextBlob
  • Line 16 we created the output dictionary with all data from the review, polarity and subjectivity
  • Line 21 we check the review polarity and if greater than 0 we add it to positive reviews if not we add to negative reviews
  • Line 26 to 32 we just print out our positive and negative reviews

Using TextBlob for Text Summaries

Another functionality of TextBlob is the extraction of nouns from a text. We can use this capability to provide summaries of particular texts, like an article.

Let’s see that in practice:

article = 'After his defeat at the Battle of Waterloo, instead of remaining in the field with his shattered army, Napoleon returned to Paris in the hope of retaining political support for his position as Emperor of the French. He hoped, with his political base secured, to then be able to continue the war. It was not to be; instead the members of the two chambers created a Provisional Government and demanded that Napoleon abdicate. Napoleon toyed with the idea of a coup d''état similar to Eighteenth of Brumaire but decided against it. On 25 June Napoleon left Paris for the final time and after staying at the Palace of Malmaison, left for the coast hoping to reach the United States of America. In the meantime, the Provisional Government deposed his son and tried to negotiate a conditional surrender with the Coalition powers. They failed to obtain any significant concessions from the Coalition who insisted on a military surrender and the restoration of Louis XVIII. Napoleon, realising he could not hope to evade the Royal Navy, surrendered to Captain Maitland upon placing himself under his protection on board HMS Bellerophon. The British Government refused to allow Napoleon to set foot in England and arranged for his exile to the remote South Atlantic island of Saint Helena where he died in 1821.'
This article is about:
- waterloo
- napoleon
- paris
- political support
- emperor
- political base
- provisional
- brumaire
- june napoleon
- final time
- malmaison
- america
- conditional surrender
- coalition
- significant concessions
- military surrender
- louis xviii
- royal navy
- maitland
- hms bellerophon
- british government
- england
- atlantic
- saint helena

As you can see we get a small summary of the main keywords of the article which allow for us to generate a summarized description of the article.

Let’s take a look at the code:

A quick code walk-trough:

  • Line 3 we define our article
  • Line 17 we extract our nouns from our article with TextBlob
  • Line 18 we remove duplicate nouns from the list by using a set and converting that set back to a list
  • Line 20 we just print out the information in a formatted way

Conclusion

TextBlob is a great library that allow us to access a lot of NLP functionalities with quite simple code like the examples we just created.

Check out the project code for Sentiment Analysis on GitHub at: https://github.com/nunombispo/PythonShorts-SentimentAnalysis

Check out the project code for Text Summary on GitHub at: https://github.com/nunombispo/PythonShorts-TextSummary

Check out the TextBlob GitHub at: https://github.com/sloria/TextBlob

And TextBlob documentation at: https://textblob.readthedocs.io/en/dev/index.html

Other related Shorts:

Follow me on Twitter: https://twitter.com/DevAsService

Check out my website at: https://developer-service.io/

If you enjoyed reading this article and found it usefull, you can support me by signing up for a Medium membership (if you are not a member). It will only cost you $5 a month — this will give you access to all stories on Medium! (and I will receive a small commission)

Besides that, if you want to stay updated when I post a new story, you can signup for my free newsletter!

Technology
Python3
AI
Development
Machine Learning
Recommended from ReadMedium