Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sample run 2 Called with hamlet.txt Main.java public static void main(String[] args) { Markov markov = new Markov(); markov.addFromFile(spam.txt); System.out.println(markov); for (int i = 0;

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed

Sample run 2

Called with hamlet.txt

Main.java

public static void main(String[] args) {

Markov markov = new Markov();

markov.addFromFile("spam.txt");

System.out.println(markov);

for (int i = 0; i

System.out.println(markov.getSentence());

}

}

Output[1]:

{Think=[it, yourself, of], (within)=[My], would,'=[Or], spoke=[to.], soldiers,=[Give], Fortinbras;=[who,], your=[hour., ears,, haste, duty., voice., coronation,, gracious, father's, nature,, father;, father, intent, lordship!, poor, enemy, own, affair, father's, father., admiration,

What is't, my lord.

A violet in their oppress'd and pious bawds, The bell then no further personal power So please you, my noble youth that lives must fear, His canon 'gainst that died to-day, 'This must confess, that usurp'st this Hyperion to thy mind, nor let your ways.

Ay, madam, it is struck.

Angels and thorny way of frame, Colleagued with what so this time of it.

Very like, very cold.

[Ghost beckons.] Still am bound to recover of queen, Th' extravagant and favour to eternity.

All saws of caution- I call'd.

Or ever thy fair hour, Laertes.

Ham.

Who's there.?

UML Diagram Markov PUNCTUATION String A PUNCTUATION_MARKS String words HashMap> prevword String Markov() m o m m o getWords () HashMap> m 1 addFromFile(String) void addLine(String) void o addword (String) void getSentence() String o randomword(String) String endsWithPunctuation(String) boolean m 1 toString() String . m 1 You will create a java file titled Markov.java. The program will read a text file, generate a collection of words and words that follow that word, and will then use those words to generate new text. Please see the methods descriptions and the example output below. Field Details Constant Fields PUNCTUATION This field is used as a key to store words that end in punctuation. It is set to " $" PUNCTUATION_MARKS This is used to store punctuation marks that we will look for in our words. Members HashMap> words This stores a list of all the words found in the text file as the key of the HashMap. The ArrayList is the list of words that directly follow the key. For example given the phrase: It is raining when it is cloudy. Our structure will looks something like the following: __$= [It], It=[is, is], is=[raining, cloudy], raining=[when], cloudy.=[] Another way to visualize this is as a json string: [ "it": {"is", "is"}, "is":{"raining", "cloudy"}, "raining": {"when"}, "cloudy.":{ ] Note: It is important to ensure that ALL words are stored. Storing duplicates is a proxy for frequency. This way when we are selecting a word from our list it will, somewhat, match the frequency of the original document. String preyWord This will store the previous word that had been read from the text file. This is used to determine how to process the current word. If the previous word ended in something from PUNCTUATION_MARKS then this should be set to PUNCTUATION Method Details Markov() The constructor for this class will initialize the HashMap words with the key PUNCTUATION and a value of a new ArrayList. This method will also set prevWord = PUNCTUATION. getWords() This is a getter that returns the list of words addFromFile(String) Others methods called: addLine(String) This method takes a String called filename that represents the file that will be parsed into the words HashMap. This method opens the file and calls the addLine method to parse the individual lines from the filename. This method should catch any errors that are generated from file operations. addLine(String) Other methods called: addWord(String) This is a simple method that performs two very important operations. First it ensures that the line being read is not 0 length. This is important because without this functionality an empty string (blank line) will break the program. Once a String is determined to have content, the String is split into individual words. These words are then passed to the addWord(String) method. addWord(String) Other methods called : endsWtihPunctuation(String) This is the method that does most of the processing for this application. The first thing that is done is the previous word is checked to see if it ended with punctuation. If the previous word ended with punctuation then the current word is added under the PUNCTUATION key in the words HashMap. If the previous word did NOT end with punctuation then the HashMap words is checked to see if the previous word has an entry in the HashMap. If the previous word is not present in the HashMap, it will need to be added. Once the previous word is present in the HashMap the current word is added to the ArrayList that uses the previous word as a key. Note: We will use a method called endsWtihPunctuation(String) to check for punctuation. The reason for this is to make debugging easier and because it is used in the next method. getSentence() Other methods called: random Word(String), endsWtihPunctuation(String) This method is responsible for building a sentence from the contents of the HashMap words. It will make use of the method randomWord(String) described below. This method first pics a random word from the values stored under the key PUNCTUATION. This word becomes the current word. If the current word does not end in punctuation, it is added to the sentence being constructed along with a space and a new random word is selected. If the current word DOES end in punctuation, it is added to the String being constructed and no additional word is chosen. The String being built is the returned. randomWord(String) This method takes a word as a parameter. That word is used to retrieve an ArrayList of words that follow from the HashMap words. A random word is chosen from the ArrayList and returned. endsWith Punctuation(String) This method takes a String and checks if the last character of the String exists in PUNCTUATION_MARKS. If the last character does exist in PUNCTUATION_MARKS the method returns true, otherwise it returns false This method should also catch any errors that may occur when checking for punctuation. If an error is caught, print the word that caused the error along with an error message. toStringh) The toString will return the toString of the HashMap words. Testing 1. Ensure the constructor creates an object 2. Ensure getWords returns a HashMap with expected values a. The 'easy' way to do this is to create a HashMap with known values and call addFromFile on a file with those values. b. I'd suggest a simple file, perhaps one with only 'Hello World.' in it. 3. Ensure that getSentence returns a sentence. a. This could be tested in the same way as getWords 4. Ensure that randomWord returns a random word. a. Again testing this the same way as getWords and getSentence is the way forward. 5. ensure endsWith Punctuation returns false for words that do NOT end with punctuation, and that it returns true for words that DO end in punctuation 6. Testing the toString is a little bit tricky as there is no guarantee that a HashMap will be printed the same way, even if it contains the same values. a. I'd suggest creating a HashMap as in items 2-4 and checking that the String length is the same for both the HashMap toString and the Markov toString. Sample run 1 Called with spam.txt: spam. txt I like poptarts and 42 and spam. Will I get spam and poptarts for the holidays? I like spam poptarts! Main.java public static void main(String[] args) { Markov markov = new Markov(); markov.addFromFile("spam. txt"); System.out.println(markov); for (int i = 0; i > words This stores a list of all the words found in the text file as the key of the HashMap. The ArrayList is the list of words that directly follow the key. For example given the phrase: It is raining when it is cloudy. Our structure will looks something like the following: __$= [It], It=[is, is], is=[raining, cloudy], raining=[when], cloudy.=[] Another way to visualize this is as a json string: [ "it": {"is", "is"}, "is":{"raining", "cloudy"}, "raining": {"when"}, "cloudy.":{ ] Note: It is important to ensure that ALL words are stored. Storing duplicates is a proxy for frequency. This way when we are selecting a word from our list it will, somewhat, match the frequency of the original document. String preyWord This will store the previous word that had been read from the text file. This is used to determine how to process the current word. If the previous word ended in something from PUNCTUATION_MARKS then this should be set to PUNCTUATION UML Diagram Markov PUNCTUATION String A PUNCTUATION_MARKS String words HashMap> prevword String Markov() m o m m o getWords () HashMap> m 1 addFromFile(String) void addLine(String) void o addword (String) void getSentence() String o randomword(String) String endsWithPunctuation(String) boolean m 1 toString() String . m 1 endsWith Punctuation(String) This method takes a String and checks if the last character of the String exists in PUNCTUATION_MARKS. If the last character does exist in PUNCTUATION_MARKS the method returns true, otherwise it returns false This method should also catch any errors that may occur when checking for punctuation. If an error is caught, print the word that caused the error along with an error message. toStringh) The toString will return the toString of the HashMap words. UML Diagram Markov PUNCTUATION String A PUNCTUATION_MARKS String words HashMap> prevword String Markov() m o m m o getWords () HashMap> m 1 addFromFile(String) void addLine(String) void o addword (String) void getSentence() String o randomword(String) String endsWithPunctuation(String) boolean m 1 toString() String . m 1 You will create a java file titled Markov.java. The program will read a text file, generate a collection of words and words that follow that word, and will then use those words to generate new text. Please see the methods descriptions and the example output below. Field Details Constant Fields PUNCTUATION This field is used as a key to store words that end in punctuation. It is set to " $" PUNCTUATION_MARKS This is used to store punctuation marks that we will look for in our words. Members HashMap> words This stores a list of all the words found in the text file as the key of the HashMap. The ArrayList is the list of words that directly follow the key. For example given the phrase: It is raining when it is cloudy. Our structure will looks something like the following: __$= [It], It=[is, is], is=[raining, cloudy], raining=[when], cloudy.=[] Another way to visualize this is as a json string: [ "it": {"is", "is"}, "is":{"raining", "cloudy"}, "raining": {"when"}, "cloudy.":{ ] Note: It is important to ensure that ALL words are stored. Storing duplicates is a proxy for frequency. This way when we are selecting a word from our list it will, somewhat, match the frequency of the original document. String preyWord This will store the previous word that had been read from the text file. This is used to determine how to process the current word. If the previous word ended in something from PUNCTUATION_MARKS then this should be set to PUNCTUATION Method Details Markov() The constructor for this class will initialize the HashMap words with the key PUNCTUATION and a value of a new ArrayList. This method will also set prevWord = PUNCTUATION. getWords() This is a getter that returns the list of words addFromFile(String) Others methods called: addLine(String) This method takes a String called filename that represents the file that will be parsed into the words HashMap. This method opens the file and calls the addLine method to parse the individual lines from the filename. This method should catch any errors that are generated from file operations. addLine(String) Other methods called: addWord(String) This is a simple method that performs two very important operations. First it ensures that the line being read is not 0 length. This is important because without this functionality an empty string (blank line) will break the program. Once a String is determined to have content, the String is split into individual words. These words are then passed to the addWord(String) method. addWord(String) Other methods called : endsWtihPunctuation(String) This is the method that does most of the processing for this application. The first thing that is done is the previous word is checked to see if it ended with punctuation. If the previous word ended with punctuation then the current word is added under the PUNCTUATION key in the words HashMap. If the previous word did NOT end with punctuation then the HashMap words is checked to see if the previous word has an entry in the HashMap. If the previous word is not present in the HashMap, it will need to be added. Once the previous word is present in the HashMap the current word is added to the ArrayList that uses the previous word as a key. Note: We will use a method called endsWtihPunctuation(String) to check for punctuation. The reason for this is to make debugging easier and because it is used in the next method. getSentence() Other methods called: random Word(String), endsWtihPunctuation(String) This method is responsible for building a sentence from the contents of the HashMap words. It will make use of the method randomWord(String) described below. This method first pics a random word from the values stored under the key PUNCTUATION. This word becomes the current word. If the current word does not end in punctuation, it is added to the sentence being constructed along with a space and a new random word is selected. If the current word DOES end in punctuation, it is added to the String being constructed and no additional word is chosen. The String being built is the returned. randomWord(String) This method takes a word as a parameter. That word is used to retrieve an ArrayList of words that follow from the HashMap words. A random word is chosen from the ArrayList and returned. endsWith Punctuation(String) This method takes a String and checks if the last character of the String exists in PUNCTUATION_MARKS. If the last character does exist in PUNCTUATION_MARKS the method returns true, otherwise it returns false This method should also catch any errors that may occur when checking for punctuation. If an error is caught, print the word that caused the error along with an error message. toStringh) The toString will return the toString of the HashMap words. Testing 1. Ensure the constructor creates an object 2. Ensure getWords returns a HashMap with expected values a. The 'easy' way to do this is to create a HashMap with known values and call addFromFile on a file with those values. b. I'd suggest a simple file, perhaps one with only 'Hello World.' in it. 3. Ensure that getSentence returns a sentence. a. This could be tested in the same way as getWords 4. Ensure that randomWord returns a random word. a. Again testing this the same way as getWords and getSentence is the way forward. 5. ensure endsWith Punctuation returns false for words that do NOT end with punctuation, and that it returns true for words that DO end in punctuation 6. Testing the toString is a little bit tricky as there is no guarantee that a HashMap will be printed the same way, even if it contains the same values. a. I'd suggest creating a HashMap as in items 2-4 and checking that the String length is the same for both the HashMap toString and the Markov toString. Sample run 1 Called with spam.txt: spam. txt I like poptarts and 42 and spam. Will I get spam and poptarts for the holidays? I like spam poptarts! Main.java public static void main(String[] args) { Markov markov = new Markov(); markov.addFromFile("spam. txt"); System.out.println(markov); for (int i = 0; i > words This stores a list of all the words found in the text file as the key of the HashMap. The ArrayList is the list of words that directly follow the key. For example given the phrase: It is raining when it is cloudy. Our structure will looks something like the following: __$= [It], It=[is, is], is=[raining, cloudy], raining=[when], cloudy.=[] Another way to visualize this is as a json string: [ "it": {"is", "is"}, "is":{"raining", "cloudy"}, "raining": {"when"}, "cloudy.":{ ] Note: It is important to ensure that ALL words are stored. Storing duplicates is a proxy for frequency. This way when we are selecting a word from our list it will, somewhat, match the frequency of the original document. String preyWord This will store the previous word that had been read from the text file. This is used to determine how to process the current word. If the previous word ended in something from PUNCTUATION_MARKS then this should be set to PUNCTUATION UML Diagram Markov PUNCTUATION String A PUNCTUATION_MARKS String words HashMap> prevword String Markov() m o m m o getWords () HashMap> m 1 addFromFile(String) void addLine(String) void o addword (String) void getSentence() String o randomword(String) String endsWithPunctuation(String) boolean m 1 toString() String . m 1 endsWith Punctuation(String) This method takes a String and checks if the last character of the String exists in PUNCTUATION_MARKS. If the last character does exist in PUNCTUATION_MARKS the method returns true, otherwise it returns false This method should also catch any errors that may occur when checking for punctuation. If an error is caught, print the word that caused the error along with an error message. toStringh) The toString will return the toString of the HashMap words

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

define seletion selectivity SS and what th maximum value of SS

Answered: 1 week ago