This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextAnalyzer.java
122 lines (92 loc) · 2.08 KB
/
TextAnalyzer.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
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
public class TextAnalyzer {
private final String fileName;
private String text;
public TextAnalyzer(String fileName) {
this.fileName = fileName;
try {
this.text = "";
Scanner in = new Scanner(new File(fileName));
while(in.hasNext()) {
this.text += in.next() + " ";
}
in.close();
} catch (Exception e) {
this.text = new String();
}
}
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
public String getFileName() {
return fileName;
}
public String toString() {
String out = fileName;
for (ScoringIndex index : ScoringIndex.values()) {
out += "\n" + getReadabilityScorer(index).toString();
}
return out;
}
public ReadabilityScorer getReadabilityScorer(ScoringIndex index) {
switch (index) {
case FLESCH:
return getFlesch();
case SMOG:
return getSmog();
case GUNNING_FOG:
return getGunningFog();
case AUTOMATED:
return getAutomated();
default:
return null;
}
}
public ReadabilityScorer getFlesch() {
return new Flesch(this);
}
public ReadabilityScorer getSmog() {
return new Smog(this);
}
public ReadabilityScorer getGunningFog() {
return new GunningFog(this);
}
public ReadabilityScorer getAutomated() {
return new Automated(this);
}
public Word[] splitWords() {
return Arrays.stream(text.split("\\W"))
.filter(a -> !a.isBlank())
.map(a -> new Word(a))
.toArray(Word[]::new);
}
public int countSentences() {
int sentences = 0;
for (char c : text.toCharArray()) {
if (c == '.' || c == ';' || c == ':' || c == '?' || c == '!') {
sentences++;
}
}
return sentences;
}
public int countWords() {
return splitWords().length;
}
public int countSyllables() {
int syllables = 0;
for (Word word : splitWords()) {
syllables += word.countSyllables();
}
return syllables;
}
public int countCharacters() {
return Arrays.stream(splitWords())
.map(a -> a.getWord().length())
.reduce(0, (a, b) -> a + b);
}
}