File size: 1,968 Bytes
46193fd
 
 
97f7d3e
 
6fadf04
 
46193fd
907c0be
46193fd
47639e3
46193fd
 
47639e3
46193fd
 
 
 
 
 
47639e3
 
 
46193fd
47639e3
 
46193fd
 
 
 
 
 
 
 
 
f7a1fef
 
 
907c0be
f7a1fef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.text_rank import TextRankSummarizer
from sumy.summarizers.lsa import LsaSummarizer
from sumy.summarizers.lex_rank import LexRankSummarizer
import nltk
nltk.download('punkt')

def summarize_with_textrank(text, sentences_count=10):
    """
    Summarizes the provided text using TextRank algorithm.

    Args:
    text (str): Text to summarize.
    sentences_count (int): Number of sentences for the summary.

    Returns:
    str: Summarized text.
    """

    # Check if the text is not empty
    if not text.strip():
        return "Provided text is empty."

    # Create a parser for the provided text
    parser = PlaintextParser.from_string(text, Tokenizer("english"))

    # Use TextRank for summarization
    text_rank_summarizer = TextRankSummarizer()
    text_rank_summary = text_rank_summarizer(parser.document, sentences_count=sentences_count)

    # Compile summary into a single string
    summary_text = "\n".join(str(sentence) for sentence in text_rank_summary)
    
    return summary_text


# Define LSA summarization function
def summarize_with_lsa(text, sentences_count=10):
    """
    Summarizes the provided text using LSA (Latent Semantic Analysis) algorithm.
    Args:
    text (str): Text to summarize.
    sentences_count (int): Number of sentences for the summary.
    Returns:
    str: Summarized text.
    """

    # Check if the text is not empty
    if not text.strip():
        return "Provided text is empty."

    # Create a parser for the provided text
    parser = PlaintextParser.from_string(text, Tokenizer("english"))

    # Use LSA for summarization
    lsa_summarizer = LsaSummarizer()
    lsa_summary = lsa_summarizer(parser.document, sentences_count=sentences_count)

    # Compile summary into a single string
    summary_text = "\n".join(str(sentence) for sentence in lsa_summary)
    
    return summary_text