-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAnalysisTool.java
More file actions
79 lines (69 loc) · 2.73 KB
/
TextAnalysisTool.java
File metadata and controls
79 lines (69 loc) · 2.73 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
import java.io.*;
import java.util.*;
public class TextAnalysisTool {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("goofy ahh uncle productions text anal-izer idek");
System.out.print("enter filename: ");
String filename = scanner.nextLine();
String content = loadFileContent(filename);
if (content == null) {
return;
}
System.out.println("word count: " + countWords(content));
System.out.println("type ze letter or phrase ze u want");
String choice = scanner.nextLine();
if ("letter".equalsIgnoreCase(choice)) {
Map<Character, Integer> frequency = letterFrequencyAnalysis(content);
frequency.forEach((k, v) -> System.out.println(k + ": " + v));
} else if ("phrase".equalsIgnoreCase(choice)) {
System.out.print("enter ze character name");
String character = scanner.nextLine();
int count = countPhrasesByCharacter(content, character);
System.out.println(character + " spoke " + count + " phrases.");
} else {
System.out.println("nuh uhhhhh");
}
scanner.close();
}
private static String loadFileContent(String filename) {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
return content.toString();
} catch (IOException e) {
System.out.println("error " + e.getMessage());
return null;
}
}
private static int countWords(String content) {
return content.split("\\s+").length;
}
private static Map<Character, Integer> letterFrequencyAnalysis(String content) {
Map<Character, Integer> frequency = new HashMap<>();
for (char c : content.toLowerCase().toCharArray()) {
if (Character.isLetter(c)) {
frequency.put(c, frequency.getOrDefault(c, 0) + 1);
}
}
return frequency;
}
private static int countPhrasesByCharacter(String content, String character) {
int count = 0;
boolean isCharacterSpeaking = false;
for (String line : content.split("\n")) {
if (line.startsWith(character + ":")) {
isCharacterSpeaking = true;
} else if (line.isEmpty() || line.matches("^[A-Z ]+:")) {
isCharacterSpeaking = false;
}
if (isCharacterSpeaking) {
count++;
}
}
return count;
}
}