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

Do writings by individual authors have statistical signatures? They certainly do

ID: 3854131 • Letter: D

Question

Do writings by individual authors have statistical signatures? They certainly do, and while such signatures say little about an author's art, they can say something about literary styles of an era, and can even help clarify historical controversies about authorship. Statistical studies, for example, have shown that the Illiad and the Odyssey were not written by a single individual.

For this assignment you are to create a program that analyzes text files -- novels perhaps, or newspaper articles -- and produces two statistics about these texts: word size frequency, and average sentence length.

In particular, you should write a two class application that produces such statistics. Your classes should be called WordMap.java, and WordMapDriver.java. The driver class should read in the name of a file that holds a text, and then provide an analysis for that text. Here is a sample:


> java WordMapDriver
enter name of a file
Analyzed text: /Users/moll/CS121/AliceInWonderland.txt
words of length 1: 7.257%
words of length 2: 14.921%
words of length 3: 24.073%
words of length 4: 20.847%
words of length 5: 12.769%
words of length 6: 7.374%
words of length 7: 6.082%
words of length 8: 3.012%
words of length 9: 1.812%
words of length 10: 0.820%
words of length 11: 0.501%
words of length 12: 0.236%
words of length 13: 0.134%
words of length 14: 0.083%
words of length 15 or larger: 0.001%
average sentence length: 16.917


Your job, then, is to code a solution to this problem, and provide these two statistics - word size percentage, and average sentence length (thus in the example given, 7.257 percent of the words are of length 1, 14.921 percent of the words are of length 2, and so forth, and the average sentence length is 16.917 words).

You can obtain interesting sample texts by, for example, visiting the Gutenberg foundation website (Gutenberg.org), and downloading books from there.

Tips

You should read external files by extending the Echo class from Chapter 10.

An easy and acceptable way to calculate the average length of sentences: count the number of words, count the number of end-of-sentence markers -- !,.,?, then divide the first by the second. Thus if a text has 21 words and 2 periods and a question mark, then its average sentence length is 7.

Show percentages using printf, as described in Chapter 5 of the text. Precision: as above in the example, 3 places to right of decimal point. To include a % symbol in a format string, include two percent symbols. The control character generates a carriage return. This statement:


prints


and then advances to the next line.


Additional Requirements

You must use a try/catch harness for your WordMapDriver code.

You must use The String method split to extract the individual words on each line of the text you are examining. In addition to the space symbol, use these characters as delimiters: ,.!?;:

You must comment your classes: add a one line comment for every method and for every instance variable. This comment should clearly state the role of that Java constituent.

Below is the Echo Class from Chp10:

Explanation / Answer

import java.util.Scanner;
import java.io.*;

public class Echo {

String fileName; // external file name
Scanner scan; // Scanner object for reading from external file

public Echo(String f) throws IOException {
fileName = f;
scan = new Scanner(new FileReader(fileName));
}

public void readLines() { // reads lines, hands each to processLine
while (scan.hasNext()) {
processLine(scan.nextLine());
}
scan.close();
}

public void processLine(String line) { // does the real processing work
System.out.println(line);
}
}

--------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.*;
import java.io.*;

public class WordMap extends Echo {
int sentencecount = 0;
int wordcount = 0;
int charcount = 0;
//creates an array that will be used to count average word length
double[] wlength = new double[15];
int fifteenless = 0;
int fifteenplus = 0;

public WordMap(String word) throws IOException {
super(word);
}

public void processLine(String line) //creates array for word
{
String[] words = line.split("\s+");
for (int a = 0; a < words.length; a++) {
words[a] = words[a].replaceAll("[\w]", "");
}

//counts words
wordcount = words.length;

//counts sentences by searching for sentence enders
for (int c = 0; c < line.length(); c++) {
if ((line.charAt(c) == '.') || (line.charAt(c) == '!') || (line.charAt(c) == '?'));
sentencecount++;
}

//if wordlength matches, adds one to wlength at d and to words less than 15 chars
for (int d = 1; d < 15; d++) {
for (int g = 0; g < words.length; g++) {
String temp = words[g];
if (d == temp.length()) {
wlength[d]++;
fifteenless++;
}
}
}
}

public void calculatePercentages() {
{//calculate percentages with words of 15 or more characters
fifteenplus = wordcount - fifteenless;
fifteenplus = fifteenplus / wordcount;
fifteenplus = fifteenplus * 100;
}

//calculate percentages for words with 1-14 characters   
for (int h = 1; h < 15; h++) {
wlength[h] /= wordcount;
wlength[h] *= 100;
}
}

public void reportLength() {
//prints out average sentence length
double length = (double)wordcount / sentencecount;
System.out.printf("average sentence length: " + "%5.3f", length);
}

public void getWordLength() {
for (int i = 1; i < 15; i++) {
System.out.printf("words of length " + i + ": %5.3f%% ", wlength[i]);
}
System.out.printf("words of 15 or more characters length: " + "%5.3f%% ", fifteenplus);
}
}

----------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.*;

public class WordMapDriver {
public static void main(String[] args) {
try {
String fileName;
Scanner textfile = new Scanner(System.in);
System.out.println("enter name of file");
fileName = textfile.nextLine(); //scans in file
WordMap k = new WordMap(fileName);//creates wordmap object
k.readLines(); //processes
k.calculatePercentages();
k.getWordLength(); //reports all wordlength percentages
k.reportLength(); //prints out average sentence length
} catch (Exception e) //catches the ioexception
{
e.printStackTrace();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote