Text summarization sample
Happy coding
# Install dependencies in Jupyter Notebook
!pip install sumy nltk
# Import necessary libraries
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lex_rank import LexRankSummarizer
# Define summarization function
def summarize_text(text: str, sentence_count: int = 5) -> str:
parser = PlaintextParser.from_string(text, Tokenizer("english"))
summarizer = LexRankSummarizer()
summary = summarizer(parser.document, sentence_count)
summarized_text = " ".join([str(sentence) for sentence in summary])
return summarized_text
# Input text
input_text = """
Artificial Intelligence (AI) is the simulation of human intelligence in machines that are programmed to think like humans and mimic their actions.
The term may also be applied to any machine that exhibits traits associated with a human mind such as learning and problem-solving.
The ideal characteristic of artificial intelligence is its ability to rationalize and take actions that have the best chance of achieving a specific goal.
AI is continuously evolving to benefit many different industries. Machines are wired using a cross-disciplinary approach based on mathematics, computer science,
linguistics, psychology, and more.
"""
# Summarize the text
summary = summarize_text(input_text, sentence_count=3)
print("Summary:")
print(summary)
Comments
Post a Comment