A-Z Guide to Text Summarization in Python for Beginners

Have you heard of the Inshorts smartphone app? It is a cutting-edge news app that embodies news stories into a concise 60-word summary. Applications like Inshorts allow you to avoid reading long articles by generating a clear and concise summary.

An average individual uses Google Search more than three times a day. Thanks to Featured Snippets, or Knowledge Panels, you receive better results for your search queries. Google uses featured snippets to display an article's summary to respond to users’ queries. These snippets are taken directly from websites.


Deploy Transformer BART Model for Text summarization on GCP

Downloadable solution code | Explanatory videos | Tech Support

Start Project

There’s one common aspect in both these cases, and that is what you will learn about in this article: Automatic Text Summarization. Automatic text summarization is a method that allows individuals to achieve a breakthrough in productivity by reducing the massive volume of information they encounter daily. This method not only allows people to reduce the amount of reading they must do, but it also allows them to read and comprehend literary pieces efficiently.

NLP Text Summarization 

Text summarization in Natural Language Processing (NLP) certainly significantly impacts human life. With the rising digital era and its impact on the publishing industry, investing time to read an article mindfully is no longer an option, given time constraints. Furthermore, with the rising number of articles being produced and the digitization of printed media articles, keeping track of the growing number of web articles has become extremely difficult. This is why we need text summarization as it aids in shortening lengthy texts.

Text Summarization

What is Text Summarization?

Text summarization in NLP is the process of creating summaries from large volumes of data while maintaining significant informational elements and content value. The language of the summary should be concise and straightforward so that it conveys the meaning to the reader. As per Statista, by 2025, the total amount of data created, recorded, copied, and consumed worldwide is expected to exceed 180 zettabytes. Most of this text data needs to be minimized to more straightforward, concise summaries containing essential details to browse and analyze them more easily. There is a high demand for machine learning algorithms that can quickly summarize lengthy texts and offer accurate insights. This is precisely where text summarization comes into the picture!

Text summarization is beneficial for several NLP tasks, including text classification, legal text summaries, news summaries, generating headlines, etc. Let us further look into the key reasons behind the growing demand for Text Summarization.

The Need For Text Summarization

Manually summarizing vast amounts of textual data is a challenging task that can lead to inefficient outcomes. As a result, we need methods for automatic text summarization.

Here are some significant reasons why we need text summarization-

  • Text summarization shortens reading time.

  • Automatic summarization improves the effectiveness of searching, i.e., it speeds up searching for necessary information among vast quantities of data.

  • The text summarization technique boosts the volume of data stored in a particular space.

  • Human summarizers are more biased and inaccurate than automatic summarizing techniques.

  • Business providers can enhance the volume of texts they can handle by using automatic or semi-automatic summarizing systems.

Types of Text Summarization

There are mainly two different text summarization approaches- extractive and abstraction.

Extractive Summarization

The extractive text summarization approach involves extracting essential words from an original document and combining them to create a summary. Extractive summarization uses a scoring mechanism to rank the relevance of phrases to select just those that are most relevant to the source document's meaning. This technique extracts the required text according to the specified criteria without making significant changes to the documents. The extractive summarization method works with the help of algorithms such as LexRank, Luhn, LSA, etc. which are implemented using the Python libraries Gensim or Sumy.

Here’s an example of how extractive text summarization works-

Original text- ProjectPro offers 200+ solved end-to-end Data Science and Big Data reusable project solutions. You can practice these projects and enhance your data science skills.

Summarized text- ProjectPro offers 200+ Data Science and Big Data project solutions.


Abstractive Summarization

Abstractive summarization concentrates on the most critical information in the original text and creates a new set of sentences for the summary. The new sentence might not be a part of the source text. This approach is entirely the opposite of the extractive text summarization approach, as the latter generates a summary based on the exact original text. This technique entails identifying key pieces, interpreting the context, and re-creating them in a new way. It guarantees to give the most crucial information in the shortest possible words.The abstractive summarization method works well with deep learning models like the seq2seq model, LSTM, etc., along with popular Python packages (Spacy, NLTK, etc.) and frameworks (Tensorflow, Keras).

Here’s an example of how abstractive text summarization works-

Original text- ProjectPro offers 120+ solved end-to-end Data Science and Big Data reusable project solutions. You can practice these projects and enhance your data science skills.

Summarized text- ProjectPro offers 120+ end-to-end Data Science and Big Data projects that one can use to enhance Data Science skills.

Top Machine Learning Text Summarization Algorithms

This section will briefly cover the text summarization machine learning algorithms and evaluation metrics.

Text Summarization Algorithms

PageRank Algorithm

Named after Larry Page, one of Google’s founders, PageRank is a Google Search algorithm that ranks websites in search engine result pages. Although Google uses multiple algorithms, PageRank is the company's first-ever and most well-known algorithm. PageRank is a means of determining how important a website's pages are. PageRank calculates a rough estimate of the importance of a website by tracking the amount and quality of links to that page. The central idea is that more important websites are more likely to gain links from other sites.

