////////////////////////////////////////////////////////////////////////////// // // Consult-O-Matic // by Greg Hewgill, December 7, 1997 // // This program generates random text based on the text in ConsultOMatic.txt, // its input file. It can be run as either an applet or an application. // // Consult-O-Matic is based on Foggy, a Turbo Pascal program by John Lawler. // ////////////////////////////////////////////////////////////////////////////// import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Event; import java.awt.Label; import java.awt.Panel; import java.awt.TextArea; import java.awt.TextField; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Random; import java.util.Vector; public class ConsultOMatic extends Applet { private final static int WIDTH = 80; private TextField SentencesField; private Button Go; private TextArea Output; private Vector Text; // Vector of Vectors of Strings public boolean action(Event evt, Object what) { if (evt.target == Go) { int sentences = 5; try { sentences = Integer.parseInt(SentencesField.getText()); } catch (NumberFormatException nfx) { } Output.setText(buildOutput(sentences)); } else { return super.action(evt, what); } return true; } private String buildOutput(int sentences) { if (sentences < 1) { sentences = 1; } else if (sentences > 9) { sentences = 9; } Vector used = new Vector(); for (int i = 0; i < Text.size(); i++) { used.addElement(new boolean[((Vector)Text.elementAt(i)).size()]); } StringBuffer r = new StringBuffer(); Random rnd = new Random(System.currentTimeMillis()); for (int i = 0; i < sentences; i++) { for (int j = 0; j < Text.size(); j++) { Vector phrases = (Vector)Text.elementAt(j); boolean[] usedphrases = (boolean[])used.elementAt(j); int k; do { k = rnd.nextInt() % phrases.size(); if (k < 0) { k = (-k) % phrases.size(); } } while (usedphrases[k]); usedphrases[k] = true; if (r.length() > 0) { r.append(' '); } r.append(phrases.elementAt(k)); } } int i = 0; while (true) { i += WIDTH-1; if (i >= r.length()) { break; } while (r.charAt(i) != ' ') { i--; } r.setCharAt(i, '\n'); } return new String(r); } public void init() { setLayout(new BorderLayout()); Panel title = new Panel(); title.setLayout(new BorderLayout()); title.add("West", new Label("Consult-O-Matic")); Panel buttons = new Panel(); buttons.add(new Label("Sentences (1-9):")); SentencesField = new TextField("5", 1); buttons.add(SentencesField); Go = new Button("Go"); buttons.add(Go); title.add("Center", buttons); add("North", title); Output = new TextArea(); add("Center", Output); } public static void main(String[] args) { new ConsultOMatic().main2(); } private void main2() { try { InputStream in = new FileInputStream("ConsultOMatic.txt"); readText(in); in.close(); System.out.println(buildOutput(5)); } catch (FileNotFoundException fnfx) { } catch (IOException iox) { } } private void readText(InputStream in) throws IOException { DataInputStream dis = new DataInputStream(in); Text = new Vector(); while (true) { Vector phrases = new Vector(); Text.addElement(phrases); while (true) { String s = dis.readLine(); if (s == null) { return; } s = s.trim(); if (s.length() == 0) { break; } phrases.addElement(s); } } } public void start() { try { InputStream in = new URL(getDocumentBase(), "ConsultOMatic.txt").openStream(); readText(in); in.close(); } catch (MalformedURLException muex) { } catch (IOException iox) { Output.setText("Error reading input file."); } } }