-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.java
174 lines (136 loc) · 4.44 KB
/
GUI.java
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
/*
Java Assignment 2nd Year
Program description: This program receives text from a text file and determines whether the text uses formal
or informal language. It works by looking at the amount of formal and informal words used and also some
elements of grammar.
OS: Windows 10
Date: 16/04/2018
James Hughes
*/
package com.languageanalyser;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JPanel;
//This class controls the GUi
public class GUI extends JFrame
{
private JPanel title, main;
private JLabel text1;
private JButton fcButton;
private JRadioButton emailLetterButton;
private JRadioButton textFileButton;
private ButtonGroup fileTypeGroup;
public Line line1;
public static boolean formal = true;
public GUI()
{
super("Language Analyser");
setLayout(new FlowLayout());
title = new JPanel(new FlowLayout());
main = new JPanel(new FlowLayout());
text1 = new JLabel("Language Analyser");
text1.setFont(new Font("Serif", Font.PLAIN, 36 ));
title.add(text1);
fcButton = new JButton("Choose File");
/*when the button is clicked it prompts the user to select a file and creates a line
class to begin processing each line in the file*/
fcButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reset(); //resets variables for multiple uses
line1 = new Line();
System.out.println(line1);
formal = formalityCalc();
System.out.printf("\n\nNumber of Lines: %d\n", Line.getNoOfLines());
System.out.printf("Number of Full Stops: %d\n", Line.getFullStopCount());
System.out.printf("Upper Case Letters: %d\n", Line.getUpperCaseCount());
System.out.printf("Word Count: %f\n", Line.getNoOfWords());
System.out.printf("Number of characters: %f\n", Word.getNoOfLetters());
System.out.printf("Average word length: %f\n", Word.getAvgWordLength());
System.out.printf("Sentences: %f\n", Sentence.getNoOfSentences());
System.out.printf("Bad Words: %f\n", Word.getBadWords());
System.out.printf("formal words: %f\n", Word.getFormalWords());
//checks if file is formal or not and displays appropriate message on screen
if(formal == true)
{
JOptionPane.showMessageDialog(main, "Text is formal");
}
else if(formal == false)
{
JOptionPane.showMessageDialog(main,"Text is informal");
}
}
});
emailLetterButton = new JRadioButton("Email / Letter");
emailLetterButton.setFont(new Font("Serif", Font.PLAIN, 18));
textFileButton = new JRadioButton("Text file");
textFileButton.setFont(new Font("Serif", Font.PLAIN, 18));
fileTypeGroup = new ButtonGroup ();
fileTypeGroup.add(emailLetterButton);
fileTypeGroup.add(textFileButton);
main.add(emailLetterButton);
main.add(textFileButton);
main.add(fcButton);
}
//--------------------------------------------
// Function to display GUI on Screen
//--------------------------------------------
public void displayGUI()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1200, 700);
this.getContentPane().setLayout(new GridLayout(2,1));
this.getContentPane().add(title);
this.getContentPane().add(main);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
//Processes the information taken from the file and decides whether it is formal or not
public boolean formalityCalc()
{
float formality = 0;
if(Word.getBadWords() > Word.getFormalWords())
{
formality--;
}
if(Line.getFullStopCount() >= Line.getUpperCaseCount())
{
formality++;
}
if(Word.getFormalWords() > 0)
{
formality++;
}
if(formality > 0)
{
return true;
}
else
{
return false;
}
}
//Resets the program for multiple uses
public void reset()
{
Line.setNoOfLines(0);
Line.setFullStopCount(0);
Line.setUpperCaseCount(0);
Line.setNoOfWords(0);
Word.setNoOfLetters(0);
Word.setAvgWordLength(0);
Sentence.setNoOfSentences(0);
Word.setBadWords(0);
Word.setFormalWords(0);
}
}