-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileAnalysis.java
More file actions
464 lines (433 loc) · 16.9 KB
/
Copy pathFileAnalysis.java
File metadata and controls
464 lines (433 loc) · 16.9 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package com.example.FileAnalysis;
// Program to provide simple text analysis on a chosen file
// Filler code by Mr. David
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FileAnalysis {
private final int WIDTH = 600, HEIGHT = 600;
private String content;
// return an array of size 26, where each entry in the array is the
// frequency of the corresponding letter (the first value represents
// the frequency of 'a', the second 'b', and so on
private int[] calcFrequencies() {
int numOfLetters[] = new int[26];
for (int i = 0; i <= content.length()-1; i++){
if (content.charAt(i) <= 'z' && content.charAt(i) >= 'a'){
numOfLetters[content.charAt(i) - 97]++;
}
if (content.charAt(i) <= 'Z' && content.charAt(i) >= 'A'){
numOfLetters[content.charAt(i) - 65]++;
}
}
return numOfLetters;
}
// returns the number of words in the file
private int numWords() {
int numOfWords = 1;
for (int i = 0; i <= content.length()-1; i++){
if (content.charAt(i) == ' '){
numOfWords++;
}
if (content.charAt(i) == ','){
numOfWords++;
}
if (content.charAt(i) == '.' && content.charAt(i-1) == '.'){
numOfWords++;
}
if (content.charAt(i) == '!'){
numOfWords++;
}
if (content.charAt(i) == '?'){
numOfWords++;
}
if (content.charAt(i) == '"'){
numOfWords++;
}
if (content.charAt(i) >= 48 && content.charAt(i) <= 57){
numOfWords++;
}
}
return numOfWords;
}
// returns the number of lines in the file
private int numLines() {
int numOfLines = 1;
for (int i = 0; i <= content.length()-1; i++){
if (content.charAt(i) == '\n'){
numOfLines++;
}
}
return numOfLines;
}
// returns the longest word in the file
private String longestWord() {
int longestLength = 0;
String word = "";
for (int i = 0; i <= content.length() - 1; i++){
for (int j = i + 1; j <= content.length() - 1; j++){
if (!Character.isLetter(content.charAt(j))){
if (j - i - 1 > longestLength){
longestLength = j - i - 1;
word = content.substring(i, j);
}
i = j + 1;
}
}
}
return word;
}
// returns the most common letter in the file
private char mostCommonLetter() {
int numOfLetters[] = new int[26];
int common = numOfLetters[0];
int commonPos = 0;
for (int i = 0; i <= content.length()-1; i++){
if (content.charAt(i) <= 'z' && content.charAt(i) >= 'a'){
numOfLetters[content.charAt(i) - 97]++;
}
if (content.charAt(i) <= 'Z' && content.charAt(i) >= 'A'){
numOfLetters[content.charAt(i) - 65]++;
}
}
for (int i = 0; i <= 25; i++){
if (numOfLetters[i] > common){
common = numOfLetters[i];
commonPos = i;
}
}
return (char)(commonPos + 97);
}
// returns the most common word in the file
private String mostCommonWord() {
int numOfWords = 1;
for (int i = 0; i <= content.length()-1; i++){
if (content.charAt(i) == ' '){
numOfWords++;
}
if (content.charAt(i) == ','){
numOfWords++;
}
if (content.charAt(i) == '.' && content.charAt(i-1) == '.'){
numOfWords++;
}
if (content.charAt(i) == '!'){
numOfWords++;
}
if (content.charAt(i) == '?'){
numOfWords++;
}
if (content.charAt(i) == '"'){
numOfWords++;
}
if (content.charAt(i) >= 48 && content.charAt(i) <= 57){
numOfWords++;
}
}
String a[] = new String[numOfWords * 2];
int pos = 0;
int num = 0;
int max = 0;
String word = "";
for (int i = 0; i <= content.length() - 1; i++){
for (int j = i + 1; j <= content.length() - 1; j++){
if (!Character.isLetter(content.charAt(j))){
a[pos] = content.substring(i, j);
pos++;
i = j + 1;
}
}
}
for (int i = 0; i <= numOfWords - 1; i++){
num = 0;
for (int j = 0; j <= numOfWords - 1; j++){
if (a[i].equals(a[j])){
num++;
}
}
if (num > max && a[i].length() >= 4){
max = num;
word = a[i];
}
}
return word;
}
// returns the ten most common words (of length > 5) in
// the file, in order from most common to least common
private String[] tenMostCommonWords() {
int numOfWords = 1;
for (int i = 0; i <= content.length()-1; i++){
if (content.charAt(i) == ' '){
numOfWords++;
}
if (content.charAt(i) == ','){
numOfWords++;
}
if (content.charAt(i) == '.' && content.charAt(i-1) == '.'){
numOfWords++;
}
if (content.charAt(i) == '!'){
numOfWords++;
}
if (content.charAt(i) == '?'){
numOfWords++;
}
if (content.charAt(i) == '"'){
numOfWords++;
}
if (content.charAt(i) >= 48 && content.charAt(i) <= 57){
numOfWords++;
}
}
String a[] = new String[numOfWords * 2];
int pos = 0;
int num = 0;
int min = 0;
int minPos = 1;
int max[] = new int[10];
boolean noDup = true;
String word[] = new String[10];
for (int i = 0; i <= content.length() - 1; i++){
for (int j = i + 1; j <= content.length() - 1; j++){
if (!Character.isLetter(content.charAt(j))){
a[pos] = content.substring(i, j);
pos++;
i = j + 1;
}
}
}
for (int i = 0; i <= numOfWords - 1; i++){
num = 0;
min = max[0];
minPos = 0;
noDup = true;
for (int j = 0; j <= numOfWords - 1; j++){
if (a[i].equals(a[j])){
num++;
}
}
for (int k = 0; k <= 9; k++){
if (max[k] < min){
min = max[k];
minPos = k;
}
}
for (int k = 0; k <= 9; k++){
if (a[i].equals(word[k])){
noDup = false;
}
}
if (num > min && a[i].length() >= 5 && noDup == true){
word[minPos] = a[i];
max[minPos] = num;
}
}
return word;
}
// returns the longest sentence in the file
private String longestSentence() {
int longestLength = 0;
String sentence = "";
for (int i = 0; i <= content.length() - 1; i++){
for (int j = i + 1; j <= content.length() - 1; j++){
if (content.charAt(j) == '.' || content.charAt(j) == '!' || content.charAt(j) == '?'){
if (j - i - 1 > longestLength){
longestLength = j - i - 1;
sentence = content.substring(i, j);
}
i = j + 1;
}
}
}
return sentence;
}
// returns the percent of characters in the file that are vowels.
// cast the decimal to a string, shorten it to a few decimal
// points, then add a % sign to the end
private String percentVowels() {
String percent = "";
double vowels = 0;
double sum = 0;
int numOfLetters[] = new int[26];
for (int i = 0; i <= content.length()-1; i++){
if (content.charAt(i) <= 'z' && content.charAt(i) >= 'a'){
numOfLetters[content.charAt(i) - 97]++;
}
if (content.charAt(i) <= 'Z' && content.charAt(i) >= 'A'){
numOfLetters[content.charAt(i) - 65]++;
}
}
for (int i = 0; i <= 25; i++){
if (i == 0 || i == 4 || i == 8 || i == 14 || i == 20){
vowels += numOfLetters[i];
}
sum += numOfLetters[i];
}
percent += vowels / sum * 100;
return percent.substring(0, 5) + "%";
}
// ***** STOP HERE ***** //
public FileAnalysis() {
JFileChooser fc = new JFileChooser();
File workingDirectory = new File(System.getProperty("user.dir"));
fc.setCurrentDirectory(workingDirectory);
fc.showOpenDialog(null);
File f = fc.getSelectedFile();
if (f == null)
System.exit(-1);
try {
BufferedReader in = new BufferedReader(new FileReader(f));
for (int letter = in.read(); letter != -1; letter = in.read()) {
content += (char)letter;
}
in.close();
content = content.toLowerCase();
} catch (FileNotFoundException e1) {
System.out.println("File not found :(");
return;
} catch (IOException e1) {
e1.printStackTrace();
}
// the main container
JFrame frame = new JFrame();
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// the inner container
JPanel panel = new JPanel();
BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxlayout);
panel.setBorder(BorderFactory.createTitledBorder("File Analysis"));
// text container
JTextArea displayarea = new JTextArea();
displayarea.setEditable(false);
// create and add listeners to the buttons
JButton freqButton = new JButton("Letter Frequencies");
freqButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String output = "";
int[] freq = calcFrequencies();
output = "\n Letter frequencies in " + f.getName()+": \n";
for (int i = 0; i < freq.length; i++)
output += "\n "+(char)(i+97)+": " +freq[i];
displayarea.setText(displayarea.getText()+"\n"+output+"\n");
}
});
JButton numWordsButton = new JButton("Number of Words in File");
numWordsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String output = "\n Number of words in "+f.getName()+": " + numWords();
displayarea.setText(displayarea.getText()+"\n"+output+"\n");
}
});
JButton numLinesButton = new JButton("Number of Lines in File");
numLinesButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String output = "\n Number of lines in "+f.getName()+": " + numLines();
displayarea.setText(displayarea.getText()+"\n"+output+"\n");
}
});
JButton mostCommonLetterButton = new JButton("Most Common Letter");
mostCommonLetterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String output = "\n Most common letter in "+f.getName()+": "+mostCommonLetter();
displayarea.setText(displayarea.getText()+"\n"+output+"\n");
}
});
JButton mostCommonWordButton = new JButton("Most Common Word");
mostCommonWordButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String output = "\n Most common word in "+f.getName()+": "+mostCommonWord();
displayarea.setText(displayarea.getText()+"\n"+output+"\n");
}
});
JButton tenMostCommonButton = new JButton("10 Most Common Words");
tenMostCommonButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String output = "";
String[] common = tenMostCommonWords();
output += "\n 10 most common words in "+f.getName()+
" (of at least length 5):\n";
for (int i = 0; i < common.length; i++)
output += "\n "+(i+1)+". "+common[i];
displayarea.setText(displayarea.getText()+"\n"+output+"\n");
}
});
JButton longestSentenceButton = new JButton("Longest Sentence");
longestSentenceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String output = "\n Longest sentence in "+f.getName()+":";
output += "\n\n "+longestSentence();
displayarea.setText(displayarea.getText()+"\n"+output+"\n");
}
});
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
displayarea.setText("\n Currently working in "+f.getName()+".");
}
});
JButton percentVowelsButton = new JButton("Percent Vowels");
percentVowelsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String output = "\n Vowels make up "+percentVowels() +
" of letters in "+f.getName()+".";
displayarea.setText(displayarea.getText()+"\n"+output+"\n");
}
});
JButton longestWordButton = new JButton("Longest Word");
longestWordButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String output = "\n Longest Word: "+longestWord();
displayarea.setText(displayarea.getText()+"\n"+output+"\n");
}
});
// add a scroll bar to the text area
JScrollPane scroll = new JScrollPane (displayarea);
scroll.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setPreferredSize(new Dimension(400,475));
panel.add(scroll);
// create three containers for the three button rows
JPanel innerPanel2 = new JPanel(),innerPanel3 = new JPanel(),
innerPanel4 = new JPanel();
innerPanel2.add(freqButton);
innerPanel2.add(numWordsButton);
innerPanel2.add(numLinesButton);
innerPanel3.add(mostCommonLetterButton);
innerPanel3.add(mostCommonWordButton);
innerPanel3.add(tenMostCommonButton);
innerPanel4.add(percentVowelsButton);
innerPanel4.add(longestSentenceButton);
innerPanel4.add(longestWordButton);
innerPanel4.add(clearButton);
panel.add(innerPanel2);
panel.add(innerPanel3);
panel.add(innerPanel4);
// final setup on the frame
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
// beginning text display
displayarea.setText("\n Currently working in "+f.getName()+".");
}
public static void main(String[] args) {
new FileAnalysis();
}
}