Python | Extractive Text Summarization using Gensim
Last Updated :
26 Feb, 2021
Summarization is a useful tool for varied textual applications that aims to highlight important information within a large corpus. With the outburst of information on the web, Python provides some handy tools to help summarize a text. This article provides an overview of the two major categories of approaches followed - extractive and abstractive. In this article, we shall look at a working example of extractive summarization.
Algorithm :
Below is the algorithm implemented in the gensim library, called "TextRank", which is based on PageRank algorithm for ranking search results.
- Pre-process the given text. This includes stop words removal, punctuation removal, and stemming.
- Make a graph with sentences that are the vertices.
- The graph has edges denoting the similarity between the two sentences at the vertices.
- Run PageRank algorithm on this weighted graph.
- Pick the highest-scoring vertices and append them to the summary.
- Based on the ratio or the word count, the number of vertices to be picked is decided.
Code : Summarizes a Wikipedia article based on (a) ratio and (b) word count.
Python
from gensim.summarization.summarizer import summarize
from gensim.summarization import keywords
import wikipedia
import en_core_web_sm
# Get wiki content.
wikisearch = wikipedia.page("Amitabh Bachchan")
wikicontent = wikisearch.content
nlp = en_core_web_sm.load()
doc = nlp(wikicontent)
# Save the wiki content to a file
# (for reference).
f = open("wikicontent.txt", "w")
f.write(wikicontent)
f.close()
# Summary (0.5% of the original content).
summ_per = summarize(wikicontent, ratio = 0.05)
print("Percent summary")
print(summ_per)
# Summary (200 words)
summ_words = summarize(wikicontent, word_count = 200)
print("Word count summary")
print(summ_words)
Output
Percent summary
Amitabh Bachchan (pronounced [?m??ta?b? ?b?t???n]; born Inquilaab Srivastava;
11 October 1942) is an Indian film actor, film producer, television host,
occasional playback singer and former politician. He first gained popularity
in the early 1970s for films such as Zanjeer, Deewaar and Sholay, and was
dubbed India's "angry young man" for his on-screen roles in Bollywood.
.
.
.
Apart from National Film Awards, Filmfare Awards and other competitive awards
which Bachchan won for his performances throughout the years, he has been
awarded several honours for his achievements in the Indian film industry.
Word count summary
Beyond the Indian subcontinent, he also has a large overseas following
in markets including Africa (such as South Africa), the Middle East
(especially Egypt), United Kingdom, Russia and parts of the United
States. Bachchan has won numerous accolades in his career, including
four National Film Awards as Best Actor and many awards at
international film festivals and award ceremonies.
.
.
.
After a three year stint in politics from 1984 to 1987, Bachchan
returned to films in 1988, playing the title role in Shahenshah,
which was a box office success.
Similar Reads
Extracting locations from text using Python In this article, we are going to see how to extract location from text using Python. While working with texts, the requirement can be the detection of cities, regions, states, and countries and relationships between them in the received text. This can be very useful for geographical studies. In this
3 min read
Wikipedia Summary Generator using Python Tkinter Prerequisite: Tkinter Wikipedia Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter outputs the fastest and easiest way to create GUI applications. Wikipedia is a Python library tha
2 min read
Rule-Based Data Extraction in HTML using Python textminer Module While working with HTML, there are various requirements to extract data from plain HTML tags in an ordered manner in form of python data containers such as lists, dict, integers, etc. This article deals with a library that helps to achieve this using a rule-based approach. Features of Python - textm
3 min read
How to build a Random Story Generator using Python? In this section, we are going to make a very interesting beginner-level project of Python. It is a random story generator. The random story generator project aims to generate random stories every time user executes the code. A story is made up of a collection of sentences. We will choose random phra
4 min read
Python | Pandas Index.summary() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.summary() function return a summarized representation of the Index. This
2 min read