Removed Hashmaps and used ArrayLists instead to create a seperate, two-dimensional Array with which the programm can see how often each word is inside the Text

main
HausCook 2026-11-09 12:51:51 +01:00
parent 6b4bab3d6b
commit 56ba0ed661
3 changed files with 36 additions and 27 deletions

View File

@ -1,3 +1,4 @@
import java.lang.classfile.constantpool.IntegerEntry;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
@ -5,28 +6,19 @@ import java.util.Map;
public class Analyser {
public static int countElements(ArrayList<String> list) {
return list.size();
}
public static HashMap<String, Integer> individualWordCount(ArrayList<String> words) {
HashMap<String, Integer> individualWordsCount = new HashMap<>();
for (String word : words) {
if (individualWordsCount.containsKey(word)) {
int currentCount = individualWordsCount.get(word);
individualWordsCount.put(word, currentCount+1);
} else {
individualWordsCount.put(word,1);
}
public static ArrayList<Integer> countElements(ArrayList<String> uniqueWordList,ArrayList<String> allWordsList ) {
ArrayList<Integer> individualWordCount = new ArrayList<>();
for (int i = 0; i<uniqueWordList.size(); i++) {
individualWordCount.add(0);
}
return individualWordsCount;
for (String word : allWordsList) {
int wordCount = individualWordCount.get(uniqueWordList.indexOf(word));
individualWordCount.set(uniqueWordList.indexOf(word),++wordCount);
}
return individualWordCount;
}
public static ArrayList< Map.Entry< String, Integer > > sortWordsByFrequency(HashMap< String, Integer > wordCounts) {
ArrayList< Map.Entry< String, Integer > > sortedList = new ArrayList<>(wordCounts.entrySet());
sortedList.sort((a, b) -> b.getValue().compareTo(a.getValue()));
return sortedList;
}
}

View File

@ -1,13 +1,20 @@
void main() {
public static void main(String[] args) {
String filePath = "C:\\Users\\Toni G\\IdeaProjects\\GitPeerGroup\\src\\BeeMovieScript.txt";
final String filePath = "C:\\Users\\Toni G\\IdeaProjects\\GitPeerGroup\\src\\BeeMovieScript.txt";
ArrayList<String> fileLines = TextReader.readFileLines(filePath);
ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
HashMap<String, Integer> wordCounts = Analyser.individualWordCount(fileWords);
ArrayList< Map.Entry< String, Integer > > sortedList = Analyser.sortWordsByFrequency(wordCounts);
final ArrayList<String> fileLines = TextReader.readFileLines(filePath);
final ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
final ArrayList<String> uniqueWords = TextReader.uniqueWordList(fileWords);
final ArrayList<Integer> wordCounts = Analyser.countElements(uniqueWords,fileWords);
IO.println(wordCounts);
IO.println(sortedList);
IO.println(uniqueWords);
printWords(uniqueWords, wordCounts);
}
static void printWords(ArrayList<String> uniqueWords, ArrayList<Integer> wordCounts) {
for (String word : uniqueWords) {
System.out.println(word + " = " + wordCounts.get(uniqueWords.indexOf(word)));
}
}

View File

@ -42,4 +42,14 @@ public class TextReader {
}
return words;
}
public static ArrayList<String> uniqueWordList(ArrayList<String> wordList) {
ArrayList<String> uniqueWords = new ArrayList<>();
for(String word : wordList) {
if (!uniqueWords.contains(word)) {
uniqueWords.add(word);
}
}
return uniqueWords;
}
}