Python Sentiment Analysis With the NLTK Library [With Examples] (2024)

Sentiment analysis is a technique to extract emotions from textual data. This data may be used to determine what people actually believe, think, and feel about specific subjects or products.

Python’s popularity as a programming language has resulted in a wide range of sentiment analysis applications. The Natural Language Toolkit (NLTK) is a common library for sentiment analysis.

In this tutorial, you will learn the fundamentals to perform sentiment analysis using Python’s NLTK library.

How to Install and Import the NLTK Library in Python

You must first know how to install and import the NLTK library into your Python distribution before you can begin sentiment analysis with NLTK.

Pip is the default Python package installer, which you can use to install NLTK. Enter the following command into your command prompt:

pip install nltk

Once the installation is completed, you can import NLTK into your python environment as shown below:

import nltk

Now you are good to go with NLTK sentiment analysis with Python.

Tokenization and Stop Words Removal with NLTK

We must first preprocess our text input before doing sentiment analysis.

The text must be modified, with stop words removed and words stemmed. NLTK offers several functions to achieve these objectives.

Let’s have a look at a few of these features:

How Do You Tokenize Text in Python?

Tokenization is the process of splitting text into discrete phrases or words. To do this, NLTK provides the word_tokenize() tokenizer part of the nltk.tokenize package.

A tokenizer converts a piece of text into a list of tokens and allows finding words and punctuation in the string.

The code snippet below uses a word tokenizer available in the NLTK library to split the given text into words.

from nltk.tokenize import word_tokenizetext = "Hello, today we will learn about Python Sentiment Analysis with NLTK."tokens = word_tokenize(text)print(tokens)

When you execute this code you might see the following error:

LookupError: ********************************************************************** Resource punkt not found. Please use the NLTK Downloader to obtain the resource: >>> import nltk >>> nltk.download('punkt')

The reason why you see the error “Resource punkt not found” is that this tokenizer needs Punkt sentence tokenization models to be installed on your machine.

To install Punkt sentence tokenization models run the following commands after opening the Python shell:

>>> import nltk>>> nltk.download('punkt')

Here is what it will look like when you do this on your computer:

>>> import nltk>>> nltk.download('punkt')[nltk_data] Downloading package punkt to[nltk_data] /Users/codefather/nltk_data...[nltk_data] Unzipping tokenizers/punkt.zip.True

Now, execute the initial Python program to tokenize the string of text and confirm you see the following list of strings.

['Hello', ',', 'today', 'we', 'will', 'learn', 'about', 'Python', 'Sentiment', 'Analysis', 'with', 'NLTK', '.']

As you can see we have split the text into words and punctuation.

Once again, in the code above, we have:

  • imported the word_tokenize() tokenizer from the nltk.tokenize package.
  • passed the text to analyze to word_tokenize().
  • this function splits the text into words and punctuation.

As we will see later in this tutorial, NLTK also provides a sentence tokenizer to split text into sentences instead of words.

How to Remove Stopwords in Python

The text you analyze may contain words like “the”, “is”, and “an” that have no significance. These are called stop words and to remove them with Python you can use the NLTK library.

We can use NLTK’s stopwords.words that provides a list of stop words to exclude them from our analysis.

from nltk.tokenize import word_tokenizefrom nltk.corpus import stopwordstext = "Hello, today we will learn about Python Sentiment Analysis with NLTK."tokens = word_tokenize(text)print("Including stop words: ", tokens)stop_words = set(stopwords.words('english'))filtered_tokens = [token for token in tokens if token.lower() not in stop_words]print("Excluding stop words: ", filtered_tokens)

When you execute this code you might see the following error:

LookupError: ********************************************************************** Resource stopwords not found. Please use the NLTK Downloader to obtain the resource: >>> import nltk >>> nltk.download('stopwords')

In the same way, we have done before, open the Python shell and execute the following two Python statements.

>>> import nltk>>> nltk.download('stopwords')

Then execute the Python program and confirm you see the following output:

Including stop words: ['Hello', ',', 'today', 'we', 'will', 'learn', 'about', 'Python', 'Sentiment', 'Analysis', 'with', 'NLTK', '.']Excluding stop words: ['Hello', ',', 'today', 'learn', 'Python', 'Sentiment', 'Analysis', 'NLTK', '.']

In the code snippet above, we are extending the previous code to remove all the stop words present in our text. In this case, the stop words are:

  • we
  • will
  • about
  • with

We have imported all the English stopwords from the nltk.corpus package and then looped through the text to remove the stopwords using a Python list comprehension.

What Is the Meaning of Stemming in Python?

Stemming is the process of bringing a word to its root form. For example, the stem for the words “programming” or “programmer” is “program”.

As a result, the vocabulary we use shrinks but sentiment analysis becomes more accurate. PorterStemmer, SnowballStemmer, and other stemming algorithms are available in the NLTK library.

from nltk.tokenize import word_tokenizefrom nltk.stem import PorterStemmertext = "Python programming is becoming very popular."tokens = word_tokenize(text)stemmer = PorterStemmer()stemmed_tokens = [stemmer.stem(token) for token in tokens]print(stemmed_tokens)

Here is what we get back when we use NLTK to perform the stemming process.

['python', 'program', 'is', 'becom', 'veri', 'popular', '.']

In the code above, we are importing PorterStemmer from nltk.stem. We are tokenizing the text and then we pass the tokenized text to PorterStemmer.

This will break down words into their first form. For example, programming is stemmed to the simpler form program.

When I executed this code I was wondering why the stemmed form for very is veri. The reason is that the idea of stemming is to get a shorter version of a word to simplify sentiment analysis. At the same time, the shorter version might not have an actual meaning.

Let’s see a different process that given a word as input returns a shorter form that has a meaning.

What Is Lemmatization in Python?

Let’s have a look at the process called lemmatization which also brings words to their basic form. The difference from stemming is that with lemmatization you will obtain words that have a meaning.

Is lemmatization better than stemming? Let’s find out!

We will use as an example the same phrase we have stemmed previously and instead of using a stemmer, we use a lemmatizer.

from nltk.tokenize import word_tokenizefrom nltk.stem.wordnet import WordNetLemmatizertext = "Python programming is becoming very popular."tokens = word_tokenize(text)lemmatizer = WordNetLemmatizer()lemmatized_tokens = [lemmatizer.lemmatize(token) for token in tokens]print(lemmatized_tokens)

To execute this code you will have to download the WordNet lexical database using the commands below in the Python shell:

>>> import nltk>>> nltk.download('wordnet')

Here is the output of the lemmatization process:

['Python', 'programming', 'is', 'becoming', 'very', 'popular', '.']

Hmmm…the lemmatized version is identical to the original phrase.

The lemmatize method also accepts a second argument that represents the Part of Speech tag, for example in this case we can pass “v” which stands for “verb”.

Update the following piece of code and rerun the program:

lemmatized_tokens = [lemmatizer.lemmatize(token, "v") for token in tokens]

Here is the output:

['Python', 'program', 'be', 'become', 'very', 'popular', '.']

The verb “programming” has been lemmatized to “program” and the verb “becoming” to “become”.

We have seen some of the functions in the NLTK library. You should have now a better understanding of what NLTK is and how it works.

Now let’s jump into some sentiment analysis using NLTK.

How Does NLTK Do Sentiment Analysis?

In this example, we will use a sentiment analysis classifier called Vader (Valence Aware Dictionary for Sentiment Reasoning). Vader has already been trained in NLTK to categorize the sentiment intensity score of sentences as positive, negative, or neutral.

This pre-trained sentiment analysis classifier, which has a high degree of accuracy, was trained using a large dataset of text from social media. Given some text, it’s able to provide a sentiment score.

Let’s look at how you can use this classifier for sentiment analysis:

from nltk.sentiment import SentimentIntensityAnalyzeranalyzer = SentimentIntensityAnalyzer()text = "I love this product! It's amazing."scores = analyzer.polarity_scores(text)print(scores)

In the code above, we import SentimentIntensityAnalyzer to see if a sentence is positive, negative, or neutral.

We pass some text to the classifier and then check the polarity score of the words. It will tell us whether the sentence has a positive or negative sentiment. In our case, the sentence is 73% positive.

{'neg': 0.0, 'neu': 0.266, 'pos': 0.734, 'compound': 0.8516}

But…

What does compound mean in the output of the sentiment intensity analyzer?

The compound score is the sum of negative, neutral, and positive normalized to the range from -1 to +1, in other words, it represents the overall sentiment of the text. A compound score of +1 indicates a strong sense of positivity, whereas a score of -1 indicates a strong sense of negativity.

You can use these scores to categorize the sentiment of your text data.

Here is how we can compare the sentiment intensity of two sentences to understand which one is more positive than the other.

from nltk.sentiment import SentimentIntensityAnalyzeranalyzer = SentimentIntensityAnalyzer()review1 = "I love this product! It's amazing."review2 = "This product is terrible. I hate it."review1_score = analyzer.polarity_scores(review1)print("Score for Review #1: {}".format(review1_score))review2_score = analyzer.polarity_scores(review2)print("Score for Review #2: {}".format(review2_score))if review1_score['compound'] > review2_score['compound']: print("The review that has a more positive sentiment is Review #1: \"{}\"".format(review1))else: print("The review that has a more positive sentiment is Review #2:\"{}\"".format(review2))

The code above is self-explanatory, we are trying to classify two reviews as positive or negative based on the compound score calculated by a classifier.

