Can someone help me my Hashtable Frequency counter of words I believe my Quadrat
ID: 3793024 • Letter: C
Question
Can someone help me my Hashtable Frequency counter of words
I believe my Quadratic probing is correct but can someone please check it for me espically my contains method and I need to use my Quadartatic probing for a word Frequency counter in the next part can someone help because it's not working for me all the words also must be descending order and if theres a tie pick the one that comes first alphabet wise also can someone check my Map to tree map i cant put the
Hashtable with Quadratic Probing Implement a Java class of hashtables using quadratic probing for collision resolution. The objects to be stored in the table are key-value pairs The keys are strings, and the values are integers The table sizes, m, are prime numbers. The first size should be 1031. Let n be the number of items in the table. When n m/2, you use the technique of dynamic arrays to enlarge the table You want to approximately double the table size but keep to the primes Use these primes: 2063 127, 8263, 16529, and 33071. You will not need larger primes for the purpose of this homework You may use Java String hashCode() as the hash function. The following is what you do for collision resolution. Let b be the hash value modulo m. If bucket b is occupied, you probe (b+12) m, (b-+22) m, (b- 32) m (b- 2)2) m, and stop as soon as you find an empty bucket. As long as n is kept less than m you will find an empty bucket by the end of the probing Use this page https docs oracle com avase/8/docs/api ava/util/Hashtable .html as a template for your class, but you do not need to implement all the methods. Implement as many as necessary to accomplish the following task 2 word Frequencies in a Text File Implement a Java program that counts word frequencies in a text file. Use a hashtable to store the data the words are the keys, and their frequencies are the values. The output of the program is the complete list of all the words and their frequencies in descending order of frequencies when two words have the same frequency, output them by alphabetical order. Each line of output consists of a word, a tab, and the frequency A sample input is "The Tragedy of Hamlet, Prince of Denmark", in the file Hamlet.txt. When processing the text, you should keep just the words, and discard all punctuation marks, digits, and so on. You also need to turn all upper case letters to lower cases.Explanation / Answer
QuadraticProbing.java :
package org.chegg;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
public class QuadraticProbing {
public static void main(String[] args) {
SortedMap<String,Integer> mp = new TreeMap<String, Integer>();
try{
FileReader fr = new FileReader("D:\ShareIt\4-2\Chegg\data.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
while((line = br.readLine())!=null){
String a[]= line.split(" ");
for(int i=0;i<a.length;i++){
String word = a[i];
if(mp.containsKey(word)){
int count = mp.get(word) + 1 ;
mp.put(word,count);
}
else
mp.put(word,1);
}
}
}
catch(Exception e){
e.printStackTrace();
}
Set<Entry<String,Integer>> st = mp.entrySet();
List<Entry<String,Integer>> li = new ArrayList<Map.Entry<String,Integer>>(st);
Collections.sort(li,new WordComparator());
Iterator<Entry<String,Integer>> ie = li.iterator();
while(ie.hasNext()){
Entry<String,Integer> en = ie.next();
System.out.println(en.getKey() + ":"+en.getValue());
}
}
}
WordComparator.java :
package org.chegg;
import java.util.Comparator;
import java.util.Map.Entry;
public class WordComparator implements Comparator<Entry<String,Integer>> {
@Override
public int compare(Entry<String, Integer> e1, Entry<String, Integer> e2) {
if(e1.getValue() > e2.getValue())
return -1;
else if(e1.getValue() < e2.getValue())
return 1;
return 0;
}
}
Input text File (data.txt) :
hello hai sam hello mama lakshmna
rao kanakala come on mama hai
kanakala lakshman rao kanakala
Output:
kanakala:3
hai:2
hello:2
mama:2
rao:2
come:1
lakshman:1
lakshmna:1
on:1
sam:1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.