Question: I am having trouble with a problm in my Java programming class. Its on Map Collection/Random Writer. The goal is to write a program to
I am having trouble with a problm in my Java programming class. Its on Map Collection/Random Writer. The goal is to write a program to generate random text based on the style of another writers work. Below is the outline for the assignment.
Problem:
There is a classic problem by the same name, "Random Writer", that was proposed by computer scientist Joseph Zachary. He found that there is a relation between the style of a writers work and the probability of a word showing up next in that persons writing. The accuracy of randomized text to the style of the writer could be increased by using chains of words and tracking the word to come next.
For instance the word sequence "I like" could be followed by many words. If we have the text body:
'
I was walking home one day and thought to myself: "I like ice cream." So I changed my destination. Once I got to the store I was unable to make up my mind on the type of ice cream to buy.
'
'I was' is followed by two possible words in the sample text so in my random text generator, if this was my input text, I should equally randomly choose between walking and unable.
Your task is to create a solution to this problem. Read in a text file and read an integer from the user. The text file will be a long text file of a writers work found on the internet. This will be the source of the statistics for the predictions of the next word in your random writer. The integer from the keyboard should be the length of the chain of words to track for the statistics on word occurrence probability. A solid way to tackle this problem is to read in enough words to act as the key and then the next word would be added into a list which would be value of that key.
Code so far:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner;
public class RandomWriter {
public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Enter the text file you want to analyze"); String filePath = console.nextLine(); System.out.println("Enter the length of phrase you want to track"); int phraseLength = console.nextInt();
ArrayList String phrase = ""; // build the phrase of length as given by the user for(int length=0;length } catch (IOException e) { System.out.println("File Not found. Program terminated..."); e.printStackTrace(); } return words; } // prints the hashmap public static void printMap(HashMap I cant get this code to work. Any help would be nice!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
