/** * Takes the input list of words and processes it, returning a list of {@link Frequency}s. * * <p>This method expects a list of lowercase alphanumeric strings. If the input list is null, an * empty list is returned. * * @param words A list of words. * @return A list of word frequencies, ordered by decreasing frequency. */ public static List<Frequency> computeWordFrequencies(String text, boolean useStopWords) { List<String> words = Modified_Utilities.tokenizeFile(text, useStopWords); for (String st : words) // for every word in words: { // check if the word is in the lstStr(if has appeared once) // if not, add it to lst and lstStr if (findIndex(st) == -1) { lst.add(new Frequency(st, 1)); } // if yes,increment the frequency of that word else { // use lstStr to get the index of the Frequency of that word in the lst int index = findIndex(st); Frequency temp = lst.get(index); temp.incrementFrequency(); lst.set(index, temp); } } Collections.sort(lst, new FrequencyComparator()); // sort the list using a custom Comparator return lst; }
public int compare(Frequency a, Frequency b) { int freComparison = b.getFrequency() - a.getFrequency(); // Firstly, sort the Frequency objects according to their frequency, // if they have the same frequency, sort them by their text return freComparison == 0 ? a.getText().compareTo(b.getText()) : freComparison; }