GitPeerGroup/src/Analyser.java

45 lines
1.5 KiB
Java

import java.util.ArrayList;
public class Analyser {
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);
}
for (String word : allWordsList) {
int wordCount = individualWordCount.get(uniqueWordList.indexOf(word));
individualWordCount.set(uniqueWordList.indexOf(word), ++wordCount);
}
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;
}
}