The PageRank algorithm generates a probability distribution that indicates the possibility of a random user clicking on links ending up on a specific page. PageRank works for almost any large set of documents. Various research publications imply that the distribution is uniformly distributed among all records in the set at the start of the computational process. PageRank computations demand multiple visits through the collection, also known as "iterations," to precisely modify estimated PageRank values to represent the actual potential value.


TextRank Algorithm

TextRank is an unsupervised extractive text summarising approach similar to Google's PageRank algorithm. It helps with keyword extraction, automatic text summarization, and phrase ranking.

The TextRank algorithm is quite similar to the PageRank algorithm in many ways, such as-

  • TextRank works with sentences, whereas PageRank works with web pages.

  • The probability of a web page transition is calculated in the PageRank algorithm, whereas the TextRank algorithm works by comparing the similarity of any two sentences.

  • The TextRank approach stores the similarity scores in a square matrix, identical to the M matrix used for the PageRank approach.

SumBasic Algorithm

SumBasic is a multi-document summarization method that finds the frequency distribution of words across all documents. The algorithm prioritizes frequently occurring words in a document over less frequently occurring words in order to provide a precise and accurate summary. It measures the average probability of each sentence based on its word pattern and selects the best ranking sentence of the most recurring word until the preferred summary length is achieved.

Text Summarization in Python

Python has about 137,000 libraries that are useful in domains such as data science, machine learning, data manipulation, and so on. The significance of these libraries stems from the fact that they save you from creating new codes every time the same process runs. Let's look at the most commonly used text summarization Python libraries:

  • NLTK

Natural Language Toolkit (NLTK) is an acronym for Natural Language Toolkit. It is the most commonly used Python package for handling human language data. It includes libraries for categorization, tokenization, stemming, tagging, parsing, and other text processing tasks. For text summarization, the NLTK employs the TF-IDF approach. The following steps show how NLTK performs Text Summarization-

  1. Tokenize the sentence: this method separates a text into tokens.

  2. It constructs a frequency matrix, a matrix of all words sentence by sentence. It tracks how many times a word appears in each sentence.

  3. Generate a matrix by calculating Term Frequency:

Term Frequency (p)= P/D,

Where P is the number of times, a specific term (term p in the above equation) appears in a document or data set. D is the total number of terms in the document/data. The term represents a word in a paragraph, while the document/data represents a paragraph.

  1. A table is formed, including documents based on keywords.

  2. Generate a matrix by calculating IDF (Inverse Document Frequency):

IDF (t) = log (N/t),

Where t is the number of times, a specific phrase (term t in the equation above) appears in the document/data. N represents the total number of terms in the document/data.

  1. Multiply the results from both matrices to calculate the TF-IDF and generate a new matrix.

  2. Using the TF-IDF score, assign weights to the sentences in the text.

  • Gensim

Gensim is a Python package that relies on other Python libraries such as NumPy and SciPy. Its unique features include-

  1. memory independence, which eliminates the need for storing the entire training compilation in RAM at any given time;

  2. simplified Latent Semantic Analysis and Latent Dirichlet Allocation; and

  3. the ability to create basic similarity queries for documents in their summarization.

It is based on corpus theory, vector theory, models, and sparse matrices. Genism implements the TextRank algorithm for Text Summarization.

  • Sumy

Sumy is a Python package for summarising text documents and HTML pages automatically. It employs a variety of techniques and methodologies for summarising -


It's an unsupervised text summarization method based on graph-based sentence centrality scoring. It adopts the technique of looking for comparable sentences, which are likely to be very important. Run pip install lexrank to install it.


Luhn summarizes text using a heuristic manner and is based on the recurrence of the most important terms.

  • SpaCy

Matthew Honnibal and Ines Montani, the creators of the software business Explosion, created SpaCy. It's majorly used for research purposes. CNN models for part-of-speech tagging, dependency parsing, text categorization, and named entity recognition are some of spaCy's most notable features.


Request a demo

Text Summarization Using Deep Learning Models

Recurrent Neural Networks (RNNs), convolutional neural networks (CNNs), and sequence-to-sequence models are the most commonly used abstractive text summarization deep learning models. This section will provide a basic idea of the sequence-to-sequence model, attention mechanism, and transformers (BERT Model).

  • Sequence-to-Sequence Model (Seq2Seq Model)

The Seq2Seq framework involves a series of sentences as input and produces another series of sentences as output. In Neural Machine Translation, input sentences are in one language, and output sentences are translated versions of that language. The seq2seq modeling involves two primary strategies- encoder and decoder.

Encoder Model 

The encoder Model is often used to encode or change the input phrases while also providing feedback at each step. If you use the LSTM layer, this feedback can be an internal state, such as a hidden state or a cell state. Encoder models extract essential information from input texts while keeping the context intact. You will send your input language into the encoder model in Neural Machine Translation, which will record contextual details without affecting the rest of the input sequence. The output sequences are obtained by feeding the encoder model's outputs into the decoder model.

Decoder Mode

The decoder model is used for word-by-word decoding or anticipates the target texts. Decoder input data accepts target sentence input and guesses the next word, which is then passed into the prediction layer below. The words 'start>' (start of target sentence) and 'end>' (end of target sentence) are used by the model to determine what will be the initial variable for predicting the following word and what will be the finishing variable for determining the sentence's ending. You initially give the model the word 'start>' during training, and the model then forecasts the following word- the decoder target data. This word is then used as input data for the next step, resulting in the prediction of the next word.


  • Attention Mechanism

Before being used for NLP tasks like text summarization, the attention mechanism was used for neural machine translation. A basic encoder-decoder architecture may struggle when providing long sentences as it cannot evaluate long input parts. The attention mechanism helps retain the information that has a considerable impact on the summary. The attention mechanism determines the weight between the output word and each input word at each output word; the weights sum up to one. The use of weights has the advantage of indicating which input word requires special attention concerning the output word. After processing each input word, the mean value of the decoder's last hidden layers is determined and given to the softmax layer alongside the final hidden layers in the current phase.

Attention Mechanism has two categories.

(a) Global Attention

(b) Local Attention

Global Attention:  It generates the context vector using all the hidden states from each time step from the encoder model.

Local Attention: It generates the context vector using some hidden states from the encoder model in local attention.

Transformers | BERT Model

The BERT (Bidirectional Encoder Representations from Transformers) word embedding method uses a multilayer bidirectional transformer encoder. The transformer neural network uses parallel attention layers instead of sequential recurrence. By merging the representations of the words or phrases, BERT builds a single big transformer. Furthermore, BERT follows an unsupervised approach for pre-training on a significant text volume. Two tokens are added to the text in the BERT model. The initial token (CLS) combines the complete text sequence information. The second token is (SEP) is used at the end of each sentence. The final text consists of multiple tokens, and each token has three types of embeddings: token, segmentation, and position embeddings.

Here are some of the main reasons behind the popularity of BERT -

  • Its key benefit is that it uses bi-directional learning to obtain the context of words from both left to right and right to left contexts at the same time, having been trained on 2.5 billion words.

  • Next Sentence Prediction (NSP) training allows the model to grasp how sentences link to one another during the model training process. As a result, the model can gather more information.

  • Since it has been efficiently pre-trained on a huge amount of data (English Wikipedia- 2500 words), the BERT model can be used on smaller datasets while still delivering good results.

Evaluation Methods for Text Summarization

Two most commonly used evaluation methods for text summarization include -

  1. Human-based Method

  2. Automatic Method

Human-based Evaluation

Human experts assign scores based on how well the summary addresses the topics and answers the questions and other variables such as grammatical structure and non-redundancy.

Automatic Evaluation

Recall-Oriented Understudy for Gisting Evaluation (ROUGE) is a way to determine a summary's quality by comparing it to other human-written summaries as a benchmark. It uses several human-created references and a machine-generated candidate summary for evaluation purposes. According to the logic, if a model generates a good summary, it is assumed to have similar standard features with human references.

Text Summarization Projects For Practice 

1. Abstractive Text Summarization using Transformers-BART Model

This NLP text summarization project aims to build a BART model for abstractive text summarization on a given dataset. This unique project will introduce you to the basic concepts of text summarization, the BART model, and the encoder-decoder architecture. You'll learn how to make a BART tokenizer and use transformers to fine-tune the BART model. You'll also learn about the Rouge evaluation metric in this project. Also, you'll build a web application for abstractive text summarization on the data. The project uses the Curation Corpus dataset available on Github, with 40,000 professionally written news story summaries and links to the original articles.

Source Code: Abstractive Text Summarization using Transformers-BART Model

2. Deploy Transformer BART Model for Text summarization on GCP

In this project, you will explore how to use Google Cloud Platform (GCP) as a cloud service to deploy the machine learning model for the project, as mentioned earlier. It will introduce you to Google Cloud Platform and Docker containers. You'll learn how to construct Docker images and set up a cloud repository. For the summarising model, learn how to execute server-based and Dockerized deployment. This project will let you explore how to create Virtual Machine instances and use Cloud Shell editors.

Source Code: Deploy Transformer BART Model for Text summarization on GCP

Key Takeaways

Text summarization applications are evolving with the advent of specialized tools and techniques to handle specific summarization activities.  If you are willing to enhance your NLP skillset, you should practice some fantastic NLP projects from ProjectPro’s repository. ProjectPro has over 200+ solved end-to-end Data Science and Big Data project solutions built by a community of more than 125,000 developers.


Where can Text Summarization be used?

>News article summaries, stock market reports, weather forecast reports, blogs, book/movie reviews, etc., are some of the use cases where automatic text summarization can be applied.

>Is Text Summarization Supervised or Unsupervised?

Text summarization might be supervised or unsupervised depending on the applied machine learning algorithms/approaches. Supervised text summarization uses the Seq2Seq model, whereas unsupervised text summarization uses latent semantic analysis.




Access Solved Big Data and Data Science Projects

aws image

Start Your First Project

Learn By Doing

What Users are saying..

profile image

Venkata Bharadwaj

IT Consulting Manager at Ernst & Young Global Services LLP
linkedin profile url
