Building a Text Summarization Application

By Bill Sharlow

Day 3: DIY Natural Language Processing Applications

Welcome back to our NLP journey! Today, we’ll dive into the exciting world of text summarization, a powerful NLP technique that allows us to distill the essence of large bodies of text into concise summaries. Whether you’re skimming through news articles or condensing research papers, text summarization can save you valuable time and effort. In this post, we’ll explore the concepts behind text summarization and demonstrate how to build a simple summarization application using Python libraries like NLTK and spaCy.

Understanding Text Summarization

Text summarization is the process of distilling the most important information from a text document while preserving its key points. There are two primary approaches to text summarization:

  1. Extractive Summarization: In extractive summarization, we identify the most important sentences or phrases from the original text and extract them to form the summary. This approach involves ranking sentences based on their relevance and selecting the top-ranked ones for inclusion in the summary.
  2. Abstractive Summarization: In abstractive summarization, we generate a summary by paraphrasing and synthesizing the content of the original text. This approach involves understanding the meaning of the text and generating new sentences that convey the same information in a more concise form.

Implementing Extractive Summarization

Let’s build a simple extractive summarization application using NLTK. We’ll tokenize the text, compute the importance scores for sentences using techniques like TF-IDF, and select the top-ranked sentences for inclusion in the summary.

from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer

def extractive_summarize(text, num_sentences=2):
    # Tokenize the text into sentences
    sentences = sent_tokenize(text)

    # Tokenize and preprocess each sentence
    tokenized_sentences = [word_tokenize(sentence.lower()) for sentence in sentences]

    # Remove stopwords from tokenized sentences
    stop_words = set(stopwords.words('english'))
    filtered_sentences = [[token for token in sentence if token not in stop_words] for sentence in tokenized_sentences]

    # Convert tokenized sentences back to text
    preprocessed_sentences = [' '.join(sentence) for sentence in filtered_sentences]

    # Compute TF-IDF scores for sentences
    vectorizer = TfidfVectorizer()
    tfidf_matrix = vectorizer.fit_transform(preprocessed_sentences)
    sentence_scores = tfidf_matrix.sum(axis=1)

    # Select top-ranked sentences for the summary
    top_sentences_indices = sentence_scores.argsort(axis=0)[-num_sentences:]
    summary_sentences = [sentences[i] for i in top_sentences_indices]

    return ' '.join(summary_sentences)

# Example usage
text = "Natural Language Processing (NLP) is a subfield of artificial intelligence that focuses on the interaction between computers and humans through natural language."
summary = extractive_summarize(text)


In today’s post, we’ve explored the concept of text summarization and demonstrated how to build a simple extractive summarization application using NLTK. By leveraging techniques like tokenization, TF-IDF, and sentence ranking, we can distill the essence of text documents into concise summaries.

In the next post, we’ll continue our NLP journey by exploring another exciting application: language translation. Stay tuned for more hands-on examples and insights as we dive deeper into the world of natural language processing!

If you have any questions or thoughts on text summarization, feel free to share them in the comments section below. Happy summarizing, and see you in the next post!

Leave a Comment