-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.java
188 lines (136 loc) · 4.11 KB
/
Line.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
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.io.*;
import java.util.*;
import javax.swing.JFileChooser;
import org.apache.commons.lang3.*;
public class Line
{
private JFileChooser fc = new JFileChooser("C:\\Users\\hughe\\OneDrive\\Documents\\College\\2nd Year\\Object Orientated programming\\Java\\Assignment\\LanguageAnalyser");
private File inFile;
BufferedReader br = null;
FileReader fr = null;
private static int noOfLines;
private static int fullStopCount, upperCaseCount;
private static float noOfWords;
public String line;
//This class reads raw lines from the file and checks punctuation, counts words, lines, fullstops and uppercase letters
public Line()
{
fc.setDialogTitle("Analyser");
if(fc.showOpenDialog(fc) == JFileChooser.APPROVE_OPTION)
{
analyseControl();
}
}
private void analyseControl()
{
/*Used to store file variable of input file
* received from fileChooser
*/
inFile = fc.getSelectedFile();
try
{
fr = new FileReader(inFile);
br = new BufferedReader(fr);
//While the file has next line
while ((line = br.readLine()) != null)
{
lineCount();
fullStopCount();
upperCase();
//adds wordCount of line to overall file wordcount
noOfWords += countWords(line);
//Creates a sentence object to split line into sentences and process it
Sentence sent = new Sentence(line);
}
}
catch (FileNotFoundException e1)
{
System.out.println("Error");
}
catch (IOException e) {
e.printStackTrace();
}
}
public void lineCount()
{
noOfLines ++;
}
//Method to count full stops in line
public void fullStopCount()
{
fullStopCount += StringUtils.countMatches(line, ".");
}
//Method to count uppercase letters in line
public void upperCase()
{
for (char letter : line.toCharArray())
{
if (Character.isUpperCase(letter))
{
upperCaseCount++;
}
}
}
//****** REF This code is taken from https://stackoverflow.com/questions/5864159/count-words-in-a-string-method ******
public static int countWords(String s){
int wordCount = 0;
boolean word = false;
int endOfLine = s.length() - 1;
for (int i = 0; i < s.length(); i++) {
// if the char is a letter, word = true.
if (Character.isLetter(s.charAt(i)) && i != endOfLine) {
word = true;
// if char isn't a letter and there have been letters before,
// counter goes up.
} else if (!Character.isLetter(s.charAt(i)) && word) {
wordCount++;
word = false;
// last word of String; if it doesn't end with a non letter, it
// wouldn't count without this.
} else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
wordCount++;
}
}
return wordCount;
}
//******** End of REF https://stackoverflow.com/questions/5864159/count-words-in-a-string-method *****
//Getters and Setters
public static int getNoOfLines() {
return noOfLines;
}
public static void setNoOfLines(int noOfLines) {
Line.noOfLines = noOfLines;
}
public static int getFullStopCount() {
return fullStopCount;
}
public static void setFullStopCount(int fullStopCount) {
Line.fullStopCount = fullStopCount;
}
public static int getUpperCaseCount() {
return upperCaseCount;
}
public static void setUpperCaseCount(int upperCaseCount) {
Line.upperCaseCount = upperCaseCount;
}
public static float getNoOfWords() {
return noOfWords;
}
public static void setNoOfWords(float noOfWords) {
Line.noOfWords = noOfWords;
}
public String toString()
{
return line ;
}
}