Question: I am stuck on how to complete this Java map collection random writer program. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Question: Random Writer Topic focus: Map Collection General Statement: Write
I am stuck on how to complete this Java map collection random writer program.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Question:
Random Writer
Topic focus:
Map Collection
General Statement:
Write a program to generate random text based on the style of another writers work.
Description:
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.
Given this map, generate a bunch of text. Ill leave how you would like to do this up to you. But you are looking for text that reminds you of the original but looks kinda like phrases are all mixed up and jump around some.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Code So far:
class ReadWrite { // main program where 2 readers and writers are created static Database RW = new Database(); public static void main(String[] arg) { int rounds = Integer.parseInt(arg[0],10); new Random_Reader(rounds, RW).start(); new Random_Reader(rounds, RW).start(); new Random_Writer(rounds, RW).start(); new Random_Writer(rounds, RW).start(); } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Random_Writer extends Thread {
int rounds; Database RW; private Random generator = new Random();
public static Random_Writer(int rounds, Database RW) { this.rounds = rounds; this.RW = RW; } public static void run() { for (int i = 0; i ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class Random_Reader extends Thread { int rounds; Database RW; private Random generator = new Random(); public Random_Reader(int rounds, Database RW) { this.rounds = rounds; this.RW = RW; } public static void run() { for (int i = 0; i ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.util.Random; class Database { private Random generator = new Random(); private int data = 0; int nr = 0; private synchronized void startRead() { nr++; } private static synchronized void endRead() { nr--; if (nr==0) notify(); } public static void read() { startRead(); System.out.println("read: " + data); endRead(); } public static synchronized void write() { int temp; while (nr>0) try { wait(); } catch (InterruptedException ex) { return; } temp = data; data = 99999; try { Thread.sleep(generator.nextInt(500)); } catch (java.lang.InterruptedException e) {} data = temp+1; System.out.println("wrote: " + data); notify(); } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Any help on how to complete this problem would be much appreciated. I have been stuck on it for quite a while.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
