Your task is to count the frequency of words in a text file, and return the most frequent word with its count. (Must use the code below without changing algorithms)
For example, given the following text:
there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.
Based on the example your program should printout the
following along with the milliseconds to finish the
computing:
The most frequent word is "there" with 3 occurrences.
The code is below, it doesn't seem to printout what word is the most frequent and how many occurrences of it there are.
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());
}
}
In: Computer Science
Suppose that in a market the current price is P1, a price higher than the equilibrium price, Pe. Is this market in equilibrium or in a disequilibrium state? Please explain by answering the following questions. ( Word answers only )
1. Is Qd=Qs, Qd>Qs or Qd<Qs at P1? Why?
2. Is there any excess (Xss) -- excess demand or excess supply in this market at P1? Why?
3. Will P1 stay the same or will there be a price adjustment? Why?
In: Economics
Using the South University Online Library, find one qualitative and one quantitative study. Summarize each study using short paragraphs and discuss and evaluate the data collection methods. Make three recommendations to improve each study (a total of six recommendations) and explain why they are logical means of improvement. Based on your summary, evaluation, and explanation of each study, prepare a report in a 3- to 4-page Microsoft Word document.
In: Statistics and Probability
According to Hayek’s article “The Use of Knowledge in Society,” what is the fundamental role of prices in the economy? Why does Hayek use the word marvel when he describes the price mechanism? What happens when prices are determined by the state rather than by the interaction of many buyers and many sellers in the marketplace? Discuss the differences in allocative efficiency between a capitalist and a socialist economic system as they relate to the price mechanism in each system.
In: Economics
If you were a HCN (Host Country National) employed by a multinational corporation, do you think pay should be equal between HCNs and expatriates in equivalent positions? If you were president of your subsidiary in a host country, as a PCN (Parent Country National) your pay is five times higher than the pay for the highest paid HCN (your vice president). What do you think now? in 200 word
In: Economics
In the form of a diagram/flowsheet, show how the concept of mole connects to other concepts that we studied in class, such mass volume, pressure, number of atoms/molecules, etc. Show the mathematical relation used to go from a number of moles to each one of those properties and vice versa. The project can be hand-written (drawn) if your handwriting is neat and easily legible. It can also be drawn using programs such as Microsoft Word.
In: Chemistry
1) In your own words define “discretisation” and “degrees of freedom” in terms of the finite element method.
a) Give the governing equation for a thermal analysis and briefly explain each term. Using this equation, describe the three basic modes of heat transfer and how they are included into an FE analysis.
[Word limit: 200]
b) What do the terms sensitivity, verification, and validation mean in regards to the accuracy of a finite element analysis and how are they utilised?
In: Mechanical Engineering
Module 05 Lab Assignment – Documentation of an Assessment of the Head, Ears, and Eyes
You will perform a history of a head, ear, or eye problem that your instructor has provided you or one that you have experienced and perform an assessment including head, ears, and eyes. You will document your findings, identify actual or potential risks, and submit this in a Word document. please write separate objective, subjective, and potential risk factors.
In: Nursing
In a 100-150 word paragraph, discuss why using research studies from peer reviewed journals is important to the development of knowledge in social work. For example, discuss the advantages of having articles reviewed by other scholars and researchers. Then using a case illustration, describe how articles from peer-reviewed journals can help to inform your social work practice decisions with clients, communities, agencies, and/or social work policies.
In: Psychology
In: Statistics and Probability