Things to do in the lab:

Lab 10 (Apr 1-Apr 2)

Hashtables and the Therapist program.

In this lab you will work with the program described on page 384 in your textbook.

We will be using a StreamTokenizer with the comments on page 156 regarding the list of delimiters.

tucotuco.cs.indiana.edu% ls -l Therap*.java
-rw-r--r--   1 dgerman  students    2278 Mar 30 15:47 Therapist.java
-rw-r--r--   1 dgerman  students     859 Mar 30 15:43 TherapistInterface.java

tucotuco.cs.indiana.edu% cat TherapistInterface.java import java.awt.*; import BreezyGUI.*; public class TherapistInterface extends GBFrame { TextField therapistField = addTextField("Good day. How can I help you?", 1, 1, 1, 1); Button acceptButton = addButton ("Accept" , 2, 1, 1, 1); TextField patientField = addTextField("" , 3, 1, 1, 1); Therapist therapist; public TherapistInterface() { setTitle("Therapist"); therapist = new Therapist(); } public void buttonClicked(Button buttonObj) { String patientString = patientField.getText(); String therapistString = therapist.reply(patientString); patientField.setText(""); therapistField.setText(therapistString); } public static void main(String[] args) { Frame frm = new TherapistInterface(); frm.setSize(400, 125); frm.setVisible(true); } }
tucotuco.cs.indiana.edu%
tucotuco.cs.indiana.edu% cat Therapist.java
import java.util.*; public class Therapist extends Object { Vector hedges; Vector qualifiers; Hashtable replacements; public Therapist() { initTables(); } private void initTables() { hedges = new Vector(); hedges.addElement("Please tell me more..."); hedges.addElement("Many of my patients tell me the very same thing."); hedges.addElement("It's getting late, should we stop here?"); qualifiers = new Vector(); qualifiers.addElement("Why do you say that "); qualifiers.addElement("You seem to think that "); qualifiers.addElement("So you're concerned that "); replacements = new Hashtable(); replacements.put("i", "you"); replacements.put("me", "you"); replacements.put("my", "your"); replacements.put("am", "are"); replacements.put("you", "I (me)"); replacements.put("your", "my"); replacements.put("are", "am"); } public String reply (String patientString) { int choice = randomInt(1, 2); String answer = "OK"; switch (choice) { case 1: answer = hedge(hedges); break; case 2: answer = qualifier(qualifiers) + changePerson(patientString); break; } return answer; } private String hedge (Vector hedges) { return (String)selectRandom(hedges); } private String qualifier (Vector qualifiers) { return (String)selectRandom(qualifiers); } private String changePerson (String s) { StringTokenizer tokens = new StringTokenizer(s, ".,;?! \t\n\r"); StringBuffer buffer = new StringBuffer(); while (tokens.hasMoreTokens()) { String keyWord = tokens.nextToken(); String replacement = findReplacement(keyWord); buffer.append(replacement + " "); } return buffer.toString(); } private String findReplacement(String word) { word = word.toLowerCase(); if (replacements.containsKey(word)) return (String)replacements.get(word); else return word; } private Object selectRandom(Vector v) { int index = randomInt (0, v.size() - 1); return v.elementAt(index); } private int randomInt (int low, int high) { return (int)(low + Math.random() * (high - low + 1)); } }
tucotuco.cs.indiana.edu%
I hope to provide more background information on which was the original Therapist program that was written by and whose basic principle of operation was essentially the one illustrated above.