In: Computer Science
Java Question
I have a Queue containing String type taking in a text file with an iterator.
I need to use ArrayList and HashMap for freqHowMany to get a frequency table in descending order by the highest frequency words in the text. Any help would be much appreciated and thanks!
final Iterator input = new Scanner(System.in).useDelimiter("(?U)[^\\p{Alpha}0-9']+"); final Queue queueFinal = new CircularFifoQueue<>(wordsLast); while (input.hasNext()) { final String queueWord = input.next(); if (queueWord.length() > minimumLength) { queueFinal.add(queueWord); // the oldest item automatically gets evicted } System.out.println(queueFinal); } } }
EXAMPLE:
Your program prints an updated word cloud for each sufficiently long word read from the standard input.
The program takes up to three positive command-line arguments:
Your program then reads a continuous stream of words, separated by whitespace or other non-word characters, from the standard input. (A word can have letters, numbers, or single quotes.) For each word read, your program prints to standard output, on a single line, a textual representation of the word cloud of the form
The idea is to connect this tool to a streaming data source, such as Twitter, or speech-to-text from a 24-hour news channel, and be able to tell from the word cloud in real time what the current "hot" topics are.
THANKS!
There are two ways to handle this.
1) Use a TreeMap which is sorted by values. In this way, you would get the words in the descending order of frequency.
Sample code:
import java.util.*; public class SortMap { public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) { Comparator<K> valueComparator = new Comparator<K>() { public int compare(K k1, K k2) { return map.get(k2).compareTo(map.get(k1)); } }; Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator); sortedByValues.putAll(map); return sortedByValues; } public static void main(String[] args) {
Queue<String> q = new LinkedList<>(); q.add("a"); q.add("b"); q.add("c"); q.add("b"); q.add("b"); q.add("b"); q.add("b"); q.add("c"); q.add("c"); HashMap<String, Integer> map = new HashMap<>(); while (!q.isEmpty()) { map.put(q.peek(), map.getOrDefault(q.poll(), 0) + 1); }
TreeMap<String, Integer> sortedMap = (TreeMap<String, Integer>) sortByValues(map); sortedMap.keySet().stream().forEach(k -> System.out.println(k+"->"+sortedMap.get(k))); } }
Output:
b->5
c->3
a->1
2) Since the frequency of words is what matters,
we can transfer the HashMap<String, Integer>
to a TreeMap<Integer, String> which sorts
the table in descending order of frequency (i.e.) store the key of
HashMap as value and value of HashMap as key.
Sample code:
import java.util.*; public class SortMap { public static void main(String[] args) { Queue<String> q = new LinkedList<>(); q.add("a"); q.add("b"); q.add("c"); q.add("b"); q.add("b"); q.add("b"); q.add("b"); q.add("c"); q.add("c"); HashMap<String, Integer> map = new HashMap<>(); while (!q.isEmpty()) { map.put(q.peek(), map.getOrDefault(q.poll(), 0) + 1); } TreeMap<Integer, String> sortedMap = new TreeMap<>((v1,v2) -> (v2 - v1)); map.keySet().stream().forEach(k -> sortedMap.put(map.get(k), k)); sortedMap.keySet().stream().forEach(k -> System.out.println(k+"->"+sortedMap.get(k))); } }
Output:
b->5
c->3
a->1