Question

In: Computer Science

given a input file, parse it and answer the following frequency related questions, using java. -list...

given a input file, parse it and answer the following frequency related questions, using java.

-list the most frequent word(s) in the whole file and its frequency.

-list sentence(s) with the max. number of occurrences of the word “of” in the entire file and also list the corresponding frequency.

program has two arguments;
1st : path to the input text file
2nd : name prefix for the output files

ex.
$ java assgn1 “./input.txt” “output”

outputs: for each question create one single output file. so program should produce 2 output files each time it runs

Solutions

Expert Solution

/////////////////////////////

// compile and run

// javac assign1.java input.txt output

////////////////////////////

import java.io.*;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Map.Entry;

public class assign1 {
public Map<String, Integer> getWordCount(String fileName){
FileInputStream fis = null;
DataInputStream dis = null;
BufferedReader br = null;
Map<String, Integer> wordMap = new HashMap<String, Integer>();
try {
fis = new FileInputStream(fileName);
dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
String line = null;
while((line = br.readLine()) != null){
StringTokenizer st = new StringTokenizer(line, " ");
while(st.hasMoreTokens()){
String tmp = st.nextToken().toLowerCase();
if(wordMap.containsKey(tmp)){
wordMap.put(tmp, wordMap.get(tmp)+1);
} else {
wordMap.put(tmp, 1);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{if(br != null) br.close();}catch(Exception ex){}
}
return wordMap;
}

public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){
Set<Entry<String, Integer>> set = wordMap.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
return list;
}

public static void main(String a[]) throws FileNotFoundException{

   assign1 mdc = new assign1();
Map<String, Integer> wordMap = mdc.getWordCount(a[0]);
List<Entry<String, Integer>> list = mdc.sortByValue(wordMap);

   PrintStream o = new PrintStream(new File(a[1]+".txt"));
   System.setOut(o);
   for(Map.Entry<String, Integer> entry:list){
System.out.println(entry.getKey()+" :\t "+entry.getValue());
}
}
}


Related Solutions

using java, parse a text file to answer the following question: -list sentences with the maximum...
using java, parse a text file to answer the following question: -list sentences with the maximum number of occurences of the word “the” in the whole file and also list the corresponding frequency. (cannot use hash maps) example output: the:3:The day had came to leave before the storm. What hit the back bumper of the car before the window cracked? The classroom doors where shut closed before the students open the project.
Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
Use the frequency and severity distributions given to answer the following questions. Frequency Distribution Severity Distribution...
Use the frequency and severity distributions given to answer the following questions. Frequency Distribution Severity Distribution Losses Probability Loss Amount Probability 0 40% $400 20% 1 35% $700 45% 2 25% $900 35% Mean=? Mean=$710 1. Calculate the mean of the frequency distribution. 2.Determined the total loss distribution. Show all of the possible losses and their associated probabilities. Prove that the probabilities add to one. 3. Calculate the mean of the total loss distribution and show that is equal to...
Given the following input-output table, answer questions a-h. Note that a one word answer is not...
Given the following input-output table, answer questions a-h. Note that a one word answer is not acceptable. For full credit, you must provide explanation for your answers and show how you arrive at them.                              Man Hour Required to Produce Unit of Output                                Country A            Country B                     Good X         3                    12                     Good Y         2                    14             a. Which country has absolute advantage in which good? Why?             b. Which country has comparative advantage in which good? Why?...
Given the following input-output table, answer questions a-h. Note that a one word answer is not...
Given the following input-output table, answer questions a-h. Note that a one word answer is not acceptable. For full credit, you must provide explanation for your answers and show how you arrive at them.                              Man Hour Required to Produce Unit of Output                                Country A            Country B                     Good X         3                    12                     Good Y         2                    14             a. Which country has absolute advantage in which good? Why?             b. Which country has comparative advantage in which good? Why?...
Given the following input-output table, answer questions a-h. Note that a one word answer is not...
Given the following input-output table, answer questions a-h. Note that a one word answer is not acceptable. Provide explanation for your answers and show how you arrive at them.                             Man Hour Required to Produce Unit of Output                               Country A           Country B                    Good X        3 12                    Good Y        2                   14            a. Which country has absolute advantage in which good? Why?            b. Which country has comparative advantage in which good? Why?            c. What is the pre-trade price ratio in each country? Why?            d. What will be...
The following Assignment Questions are related to Java Collection Interfaces (Queue, List, Map, Set, etc), and...
The following Assignment Questions are related to Java Collection Interfaces (Queue, List, Map, Set, etc), and Java IOStream. As mentioned at the beginning of our Collection’s class that the basis of java interfaces is mainly based on "TREES". However, one of these interfaces forms a different "TREE". Describe that particular interface. [1 Marks]. Why do we need to declare a type of object within each interface in the collection packages by "<E>" for example: <Integer, boolean, float, etc>”. [1 Marks]....
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Please answer the following questions using the data in the attached Excel file. You are thinking...
Please answer the following questions using the data in the attached Excel file. You are thinking of investing in Abercrombie and Fitch Co. (ANF). The returns for ANF are embedded in an Excel document below (Source: yahoo.com). 1.      For the investment in ANF that you are considering, for all of 2012 determine the following items: a) the mean return b) the median return c) the standard deviation d) the variance e) the coefficient of variation The weekly rates of return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT