There is some debate on influence of Jane Austen on Charlotte Bronte work as a w
ID: 3835495 • 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.eutenberg.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.Explanation / Answer
PROGRAM CODE:
TextAnalysis.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class TextAnalysis {
HashMap<String, Integer> map;
public TextAnalysis() {
map = new HashMap<>();
}
public void readFiles(String filenames[])
{
for(int i=0; i<filenames.length; i++)
{
try {
Scanner reader = new Scanner(new File(filenames[i]));
while(reader.hasNextLine())
{
String tokens[] = reader.nextLine().split(" ");
for(int j=0; j<tokens.length; j++)
{
if(!tokens[j].contains("gutenberg") && !tokens[j].contains(" "))
{
if(map.containsKey(tokens[j]))
map.put(tokens[j], map.get(tokens[j])+1);
else map.put(tokens[j], 1);
}
}
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void printFirst10Words(String message)
{
ArrayList<Integer> values = new ArrayList<>();
ArrayList<Integer> duplicate = new ArrayList<>();
ArrayList<String> keys = new ArrayList<>();
for (Map.Entry<String, Integer> entry : map.entrySet())
{
values.add(entry.getValue());
duplicate.add(entry.getValue());
keys.add(entry.getKey());
}
System.out.println(message);
Collections.sort(duplicate, Collections.reverseOrder());
System.out.println("Word Count");
int counter =0;
for(int i=0; i<map.size(); i++)
{
int value = duplicate.get(i);
int index = values.indexOf(value);
if(keys.get(index) != "\s+")
{
System.out.println(keys.get(index) + " " + values.get(index));
counter++;
}
keys.remove(index);
values.remove(index);
if(counter==10)
break;
}
System.out.println(" ");
}
}
TextAnalysisApp.java
public class TextAnalysisApp {
public static void main(String[] args) {
String JaneAustenBooks[] = {"Emma", "Mansfield_park", "Persuasion", "Pride_and_prejudice", "sense_and_sensibility"};
String CharllotteBooks[] = {"Jane_Eyre", "Shirley", "The_Professor", "villette"};
TextAnalysis janeAusten = new TextAnalysis();
janeAusten.readFiles(JaneAustenBooks);
janeAusten.printFirst10Words("The frequent words of Jane Austen");
TextAnalysis charlotte = new TextAnalysis();
charlotte.readFiles(CharllotteBooks);
charlotte.printFirst10Words("The frequent words of Charlotte Bronte");
}
}
OUTPUT:
The frequent words of Jane Austen
Word Count
the 22397
to 21449
of 19039
and 18792
18264
a 11614
was 9709
in 9590
her 9391
I 8999
The frequent words of Charlotte Bronte
Word Count
25792
the 25289
and 19542
to 16638
I 16394
of 15997
a 14237
in 9342
was 7864
her 6641
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.