CS5App1.java for HW6Pr3


/*
 * CS5App.java -- Text Readability
 *
 * PAIR PROGRAMMING PROBLEM
 *
 * Homework # 6
 * Problem  # 3
 *
 * Name:
 * Date:
 * Time spent:
 *
 * Comments: Your comments here are very important. In particular, I'd
 * like to know if working w/an assignment that has so much code provided
 * up front contributed to your learning experience (and how this experience
 * was enhanced by working w/a partner). Thanks!
 */

import java.awt.*;

/* This assignment is a bit of reading and understanding existing code (provided 
 * here) and a bit of "modify the existing code" to finish the assignment.
 * 
 * If you think carefully about the code you write (reusing code whenever possible)
 * many of the functions won't require that you write too much code (all but
 * two of the function bodies you need to write can be done in 2 to 6 lines of code).
 * Rather, more of your writing in this problem should be answering pointed questions
 * and describing your test cases.
 *
 * In this part of the pair assignment, you should take turns writing answers.
 * In addition to the list of answers you need to compose below, you should
 * also search for "todo" --- when we grade your code these are the locations where
 * we'll be expecting some input from you. When answering questions, answer them in
 * the code (as comments). And don't forget to have fun with this :-)!
 *
 * todo questions to answer here (none of these answers should require more than five 
 * or six sentences max!):
 *
 * 0) Try your program out using a text file of your own choice (your hum paper
 *    would be a great choice! You can "save as plain text" a word document by
 *    changing the type of file in the "Save File" menu). Report the type
 *    of file you tested it on, as well as the result.
 *
 * 1) Identify at least three words whose syllables aren't calculated correctly
 *    when using the rules we specified in class. Use option 0 to verify
 *    that your code parses these words correctly according to these
 *    rules. Where do the rules break down on these examples?
 *
 * 2) Can you identify a block of text (ending with END) for which the
 *    number of words is incorrect? Use option 1 to play with this.
 *    If you don't think such a block exists, justify your answer.
 *    Otherwise, provide a specific example which breaks the code and
 *    discuss why your code exhibits this loophole.
 *
 * 3) Can you identify a block of text (ending with END) for which the
 *    number of sentences is incorrect? Again, use option 1 to play with this.
 *    If you don't think such a block exists, justify your answer.
 *    Otherwise, provide a specific example which breaks the code and
 *    discuss why your code exhibits this loophole.
 * 
 * 4) What kind of in-progress printing do you see when using option 1
 *    and entering text on multiple lines (e.g. text and multiple carriage
 *    returns occur before the user types END). Why does this behavior
 *    happen?
 *
 * 5) Using option 1, what happens if the last line they enter is
 *    "END 9" (i.e. END followed by a 9 followed by a carriage return). 
 *    Why does this behavior happen?
 */

class CS5App
{

    //purpose: the main function is the function java calls
    //input:   array of Strings (this is ignored by the application)
    //output:  nothing

    public static void main(String[] args)
    {
		while(true) {
	
			display_menu();
			H.p("What option would you like? ");
			int ui = H.ni();
			H.pl();

			//todo: in one sentence, explain why we don't handle case 9 inside the switch
			if(ui == 9) {
				H.pl("Bye-bye!");
				break;
			}

			switch(ui) {

			case 0: 

				//todo: briefly describe what this option is
				H.p("Enter word to count syllables (verbose): ");
				String word = H.nw(); //get the word
				H.pl();
				int n = numSyllables(word,true);
				H.pl("\nThere are " + n + " syllables in \"" + word + "\"");
				break;

			case 1:

				//todo: briefly describe what this option is
				H.pl("Enter text to estimate verbosity of (END to quit):\n");
				estimate_readability(false,true);
				break;

			case 2:

				//todo: briefly describe what this option is
				H.pl("Enter text to estimate verbosity of (END to quit):\n");
				estimate_readability(false,false);
				break;

			case 3:

				//todo: briefly describe what this option is
				estimate_readability(true,false);
				break;

			default:

				H.pl("You didn't enter a valid option!");
			}
		}
    }
     
    //purpose: todo 
    //input:   todo
    //output:  todo

    public static void display_menu() {

	H.pl("\nWelcome to the text verbosity calculator! Your options include:\n");
	H.pl("    (0) Calculate number of syllables in word (with work-in-progress printing)");
        H.pl("    (1) Calculate verbosity of console input  (with work-in-progress printing)");
        H.pl("    (2) Calculate verbosity of console input");
        H.pl("    (3) Calculate verbosity of file");
        H.pl("    (9) Quit\n");

    }

    //purpose: stripPunctuation removes punctuation from a String
    //input:   any String
    //output:  copy of the original String, with punctuation removed
    //
    //todo:    1. in english clearly document how "punctuation" is defined
    //         2. describe at a high-level (in terms of charAt, length,
    //            and + operator) how the new string is constructed. in
    //            particular, describe what types these functions and 
    //            operators are operating upon and what (if anything)
    //            goes on "underneath the hood" to get these types

    public static String stripPunctuation(String s)
    {
        String newString = "";
        for (int i = 0 ; i < s.length() ; ++i)
        {
            char c = s.charAt(i);
			
	    //todo: are the inner terms parentheses needed?
            if (('A' <= c && c <= 'Z') ||
                ('a' <= c && c <= 'z') )
			{
                newString += c;
            }
        }
        return newString;
    }


    //purpose: todo
    //input:   i) fromFile determines if the input whose verbosity is to be
    //            interpreted should be taken from a file
    //         ii) inProgressPrinting determines when in-progress work should be
    //             printed while interpreting this input
    //output:  none 
    //
    //more todo:
    //         1. argue how the arguments used in this function result in code 
    //            reuse
    //         2. describe why this function doesn't need to return an output
    //         3. provide the remaining code needed to calculate nword, nsent, and
    //            nsyl. reuse other code as much as possible and clearly identify
    //            what you are focusing on for each line you write. at any point where 
    //            you are relying on a particular invariant, also clearly identify this.
    //
    //this function will be graded under a microscope and is one of the most important in
    //the assignment! 

    public static void estimate_readability(boolean fromFile,boolean inProgressPrinting) {

        if(fromFile) {
            H.p("Enter file name (whose contents must contain an END token)\n"+
	        "that you'd like to estimate verbosity of: ");
            String fname = H.nl();
	    H.pl();

            //this function redirects input so that (as opposed to console) 
            //it now reads from file. important: this file better live
	    //in your Hw6Pr3 directory!
            H.inputFromFile("../"+fname); 

	    //aside: as soon as the end-of-file is reached, input
	    //automatically comes again from console.  
            //
            //todo: why is it important that input is then redirected?
        }

        //these variables store the _cumulative_ number of words, sentences, 
		//and syllables interpreted so far
        int nwords = 0, nsents = 0, nsyls = 0; 

	if(inProgressPrinting) {
	    H.pl("ENTERING VERBOSITY INTERPRETER...\n");
	}

	//the following loop is our "verbosity interpreter" --- parsing words:
	//continually updating the number of syllables, words, and sentences seen
	//so far until a special "stop" word, i.e. END, is seen
        while (true)
        {
	    //get next word
            String word = H.nw();

	    //these variables store the number words, sentences, and syllables
	    //for the interpretation of the _next_ word
	    int nword = 0, nsent = 0, nsyl= 0;

	    //the END word is special: it signals that we're done interpreting
	    //the verbosity of the input
	    if(word.equals("END")) {
		H.pl();
		break;
	    }

	    //all of the following code is a stub--------------------------------
	    //your job is to replace it with something that does what it should
            //end of stub--------------------------------------------------------

	    //after the word's done being interpreted, update cumulative totals
	    nsyls += nsyl;
	    nwords += nword;
	    nsents += nsent;

	    //report per-word printing if in-progress display's been requested
	    if(inProgressPrinting) {
		H.pl("Interpret "+"\""+word+"\""+": nsyl="+nsyl+
		     " nword:"+nword+" nsent:"+nsent+
		     " (running totals: nsyls="+nsyls+
		     " nwords:"+nwords+" nsents:"+nsents);
            }

         }

   	  if(inProgressPrinting) {
 	  H.pl("...LEAVING THE INTERPRETER\n");
      }

      //drum roll: what we care about!
      display_readability(nsents,nwords,nsyls);

    }

    //purpose: todo
    //input:   todo
    //output:  todo
    //
    //more todo:  fill in this function's body

    public static int readability(int nsents, int nwords, int nsyls) {
		return 0; //a bogus value just to make it compile
    }

    //purpose: todo
    //input:   todo
    //output:  todo

    public static void display_readability(int nsents, int nwords, int nsyls) {
        H.pl("Total sentences: " + nsents );
        H.pl("Total words:     " + nwords );
        H.pl("Total syllables: " + nsyls  );
	H.pl("");
        H.pl("Flesch readability index:  " + readability(nsents,nwords,nsyls));
    }

    //purpose: todo
    //input:   todo
    //output:  todo
    //
    //more todo: fill in this function's body.
    //
    //this function will be graded under a microscope and is one of the most important 
    //in the assignment! 
    //
    //part of your documentation should include:
    //
    //  i) when you're handling a particular rule, identify it as such.
    //  ii) if at a particular point you're relying on a particular invariant, 
    //      clearly identify what it is at that point.
    //
    //from a coding perspective, maximize code reuse: use the other functions we've
    //asked you to write whenever possible.
 
    public static int numSyllables(String word,boolean inProgressPrinting)
    {
        //hint: this function's inProgressPrinting flag should only be true
        //(i.e. display details regarding a word's per syllables calculation)
	//when servicing option 1. options 1-3 should call this function 
	//(indirectly through estimate_readability) with the flag set to false
        //(so that console output doesn't overwhelm the user).

	//all of the following code is a stub--------------------------------------
	//your job is to replace it with something that does what it should
	int n = 0; 
	if (inProgressPrinting) {
		H.pl("numSyllables work for \""+word+"\":");
		H.pl("    syllables bogusly set to "+n);
	}
	return n;
	//end of stub--------------------------------------------------------------
    } 

    //purpose: todo
    //input:   todo
    //output:  todo
    //
    //more todo: fill in this function's body.

    public static boolean isVowel(char c)
    {
	return false; //a bogus value just to make it compile
    }

    //purpose: todo
    //input:   todo
    //output:  todo
    //
    //more todo: fill in this function's body.

    public static boolean isVowelFollowsConsonant(String s, int i) 
    {
	return false; //a bogus value just to make it compile
    }

    //purpose: todo
    //input:   todo
    //output:  todo
    //
    //more todo: fill in this function's body.

    public static boolean isEndOfSentence(String word) {
	return false; //a bogus value just to make it compile
    }
}