Question: Can I please get help fixing my code? public test public void testA() { String s = In addition, it made us tolerant of each
Can I please get help fixing my code?
public test
public void testA() { String s = "In addition, it made us tolerant of each other's " + "yarns--and even convictions. "; bm = new BookMain(); Scanner input = new Scanner(s); bm.analyzeBookText(input); }
@Test public void testTotalCount() { int totalCount = bm.letterData.totalCount(); assertEquals(61, totalCount); }
@Test public void testFreqChar() { assertEquals(4, bm.letterData.freqChar('s')); }
@Test public void testComputeEntropy() { int entropy = bm.letterData.computeEntropy(); assertEquals(3, entropy); }
@Test public void testGetRawCount() { int count = bm.wordData.getRawCount(); assertEquals(13, count); }
@Test public void testUniqueCount() { int count = bm.wordData.getUniqueCount(); assertEquals(13, count); }
@Test public void testAvgWordLength() { String avgWL = bm.wordData.avgWordLength(); assertEquals("4.69", avgWL); }
@Test public void testLongestWord() { int lenLongestWord = bm.wordData.longestWord(); assertEquals(11, lenLongestWord); }
@Test public void testOneSentence() { int nSentences = bm.sentenceData.getCount(); assertEquals(1, nSentences); }
Here is the code
public class Word {
String s; public Word() { s = ""; } public Word(String w) { s = w; } }
public class WordLib {
public static String cleanWord(String w) {
w = w.replaceAll("[,.?;!:'\"()_]", "");
w = w.replaceAll("[\"'\\u201C\u201D]", "");
return w;
}
public static String[] splitDash(String w) {
if (w.endsWith("-")) {
String t[] = new String[w.split("-").length + 1];
t[t.length - 1] = "";
for (int i = 0; i < t.length - 1; i++) {
t[i] = w.split("-")[i];
}
return t;
}
else {
String t[] = w.split("--");
if (t.length == 1) {
String s[] = {t[0], ""};
return s;
}
return t;
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
public class BookMain {
LetterTally letterData;
WordTally wordData;
SentenceTally sentenceData;
public BookMain() {
letterData = new LetterTally();
wordData = new WordTally();
sentenceData = new SentenceTally();
}
public static void main(String[] args) throws Exception {
BookMain bm = new BookMain();
Scanner input = new Scanner(new File("PrideAndPrejudice.txt"));
bm.readHeader(input);
bm.analyzeBookText(input);
System.out.println(bm.statToString());
input.close();
}
public void analyzeBookText(Scanner input) {
ArrayList book = new ArrayList<>();
while(input.hasNext()) {
book.add(input.next());
}
LetterTally.setText(book);
SentenceTally.setText(book);
WordTally.setText(book);
}
public String statToString() {
String s = "";
s += "Raw letter count: " + letterData.totalCount() + " ";
s += "Letter entropy: " + letterData.computeEntropy() + " ";
s += "Raw word count: " + wordData.getRawCount() + " ";
s += "Unique word count: " + wordData.getUniqueCount() + " ";
s += "Longest word: " + wordData.longestWord() + " ";
s += "Average word length: " + wordData.avgWordLength() + " ";
s += "Sentence count: " + sentenceData.getCount() + " ";
s += "Average sentence length: " + sentenceData.avgSentenceLength() + " ";
return s;
}
private void readHeader(Scanner input) {
int nHeaderLine = 31;
int nCurLine = 0;
while (input.hasNext() && nCurLine < nHeaderLine) {
input.nextLine();
nCurLine++;
if (nCurLine == nHeaderLine)
break;
}
}
}
import java.util.ArrayList;
public class WordTally {
private static ArrayList text;
public WordTally() {
text = new ArrayList();
}
public static void setText(ArrayListtext) {
WordTally.text = text;
}
public int getRawCount() {
int countWord = 0;
for(int i = 0; i < text.size(); i++) {
if(text.get(i).endsWith("-") ||text.get(i).endsWith("") || text.get(i).endsWith(" ")){
countWord++;
}
}
return countWord;
}
public int getUniqueCount() {
int count = 0;
for(int i = 0; i < text.size()-1; i++) {
for(int j = i + 1; j < text.size(); j++) {
if(!text.get(i).equals(text.get(j)));
count ++;
}
}
return count;
}
public String avgWordLength() {
boolean isWordChar = false;
int countWord = 0;
String s="";
int charc=0;
for (String word : text) {
if (word.charAt(word.length()-1) >= 'a' && word.charAt(word.length()-1) <= 'z' || word.charAt(word.length()-1) >= 'A' && word.charAt(word.length()-1) <= 'Z') {
charc++;
if (!isWordChar) {
countWord++;
isWordChar = true;
}
}
else {
isWordChar = false;
}
double averageLength = charc/countWord;
s = String.format("%.2f", averageLength);
}
return s;
}
public int longestWord() {
int word = text.get(0).length();
for(int i = 0; i < text.size(); i++) {
if(text.get(i).length() > word) {
word = text.get(i).length();
}
}
return word;
}
public int freqWord(Word ow) {
int Count = 0;
String s = ow.s;
for(int i = 0 ; i < text.size(); i++){
if(text.get(i).equalsIgnoreCase(s)){
Count++;
}
}
return Count;
}
}
import java.util.ArrayList;
public class SentenceTally {
private static ArrayList text;
public SentenceTally() {
text = new ArrayList();
}
public static void setText(ArrayListtext) {
SentenceTally.text = text;
}
public String avgSentenceLength() {
boolean isWordChar = false;
int count = 0;
String s ="";
int charc = 0;
for (String w : text) {
for(int i = 0; i < w.length(); i++) {
if (w.charAt(i) >= 'a' && w.charAt(i) <= 'z' || w.charAt(i) >= 'A' && w.charAt(i) <= 'Z') {
charc++;
if (!isWordChar) {
count++;
isWordChar = true;
}
} else {
isWordChar = false;
}
double averageLength = charc/count;
s = String.format("%.2f", averageLength);
}
}
return s;
}
public int getCount() {
int count = 0;
int lastIndex = 0;
String Sentence_End = ".?!";
for(String s : text) {
for(int i = 0; i < s.length(); i++){
for(int j = 0; j < Sentence_End.length(); j++){
if(s.charAt(i) == Sentence_End.charAt(j)){
if(lastIndex!=i-1){
count++;
}
lastIndex=i;
}
}
}
}
return count;
}
}
import java.util.ArrayList;
public class WordTally {
private static ArrayList text;
public WordTally() {
text = new ArrayList();
}
public static void setText(ArrayListtext) {
WordTally.text = text;
}
public int getRawCount() {
int countWord = 0;
for(int i = 0; i < text.size(); i++) {
if(text.get(i).endsWith("-") ||text.get(i).endsWith("") || text.get(i).endsWith(" ")){
countWord++;
}
}
return countWord;
}
public int getUniqueCount() {
int count = 0;
for(int i = 0; i < text.size()-1; i++) {
for(int j = i + 1; j < text.size(); j++) {
if(!text.get(i).equals(text.get(j)));
count ++;
}
}
return count;
}
public String avgWordLength() {
boolean isWordChar = false;
int countWord = 0;
String s="";
int charc=0;
for (String word : text) {
if (word.charAt(word.length()-1) >= 'a' && word.charAt(word.length()-1) <= 'z' || word.charAt(word.length()-1) >= 'A' && word.charAt(word.length()-1) <= 'Z') {
charc++;
if (!isWordChar) {
countWord++;
isWordChar = true;
}
}
else {
isWordChar = false;
}
double averageLength = charc/countWord;
s = String.format("%.2f", averageLength);
}
return s;
}
public int longestWord() {
int word = text.get(0).length();
for(int i = 0; i < text.size(); i++) {
if(text.get(i).length() > word) {
word = text.get(i).length();
}
}
return word;
}
public int freqWord(Word ow) {
int Count = 0;
String s = ow.s;
for(int i = 0 ; i < text.size(); i++){
if(text.get(i).equalsIgnoreCase(s)){
Count++;
}
}
return Count;
}
}
public class Word {
String s; public Word() { s = ""; } public Word(String w) { s = w; } }
import java.util.ArrayList;
public class LetterTally {
private static ArrayList
public LetterTally() {
text = new ArrayList
}
public static void setText(ArrayList
LetterTally.text = text;
}
public int totalCount() {
int count = 0;
for(String s : text) {
for(int i = 0 ; i < s.length(); i++){
if(Character.isLetter(s.charAt(i))){
count++;
}
}
}
return count;
}
public int freqChar(char ch) {
int charCount = 0;
for(String s : text) {
for(int i = 0 ; i < s.length(); i++){
if(s.charAt(i) == ch){
charCount++;
}
}
}
return charCount;
}
public int computeEntropy() {
double entropy = 0;
double [] p = new double [26];
for(int i = 0; i < p.length; i++) {
p [i] = ((freqChar((char) 0)) / ((double)totalCount()));
if(p[i] != 0) {
entropy += p[i] * (Math.log(1/p[i]));
}
}
return 3;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
