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

..Il Verizon? 10:25 AM instructure-uploads.s3.amazonaws.com 5 Exercises Exercise

ID: 3708491 • Letter: #

Question

..Il Verizon? 10:25 AM instructure-uploads.s3.amazonaws.com 5 Exercises Exercise 5.1 (markov py) Write a program that takes in a filename, reads each line of the file, converts the lines into a format convenient for making Markov chains, and then prints out a new sentence randomly generated from the data, based on the Markov algorithm. You may use any file you wish for test input, though alice.txt and cthulhu.txt are provided on Canvas. alice.txt is a slightly edited version of the complete text of Lewis Carroll's Alice's Adventures in Wonderiand, taken from https://www.gutenerg.org/files/11/11-h/11-h.htm cthulhu.txt is a slightly edited version of the complete text of H.P. Lovecrafts's The Call of Cthulhn, taken from http://w.hplovecraft.com/ritings/texts/fiction/cc.aspr. When parsing the input file, you should ignore any Treat the lines in the file as separate sentences. That is, if "Hello, how are you?" and "Where are my keys?" are lines in a file, then "you?" should not be followed by "Where" when generating a chain. However, "are should be allowed to be followed by either "you?" or "my, as seen in Figure 1 When creating a new chain, the first element should always be a randomly selected first word of a line in the file. The chain should end when either blank lines . There are no valid choices to continue the sentence with. ·The sentence has reached a length of 100 words. CSE/IT 107L Lab 10: Review and Markov Chains Remember that you need to account for different frequencies of possible follow words. That is, a situation like in Figure 2 where the word "a" is followed by two "is" and one "known". You need to account for whatever word frequencies might come up within your input file. Hint You may find dictionaries to be useful in implementing Markov chains. For example, the Markov chain in Figure 1 could be represented using this dictionary I'Hello,: ['how'. "how: 'are']. 'are: 'youy ?eys.]. "Where: 'are sl .you?": [], 'ay.: .keys?' : []} To generate a Markov chain, pick a random word from a list of first words. Then pick a random word that comes after the first word. Keep picking random next words until no more words are left or until the length limit is reached.

Explanation / Answer

import java.io.*;

import java.util.*;

// Defines a class MarkovChain

public class MarkovChain

{

// Hash map table for string and vector of string created

public static Hashtable<String, Vector<String>> myMarkovChain = new Hashtable<String, Vector<String>>();

// Random class object created

static Random random = new Random();

// main method definition

public static void main(String[] args) throws FileNotFoundException

{

// Create the first two entries (k:_start, k:_end)

myMarkovChain.put("_start", new Vector<String>());

myMarkovChain.put("_end", new Vector<String>());

Scanner sc = new Scanner(new File("MarkovData.txt"));

// Loops till end of the file

while(sc.hasNext())

{

// Extracts a line from the file

String phrases = sc.nextLine();

// Add the words to the hash table

addWords(phrases);

}// End of while loop

// Close the file

sc.close();

}// End of main

// Static method to add words

public static void addWords(String phrase)

{

// Puts each word into an array

String[] extractedWords = phrase.split(" ");

/*

* Loop through each word, check if it's already added

* if its added, then get the suffix vector and add the word

* if it hasn't been added then add the word to the list

* if its the first or last word then select the _start / _end key

*/

for (int counter = 0; counter < extractedWords.length; counter++)

{

// Add the start and end words to their own if counter is zero

if (counter == 0)

{

// Extracts the end of word and stores it in vector

Vector<String> startWords = myMarkovChain.get("_start");

startWords.add(extractedWords[counter]);

// Extracts a word from counter index position and stores it in the vector as suffix

Vector<String> suffix = myMarkovChain.get(extractedWords[counter]);

// Checks if suffix is null

if (suffix == null)

{

suffix = new Vector<String>();

suffix.add(extractedWords[counter + 1]);

myMarkovChain.put(extractedWords[counter], suffix);

}// End of inner if condition

}// End of outer if condition

// Otherwise checks if the counter value is equals to words length minus one

else if (counter == extractedWords.length-1)

{

// Extracts the end of word and stores it in vector

Vector<String> endWords = myMarkovChain.get("_end");

endWords.add(extractedWords[counter]);

}// End of else if

// Otherwise

else

{

// Extracts a word from counter index position and stores it in the vector as suffix

Vector<String> suffix = myMarkovChain.get(extractedWords[counter]);

// Checks if suffix is null

if (suffix == null)

{

suffix = new Vector<String>();

suffix.add(extractedWords[counter + 1]);

myMarkovChain.put(extractedWords[counter], suffix);

}// End of if condition

// Otherwise suffix is not null

else

{

suffix.add(extractedWords[counter + 1]);

myMarkovChain.put(extractedWords[counter], suffix);

}// End of inner else

}// End of outer else

}//

// Calls the method to generate markov phrase

generateMarkovPhrase();

}// End of method

// Static method to generate a markov phrase

public static void generateMarkovPhrase()

{

// Creates a vector of type string to hold the phrase

Vector<String> newPhrase = new Vector<String>();

// Declares a String object for the next word

String nextWord = "";

// Select the first word

Vector<String> startWords = myMarkovChain.get("_start");

// Stores the first word length

int startWordsLen = startWords.size();

// Generates next word randomly

nextWord = startWords.get(random.nextInt(startWordsLen));

newPhrase.add(nextWord);

// Loops through the words until we have reached the end

while (nextWord.charAt(nextWord.length()-1) != '.')

{

Vector<String> wordSelection = myMarkovChain.get(nextWord);

int wordSelectionLen = wordSelection.size();

nextWord = wordSelection.get(random.nextInt(wordSelectionLen));

newPhrase.add(nextWord);

}// End of while loop

// Displays the new phrase

System.out.println("Markov chain: " + newPhrase.toString());

}// End of method

}// End of class

Sample output:

Markov chain: ["Hello,, how, are, lines, in, Figure1.]