Change language

Python | Summing extractive text using Gensim

| | | |

Algorithm:
Below is the algorithm implemented in the gensim library called "TextRank" , which is based on the PageRank algorithm to rank search results.

  1. Pre-process this text. This includes removing stopwords, removing punctuation, and traversing.
  2. Construct a graph with vertex sentences.
  3. A graph has edges indicating the similarity between two sentences at the vertices.
  4. Run the PageRank algorithm on this weighted graph.
  5. Select the vertices with the highest score and add them to the summary.
  6. Based on the ratio or word count, the number of vertices selected is determined.

Code: Summarizes a Wikipedia entry based on (a) ratio and (b) word count.

from gensim.summarization.summarizer import summarize

from gensim.summarization import keywords 

import wikipedia

 
# Get wiki content.

wikisearch = wikipedia.page ( "Amitabh Bachchan" )

wikicontent = wikisearch. content

nlp = en_core_web_sm.load ( )

doc = nlp (wikicontent)

 
# Save wiki content to file
# (for reference).

f = open ( " wikicontent.txt " , "w" )

f.write (wikicontent)
f.close ()

 
# Summary (0.5% of 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)

Exit

 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 honors 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. 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically