Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 : Implement a hash table into the array in the first method - it will

ID: 3754691 • Letter: Q

Question

Question 1: Implement a hash table into the array in the first method - it will be used to count the frequency of words in a text file and then display the most frequent word and a count of how many times it appeared in the file.

The Code:

import java.io.File;

import java.util.Scanner;

import java.util.Map.Entry;

import java.util.AbstractMap;

import java.util.LinkedList;

public class WordCountLinkedList254 {

        public static Entry<String, Integer> count_ARRAY(String[] tokens) {

               int CAPACITY = 10000;

               String[] words = new String[CAPACITY];

               int[] counts = new int[CAPACITY];

               for (int j = 0; j < tokens.length; j++) {

                       String token = tokens[j];

                       for (int i = 0; i < CAPACITY; i++) {

                               if (words[i] == null) {

                                      words[i] = token;

                                      counts[i] = 1;

                                      break;

                               } else if (words[i].equals(token))

                                      counts[i] = counts[i] + 1;

                       }

               }

               int maxCount = 0;

               String maxWord = "";

               for (int i = 0; i < CAPACITY & words[i] != null; i++) {

                       if (counts[i] > maxCount) {

                               maxWord = words[i];

                               maxCount = counts[i];

                       }

               }

               return new AbstractMap.SimpleEntry<String, Integer>(maxWord, maxCount);

        }

        public static Entry<String, Integer> count_LINKED_LIST(String[] tokens) {

               LinkedList<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>();

               for (int j = 0; j < tokens.length; j++) {

                       String word = tokens[j];

                       boolean found = false;

                       for (int i = 0; i < list.size(); i++) {

                               Entry<String, Integer> e = list.get(i);

                               if (word.equals(e.getKey())) {

                                      e.setValue(e.getValue() + 1);

                                      list.set(i, e);

                                      found = true;

                                      break;

                               }

                       }

                       if (!found)

                               list.add(new AbstractMap.SimpleEntry<String, Integer>(word, 1));

               }

               int maxCount = 0;

               String maxWord = "";

               for (int i = 0; i < list.size(); i++) {

                       int count = list.get(i).getValue();

                       if (count > maxCount) {

                               maxWord = list.get(i).getKey();

                               maxCount = count;

                       }

               }

               return new AbstractMap.SimpleEntry<String, Integer>(maxWord, maxCount);

        }

        static String[] readText(String PATH) throws Exception {

               Scanner doc = new Scanner(new File(PATH)).useDelimiter("[^a-zA-Z]+");

               int length = 0;

               while (doc.hasNext()) {

                       doc.next();

                       length++;

               }

               String[] tokens = new String[length];

               Scanner s = new Scanner(new File(PATH)).useDelimiter("[^a-zA-Z]+");

               length = 0;

               while (s.hasNext()) {

                       tokens[length] = s.next().toLowerCase();

                       length++;

               }

               doc.close();

               return tokens;

        }

        public static void main(String[] args) throws Exception {

               String PATH = "/Users/jianguolu/Dropbox/254/code/dblp1k.txt";

               String[] tokens = readText(PATH);

               long startTime = System.currentTimeMillis();

               Entry<String, Integer> entry = count_LINKED_LIST(tokens);

               long endTime = System.currentTimeMillis();

               String time = String.format("%12d", endTime - startTime);

               System.out.println("time " + time + " " + entry.getKey() + ":" + entry.getValue());

               tokens = readText(PATH);

               startTime = System.currentTimeMillis();

               entry = count_ARRAY(tokens);

               endTime = System.currentTimeMillis();

               time = String.format("%12d", endTime - startTime);

               System.out.println("time " + time + " " + entry.getKey() + ":" + entry.getValue());

        }

}

Explanation / Answer

HASH TABLE :

Hash table contains object as a value ( paremeters).

It is used to check the mapped keys present in the table.

Syntax: Hash_table.contains(object value)

The method returns a boolean value if the passed value is mapped by any of the keys in the Hashtable.

for example: Hashtable<Integer, String > hash_T=new hashtable<Integer, String>();

For checking the word:

System.out.println("Word" + hash_T.contains('word'));

use packages:

import java.util.hashmap;

import java.util.scanner;

import java.util.map;

Flow:

text file :

Frequency of word :

//declare wcount using hashtable

2-->

hashtable map = new hashtable();

if (map.containsKey(word))

{

int count = (Integer) map.get(word);

map.put(word, count + 1);

}

else

{

map.put(word, 1);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote