-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCounting.java
More file actions
105 lines (90 loc) · 3.49 KB
/
Copy pathWordCounting.java
File metadata and controls
105 lines (90 loc) · 3.49 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class WordCounting extends JFrame {
private JTextField textField;
private JTextArea textArea;
private JLabel wordCountLabel;
private JButton countButton;
public WordCounting() {
setTitle("Word Count GUI");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
textField = new JTextField(30);
textArea = new JTextArea(15, 30);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
wordCountLabel = new JLabel("Word Count: ");
countButton = new JButton("Count Words");
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("Enter Text or File Path:"));
inputPanel.add(textField);
inputPanel.add(countButton);
add(inputPanel, BorderLayout.NORTH);
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(wordCountLabel, BorderLayout.SOUTH);
countButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
countWords();
}
});
}
private void countWords() {
String inputText = textField.getText();
String resultText = "";
int wordCount = 0;
Map<String, Integer> wordFrequency = new HashMap<>();
if (!inputText.isEmpty()) {
if (inputText.endsWith(".txt")) {
try {
inputText = readTextFromFile(inputText);
} catch (IOException e) {
resultText = "Error reading the file.";
}
}
String[] words = inputText.split("[\\s.,;?!]+");
wordCount = words.length;
wordFrequency = countWordFrequency(words);
resultText = "Total word count: " + wordCount + "\n";
resultText += "Word frequency: \n";
for (Map.Entry<String, Integer> entry : wordFrequency.entrySet()) {
resultText += entry.getKey() + ": " + entry.getValue() + "\n";
}
} else {
resultText = "Please enter text or a valid file path.";
}
textArea.setText(resultText);
wordCountLabel.setText("Word Count: " + wordCount);
}
private String readTextFromFile(String filePath) throws IOException {
StringBuilder text = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
text.append(line).append(" ");
}
}
return text.toString();
}
private Map<String, Integer> countWordFrequency(String[] words) {
Map<String, Integer> wordFrequency = new HashMap<>();
for (String word : words) {
wordFrequency.put(word, wordFrequency.getOrDefault(word, 0) + 1);
}
return wordFrequency;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
WordCounting wordCountGUI = new WordCounting();
wordCountGUI.setVisible(true);
});
}
}