|
| 1 | +import java.util.*; |
| 2 | +import java.util.stream.Collectors; |
| 3 | + |
| 4 | +public class TextSummarizer { |
| 5 | + |
| 6 | + // List of common stopwords |
| 7 | + private static final Set<String> STOPWORDS = Set.of( |
| 8 | + "a", "an", "the", "and", "or", "but", "is", "are", "was", "were", |
| 9 | + "in", "on", "at", "to", "for", "with", "of", "by", "that", "this" |
| 10 | + ); |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + String text = """ |
| 14 | + Java is a high-level, class-based, object-oriented programming language |
| 15 | + that is designed to have as few implementation dependencies as possible. |
| 16 | + It is a general-purpose programming language intended to let programmers |
| 17 | + write once, run anywhere (WORA), meaning that compiled Java code can run |
| 18 | + on all platforms that support Java without the need for recompilation. |
| 19 | + Java applications are typically compiled to bytecode that can run on any |
| 20 | + Java virtual machine (JVM) regardless of the underlying computer architecture. |
| 21 | + """; |
| 22 | + |
| 23 | + int summarySize = 2; // Number of sentences in summary |
| 24 | + String summary = summarizeText(text, summarySize); |
| 25 | + System.out.println("Summary:\n" + summary); |
| 26 | + } |
| 27 | + |
| 28 | + public static String summarizeText(String text, int numSentences) { |
| 29 | + // Split text into sentences |
| 30 | + String[] sentences = text.split("(?<=[.!?])\\s+"); |
| 31 | + |
| 32 | + // Count word frequencies |
| 33 | + Map<String, Integer> wordFreq = new HashMap<>(); |
| 34 | + for (String sentence : sentences) { |
| 35 | + String[] words = sentence.toLowerCase().split("\\W+"); |
| 36 | + for (String word : words) { |
| 37 | + if (!STOPWORDS.contains(word) && word.length() > 1) { |
| 38 | + wordFreq.put(word, wordFreq.getOrDefault(word, 0) + 1); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Score sentences |
| 44 | + Map<String, Integer> sentenceScores = new HashMap<>(); |
| 45 | + for (String sentence : sentences) { |
| 46 | + int score = 0; |
| 47 | + for (String word : sentence.toLowerCase().split("\\W+")) { |
| 48 | + score += wordFreq.getOrDefault(word, 0); |
| 49 | + } |
| 50 | + sentenceScores.put(sentence, score); |
| 51 | + } |
| 52 | + |
| 53 | + // Pick top sentences |
| 54 | + List<String> summarySentences = sentenceScores.entrySet().stream() |
| 55 | + .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) |
| 56 | + .limit(numSentences) |
| 57 | + .map(Map.Entry::getKey) |
| 58 | + .collect(Collectors.toList()); |
| 59 | + |
| 60 | + return String.join(" ", summarySentences); |
| 61 | + } |
| 62 | +} |
0 commit comments