-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadabilityScore.java
More file actions
252 lines (219 loc) · 8.06 KB
/
Copy pathReadabilityScore.java
File metadata and controls
252 lines (219 loc) · 8.06 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
package readability;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Objects;
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
private static final Analyser analyser = new Analyser();
public static void main(String[] args) {
String fileName = args[0];
analyser.analyse(Objects.requireNonNull(sentenceReader(fileName)));
System.out.println("Words: " + analyser.getWordNum());
System.out.println("Sentences: " + analyser.getSentenceNum());
System.out.println("Characters: " + analyser.getCharacterNum());
System.out.println("Syllables: " + analyser.getSyllableNum());
System.out.println("Polysyllables: " + analyser.getPolysyllableNum());
System.out.print("Enter the score you want to calculate (ARI, FK, SMOG, CL, all): ");
String option = scanner.nextLine();
System.out.println();
displayScore(option);
}
private static String[] sentenceReader(String fileName) {
File file = new File(fileName);
try (Scanner reader = new Scanner(file)) {
StringBuilder sb = new StringBuilder();
while (reader.hasNext()) {
sb.append(reader.nextLine().toLowerCase());
}
if ('.' != sb.charAt(sb.length() - 1)) {
analyser.setEndWithDot(false);
}
return sb.toString().split("[\\.\\?!]\\s*");
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
return null;
}
private static void displayScore(String option) {
switch (option) {
case "ARI":
double scoreARI = analyser.getScoreARI();
System.out.printf("Automated Readability Index: %.2f ", scoreARI);
System.out.println("(about " + getAge(scoreARI) + " year olds).");
break;
case "FK":
double scoreFK = analyser.getScoreFK();
System.out.printf("Flesch-Kincaid readability tests: %.2f ", scoreFK);
System.out.println("(about " + getAge(scoreFK) + " year olds).");
break;
case "SMOG":
double scoreSMOG = analyser.getScoreSMOG();
System.out.printf("Simple Measure of Gobbledgook: %.2f ", scoreSMOG);
System.out.println("(about " + getAge(scoreSMOG) + " year olds).");
break;
case "CL":
double scoreCL = analyser.getScoreCL();
System.out.printf("Coleman-Liau index: %.2f ", scoreCL);
System.out.println("(about " + getAge(scoreCL) + " year olds).");
break;
case "all":
double scoreA = analyser.getScoreARI();
int ageA = getAge(scoreA);
System.out.printf("Automated Readability Index: %.2f ", scoreA);
System.out.println("(about " + ageA + " year olds).");
double scoreF = analyser.getScoreFK();
int ageF = getAge(scoreF);
System.out.printf("Flesch–Kincaid readability tests: %.2f ", scoreF);
System.out.println("(about " + ageF + " year olds).");
double scoreS = analyser.getScoreSMOG();
int ageS = getAge(scoreS);
System.out.printf("Simple Measure of Gobbledygook: %.2f ", scoreS);
System.out.println("(about " + ageS + " year olds).");
double scoreC = analyser.getScoreCL();
int ageC = getAge(scoreC);
System.out.printf("Coleman–Liau index: %.2f ", scoreC);
System.out.println("(about " + ageC + " year olds).");
double avgAge = (ageA + ageF + ageS + ageC) / 4.0;
System.out.println();
System.out.printf("This text should be understood in average by %.2f year olds.\n", avgAge);
break;
}
}
private static int getAge(double indexScore) {
int score = Integer.parseInt(String.valueOf(Math.round(indexScore)));
return Objects.requireNonNull(ReadabilityLevel.findByScore(score)).age;
}
}
class Analyser {
private int characterNum = 0;
private int wordNum = 0;
private int sentenceNum = 0;
private boolean endWithDot = true;
private int syllableNum = 0;
private int polysyllableNum = 0;
private double scoreARI = 0;
private double scoreFK = 0;
private double scoreSMOG = 0;
private double scoreCL = 0;
public void analyse(String[] sentences) {
this.sentenceNum = sentences.length;
for (String sentence : sentences) {
String[] sentenceWords = sentence.split("\\s+");
for (String word : sentenceWords) {
this.characterNum += word.length();
int currentSyllables = getSyllablesOfOneWord(word);
currentSyllables = currentSyllables > 0 ? currentSyllables : 1;
this.syllableNum += currentSyllables;
if (currentSyllables > 2) {
this.polysyllableNum += 1;
}
}
this.wordNum += sentenceWords.length;
if ("".equals(sentenceWords[0])) {
this.wordNum -= 1;
}
}
this.characterNum += this.sentenceNum;
if (!this.endWithDot) {
this.characterNum -= 1;
}
calARI();
calFK();
calSMOG();
calCL();
}
private int getSyllablesOfOneWord(String word) {
String vowels = "aeiouy";
int syllables = 0;
boolean flag = false;
for (int i = 0; i < word.length(); i++) {
char currentChar = word.charAt(i);
if (i == word.length() - 1 && word.charAt(i) == 'e') {
break; // if last char is 'e' it is not counted
}
// the below if else ensure more than one consecutive vowels are counted as one syllable
if (vowels.indexOf(currentChar) >= 0) {
if (!flag) {
syllables++;
}
flag = true;
} else {
flag = false;
}
}
return syllables;
}
private void calARI() {
this.scoreARI = 4.71 * characterNum / wordNum + 0.5 * wordNum / sentenceNum - 21.43;
}
private void calFK() {
this.scoreFK = 0.39 * wordNum / sentenceNum + 11.8 * syllableNum / wordNum - 15.59;
}
private void calSMOG() {
this.scoreSMOG = 1.043 * Math.sqrt((double) polysyllableNum * 30 / sentenceNum) + 3.1291;
}
private void calCL() {
this.scoreCL = (0.0588 * characterNum - 0.296 * sentenceNum) * 100 / wordNum - 15.8;
}
public int getWordNum() {
return wordNum;
}
public int getSentenceNum() {
return sentenceNum;
}
public int getCharacterNum() {
return characterNum;
}
public int getSyllableNum() {
return syllableNum;
}
public int getPolysyllableNum() {
return polysyllableNum;
}
public void setEndWithDot(boolean dot) {
endWithDot = dot;
}
public double getScoreARI() {
return scoreARI;
}
public double getScoreFK() {
return scoreFK;
}
public double getScoreSMOG() {
return scoreSMOG;
}
public double getScoreCL() {
return scoreCL;
}
}
enum ReadabilityLevel {
KINDERGARTEN(1, 6),
FIRST_SECOND(2, 7),
THIRD(3, 9),
FOURTH(4, 10),
FIFTH(5, 11),
SIXTH(6, 12),
SEVENTH(7, 13),
EIGHTH(8, 14),
NINTH(9, 15),
TENTH(10, 16),
ELEVENTH(11, 17),
TWELFTH(12, 18),
COLLEGE(13, 24),
PROFESSOR(14, -1);
int score;
int age;
ReadabilityLevel(int score, int age) {
this.score = score;
this.age = age;
}
public static ReadabilityLevel findByScore(int score) {
for (ReadabilityLevel value: values()) {
if (score == value.score) {
return value;
}
}
return null;
}
}