-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAnalyzerUI.java
More file actions
88 lines (70 loc) · 2.86 KB
/
TextAnalyzerUI.java
File metadata and controls
88 lines (70 loc) · 2.86 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package application;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class TextAnalyzerUI extends Application {
private Map<String, Integer> wordFrequencies;
private TextArea outputTextArea;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Text Analyzer");
Button openButton = new Button("Open File");
openButton.setOnAction(e -> analyzeFile(primaryStage));
outputTextArea = new TextArea();
outputTextArea.setEditable(false);
outputTextArea.setPrefHeight(800);
VBox vbox = new VBox(10);
vbox.setPadding(new Insets(10));
vbox.getChildren().addAll(openButton, outputTextArea);
Scene scene = new Scene(vbox, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private void analyzeFile(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
try {
wordFrequencies = countWordOccurrences(file);
displayWordFrequencies();
} catch (IOException e) {
outputTextArea.setText("An error occurred while reading the file: " + e.getMessage());
}
}
}
Map<String, Integer> countWordOccurrences(File file) throws IOException {
Map<String, Integer> wordFrequencies = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] words = line.split("\\s+"); // Split line into words using whitespace as delimiter
for (String word : words) {
wordFrequencies.put(word, wordFrequencies.getOrDefault(word, 0) + 1);
}
}
}
return wordFrequencies;
}
private void displayWordFrequencies() {
List<Map.Entry<String, Integer>> entries = new ArrayList<>(wordFrequencies.entrySet());
entries.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Integer> entry : entries) {
sb.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
outputTextArea.setText(sb.toString());
}
}