<jupyter_start><jupyter_text>Automatic summarization using GensimThis module automatically summarizes the given text, by extracting one or more important sentences from the text. In a similar way, it can also extract keywords. This tutorial will teach you to use this summarization module via some examples. First, we will try a small example, then we will try two larger ones, and then we will review the performance of the summarizer in terms of speed.This summarizer is based on the "TextRank" algorithm, from an [article](http://web.eecs.umich.edu/%7Emihalcea/papers/mihalcea.emnlp04.pdf) by Mihalcea et al. This algorithm was later improved upon by Barrios et al. in another [article](https://raw.githubusercontent.com/summanlp/docs/master/articulo/articulo-en.pdf), by introducing something called a "BM25 ranking function". This tutorial assumes that you are familiar with Python and have [installed Gensim](http://radimrehurek.com/gensim/install.html).Note: Gensim's summarization only works for English for now, because the text is pre-processed so that stopwords are removed and the words are stemmed, and these processes are language-dependent.Small exampleFirst of all, we import the function "summarize".<jupyter_code># import logging
# logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

from gensim.summarization import summarize<jupyter_output><empty_output><jupyter_text>We will try summarizing a small toy example; later we will use a larger piece of text. In reality, the text is too small, but it suffices as an illustrative example.<jupyter_code>text = "<NAME> is a man living two lives. By day he is an " + \
    "average computer programmer and by night a hacker known as " + \
    "Neo. Neo has always questioned his reality, but the truth is " + \
    "far beyond his imagination. Neo finds himself targeted by the " + \
    "police when he is contacted by Morpheus, a legendary computer " + \
    "hacker branded a terrorist by the government. Morpheus awakens " + \
    "Neo to the real world, a ravaged wasteland where most of " + \
    "humanity have been captured by a race of machines that live " + \
    "off of the humans' body heat and electrochemical energy and " + \
    "who imprison their minds within an artificial reality known as " + \
    "the Matrix. As a rebel against the machines, Neo must return to " + \
    "the Matrix and confront the agents: super-powerful computer " + \
    "programs devoted to snuffing out Neo and the entire human " + \
    "rebellion. "

print ('Input text:')
print (text)<jupyter_output><empty_output><jupyter_text>To summarize this text, we pass the raw string data as input to the function "summarize", and it will return a summary.Note: make sure that the string does not contain any newlines where the line breaks in a sentence. A sentence with a newline in it (i.e. a carriage return, "\n") will be treated as two sentences.<jupyter_code>print ('Summary:')
print (summarize(text))<jupyter_output><empty_output><jupyter_text>Use the "split" option if you want a list of strings instead of a single string.<jupyter_code>print(summarize(text, split=True))<jupyter_output><empty_output><jupyter_text>You can adjust how much text the summarizer outputs via the "ratio" parameter or the "word_count" parameter. Using the "ratio" parameter, you specify what fraction of sentences in the original text should be returned as output. Below we specify that we want 50% of the original text (the default is 20%).<jupyter_code>print ('Summary:')
print (summarize(text, ratio=0.5))<jupyter_output><empty_output><jupyter_text>Using the "word_count" parameter, we specify the maximum amount of words we want in the summary. Below we have specified that we want no more than 50 words.<jupyter_code>print ('Summary:')
print (summarize(text, word_count=50))<jupyter_output><empty_output><jupyter_text>As mentioned earlier, this module also supports keyword extraction. Keyword extraction works in the same way as summary generation (i.e. sentence extraction), in that the algorithm tries to find words that are important or seem representative of the entire text. They keywords are not always single words; in the case of multi-word keywords, they are typically all nouns.<jupyter_code>from gensim.summarization import keywords

print ('Keywords:')
print (keywords(text))<jupyter_output><empty_output><jupyter_text>Larger exampleLet us try an example with a larger piece of text. We will be using a synopsis of the movie "The Matrix", which we have taken from [this](http://www.imdb.com/title/tt0133093/synopsis?ref_=ttpl_pl_syn) IMDb page.In the code below, we read the text file directly from a web-page using "requests". Then we produce a summary and some keywords.<jupyter_code>with open('./data/the_matrix_synopsis.txt', 'r') as file:
    text=file.read()

print ('Summary:')
print (summarize(text, ratio=0.01))

print ('\nKeywords:')
print (keywords(text, ratio=0.01))<jupyter_output><empty_output><jupyter_text>If you know this movie, you see that this summary is actually quite good. We also see that some of the most important characters (Neo, Morpheus, Trinity) were extracted as keywords.Another exampleLet's try an example similar to the one above. This time, we will use the [IMDb synopsis](http://www.imdb.com/title/tt0118715/synopsis?ref_=tt_stry_pl) of "The Big Lebowski".Again, we download the text and produce a summary and some keywords.<jupyter_code>with open('./data/the_big_lebowski_synopsis.txt', 'r') as file:
    text=file.read()

print ('Summary:')
print (summarize(text, ratio=0.01))

print ('\nKeywords:')
print (keywords(text, ratio=0.01))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08527811896824004, 'fraction_numerical': 0.007194244604316547, 'mean_word_length': 5.08324439701174, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '613995', 'license': 'MIT', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1619, 'n_words': 702, 'path': 'assets/notebooks/07-Gensim.ipynb', 'repo_name': 'halkypi/lh', 'size': '5711'}