-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextSummarizer.java
More file actions
47 lines (38 loc) · 1.89 KB
/
TextSummarizer.java
File metadata and controls
47 lines (38 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
public class TextSummarizer {
public static String summarize(String text, int numSentences) {
// Sentence splitting
String[] sentences = text.split("(?<=[.!?])\\s+");
// Word frequency map
Map<String, Integer> wordFreq = new HashMap<>();
for (String sentence : sentences) {
for (String word : sentence.toLowerCase().replaceAll("[^a-zA-Z ]", "").split("\\s+")) {
if (!word.isEmpty()) {
wordFreq.put(word, wordFreq.getOrDefault(word, 0) + 1);
}
}
}
// Score each sentence
Map<String, Integer> sentenceScores = new HashMap<>();
for (String sentence : sentences) {
int score = 0;
for (String word : sentence.toLowerCase().replaceAll("[^a-zA-Z ]", "").split("\\s+")) {
score += wordFreq.getOrDefault(word, 0);
}
sentenceScores.put(sentence, score);
}
// Sort and pick top sentences
List<Map.Entry<String, Integer>> sorted = new ArrayList<>(sentenceScores.entrySet());
sorted.sort((a, b) -> b.getValue() - a.getValue());
StringBuilder summary = new StringBuilder();
for (int i = 0; i < Math.min(numSentences, sorted.size()); i++) {
summary.append(sorted.get(i).getKey()).append(" ");
}
return summary.toString().trim();
}
public static void main(String[] args) {
String text = "Machine learning is the study of computer algorithms that improve automatically through experience. It is seen as a part of artificial intelligence. Machine learning algorithms build a model based on sample data, known as training data. The field of machine learning is closely related to computational statistics.";
String summary = summarize(text, 2);
System.out.println("Summary:\n" + summary);
}
}