Feinschliff und Strukturierung der User Inputs, als auch verbesserte Eingabe der Datei
parent
56ba0ed661
commit
36fe58154e
|
|
@ -1,24 +1,45 @@
|
||||||
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 {
|
||||||
|
|
||||||
public static ArrayList<Integer> countElements(ArrayList<String> uniqueWordList,ArrayList<String> allWordsList ) {
|
public static ArrayList<Integer> countElements(ArrayList<String> uniqueWordList, ArrayList<String> allWordsList) {
|
||||||
ArrayList<Integer> individualWordCount = new ArrayList<>();
|
ArrayList<Integer> individualWordCount = new ArrayList<>();
|
||||||
for (int i = 0; i<uniqueWordList.size(); i++) {
|
for (int i = 0; i < uniqueWordList.size(); i++) {
|
||||||
individualWordCount.add(0);
|
individualWordCount.add(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String word : allWordsList) {
|
for (String word : allWordsList) {
|
||||||
int wordCount = individualWordCount.get(uniqueWordList.indexOf(word));
|
int wordCount = individualWordCount.get(uniqueWordList.indexOf(word));
|
||||||
individualWordCount.set(uniqueWordList.indexOf(word),++wordCount);
|
individualWordCount.set(uniqueWordList.indexOf(word), ++wordCount);
|
||||||
}
|
}
|
||||||
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,20 +1,58 @@
|
||||||
public static void main(String[] args) {
|
import java.util.ArrayList;
|
||||||
|
|
||||||
final String filePath = "C:\\Users\\Toni G\\IdeaProjects\\GitPeerGroup\\src\\BeeMovieScript.txt";
|
public class Main {
|
||||||
|
|
||||||
final ArrayList<String> fileLines = TextReader.readFileLines(filePath);
|
public static void main(String[] args) {
|
||||||
final ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
|
ArrayList<String> fileLines = null;
|
||||||
final ArrayList<String> uniqueWords = TextReader.uniqueWordList(fileWords);
|
|
||||||
final ArrayList<Integer> wordCounts = Analyser.countElements(uniqueWords,fileWords);
|
|
||||||
IO.println(wordCounts);
|
|
||||||
IO.println(uniqueWords);
|
|
||||||
printWords(uniqueWords, wordCounts);
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
|
||||||
|
ArrayList<String> uniqueWords = TextReader.uniqueWordList(fileWords);
|
||||||
|
ArrayList<Integer> wordCounts = Analyser.countElements(uniqueWords, fileWords);
|
||||||
|
|
||||||
static void printWords(ArrayList<String> uniqueWords, ArrayList<Integer> 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) {
|
||||||
for (String word : uniqueWords) {
|
for (String word : uniqueWords) {
|
||||||
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,23 +1,23 @@
|
||||||
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);
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
System.out.println("es gibt keine datei im angegebenen dateipfad: "+filePath);
|
System.out.println("es gibt keine datei im angegebenen dateipfad: " + filePath);
|
||||||
}
|
}
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
@ -45,7 +43,7 @@ public class TextReader {
|
||||||
|
|
||||||
public static ArrayList<String> uniqueWordList(ArrayList<String> wordList) {
|
public static ArrayList<String> uniqueWordList(ArrayList<String> wordList) {
|
||||||
ArrayList<String> uniqueWords = new ArrayList<>();
|
ArrayList<String> uniqueWords = new ArrayList<>();
|
||||||
for(String word : wordList) {
|
for (String word : wordList) {
|
||||||
if (!uniqueWords.contains(word)) {
|
if (!uniqueWords.contains(word)) {
|
||||||
uniqueWords.add(word);
|
uniqueWords.add(word);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue