Summary Extraction From News Articles

kamal_DS
6 min readFeb 24, 2023

--

In today’s digital age, we are constantly bombarded with large amounts of textual data in the form of articles, reports, and other documents. As a result, it can be challenging and time-consuming to sift through all of this information and extract the key points. This is where Natural Language Processing (NLP) techniques come into play. One such technique is summary extraction, which involves the automatic generation of a brief, yet comprehensive, summary of a given text. In this blog, we will explore summary extraction using NLP techniques. So, let’s dive into the world of summary extraction and discover how it can help us make sense of the vast amounts of textual data that surround us.

Python provides us with a library called newspaper3k where we can directly pass the link of the Article. If we Paste the Link of the article and use the download method and text method to save the entire text of an Article into a variable.

# for Installing the package follow the below command 
# !pip install newspaper3k

from newspaper import Article
reg = Article('https://www.gadgetsnow.com/gn-advertorial/10-cracking-deals-you-can-avail-of-at-the-jiomart-mobiles-electronics-fest/articleshow/98156783.cms?_gl=1*1llhbk0*_ga*MTE0MDA5NzY5MS4xNjc2NTk3OTM5*_ga_FCN624MN68*MTY3NzIyMDczMS4zLjEuMTY3NzIyMDc0MS41MC4wLjA.')

reg.download() # to download the data form url

reg.parse() # parse the data

text = reg.text # saving entire text in a variable called text:

Now Entire text is in a variable called text, Using NLP Pipe Line we Need to clean the entire data and collect the summary from the text.

I am following a simple Technique where finding the count of each word occurrence and Normalizing the value using a High Count.

import nltk  # nltk library 
from nltk.corpus import stopwords # stopwords library
import string
punctuation = string.punctuation # for removing punctuations

text = text.split()
word_frequencies = {}
for i in text:
if i.lower() not in stopwords.words('english'):
if i.lower() not in punctuation:
if i not in word_frequencies.keys():
word_frequencies[i] = 1
else:
word_frequencies[i] += 1

In the above code, I am removing the stopwords, and punctuation and finding each value count. Example output for the above code:

{'Python': 6,
'general': 1,
'purpose,': 1,
'dynamic,': 1,
'high-level,': 1,
'interpreted': 2,
'programming': 5,
'language.': 1,
'supports': 2,
'Object': 1,
'Oriented': 1,
'approach': 1,
'develop': 1,
'applications.': 1,
'simple': 1,
'easy': 2,
'learn': 2,
'provides': 1,
'lots': 1,
'high-level': 1,
'data': 2,
'structures.': 1,
'yet': 1,
'powerful': 1,
'versatile': 1,
'scripting': 2,
'language,': 1,
'makes': 2,
'attractive': 1,
'Application': 1,
'Development.': 1,
"Python's": 1,
'syntax': 1,
'dynamic': 1,
'typing': 1,
'nature': 1,
'make': 1,
'ideal': 1,
'language': 2,
'rapid': 1,
'application': 1,
'development.': 1,
'multiple': 1,
'pattern,': 1,
'including': 1,
'object-oriented,': 1,
'imperative,': 1,
'functional': 1,
'procedural': 1,
'styles.': 1,
'intended': 1,
'work': 1,
'particular': 1,
'area,': 1,
'web': 1,
'programming.': 1,
'known': 1,
'multipurpose': 1,
'used': 1,
'web,': 1,
'enterprise,': 1,
'3D': 1,
'CAD,': 1,
'etc.': 1,
'need': 1,
'use': 1,
'types': 1,
'declare': 1,
'variable': 1,
'dynamically': 1,
'typed': 1,
'write': 1,
'a=10': 1,
'assign': 1,
'integer': 2,
'value': 1,
'variable.': 1,
'development': 1,
'debugging': 1,
'fast': 1,
'compilation': 1,
'step': 1,
'included': 1,
'development,': 1,
'edit-test-debug': 1,
'cycle': 1,
'fast.': 1}

Now Using the Highest Number I am normalizing the word values

max_frequency = max(word_frequencies.values())


for j in word_frequencies.keys():
word_frequencies[j] = word_frequencies[j] / max_frequency
word_frequencies

for example, the max value is 6 in the above dictionary and each word value will be divided by 6 for normalization purposes, then the outcome looks like this.

{'Python': 1.0,
'general': 0.16666666666666666,
'purpose,': 0.16666666666666666,
'dynamic,': 0.16666666666666666,
'high-level,': 0.16666666666666666,
'interpreted': 0.3333333333333333,
'programming': 0.8333333333333334,
'language.': 0.16666666666666666,
'supports': 0.3333333333333333,
'Object': 0.16666666666666666,
'Oriented': 0.16666666666666666,
'approach': 0.16666666666666666,
'develop': 0.16666666666666666,
'applications.': 0.16666666666666666,
'simple': 0.16666666666666666,
'easy': 0.3333333333333333,
'learn': 0.3333333333333333,
'provides': 0.16666666666666666,
'lots': 0.16666666666666666,
'high-level': 0.16666666666666666,
'data': 0.3333333333333333,
'structures.': 0.16666666666666666,
'yet': 0.16666666666666666,
'powerful': 0.16666666666666666,
'versatile': 0.16666666666666666,
'scripting': 0.3333333333333333,
'language,': 0.16666666666666666,
'makes': 0.3333333333333333,
'attractive': 0.16666666666666666,
'Application': 0.16666666666666666,
'Development.': 0.16666666666666666,
"Python's": 0.16666666666666666,
'syntax': 0.16666666666666666,
'dynamic': 0.16666666666666666,
'typing': 0.16666666666666666,
'nature': 0.16666666666666666,
'make': 0.16666666666666666,
'ideal': 0.16666666666666666,
'language': 0.3333333333333333,
'rapid': 0.16666666666666666,
'application': 0.16666666666666666,
'development.': 0.16666666666666666,
'multiple': 0.16666666666666666,
'pattern,': 0.16666666666666666,
'including': 0.16666666666666666,
'object-oriented,': 0.16666666666666666,
'imperative,': 0.16666666666666666,
'functional': 0.16666666666666666,
'procedural': 0.16666666666666666,
'styles.': 0.16666666666666666,
'intended': 0.16666666666666666,
'work': 0.16666666666666666,
'particular': 0.16666666666666666,
'area,': 0.16666666666666666,
'web': 0.16666666666666666,
'programming.': 0.16666666666666666,
'known': 0.16666666666666666,
'multipurpose': 0.16666666666666666,
'used': 0.16666666666666666,
'web,': 0.16666666666666666,
'enterprise,': 0.16666666666666666,
'3D': 0.16666666666666666,
'CAD,': 0.16666666666666666,
'etc.': 0.16666666666666666,
'need': 0.16666666666666666,
'use': 0.16666666666666666,
'types': 0.16666666666666666,
'declare': 0.16666666666666666,
'variable': 0.16666666666666666,
'dynamically': 0.16666666666666666,
'typed': 0.16666666666666666,
'write': 0.16666666666666666,
'a=10': 0.16666666666666666,
'assign': 0.16666666666666666,
'integer': 0.3333333333333333,
'value': 0.16666666666666666,
'variable.': 0.16666666666666666,
'development': 0.16666666666666666,
'debugging': 0.16666666666666666,
'fast': 0.16666666666666666,
'compilation': 0.16666666666666666,
'step': 0.16666666666666666,
'included': 0.16666666666666666,
'development,': 0.16666666666666666,
'edit-test-debug': 0.16666666666666666,
'cycle': 0.16666666666666666,
'fast.': 0.16666666666666666}

Now I am focusing on sentences which means I am making sentences as a key and each normalized word value which is in the above dictionary as value.

just an ex: [I am good] take this as one sentence after normalizing think the value for I is 0.1 and value for am is 0.3 and the value for good is 0.5 now count each work value 0.1 + 0.3 + 0.5 = 0.9 so for sentence [I am good] value is 0.9

sent_tok = sent_tokenize(text)
sentence_tokens = [sent for sent in sent_tok]
sentence_scores = {}

for sent in sentence_tokens:
for word in sent.split():
if word.lower() in word_frequencies.keys():
if sent not in sentence_scores.keys():
sentence_scores[sent] = word_frequencies[word.lower()]
else:
sentence_scores[sent] += word_frequencies[word.lower()]

The Outcome of the above code

{'Python is a general purpose, dynamic, high-level, and interpreted programming language.': 2.0, 'It supports Object Oriented programming approach to develop applications.': 1.666666666666667, 'It is simple and easy to learn and provides lots of high-level data structures.': 1.8333333333333333, 'Python is easy to learn yet powerful and versatile scripting language, which makes it attractive for Application Development.': 2.4999999999999996, "Python's syntax and dynamic typing with its interpreted nature make it an ideal language for scripting and rapid application development.": 2.4999999999999996, 'Python supports multiple programming pattern, including object-oriented, imperative, and functional or procedural programming styles.': 3.3333333333333335, 'Python is not intended to work in a particular area, such as web programming.': 0.9999999999999999, 'That is why it is known as multipurpose programming language because it can be used with web, enterprise, 3D CAD, etc.': 2.1666666666666665, "We don't need to use data types to declare variable because it is dynamically typed so we can write a=10 to assign an integer value in an integer variable.": 3.0, 'Python makes the development and debugging fast because there is no compilation step included in Python development, and edit-test-debug cycle is very fast.': 2.0}

Now find the total sentence in the text and multiply with a value [0.3]. The purpose of using value is to select the sentence which is having a value of around 0.3 In this way we will get some sentences and joining those sentences will give us a Summary of the text.

summary = nlargest(select_length, sentence_scores, key=sentence_scores.get)
# selecting 0.3 % sentence
final_summary = [word.text for word in summary] # joining each sentence to make summary

Now You will get the summary of an article easily using NLP and Python techniques.

Now Using Flask I Deployed this project. Accessing the article URL from the post method and doing the same operation as seen above.

Example:

Run the flask code in the local host and paste the link of an Article

Result:

You can get the Entire Code from here: https://github.com/saikamal3344/Summary-Extraction-From-News-Articles

Follow my previous blog to explore more about computer vision and NLP.

--

--

kamal_DS

Interested to work in the field of Artificial Intelligence, Machine Learning, Deep Learning, NLP and Computer Vision.