Here are the scores of the two reviews:

Score for Review #1: {'neg': 0.0, 'neu': 0.266, 'pos': 0.734, 'compound': 0.8516}Score for Review #2: {'neg': 0.63, 'neu': 0.37, 'pos': 0.0, 'compound': -0.7783}

Based on the compound scores you can see that review1 is a positive review and review2 is a negative review.

Based on the score the classifier confirms that the first review has a more positive sentiment than the second review.

The review that has a more positive sentiment is Review #1: "I love this product! It's amazing."

You can play with the code and pass different sentences to see different results.

How Do You Identify Sentences in Text Using Python NLTK?

In the first example of this tutorial, we used a word tokenizer when analyzing the tokens in a sentence.

The NLTK library also provides a sentence tokenizer that allows identifying sentences within a text string instead of simple words.

Let’s see how the same text can be split based on words or sentences.

from nltk.tokenize import word_tokenizefrom nltk.tokenize import sent_tokenizetext = "NLTK stands for Natural Language Toolkit. Today we will learn about Python Sentiment Analysis with NLTK."words = word_tokenize(text)sentences = sent_tokenize(text)print("Words: ", words)print("Sentences: ", sentences)

Here is the output:

Words: ['NLTK', 'stands', 'for', 'Natural', 'Language', 'Toolkit', '.', 'Today', 'we', 'will', 'learn', 'about', 'Python', 'Sentiment', 'Analysis', 'with', 'NLTK', '.']Sentences: ['NLTK stands for Natural Language Toolkit.', 'Today we will learn about Python Sentiment Analysis with NLTK.']

To get the words for each sentence you can apply word_tokenize to every sentence using a list comprehension.

words_in_sentences = [word_tokenize(sentence) for sentence in sentences]print("Words in sentences: ", words_in_sentences)

The NLTK library provides a list of lists where the words in the first list are the words in the first sentence and the words in the second list belong to the second sentence.

Words in sentences: [['NLTK', 'stands', 'for', 'Natural', 'Language', 'Toolkit', '.'], ['Today', 'we', 'will', 'learn', 'about', 'Python', 'Sentiment', 'Analysis', 'with', 'NLTK', '.']]

How Do You Extract the Frequency of Words With NLTK?

What else can we do with text analysis in Python?

Imagine you want to identify the frequency distribution of each word in a document.

To identify the frequency distribution of tokens in a text you can use nltk.probability.FreqDist available in the NLTK library.

Before calling the FreqDist function you have to extract tokens from the text in the way you have learned in this tutorial.

Then you can pass the list of tokens to the FreqDist function (that accepts an iterable of tokens).

from nltk.tokenize import word_tokenizefrom nltk.probability import FreqDisttext = "NLTK stands for Natural Language Toolkit. Today we will learn about Python Sentiment Analysis with NLTK."words = word_tokenize(text)frequency_distribution = FreqDist(words)print(frequency_distribution)

If you execute this program you get the following:

<FreqDist with 16 samples and 18 outcomes>

What can we do with the data returned by FreqDist?

One useful thing we can do is to plot the frequency distribution of each token in the text using FreqDist.plot().

frequency_distribution.plot()

You will see the following graph.

Python Sentiment Analysis With the NLTK Library [With Examples] (1)

Conclusion:

In this post, we covered the fundamentals of sentiment analysis using Python with NLTK.

We learned how to install and import Python’s Natural Language Toolkit (NLTK), as well as how to analyze text and preprocess text with NLTK capabilities like word tokenization, stopwords, stemming, and lemmatization.

In addition, we classified the sentiment of our text data using NLTK’s pre-trained sentiment analysis classifier.

Businesses may use sentiment analysis to learn what their customers think and feel about the goods and services they provide.

Python Sentiment Analysis With the NLTK Library [With Examples] (2)

Claudio Sabato

Claudio Sabato is an IT expert with over 15 years of professional experience in Python programming, Linux Systems Administration, Bash programming, and IT Systems Design. He isa professional certified by the Linux Professional Institute.

With a Master’s degree in Computer Science, he has a strong foundation in Software Engineering and a passion for robotics with Raspberry Pi.

No related posts.

Python Sentiment Analysis With the NLTK Library [With Examples] (2024)

FAQs

How to do sentiment analysis in Python NLTK? ›

Sentiment Analysis: First Steps With Python's NLTK Library
  1. Getting Started With NLTK. Installing and Importing. Compiling Data. ...
  2. Using NLTK's Pre-Trained Sentiment Analyzer.
  3. Customizing NLTK's Sentiment Analysis. Selecting Useful Features. ...
  4. Comparing Additional Classifiers. Installing and Importing scikit-learn. ...
  5. Conclusion.

How to use NLTK library in Python? ›

