Feinschliff und Strukturierung der User Inputs, als auch verbesserte Eingabe der Datei
parent
56ba0ed661
commit
36fe58154e
|
|
@ -1,8 +1,4 @@
|
||||||
import java.lang.classfile.constantpool.IntegerEntry;
|
|
||||||
import java.lang.reflect.Array;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class Analyser {
|
public class Analyser {
|
||||||
|
|
||||||
|
|
@ -19,6 +15,31 @@ public class Analyser {
|
||||||
return individualWordCount;
|
return individualWordCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String[] frequentWords(ArrayList<String> uniqueWordList, ArrayList<Integer> wordCounts) {
|
||||||
|
ArrayList<String> wordsCopy = new ArrayList<>(uniqueWordList);
|
||||||
|
ArrayList<Integer> countsCopy = new ArrayList<>(wordCounts);
|
||||||
|
|
||||||
|
String[] frequentWords = new String[10];
|
||||||
|
|
||||||
|
for (int i = 0; i < frequentWords.length; i++) {
|
||||||
|
if (wordsCopy.isEmpty()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
int highestFrequency = 0;
|
||||||
|
int highestIndex = 0;
|
||||||
|
|
||||||
|
for (int j = 0; j < wordsCopy.size(); j++) {
|
||||||
|
if (countsCopy.get(j) > highestFrequency) {
|
||||||
|
highestFrequency = countsCopy.get(j);
|
||||||
|
highestIndex = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
frequentWords[i] = wordsCopy.get(highestIndex);
|
||||||
|
countsCopy.remove(highestIndex);
|
||||||
|
wordsCopy.remove(highestIndex);
|
||||||
|
}
|
||||||
|
return frequentWords;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,16 +1,45 @@
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
ArrayList<String> fileLines = null;
|
||||||
|
|
||||||
final String filePath = "C:\\Users\\Toni G\\IdeaProjects\\GitPeerGroup\\src\\BeeMovieScript.txt";
|
while (!TextReader.isRealFile) {
|
||||||
|
String file = IO.readln("Enter the name of the file: ");
|
||||||
|
final String filePath = "C:\\Users\\Toni G\\IdeaProjects\\GitPeerGroup\\src\\Textfiles\\" + file;
|
||||||
|
fileLines = TextReader.readFileLines(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
final ArrayList<String> fileLines = TextReader.readFileLines(filePath);
|
ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
|
||||||
final ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
|
ArrayList<String> uniqueWords = TextReader.uniqueWordList(fileWords);
|
||||||
final ArrayList<String> uniqueWords = TextReader.uniqueWordList(fileWords);
|
ArrayList<Integer> wordCounts = Analyser.countElements(uniqueWords, fileWords);
|
||||||
final ArrayList<Integer> wordCounts = Analyser.countElements(uniqueWords,fileWords);
|
|
||||||
IO.println(wordCounts);
|
|
||||||
IO.println(uniqueWords);
|
|
||||||
printWords(uniqueWords, wordCounts);
|
|
||||||
|
|
||||||
|
boolean running = true;
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
IO.println("\nText Viewer ");
|
||||||
|
IO.println("1: Total Word Count");
|
||||||
|
IO.println("2: All Words and their Frequency");
|
||||||
|
IO.println("3: Top 10 Words");
|
||||||
|
IO.println("4: Exit");
|
||||||
|
|
||||||
|
String choice = IO.readln("Pick a Number [1. |2. |3. |4. ]");
|
||||||
|
|
||||||
|
switch (choice) {
|
||||||
|
case "1" -> {
|
||||||
|
IO.println("Woerter gesamt: " + fileWords.size());
|
||||||
|
IO.println("Unterschiedliche Woerter: " + uniqueWords.size());
|
||||||
|
}
|
||||||
|
case "2" -> printWords(uniqueWords, wordCounts);
|
||||||
|
case "3" -> {
|
||||||
|
String[] frequentWords = Analyser.frequentWords(uniqueWords, wordCounts);
|
||||||
|
printFrequentWords(frequentWords);
|
||||||
|
}
|
||||||
|
case "4" -> running = false;
|
||||||
|
default -> IO.println("Ungueltige Eingabe.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void printWords(ArrayList<String> uniqueWords, ArrayList<Integer> wordCounts) {
|
static void printWords(ArrayList<String> uniqueWords, ArrayList<Integer> wordCounts) {
|
||||||
|
|
@ -18,3 +47,12 @@ static void printWords(ArrayList<String> uniqueWords, ArrayList<Integer> wordCou
|
||||||
System.out.println(word + " = " + wordCounts.get(uniqueWords.indexOf(word)));
|
System.out.println(word + " = " + wordCounts.get(uniqueWords.indexOf(word)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void printFrequentWords(String[] frequentWords) {
|
||||||
|
for (int i = 0; i < frequentWords.length; i++) {
|
||||||
|
if (frequentWords[i] != null) {
|
||||||
|
System.out.println((i + 1) + ". " + frequentWords[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class TextReader {
|
public class TextReader {
|
||||||
|
static boolean isRealFile = false;
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
|
|
||||||
public static ArrayList<String> readFileLines(String filePath) {
|
public static ArrayList<String> readFileLines(String filePath) {
|
||||||
ArrayList<String> lines = new ArrayList<>();
|
ArrayList<String> lines = new ArrayList<>();
|
||||||
File file = new File(filePath);
|
File file = new File(filePath);
|
||||||
|
|
||||||
try (Scanner scanner = new Scanner(file)) {
|
try (Scanner scanner = new Scanner(file)) {
|
||||||
|
isRealFile = true;
|
||||||
while (scanner.hasNextLine()) {
|
while (scanner.hasNextLine()) {
|
||||||
String line = scanner.nextLine();
|
String line = scanner.nextLine();
|
||||||
lines.add(line);
|
lines.add(line);
|
||||||
|
|
@ -25,7 +25,6 @@ public class TextReader {
|
||||||
public static ArrayList<String> linesToWords(ArrayList<String> lines) {
|
public static ArrayList<String> linesToWords(ArrayList<String> lines) {
|
||||||
ArrayList<String> words = new ArrayList<>();
|
ArrayList<String> words = new ArrayList<>();
|
||||||
for (String line : lines) {
|
for (String line : lines) {
|
||||||
|
|
||||||
String[] currentLine = line.split(" ");
|
String[] currentLine = line.split(" ");
|
||||||
|
|
||||||
for (String word : currentLine) {
|
for (String word : currentLine) {
|
||||||
|
|
@ -35,7 +34,6 @@ public class TextReader {
|
||||||
.replace("!", "")
|
.replace("!", "")
|
||||||
.replace("?", "")
|
.replace("?", "")
|
||||||
.replace("\"", "");
|
.replace("\"", "");
|
||||||
|
|
||||||
}
|
}
|
||||||
words.add(word);
|
words.add(word);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue