|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Text Summarizer using Frequency-based Extractive Algorithm |
| 4 | +Summarizes text by keeping only the most important sentences. |
| 5 | +""" |
| 6 | + |
| 7 | +import re |
| 8 | +from collections import Counter |
| 9 | +from string import punctuation |
| 10 | + |
| 11 | + |
| 12 | +def clean_text(text): |
| 13 | + """Remove extra whitespace and normalize text.""" |
| 14 | + text = re.sub(r'\s+', ' ', text) |
| 15 | + return text.strip() |
| 16 | + |
| 17 | + |
| 18 | +def tokenize_sentences(text): |
| 19 | + """Split text into sentences.""" |
| 20 | + sentences = re.split(r'[.!?]+', text) |
| 21 | + return [s.strip() for s in sentences if s.strip()] |
| 22 | + |
| 23 | + |
| 24 | +def tokenize_words(text): |
| 25 | + """Extract words and convert to lowercase.""" |
| 26 | + words = re.findall(r'\b[a-zA-Z]+\b', text.lower()) |
| 27 | + return words |
| 28 | + |
| 29 | + |
| 30 | +def get_word_frequencies(sentences): |
| 31 | + """Calculate word frequency scores.""" |
| 32 | + all_words = [] |
| 33 | + for sentence in sentences: |
| 34 | + all_words.extend(tokenize_words(sentence)) |
| 35 | + |
| 36 | + # Remove common stop words |
| 37 | + stop_words = {'the', 'is', 'at', 'which', 'on', 'a', 'an', 'and', 'or', |
| 38 | + 'but', 'in', 'with', 'to', 'for', 'of', 'as', 'by', 'that', |
| 39 | + 'this', 'it', 'from', 'be', 'are', 'was', 'were', 'been', |
| 40 | + 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', |
| 41 | + 'could', 'should', 'may', 'might', 'can'} |
| 42 | + |
| 43 | + filtered_words = [w for w in all_words if w not in stop_words] |
| 44 | + |
| 45 | + word_freq = Counter(filtered_words) |
| 46 | + max_freq = max(word_freq.values()) if word_freq else 1 |
| 47 | + |
| 48 | + # Normalize frequencies |
| 49 | + for word in word_freq: |
| 50 | + word_freq[word] = word_freq[word] / max_freq |
| 51 | + |
| 52 | + return word_freq |
| 53 | + |
| 54 | + |
| 55 | +def score_sentences(sentences, word_freq): |
| 56 | + """Score each sentence based on word frequencies.""" |
| 57 | + sentence_scores = {} |
| 58 | + |
| 59 | + for sentence in sentences: |
| 60 | + words = tokenize_words(sentence) |
| 61 | + score = sum(word_freq.get(word, 0) for word in words) |
| 62 | + |
| 63 | + if len(words) > 0: |
| 64 | + sentence_scores[sentence] = score / len(words) |
| 65 | + else: |
| 66 | + sentence_scores[sentence] = 0 |
| 67 | + |
| 68 | + return sentence_scores |
| 69 | + |
| 70 | + |
| 71 | +def summarize(text, ratio=0.3): |
| 72 | + """Summarize text by extracting top sentences. |
| 73 | + |
| 74 | + Args: |
| 75 | + text: Input text to summarize |
| 76 | + ratio: Proportion of sentences to keep (0.0 to 1.0) |
| 77 | + |
| 78 | + Returns: |
| 79 | + Summarized text |
| 80 | + """ |
| 81 | + text = clean_text(text) |
| 82 | + sentences = tokenize_sentences(text) |
| 83 | + |
| 84 | + if len(sentences) <= 2: |
| 85 | + return text |
| 86 | + |
| 87 | + word_freq = get_word_frequencies(sentences) |
| 88 | + sentence_scores = score_sentences(sentences, word_freq) |
| 89 | + |
| 90 | + # Select top sentences |
| 91 | + num_sentences = max(1, int(len(sentences) * ratio)) |
| 92 | + top_sentences = sorted(sentence_scores.items(), key=lambda x: x[1], reverse=True)[:num_sentences] |
| 93 | + |
| 94 | + # Maintain original order |
| 95 | + summary_sentences = sorted(top_sentences, key=lambda x: sentences.index(x[0])) |
| 96 | + summary = '. '.join([s[0] for s in summary_sentences]) + '.' |
| 97 | + |
| 98 | + return summary |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + print("Text Summarizer - Frequency-based Extractive Algorithm") |
| 103 | + print("=" * 55) |
| 104 | + text = input("\nEnter text to summarize:\n") |
| 105 | + |
| 106 | + if text.strip(): |
| 107 | + summary = summarize(text) |
| 108 | + print("\n" + "=" * 55) |
| 109 | + print("SUMMARY:") |
| 110 | + print("=" * 55) |
| 111 | + print(summary) |
| 112 | + else: |
| 113 | + print("No text provided!") |
0 commit comments