Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 words = getWordsFromFile(filePath); for(String word:words){ System.out.println(word); } // create a hashmap that will store the phrase as a key, the values will be the list of words following that phrase HashMap> phrases = new HashMap>(100); for(int wordCount=0; wordCount

String phrase = ""; // build the phrase of length as given by the user for(int length=0;length"+wordFollowingPhrase); // if the phrase is already repeated earlier we simply add the word to the list if(phrases.containsKey(phrase)){ ArrayList wordList = phrases.get(phrase); wordList.add(wordFollowingPhrase); } else { ArrayList wordList = new ArrayList(1); wordList.add(wordFollowingPhrase); phrases.put(phrase,wordList); } } printMap(phrases); } // reads word from a file and returns are list of words in the order they appeared in the file public static ArrayList getWordsFromFile(String filePath){ BufferedReader reader; ArrayList words = new ArrayList(50); try { reader = new BufferedReader(new FileReader(filePath)); String readLine = reader.readLine(); while(readLine!=null) { String[] wordArray = readLine.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+"); for(String word:wordArray){ if(word.length()>0) { words.add(word); } } }

} catch (IOException e) { System.out.println("File Not found. Program terminated..."); e.printStackTrace(); } return words; } // prints the hashmap public static void printMap(HashMap> wordMap) { for (String key : wordMap.keySet()){ System.out.print("Phrase: "+key); System.out.print(" Words followed: ["); for (String word: wordMap.get(key)){ System.out.print(word+" "); } System.out.println("] "); } } }

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

Advanced MySQL 8 Discover The Full Potential Of MySQL And Ensure High Performance Of Your Database

Authors: Eric Vanier ,Birju Shah ,Tejaswi Malepati

1st Edition

1788834445, 978-1788834445

More Books

Students also viewed these Databases questions

Question

LO4 Identify a system for controlling absenteeism.

Answered: 1 week ago

Question

LO2 Explain the nature of the psychological contract.

Answered: 1 week ago