Stemming
  1. from nltk.stem import PorterStemmerstemmer = PorterStemmer() words = ['play', 'playing', 'plays', 'played', ...
  2. sent2 = "I played the play playfully as the players were playing in. the play with playfullness" ...
  3. from nltk import pos_tag token = word_tokenize(sent) + word_tokenize(sent2) tagged = pos_tag(cleaned_token)

What is sentiment analysis in NLP with example? ›

Sentiment analysis (or opinion mining) is a natural language processing (NLP) technique used to determine whether data is positive, negative or neutral. Sentiment analysis is often performed on textual data to help businesses monitor brand and product sentiment in customer feedback, and understand customer needs.

How do you do sentiment analysis step by step? ›

Here's a step-by-step guide to get you started with your sentiment analysis project:
  1. Step 1: Dataset Exploration. First, we need to understand our dataset. ...
  2. Step 2: Data Preprocessing. ...
  3. Step 3: Split the Dataset. ...
  4. Step 4 : Feature Extraction. ...
  5. Step 5 : Build and Train a Sentiment Analysis Model. ...
  6. Step 6 : Evaluate the Model.
Jun 3, 2023

Which Python tool for sentiment analysis? ›

Python libraries like NLTK, TextBlob, and VADER provide pre-trained models, simple APIs, and specialized functionality for analyzing sentiments in textual data. These top Python libraries for sentiment analysis make it easy for beginners to start with this important natural language processing technique.

Which model is best for sentiment analysis? ›

Statistical machine learning models like Naive Bayes Classifier, Support Vector Machine (SVM), Logistic Regression, Random Forest, and Gradient Boosting Machines (GBM) are all valuable for sentiment analysis, each with their strengths.

How to create a sentiment analysis model in Python? ›

To perform sentiment analysis, here are the steps we will follow;
  1. Preprocess the text data.
  2. Split the dataset.
  3. Vectorize the dataset.
  4. Data Conversion.
  5. Label Encoding.
  6. Train a Neural Networks.
  7. Train the model.
  8. Evaluate the Model ( With Plotting)
Feb 7, 2024

Which algorithm can be used for sentiment analysis? ›

Classification algorithms such as Naïve Bayes, linear regression, support vector machines, and deep learning are used to generate the output. The AI model provides a sentiment score to the newly processed data as the new data passes through the ML classifier.

Is NLTK outdated? ›

You can work your way down the vast number of nltk modules, and you'll find almost none of them are useful for real work, and those that are, ship a host of alternatives that are all much worse than the current state-of-the-art. nltk makes most sense as a teaching tool, but even then it's mostly out of date.

What is the difference between NLP and NLTK? ›

Natural language processing (NLP) is a field that focuses on making natural human language usable by computer programs. NLTK, or Natural Language Toolkit, is a Python package that you can use for NLP. A lot of the data that you could be analyzing is unstructured data and contains human-readable text.

What are the advantages of NLTK in Python? ›

NLTK is widely used by researchers, developers, and data scientists worldwide to develop NLP applications and analyze text data. One of the major advantages of using NLTK is its extensive collection of corpora, which includes text data from various sources such as books, news articles, and social media platforms.

What are the three types of sentiment analysis? ›

Existing approaches to sentiment analysis can be grouped into three main categories: knowledge-based techniques, statistical methods, and hybrid approaches. Knowledge-based techniques classify text by affect categories based on the presence of unambiguous affect words such as happy, sad, afraid, and bored.

What is a real example of sentiment analysis? ›

Examples of Sentiment Analysis

For instance, sentiment analysis may be performed on Twitter to determine overall opinion on a particular trending topic. Companies and brands often utilize sentiment analysis to monitor brand reputation across social media platforms or across the web as a whole.

How to collect data for sentiment analysis? ›

3 Use appropriate data collection methods

There are different methods for collecting data for sentiment analysis, such as web scraping, surveys, or APIs. Each method has its advantages and disadvantages, depending on your research goals, data sources, and resources.

How to calculate sentiment score in Python? ›

Calculate Sentiment Scores

polarity_scores() and input a string of text. This method returns a Python dictionary of sentiment scores: how negative the sentence is between 0-1, how neutral the sentence is between 0-1, how positive the sentence is between 0-1, as well as a compound score between -1-1.

How to do sentiment analysis in Python with spaCy? ›

How to Use spaCy for Text Classification
  1. Add the textcat component to the existing pipeline.
  2. Add valid labels to the textcat component.
  3. Load, shuffle, and split your data.
  4. Train the model, evaluating on each training loop.
  5. Use the trained model to predict the sentiment of non-training data.

What is sentiment analysis using NLTK kaggle? ›

Sentiment Analysis: the process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writer's attitude towards a particular topic, product, etc. is positive, negative, or neutral.

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5797

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.