I am really lost on this assgnment. I dont even know what to do with the string
ID: 3760364 • Letter: I
Question
I am really lost on this assgnment. I dont even know what to do with the string counter as well as how to do the buckets and the three hash functions. part 2 is fine but part 1 is really hard for me. any help or advice would be great and really helpful as this assignment is due today by midnight!!! Thanks
For this assignment you will create a class for counting occurrences of Strings. Effectively, this will be a hashmap from String to int. Do not use java.util.HashMap. Code must compile to be eligible for points. 10 point bonus: write a generic class that maps arbitrary keys to arbitrary values. Late work is not eligible for bonus points. The StringCounter Use the following class as a starting point for yours. Note that you are using the separate chaining collision resolution strategy, with a fixed table size - no rehashing is required. When you construct a new StringCounter you specify the table size and an object to use for hashing the strings. public class StringCounter { public interface HashFunction { int hash(String key); } private class Pair { public String key; public int count; } private LinkedList[] buckets; private HashFunction hasher; @SuppressWarnings("unchecked") public StringCounter(int tableSize, HashFunction hasher) { buckets = (LinkedList[]) new LinkedList[tableSize]; this.hasher = hasher; } public void increment(String word) { } public int getCount(String word) { } public void printStatistics() { } public String mostCommonWord() { } } Part 1 Write a program that uses this StringCounter to process all words in the provided words file (this file is a spell checking dictionary for unix systems), and calls the printStatistics() method to print out the following pieces of information: 1. total number of buckets 2. number of used and unused buckets 3. average size of non-empty buckets 4. maximum bucket size Using a table size of 235886, run this program 3 times, with the 3 different hash functions as listed here. For each, collect the output of printStatistics(): 1. Sum of ascii values of each String character 2. The horner’s rule-based hash function for English words (the one with the 37 in it). 3. A hash function of your own design. Part 2 Use your StringCounter to count the words in the novel “Crime and Punishment”. Your program should print out the most common word in the novel, along with the number of times it appears. It should then prompt the user to enter words, one per line, and print out the number of times that word appeared in the novel. Hints Your keys should be case insensitive. Use the String method toLowerCase(). Use a java.util.Scanner to process the text file. The best way to break this file into individual words is to configure the Scanner with useDelimiter. I suggest using the delimiter “[^a-zA-Z]+”. This will cause the Scanner to discard all non-alphabetic characters.
Explanation / Answer
package com.count; import java.util.LinkedList; import java.util.Scanner; public class StringCounter { public interface HashFunction { int hash(String key); } private class Pair { public String key; public int count; } private LinkedList[] buckets; private HashFunction hasher; @SuppressWarnings("unchecked") public StringCounter(int tableSize, HashFunction hasher) { buckets = (LinkedList[]) new LinkedList[tableSize]; this.hasher = hasher; } public void increment(String word) { } int i,c=0,res; public int getCount(String word) { char ch[]= new char[word.length()]; //in string especially we have to mention the () after length for(i=0;i<word.length();i++) { ch[i]= word.charAt(i); if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) ) c++; } return c; } public void printStatistics() { System.out.println(" count of words is"+ c); } public String mostCommonWord() { return null; } public static void userChoice() { Scanner sc = new Scanner(System.in); System.out.println("Enter a string :"); String s1= sc.nextLine(); //formatting String to char array String s2=s1.replace(" ",""); char [] ch=s2.toCharArray(); int counter=0; //for-loop tocompare first character with the whole character array for(int i=0;i<ch.length;i++) { int count=0; for(int j=0;j<ch.length;j++) { if(ch[i]==ch[j]) count++; //if character is matching with others } if(count>1) { boolean flag=false; //for-loop to check whether the character is already refferenced or not for (int k=i-1;k>=0 ;k-- ) { if(ch[i] == ch[k] ) //if the character is already refferenced flag=true; } if( !flag ) //if(flag==false) counter=counter+1; } } if(counter > 0) //if there is/are any repeating characters System.out.println("Number of repeating charcters in the given string is/are " +counter);
else
System.out.println("Sorry there is/are no repeating charcters in the given string"); } } }
package com.count; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class MAXOccures { public static void main(String[] args) throws FileNotFoundException, IOException { File file = new File("/Users/<username>/Documents/test.txt"); BufferedReader bufferedReader = null; bufferedReader = new BufferedReader(new FileReader(file)); String inputLine = null; Map<String, Integer> map = new HashMap<>(); try { while ((inputLine = bufferedReader.readLine()) != null) { String[] words = inputLine.split("[^a-zA-Z]+"); for (int counter = 0; counter < words.length; counter++) { String key = words[counter].toLowerCase(); // remove .toLowerCase for Case Sensitive result. if (key.length() > 0) { if (map.get(key) == null) { map.put(key, 1); } else { int value = map.get(key).intValue(); value++; map.put(key, value); } } } } Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); System.out.println("Words" + " " + "# of Occurances"); for (Map.Entry<String, Integer> entry : entrySet) { System.out.println(entry.getKey() + " " + entry.getValue()); } List<String> myTopOccurrence = findMaxOccurance(map, 1); System.out.println(" Maixmum Occurance of Word in file: "); for (String result : myTopOccurrence) { System.out.println("==> " + result); } } catch (IOException error) { System.out.println("Invalid File"); } finally { bufferedReader.close(); } } /** * @param map * = All Words in map * @param n * = How many top elements you want to print? If n=1 it will print highest occurrence word. If n=2 it * will print top 2 highest occurrence words. * @returns list of String */ public static List<String> findMaxOccurance(Map<String, Integer> map, int n) { List<StringComparable> l = new ArrayList<>(); for (Map.Entry<String, Integer> entry : map.entrySet()) l.add(new StringComparable(entry.getKey(), entry.getValue())); Collections.sort(l); List<String> list = new ArrayList<>(); for (StringComparable w : l.subList(0, n)) list.add(w.wordFromFile + ":" + w.numberOfOccurrence); return list; } } class StringComparable implements Comparable<StringComparable> { public String wordFromFile; public int numberOfOccurrence; public StringComparable(String wordFromFile, int numberOfOccurrence) { super(); this.wordFromFile = wordFromFile; this.numberOfOccurrence = numberOfOccurrence; } @Override public int compareTo(StringComparable arg0) { int compare = Integer.compare(arg0.numberOfOccurrence, this.numberOfOccurrence); return compare != 0 ? compare : wordFromFile.compareTo(arg0.wordFromFile); } @Override public int hashCode() { final int uniqueNumber = 19; int result = 9; result = uniqueNumber * result + numberOfOccurrence; result = uniqueNumber * result + ((wordFromFile == null) ? 0 : wordFromFile.hashCode()); return result; } @Override public boolean equals(Object Obj) { if (this == Obj) return true; if (Obj == null) return false; if (getClass() != Obj.getClass()) return false; StringComparable other = (StringComparable) Obj; if (numberOfOccurrence != other.numberOfOccurrence) return false; if (wordFromFile == null) { if (other.wordFromFile != null) return false; } else if (!wordFromFile.equals(other.wordFromFile)) return false; return true; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.