ToS-Summarization / extractive_model.py
EE21's picture
Update extractive_model.py
f7a1fef
raw
history blame
2.53 kB
"""from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer
from sumy.summarizers.lex_rank import LexRankSummarizer
from sumy.summarizers.text_rank import TextRankSummarizer
from pysummarization.nlpbase.auto_abstractor import AutoAbstractor
from pysummarization.tokenizabledoc.simple_tokenizer import SimpleTokenizer
from pysummarization.abstractabledoc.top_n_rank_abstractor import TopNRankAbstractor
from sumy.nlp.stemmers import Stemmer
from sumy.utils import get_stop_words"""
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=5):
"""
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=5):
"""
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