TEXT_SUMMARIZER in Python

INTRODUCTION

The TEXT_SUMMARIZER in Python is a simple NLP-based application that shortens large texts while preserving key information. It makes use of the nltk or sumy library to investigate and extract the maximum relevant sentences, making it beneficial for summarizing articles, reports, and files.

STEP 1: Import Required Libraries: The program imports nltk for natural language processing, Counter for word frequency evaluation, and heapq to extract key sentences.

import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from collections import Counter
import heapq

STEP 2: Define Summarization Function: The summarize_text() feature tokenizes the input text into sentences and phrases, then calculates word frequency. Score Sentences Based on Word Frequency: The program determines each sentence’s score by summing the frequencies of its words. Select Key Sentences for Summary: The software choices the top-ranked sentences the use of heapq.Nlargest() and combines them right into a summary.

def summarize_text(text, num_sentences=3):
    sentences = sent_tokenize(text)
    words = word_tokenize(text.lower())

    word_freq = Counter(words)
    sentence_scores = {}

    for sentence in sentences:
        for word in word_tokenize(sentence.lower()):
            if word in word_freq:
                sentence_scores[sentence] = sentence_scores.get(sentence, 0) + word_freq[word]

    summary_sentences = heapq.nlargest(num_sentences, sentence_scores, key=sentence_scores.get)
    return " ".join(summary_sentences)

Step 3: Take User Input: The application asks the user to enter a long text and stores it in the text variable.

text = input("Enter a long text: ")
summary = summarize_text(text)

STEP 4: Generate and Display Summary: The feature methods the textual content, extracts key sentences, and prints the summarized model.

print("\nSummarized Text:")
print(summary)
OUTPUT
Enter a long text: Natural Language Processing (NLP) is a field of AI that focuses on the 
interaction between computers and humans using natural language. NLP helps machines understand, 
interpret, and respond to text efficiently. 
Summarized Text: NLP helps machines understand, interpret, and respond to text efficiently.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top