Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(IN JAVA) - NEED HELP FIXING CODE In this you are to implement a simplified version of the Flesch reading-ease test. Create a program called

(IN JAVA) - NEED HELP FIXING CODE

In this you are to implement a simplified version of the Flesch reading-ease test. Create a program called FleschReadingEase that meets the following requirements

1. Takes one argument from the command line. The name of the text file to read

a. If an invalid number of arguments is provided print

Usage: java FleschReadingEase filename

b. If the file doesn't exist print

FILE NOT FOUND

2. Implements the algorithm described below and prints the computed score

3. To help you develop the code you will be required to implement the following methods (all public and static):

a. getTokensFromFile

i. Input parameter: String

Filename to be read

ii. returns: String[]

Tokens of text in file

iii. Note: you need to implement error handling. Have the method throw FileNotFound Exception.

Your main method will have a try/catch block to handle any exception thrown by this method. See the class notes on using a try/catch block.

b. removeNonAlphabetCharacters

i. Input parameter: String

The original string possibly containing alphabet and non-alphabetic characters

ii. returns: String

A new string with all non-alphabetic characters removed

Hello! should return Hello

!@#$!goodbye<>: should return goodbye

You may find the Character.isLetter method helpful

c. getSentenceCount

i. Input parameter: String[]

Word tokens

ii. returns: int

Count of sentences using word counting algorithm listed below

d. getSyllableCount

i. Input parameter: String

A word

ii. returns: int

Count of syllables in word using the word counting algorithm listed below

e. getFleschScore

i. Input parameter: String[]

Tokenized input text

ii. returns: int

Calculated Flesch score using algorithm listed below

Algorithm Details

Count all tokens in the file. A token is any sequence of characters delimited by white space, whether or not it is an actual English word. There are multiple ways to do this. However, to make grading consistent you are to use a Scanner to generate tokens (which we assume to be words) using white space as the delimiter.

Count all syllables in each token. Use the following rules:

Vowels are: aeiouyAEIOUY

Each group of two vowels counts as one syllable (for example, the "ea" in "real" is one syllable.

Following our rules, the word you has two(2) syllables. The grouping of yo and the u

Each individual vowel that is not adjacent counts as one syllable ( for example, the "e .. a" in "regal" counts as two syllables).

However, an "e" or "E" at the end of a word doesn't count as a syllable.

Note: you will need to remove punctuation before counting syllables

Hint: use the removeNonAlphabetCharacters method

Also, each token has at least one syllable, even if the previous rules produce a count of 0.

Note: The above rules may not work in all cases and may produce incorrect syllable counts. There is no simple syllable counting method that works in all cases. These syllable rules apply to this assignment only.

Count all sentences. A sentence is ended by a period, colon, semicolon, question mark, or exclamation mark .:;?!. Assume the number of times these characters appear is equivalent to the number of sentences. ONLY count if the last character in the token is one of the punctuation marks.

The score is computed by the following formula rounded to the nearest integer.

Scores can be interpreted as shown in the table below.

Score

School level (US)

Notes

100 90

5th grade

Very easy to read. Easily understood by an average 11-year-old student.

90 80

6th grade

Easy to read. Conversational English for consumers.

80 70

7th grade

Fairly easy to read.

70 60

8th & 9th grade

Plain English. Easily understood by 13- to 15-year-old students.

60 50

10th to 12th grade

Fairly difficult to read.

50 30

College

Difficult to read.

30 10

College graduate

Very difficult to read. Best understood by university graduates.

10 0

Professional

Extremely difficult to read. Best understood by university graduates.

CODE:

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FleschReadingEase { public static void main(String[] args) { String[] token = {}; if (args.length != 1) { System.out.println("Usage: java FleschReadingEase filename"); } else { try { token = getTokensFromFile(args[0]); System.out.println(getFleschScore(token)); } catch (FileNotFoundException e) { System.out.println("FILE NOT FOUND"); } } } public static String[] getTokensFromFile(String filename) throws FileNotFoundException { File file = new File(filename); Scanner scanner = new Scanner(file); scanner.useDelimiter("\\s+"); String[] tokens = scanner.tokens().toArray(String[]::new); scanner.close(); return tokens; } public static String removeNonAlphabetCharacters(String original) { return original.replaceAll("[^a-zA-Z]", ""); } public static int getSentenceCount(String[] tokens) { int count = 0; for (String token : tokens) { char lastChar = token.charAt(token.length() - 1); if (lastChar == '.' || lastChar == ':' || lastChar == ';' || lastChar == '?' || lastChar == '!') { count++; } } return count; } public static int getSyllableCount(String word) { int count = 0; boolean prevVowel = false; for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); boolean isVowel = "aeiouyAEIOUY".indexOf(c) >= 0; if (isVowel) { if (!prevVowel || i == word.length() - 1) { count++; } prevVowel = true; } else { prevVowel = false; } } if (word.endsWith("e") || word.endsWith("E")) { count--; } return count > 0 ? count : 1; } public static int getFleschScore(String[] token) { int totalSentence = getSentenceCount(token); int totalWord = token.length; int totalSyllable = 0; int i; for (i = 0; i < token.length; i++) { totalSyllable += getSyllableCount(token[i]); } float ftotalSentence = totalSentence; float ftotalWord = totalWord; float ftotalSyllable = totalSyllable; long score; System.out.println(totalWord); System.out.println(totalSentence); System.out.println(totalSyllable); score = Math.round(206.835 - 1.015 * (ftotalWord / ftotalSentence) - 84.6 * (ftotalSyllable / ftotalWord)); return (int) score; } }

ERROR RECEIVED:

Starting a Gradle Daemon (subsequent builds will be faster) > Task :clean > Task :compileJava > Task :processResources NO-SOURCE > Task :classes > Task :jar > Task :assemble > Task :compileTestJava > Task :processTestResources > Task :testClasses > Task :test FAILED FleschReadingEaseCommandLineTest Test testCommandLine() FAILED org.opentest4j.AssertionFailedError: expected: <105 > but was: <6 2 7 105> at app//FleschReadingEaseCommandLineTest.testCommandLine(FleschReadingEaseCommandLineTest.java:45) Test testCommandLine1() FAILED org.opentest4j.AssertionFailedError: expected: <25 > but was: <640 34 1245 23> at app//FleschReadingEaseCommandLineTest.testCommandLine1(FleschReadingEaseCommandLineTest.java:61) Test testCommandLine2() PASSED Test testCommandLineFileNotFound() PASSED Test testCommandLine3args() PASSED FleschReadingEaseTest Test getFleschScore() FAILED org.opentest4j.AssertionFailedError: expected: <25> but was: <23> at app//FleschReadingEaseTest.getFleschScore(FleschReadingEaseTest.java:318) 640 34 1245 Test getSyllableCount2() PASSED Test getSyllableCount3() PASSED Test getSyllableCount4() PASSED Test getSyllableCount5() PASSED Test getSyllableCount6() FAILED org.opentest4j.AssertionFailedError: array contents differ at index [10], expected: <1> but was: <2> at app//FleschReadingEaseTest.getSyllableCount6(FleschReadingEaseTest.java:254) Test getSyllableCount7() FAILED org.opentest4j.AssertionFailedError: array contents differ at index [31], expected: <4> but was: <3> at app//FleschReadingEaseTest.getSyllableCount7(FleschReadingEaseTest.java:279) Test getSyllableCount8() FAILED org.opentest4j.AssertionFailedError: array contents differ at index [27], expected: <1> but was: <2> at app//FleschReadingEaseTest.getSyllableCount8(FleschReadingEaseTest.java:304) Test getSyllableCount5ga() PASSED Test getSyllableCountHelper() FAILED org.opentest4j.AssertionFailedError: expected: <1> but was: <2> at app//FleschReadingEaseTest.getSyllableCountHelper(FleschReadingEaseTest.java:228) Test getSentenceCount() PASSED Test getSentenceCount2() PASSED Test getSentenceCount3() PASSED Test removeNonAlphabetCharacters1() PASSED Test removeNonAlphabetCharacters2() PASSED Test removeNonAlphabetCharacters3() PASSED Test getTokensFromFile() PASSED Test getSyllableCount5f() FAILED org.opentest4j.AssertionFailedError: expected: <5> but was: <4> at app//FleschReadingEaseTest.getSyllableCount5f(FleschReadingEaseTest.java:201) Test getSyllableCount5g() FAILED org.opentest4j.AssertionFailedError: expected: <3> but was: <2> at app//FleschReadingEaseTest.getSyllableCount5g(FleschReadingEaseTest.java:210) Test getSyllableCount() PASSED Test getTokensFromFile2() PASSED Test getTokensFromFile3() PASSED Test getTokensFromFile4() PASSED Test getTokensFromFile5() PASSED Test getFleschScore2() FAILED org.opentest4j.AssertionFailedError: expected: <76> but was: <74> at app//FleschReadingEaseTest.getFleschScore2(FleschReadingEaseTest.java:332) 1296 61 1699 Test getFleschScore3() PASSED 6 2 7 Test getFleschScore4() FAILED org.opentest4j.AssertionFailedError: expected: <53> but was: <52> at app//FleschReadingEaseTest.getFleschScore4(FleschReadingEaseTest.java:360) 777 26 1148 STUDENTS TOTAL SCORE: 52.0 FAILURE: Executed 32 tests in 989ms (11 failed) 32 tests completed, 11 failed FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':test'. > There were failing tests. See the report at: file:///autograder/source/0P/build/reports/tests/test/index.html * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 10s 6 actionable tasks: 6 executed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions