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.
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!
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...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: 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;   ...
hi,I have this C++ program,can someone add few comments with explanation what is the logic and...
hi,I have this C++ program,can someone add few comments with explanation what is the logic and what is what?thank you I m total beginner #include <iostream> using namespace std; int ArraySum(int MyArray[], int size){ int* p = MyArray; int sum = 0; while(p<MyArray+size){ sum += *p; p++; } return sum; } int main() { int MyArray[10] = {4, 0, 453, 1029, 44, 67, 111, 887, 4003, 1002}; cout<<ArraySum(MyArray,10); return 0; }
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803...
Please write code in java ASAP and add comments too, will be really appreciated. Thanks CSCI203/CSCI803 This assignment involves extension to the single source-single destination shortest path problem. The Program Your program should: 1. Read the name of a text file from the console. (Not the command line) 2. Read an undirected graph from the file. 3. Find the shortest path between the start and goal vertices specified in the file. 4. Print out the vertices on the path, in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT