Question

In: Computer Science

Can someone please add clear and concise comments thoroughly explaining each line of code below. Just...

Can someone please add clear and concise comments thoroughly explaining each line of code below. Just need help understanding the code, don't need to modify it.

The purpose of the code is to count the frequency of words in a text file, and return the most frequent word with its count. It uses two algorithms:

Algorithm 1 is based on the data structure LinkedList. It maintains a list for word frequencies. The algorithm runs by scanning every token in the input stream, and increments its corresponding frequency count.

Algorithm 2 uses two arrays to keep track of the counts.

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 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 count_LINKED_LIST(String[] tokens) {
        LinkedList> list = new LinkedList> ();
        for (int j = 0; j < tokens.length; j++) {
            String word = tokens[j];
            boolean found = false;

            /* for (int i = 0; i < list.size(); i++) {
            Entry e = list.get(i);
            if (word.equals(e.getKey())) {
            e.setValue(e.getValue() + 1);
            list.set(i, e);
            found = true;
            break;
               }
            }*/

            int i = 0;
            for (Entry e: list) {
                if (word.equals(e.getKey())) {
                    e.setValue(e.getValue() + 1);
                    list.set(i, e);
                    i++;
                    found = true;
                    break;
                }
            }

            if (!found)
                list.add(new AbstractMap.SimpleEntry (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 entry = count_LINKED_LIST(tokens);
        long endTime = System.currentTimeMillis();
        String time = String.format("%12d", endTime - startTime);
        System.out.println("time\t" + time + "\t" + 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\t" + time + "\t" + entry.getKey() + ":" + entry.getValue());
    }
}

Solutions

Expert Solution

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 count_ARRAY(String[] tokens) {

int CAPACITY = 10000;
// here declaring array to store the 10000 words
String[] words = new String[CAPACITY];
//another integer array to maintain the count of words
int[] counts = new int[CAPACITY];

// iterating the words in the array
// by taking each word
for (int j = 0; j < tokens.length; j++) {
   // pulling out word into token
String token = tokens[j];
// starting from the 0 so compare the selected word with every word in the words list
// so that we will get the count
for (int i = 0; i < CAPACITY; i++) {
   // if words[i] is null than it is first time
   //adding count as 1
if (words[i] == null) {
words[i] = token;
counts[i] = 1;
break;
  
}// if already exist same token increase count at i +1
else if (words[i].equals(token))
counts[i] = counts[i] + 1;
}
}
int maxCount = 0;
String maxWord = "";
// iterating the counts array to find the word which occurs more number of times
for (int i = 0; i < CAPACITY & words[i] != null; i++) {
if (counts[i] > maxCount) {
maxWord = words[i];
maxCount = counts[i];
}
}
// returning the word and its count
return new AbstractMap.SimpleEntry < String, Integer > (maxWord, maxCount);
}
  

// this part the program is not correct. it is wrong
public static Entry count_LINKED_LIST(String[] tokens) {
//created linked list to stored the value
   LinkedList list = new LinkedList ();
  
for (int j = 0; j < tokens.length; j++) {
   // pulling the each token
String word = tokens[j];
boolean found = false;

/* for (int i = 0; i < list.size(); i++) {
Entry e = list.get(i);
if (word.equals(e.getKey())) {
e.setValue(e.getValue() + 1);
list.set(i, e);
found = true;
break;
}
}*/

int i = 0;
//iterating the list
// check if value already exist
for (Entry e: list) {
if (word.equals(e.getKey())) {
e.setValue(e.getValue() + 1);
list.set(i, e);
i++;
found = true;
break;
}
}

if (!found)
list.add(new AbstractMap.SimpleEntry (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);
}
  
// this method reads the words from the file and returns array of words
static String[] readText(String PATH) throws Exception {
Scanner doc = new Scanner(new File(PATH)).useDelimiter("[^a-zA-Z]+");
int length = 0;
//finding the number of words in the file
while (doc.hasNext()) {
doc.next();
length++;
}
  
//creating the array with size number of words
String[] tokens = new String[length];
Scanner s = new Scanner(new File(PATH)).useDelimiter("[^a-zA-Z]+");
length = 0;
// splitting each word
while (s.hasNext()) {
   //putting into the array
tokens[length] = s.next().toLowerCase();
length++;
}
doc.close();
  
//returning array
return tokens;
}
  
public static void main(String[] args) throws Exception {
  
   // path where file exist
String PATH = "/Users/jianguolu/Dropbox/254/code/dblp1k.txt ";
//calling method to get the array of words
String[] tokens = readText(PATH);
// noted starting the timer
long startTime = System.currentTimeMillis();
//calling linked list method
Entry entry = count_LINKED_LIST(tokens);
//noting end time
long endTime = System.currentTimeMillis();
//finding total time it took to compute
String time = String.format("%12d", endTime - startTime);
System.out.println("time\t" + time + "\t" + entry.getKey() + ":" + entry.getValue());
  
//reading tokens into another array
tokens = readText(PATH);
//noting starting time
startTime = System.currentTimeMillis();
///caling array functionality
entry = count_ARRAY(tokens);
//noted end time
endTime = System.currentTimeMillis();
//finding time took to complete functionality
time = String.format("%12d", endTime - startTime);
System.out.println("time\t" + time + "\t" + entry.getKey() + ":" + entry.getValue());
}
}

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

Can someone please write clear and concise comments explaining what each line of code is doing...
Can someone please write clear and concise comments explaining what each line of code is doing for this program in C. I just need help tracing the program and understand what its doing. Thanks #include <stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/wait.h> int join(char *com1[], char *com2[]) {    int p[2], status;    switch (fork()) {        case -1:            perror("1st fork call in join");            exit(3);        case 0:            break;        default:...
****user comments: PLEASE READ INSTRUCTIONS THOROUGHLY AND KEEP THE STRUCTURE OF THE PROGRAM. JUST ADD THE...
****user comments: PLEASE READ INSTRUCTIONS THOROUGHLY AND KEEP THE STRUCTURE OF THE PROGRAM. JUST ADD THE ADDITIVES NEEDED IN ORDER TO SUFFICE THE PROGRAM. PLEASE MAKE SURE IT IS IN C++ AND WORKS! THANK YOU!**** Write a program that uses a structure to store the following information for a particular month at the local airport: Total number of planes that landed Total number of planes that departed Greatest number of planes that landed in a given day that month Least...
Econometrics: Can someone please give a clear, concise and intuitive explanation of the rank of a...
Econometrics: Can someone please give a clear, concise and intuitive explanation of the rank of a matrix and how to find the rank using examples. WITHOUT REFERENCE TO ECHELON FORM.
Assembly language: please comment on every line of code explaining each part. include head comments describing...
Assembly language: please comment on every line of code explaining each part. include head comments describing what your program does. Assignment 3A - A program that adds and subtracts 32-bit numbers After installing the assembler on the computer, enter the following program, save it, assemble it and run it. Do not forget to add a comment with your name in it. You will hand in a listing (e.g., addsum.asm) that should include your name ________________________________________ TITLE Add and Subtract (AddSum.asm)...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
Can you add more comments explaining what this code does? i commented what I know so...
Can you add more comments explaining what this code does? i commented what I know so far #include<stdio.h> #include<pthread.h> #include<semaphore.h> #include<unistd.h> sem_t mutex,writeblock; int data = 0,rcount = 0; int sleepLength = 2; // used to represent work void *reader(void *arg) { int f; f = ((int)arg); sem_wait(&mutex); // decrement by 1 if rcount = rcount + 1; if(rcount==1) sem_wait(&writeblock); sem_post(&mutex); printf("Data read by the reader%d is %d\n",f,data); //shows current reader and data sleep(sleepLength); // 1 second of "work" is...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked with writing a Python program (using linked lists) to keep track of computer equipment. For each piece of equipment, we track its name, purchase date, purchase amount, and quantity on hand. Write a program that completes the following tasks: allow the user to add a piece of equipment to the front of the list; allow the user to update the quantity of a piece...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT