File size: 2,705 Bytes
769af1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Summary tab rendering functionality"""

from config import app_config
import utils
import sys
import streamlit as st
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.evaluation import rouge_n


###
### INTERNAL FUNCTIONS
###
def __get_summarizer(summarizer_type):
    """Helper to get summarizer object given its name as string"""
    summarizer_dict = app_config.summarizers.get(summarizer_type)
    module = sys.modules[summarizer_dict["module"]]
    summarizer = utils.get_class_from_name(module, summarizer_type)
    desc = summarizer_dict["desc"]
    return summarizer(), desc


def __summarize(text, summarizer, n_sentences):
    ### instantiate the text parser, summarize text and return the summary text
    parser = PlaintextParser.from_string(text, Tokenizer("english"))
    summary_tuple = summarizer(parser.document, n_sentences)
    summary_text = ""
    for sentence in summary_tuple:
        summary_text += str(sentence)
    ### compute length of sentences are ROUGE score for summary text
    rouge = rouge_n(
        evaluated_sentences=summary_tuple,
        reference_sentences=parser.document.sentences,
        n=2,
    )
    stats = f"""
    Number of sentences in original text: **{len(parser.document.sentences)}**  
    Number of sentences in summary text: **{len(summary_tuple)}**  
    ROUGE (bi-gram) score: **{rouge}**
    """
    return summary_text, stats


def __section(header):
    """Build page UI elements"""
    st.header(header)
    st.write(
        "Choose the `Summarization Method`, `Enter Text` in the text "
        + "area, choose the `Number Of Sentences` required in summary text "
        + "and then click `Summarize`"
    )
    summarizer_type = st.radio(
        "Summarization Method:",
        options=[
            # "WordFrequency",
            "TextRankSummarizer",
            "LexRankSummarizer",
            "LsaSummarizer",
        ],
    )
    ### Based on type selected, fetch the summarizer object and show short description
    summarizer, desc = __get_summarizer(summarizer_type)
    st.info(body=f"{desc}", icon=app_config.icon_info)
    text = st.text_area("Enter text:", height=300, key="summarization")
    n_sentences = st.slider(
        label="Number Of Sentences", min_value=1, max_value=10, value=3
    )
    ### summarize the entered text and show the results
    if st.button("Summarize"):
        summary, stats = __summarize(text, summarizer, n_sentences)
        st.divider()
        st.subheader("Summary")
        st.success(stats)
        st.write(summary)
        st.divider()


###
### MAIN FLOW, entry point
###
def render():
    __section("Text Summarization")