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

I need to write the code or correct the ones already there to get it work. I sho

ID: 3630190 • Letter: I

Question

I need to write the code or correct the ones already there to get it work.

I should see green bar in the J-unit test

make it simple as possible

It should be short and simple.....****







package code;

import java.util.HashMap;

/**
* *
* Like previous homeworks, this homework involves character by character
* processing, but this time the characters are not coming from a String,
* they are coming from a file.
*
* Because Java's file input classes throw various exceptions, which we do
* not yet know how to handle, I am providing you with a class which deals
* with those exceptions: CharacterFromFileReader. This is an iterator for
* a file. You will be able to use this class during your write-up, but its
* source will be hidden from you.
*
* The pedagogical goals of this homework are for you to:
* 1) experience writing code to pass a suite of unit tests,
* 2) practice writing loops,
* 3) practice working with the primitive type char, in a more involved
* way than in the previous homeworks
* 4) learn how to use a hash table implementation, java.util.HashMap<K,V>
* 5) practice using Iterators
* 6) work with both the char primitive type and the Character wrapper class
* 7) reinforce your individual code-writing skills.
*
* Your task is to define the method(s) in this class so that their functionality matches what
* is described in their comments. The functionality of your definitions will be verified by
* JUnit tests defined the in the tests package. You may run the tests from your local
* workspace.
*
* You may work as much as you want during the week. You will be required to write up your
* solution in recitation next week, starting with an EMPTY WORKSPACE. In other words, you
* must understand the solution well enough that you can write the solution code without any
* external assistance. You will not be permitted to consult any notes while you write up
* your solution in recitation next week.
*
*/
public class WordCounter {

/**
* Reads a file (identified by inputFilePath), one character at a time, and tracks
* word counts using a java.util.HashMap<String,Integer>.
*
* A word is defined as a contiguous sequence of characters that does not contain
* word separator characters, where word separator characters are: ' ', ' ', ' ',
* ',' and '.'
*
* You may use only CharacterFromFileReader to read characters from the input file.
*
* In order to keep your code readable, break your code into several methods. Only
* the wordCounts method may be public; define meaningful private helper methods that
* you call from the wordCounts method.
*
* @param String inputPath the path on the local filesystem to the input file
* @returns a HashMap containing the word->count mappings
*/


public HashMap<String,Integer> wordCounts(String inputPath)
{
return null;
}

}

Explanation / Answer

Dear.......... import java.io.*; import java.util.HashMap; import java.util.Scanner; import java.util.HashSet; public class count
{ public static File[] find_filenames(String path)
{
File folder = new File(path); return folder.listFiles(new TXTFilter());
} public static HashSet<String> read_stopwords(String filename)
{
HashSet<String> stopwords = new HashSet<String>(); try
{
BufferedReader input = new BufferedReader(new FileReader(filename)); try
{
String line = null; while (( line = input.readLine()) != null)
{
stopwords.add(line);
}
} finally
{
input.close();
}
} catch (IOException ex)
{
ex.printStackTrace();
} return stopwords;
} public static void main(String[] args)
{
String count_path = " ";
File macbethfile = new File(count_path + "beth.txt");
HashSet<String> stopwords = read_stopwords(count_path + "english");
BOW mcb_without_stopwords = new BOW(macbethfile, stopwords);
test_1(mcb_without_stopwords);
} private static void test_1(BOW mcb)
{
String[] search = new String[5];
search[0] = "beth";
search[1] = "beth";
search[2] = "BETH";
search[3] = "BETH.";
search[4] = "the"; for (int i=0; i < search.length; i++) {
System.out.println("'" + search[i] + "': " + mcb.get_count(search[i]));
}
}
} class BOW
{ private HashSet<String> stopwords = new HashSet<String>(); private HashMap<String, Integer> wordcounts = new HashMap<String, Integer>(); public BOW(File doc)
{ try { this.process(doc);
} catch(IOException ex)
{
ex.printStackTrace();
}
} public BOW(File doc, HashSet<String> stopwords)
{ try { this.stopwords = stopwords; this.process(doc);
} catch(IOException ex)
{
ex.printStackTrace();
}
} public HashMap<String, Integer> get_wordcounts()
{ return this.wordcounts;
} public void process(File document) throws IOException
{ FileInputStream fstream = new FileInputStream(document);
Scanner scanner = new Scanner(fstream); try
{ while (scanner.hasNext())
{ int count = 0;
String word = scanner.next().trim();
word = lower(word);
word = remove_punctuation(word); if (!(stopwords.contains(word)) && wordcounts.containsKey(word))
{
count = wordcounts.get(word);
} if (!stopwords.contains(word))
{
wordcounts.put(word, count+1);
}
}
} finally {
scanner.close();
fstream.close();
}
} public boolean contains_word(String word)
{ if (this.wordcounts.containsKey(word))
{ return true;
} else
{ return false;
}
} public int get_count(String word)
{ if (wordcounts.containsKey(word))
{
return wordcounts.get(word);
} else { return 0;
}
} private String remove_punctuation(String word)
{ return word.replaceAll("\W","");
} private String lower(String word)
{ return word.toLowerCase();
}
} class TXTFilter implements FilenameFilter
{ public boolean accept(File dir, String name)
{ return (name.endsWith(".txt"));
}
}