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
parent
6b4bab3d6b
commit
56ba0ed661
|
|
@ -1,3 +1,4 @@
|
||||||
|
import java.lang.classfile.constantpool.IntegerEntry;
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
@ -5,28 +6,19 @@ import java.util.Map;
|
||||||
|
|
||||||
public class Analyser {
|
public class Analyser {
|
||||||
|
|
||||||
public static int countElements(ArrayList<String> list) {
|
public static ArrayList<Integer> countElements(ArrayList<String> uniqueWordList,ArrayList<String> allWordsList ) {
|
||||||
return list.size();
|
ArrayList<Integer> individualWordCount = new ArrayList<>();
|
||||||
}
|
for (int i = 0; i<uniqueWordList.size(); i++) {
|
||||||
|
individualWordCount.add(0);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
final ArrayList<String> fileLines = TextReader.readFileLines(filePath);
|
||||||
ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
|
final ArrayList<String> fileWords = TextReader.linesToWords(fileLines);
|
||||||
HashMap<String, Integer> wordCounts = Analyser.individualWordCount(fileWords);
|
final ArrayList<String> uniqueWords = TextReader.uniqueWordList(fileWords);
|
||||||
ArrayList< Map.Entry< String, Integer > > sortedList = Analyser.sortWordsByFrequency(wordCounts);
|
final ArrayList<Integer> wordCounts = Analyser.countElements(uniqueWords,fileWords);
|
||||||
IO.println(wordCounts);
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -42,4 +42,14 @@ public class TextReader {
|
||||||
}
|
}
|
||||||
return words;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue