Question
Problem: Whenever I run the code, it gets stuck after I input the length of text I want to track through the scanner. From here
Problem: Whenever I run the code, it gets stuck after I input the length of text I want to track through the scanner. From here on the program does not work. You can keep entering input into the scanner but nothing comes of it. I can't figure this one out and have been stuck on it for over a week. Any help would be much appreciated.
Question:
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started