-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
165 lines (154 loc) · 6.04 KB
/
Main.java
File metadata and controls
165 lines (154 loc) · 6.04 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
package readability;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class Main {
public static int returnYears(double score) {
switch ((int) Math.ceil(score)) {
case 1:
return 6;
case 2:
return 7;
case 3:
return 9;
case 4:
return 10;
case 5:
return 11;
case 6:
return 12;
case 7:
return 13;
case 8:
return 14;
case 9:
return 15;
case 10:
return 16;
case 11:
return 17;
case 12:
return 18;
case 13:
return 24;
case 14:
return 24;
default:
return 0;
}
}
public static String readFileAsString(String fileName) throws IOException {
return new String(Files.readAllBytes(Paths.get(fileName)));
}
public static int automatedReadabilityIndex(double characters, double words, double sentences) {
double score = 4.71 * characters / words + 0.5 * words / sentences - 21.43;
System.out.printf("Automated Readability Index: %.2f (about %d-year-olds).\n", score, returnYears(score));
return returnYears(score);
}
public static int fleschKincardTest(double words, double sentences, double syllables) {
double score = 0.39 * words / sentences + 11.8 * syllables / words - 15.59;
System.out.printf("Flesch–Kincaid readability tests: %.2f (about %d-year-olds).\n", score, returnYears(score));
return returnYears(score);
}
public static int smogIndex(double polysyllables, double sentences) {
double score = 1.043 * Math.sqrt(polysyllables * 30 / sentences) + 3.1291;
System.out.printf("Simple Measure of Gobbledygook: %.2f (about %d-year-olds).\n", score, returnYears(score));
return returnYears(score);
}
public static int colemanLiauIndex(double L, double S) {
double score = 0.0588 * L - 0.296 * S - 15.8;
System.out.printf("Coleman–Liau index: %.2f (about %d-year-olds).\n", score, returnYears(score));
return returnYears(score);
}
public static double calculateCharacters(String userText) {
String[] wordsArr = userText.split("\\s+");
double characters = 0;
for (int i = 0; i < wordsArr.length; i++) {
char[] wordInLetters = wordsArr[i].toCharArray();
characters += wordInLetters.length;
}
return characters;
}
public static double vowels(String words) {
double count = 0;
for (int i = 0; i < words.length(); i++) {
if (words.charAt(i) == 'a') {
count++;
}
}
if (count == 0) {
return 1;
} else {
return count;
}
}
public static double[] calculateSyllables(String[] wordsArr) {
double[] syllables = new double[2];
String res = "";
for (String s : wordsArr) {
if (s.endsWith("e") || s.endsWith("E")) {
res = s.substring(0 , s.length() - 1);
} else {
res = s;
}
if (vowels(res.replaceAll("[aeiuoyAEIOUY]+", "a")) > 2) {
syllables[1]++;
}
syllables[0] += vowels(res.replaceAll("[aeiuoyAEIOUY]+", "a"));
}
return syllables;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String userText = "";
try {
userText = readFileAsString(args[0]);
} catch (Exception e) {
System.out.print("Error");
}
String[] wordsArr = userText.split("\\s+");
double sentences = userText.split("\\.|\\?|!").length;
double words = wordsArr.length;
double characters = calculateCharacters(userText);
double syllables = calculateSyllables(wordsArr)[0];
double polysyllables = calculateSyllables(wordsArr)[1];
System.out.println("The text is:");
System.out.println(userText);
System.out.printf("Words: %.2f\n", words);
System.out.printf("Sentences: %.2f\n", sentences);
System.out.printf("Characters: %.2f\n", characters);
System.out.printf("Syllables: %.2f\n", syllables);
System.out.printf("Polysyllables: %.2f\n", polysyllables);
System.out.println("Enter the score you want to calculate (ARI, FK, SMOG, CL, all): ");
String index = scanner.next();
int k = 0;
switch (index) {
case "ARI":
k = automatedReadabilityIndex(characters, words, sentences);
System.out.printf("This text should be understood in average by %d-year-olds.", k);
break;
case "FK":
k = fleschKincardTest(words, sentences, syllables);
System.out.printf("This text should be understood in average by %d-year-olds.", k);
break;
case "SMOG":
k = smogIndex(polysyllables, sentences);
System.out.printf("This text should be understood in average by %d-year-olds.", k);
break;
case "CL":
k = colemanLiauIndex(characters / words * 100, sentences / words * 100);
System.out.printf("This text should be understood in average by %d-year-olds.", k);
break;
case "all":
k += automatedReadabilityIndex(characters, words, sentences);
k += fleschKincardTest(words, sentences, syllables);
k += smogIndex(polysyllables, sentences);
k += colemanLiauIndex(characters / words * 100, sentences / words * 100);
System.out.printf("This text should be understood in average by %d-year-olds.", k);
break;
default:
break;
}
}
}