There is some debate on influence of Jane Austen on Charlotte Bronte work as a w
ID: 3859353 • Letter: T
Question
There is some debate on influence of Jane Austen on Charlotte Bronte work as a writer (and in general on all three Bronte sisters'). If you are interested in finding more, feel free to google for their works and the debate. For this exercise, using the publicly available books on Project Gutenberg (http: //www.gutenberg.org), you are asked to find the top 10 words and number of times they occur in books by Charlotte Bronte but not used by Jane Austen. To simply this exercise, we will only use the following books: HashMap (or HashTree) Java Collection will come handy for finding and keeping the count of words. You are only allowed to use Java Collections as described in Chapter 11 of our class textbook. To receive full credit, submit the following: Java source code file(s) Nicely formatted report containing a table of top 10 words found in Charlotte Bronte's books and not in Jane Austen's and their counts, and a summary of your insights from this exercise including how this type of analysis can be useful and any suggestions for improvements. Don't submit the text files for the books!Explanation / Answer
mport java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Comparator;
import java.util.LinkedList;
public class WordCounter{
//this map store word and its count
static HashMap<String, Integer> wordCountMapJane = new HashMap<String, Integer>();
static HashMap<String, Integer> wordCountMapCharlotte = new HashMap<String, Integer>();
//this function count words and add to word map
public static void countWords(String filename,String author){
BufferedReader buffReader = null;
try
{
buffReader = new BufferedReader(new FileReader(filename));
String currentLine = buffReader.readLine();
while (currentLine != null)
{
String[] words = currentLine.toLowerCase().split(" ");
for (String word : words)
{
if(word.length()!=0)
{
if(author.compareToIgnoreCase("jane")==0){ //if author is jane add to wordCountMapJane else to wordCountMapCharlotte
if(wordCountMapJane.containsKey(word))
{
wordCountMapJane.put(word,wordCountMapJane.get(word)+1);
}
else
{
wordCountMapJane.put(word, 1);
}
}else{
if(wordCountMapCharlotte.containsKey(word))
{
wordCountMapCharlotte.put(word,wordCountMapCharlotte.get(word)+1);
}
else
{
wordCountMapCharlotte.put(word, 1);
}
}
}
}
currentLine = buffReader.readLine();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
//this function sort word map and return a sorted list by word count
public static List<Map.Entry<String,Integer>> sortMapByCount(HashMap<String,Integer> map){
List<Map.Entry<String, Integer>> list =
new LinkedList<Map.Entry<String, Integer>>(map.entrySet());//conver to list
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {//sort in ascending order
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
return list;
}
public static void main(String []args){
//count words in janes works
countWords("PrideAndPrejudice.txt","jane");
countWords("Emma.txt","jane");
countWords("SenseAndSensibility.txt","jane");
countWords("Persuasion.txt","jane");
countWords("MansfieldPark.txt","jane");
//count words in janes works
countWords("JaneEyre.txt","charlotte");
countWords("Villette.txt","charlotte");
countWords("Shirley.txt","charlotte");
countWords("TheOfficer.txt","charlotte");
//sort words from charlotte works
List<Map.Entry<String,Integer>> sortedListCharlotte=sortMapByCount(wordCountMapCharlotte);
//print top 10 words by count from charlotte works but not in jane austen works
System.out.println("Top 10 Words occured in Charlotte Bosten Works but not used by Jane Austen: ");
int i=sortedListCharlotte.size()-1;//start from reverse (most occurred word)
int wordsFound=0;
while(wordsFound<10&&i>=0){
Map.Entry<String, Integer> entry = sortedListCharlotte.get(i);
i--;
if(!wordCountMapJane.containsKey(entry.getKey())){
System.out.println(entry.getKey()+" "+entry.getValue());
wordsFound++;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.