/////////////////////////////////////////////////////////////////////// // VerbLearn System // // A crosslinguistic bodily-grounded hand-action // verb semantics acquisition system. // // David Bailey 1/97 // Converted from Sather 1.1 // // File: VerbLearn.java // // Description: Contains GUI and top level functionality. // // Classes: VerbLearn, MainPanel, GeneratePanel, LabelPanel, // TrainPanel, LexiconPanel, RecognizePanel, ObeyPanel, // LangChoice, LangLabel, ScenarioList, ParamChoice, // DatasetChoice, LexiconChoice, LexiconGrid import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; import java.util.*; /////////////////////////////////////////////////////////////////////// // Top level user interface for VerbLearn system. // Can be run as either an application or an applet, although you'll // have to deal with security restrictions if running as an applet. public class VerbLearn extends Applet { // Base directory containing the VerbLearn files, in two forms. public static final String BASE_DIR = "../../"; public static final String WEB_BASE_DIR = "http://www.icsi.berkeley.edu/~dbailey/verblearn/"; public static final String INITIAL_PARAM_FILE = "default"; public static final int WIDTH = 850; public static final int HEIGHT = 850; // The current context: private static Context context; // Top level user interface stuff: private static boolean isApplication; private static Frame frame; private static CheckboxGroup topLevel; private static Checkbox quit; // if application private static Checkbox restart; // if applet private static Panel cardPanel; private static CardLayout cardLayout; private static MainPanel mainPanel; private static GeneratePanel generatePanel; private static LabelPanel labelPanel; private static TrainPanel trainPanel; private static LexiconPanel lexiconPanel; private static RecognizePanel recognizePanel; private static ObeyPanel obeyPanel; public static Frame frame() { return frame; } public static Context context() { return context; } public static boolean isApplication() { return isApplication; } // Some panels must be accessible to others and to the Context: public static GeneratePanel generatePanel() { return generatePanel; } public static LabelPanel labelPanel() { return labelPanel; } public static LexiconPanel lexiconPanel() { return lexiconPanel; } public static ObeyPanel obeyPanel() { return obeyPanel; } public static TextArea trainLog() { return trainPanel.log(); } public static void main(String[] args) { isApplication = true; frame = new Frame(); frame.setTitle("VerbLearn"); frame.setSize(WIDTH, HEIGHT); VerbLearn vl = new VerbLearn(); frame.setLayout(new GridBagLayout()); Constrain.add(frame, vl, 0, 0, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0, 0, 0, 0); vl.init(); } public void init() { // Early GUI initialization: if (frame == null) { Container p = getParent(); while (!(p instanceof Frame)) { p = p.getParent(); } frame = (Frame)p; } setSize(WIDTH, HEIGHT); setBackground(new Color(0xddddff)); setLayout(new BorderLayout(15, 15)); setFont(new Font("Helvetica", Font.PLAIN, 12)); frame.validate(); frame.show(); // Figure out where the data will come from... if (isApplication) { DataSource.setUseFiles(BASE_DIR); } else { DataSource.setUseWeb(WEB_BASE_DIR); } // Application initialization: context = new Context(); // Rest of GUI initialization: Panel northPanel = new Panel(); northPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); topLevel = new CheckboxGroup(); northPanel.add(new Checkbox("Main", topLevel, true)); northPanel.add(new Checkbox("Generate", topLevel, false)); northPanel.add(new Checkbox("Label", topLevel, false)); northPanel.add(new Checkbox("Train", topLevel, false)); northPanel.add(new Checkbox("Lexicon", topLevel, false)); northPanel.add(new Checkbox("Recognize", topLevel, false)); northPanel.add(new Checkbox("Obey", topLevel, false)); if (isApplication) { quit = new Checkbox("Quit"); northPanel.add(quit); } else { restart = new Checkbox("Restart"); northPanel.add(restart); } add("North", northPanel); cardPanel = new Panel(); cardLayout = new CardLayout(); cardPanel.setLayout(cardLayout); mainPanel = new MainPanel(); generatePanel = new GeneratePanel(); labelPanel = new LabelPanel(); trainPanel = new TrainPanel(); lexiconPanel = new LexiconPanel(); recognizePanel = new RecognizePanel(); obeyPanel = new ObeyPanel(); cardPanel.add("Main", mainPanel); cardPanel.add("Generate", generatePanel); cardPanel.add("Label", labelPanel); cardPanel.add("Train", trainPanel); cardPanel.add("Lexicon", lexiconPanel); cardPanel.add("Recognize", recognizePanel); cardPanel.add("Obey", obeyPanel); add("Center", cardPanel); cardLayout.show(cardPanel, "Main"); frame.validate(); frame.show(); } public boolean action(Event e, Object arg) { if (e.target instanceof Checkbox && ((Checkbox)e.target).getCheckboxGroup() == topLevel) { if (((Boolean)arg).booleanValue()) { cardLayout.show(cardPanel, ((Checkbox)e.target).getLabel()); } return true; } if (e.target == quit) { generatePanel().saveScenarios(); labelPanel().saveLabels(); System.exit(0); return true; } if (e.target == restart) { generatePanel().saveScenarios(); labelPanel().saveLabels(); removeAll(); init(); invalidate(); validate(); return true; } return super.action(e, arg); } // This is for separate applications which want to use Context-dependent // objects. It basically just establishes VerbLearn.context(). public void outsideInit(boolean isApp) { isApplication = isApp; // First, figure out where the data will come from... if (isApplication) { DataSource.setUseFiles(BASE_DIR); } else { DataSource.setUseWeb(WEB_BASE_DIR); } // Application initialization: context = new Context(); } } /////////////////////////////////////////////////////////////////////// // Main Panel class MainPanel extends Panel { MultiLineLabel label; Picture jackPict; MainPanel() { setLayout(new GridBagLayout()); try { jackPict = new Picture(DataSource.getLoadedImage("image/sample.jpg", this)); } catch (Exception e) { System.err.println("MainPanel exception loading image: " + e.getMessage()); } Constrain.add(this, jackPict, 0, 0, 1, 1, 20, 20, 30, 20); label = new MultiLineLabel("Welcome to VerbLearn\n \n" + "Click on a tab to proceed.\n", MultiLineLabel.CENTER); label.setFont(new Font("Helvetica", Font.PLAIN, 24)); label.setForeground(new Color(150, 0, 0)); Constrain.add(this, label, 0, 1, 1, 1, 30, 20, 20, 20); if (!VerbLearn.isApplication()) { MultiLineLabel warn = new MultiLineLabel( "Note: Since you're running the system as an applet, you \n" + "may not run the Jack animator, restricting your use of the \n" + "Generate and Obey panels. Nor may you save any files.\n" + "Also, popup windows don't work correctly due to a Netscape bug.", MultiLineLabel.CENTER); Constrain.add(this, warn, 0, 2, 1, 1, 20, 20, 50, 20); } } } /////////////////////////////////////////////////////////////////////// // Panel for Generation of Fstructs and movies for each scenario, // by running Jack. class GeneratePanel extends Panel { private Label title; private Panel east, west, south; private Label cgLabel; private CheckboxGroup cg; private Checkbox unexecuted, all, selected; private Button doIt; private Label slLabel; private ScenarioList scenarioList; private TextField status; private Button view; private Button save; GeneratePanel() { setLayout(new GridBagLayout()); title = new Label("Schema Execution & Linking Fstruct Generation Window"); title.setFont(new Font("Helvetica", Font.BOLD, 18)); Constrain.add(this, title, 0, 0, 2, 1, 10, 10, 20, 10); west = new Panel(); west.setLayout(new GridBagLayout()); cgLabel = new Label("Execute:"); Constrain.add(west, cgLabel, 0, 0, 1, 1, 10, 10, 10, 10); cg = new CheckboxGroup(); unexecuted = new Checkbox("Unexecuted", cg, true); Constrain.add(west, unexecuted, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 10, 10, 3, 10); all = new Checkbox("All", cg, false); Constrain.add(west, all, 0, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 3, 10, 3, 10); selected = new Checkbox("Selected", cg, false); Constrain.add(west, selected, 0, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 3, 10, 10, 10); doIt = new Button("Do It !"); doIt.setFont(new Font("Helvetica", Font.PLAIN, 18)); Constrain.add(west, doIt, 0, 4, 1, 1, 20, 10, 10, 10); Constrain.add(this, west, 0, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); east = new Panel(); east.setLayout(new GridBagLayout()); slLabel = new Label("Scenario List:"); Constrain.add(east, slLabel, 0, 0, 1, 1, 10, 10, 10, 10); scenarioList = new ScenarioList(false, true, false); Constrain.add(east, scenarioList, 0, 1, 1, 1, 10, 10, 10, 10); Constrain.add(this, east, 1, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); south = new Panel(); south.setLayout(new GridBagLayout()); status = new TextField("Make your selection and then Do It !", 50); status.setEditable(false); Constrain.add(south, status, 0, 0, 1, 1, 10, 10, 10, 10); view = new Button("View Scenarios"); Constrain.add(south, view, 1, 0, 1, 1, 10, 10, 10, 10); save = new Button("Save Scenarios"); Constrain.add(south, save, 2, 0, 1, 1, 10, 10, 10, 10); Constrain.add(this, south, 0, 2, 2, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); } public boolean action(Event e, Object junk) { if (e.target == doIt) { if (VerbLearn.isApplication()) { executeScenarios(); } else { status.setText("Sorry, applet version can't run Jack."); } return true; } else if (e.target == view) { viewScenarios(); return true; } else if (e.target == save) { if (VerbLearn.isApplication()) { saveScenarios(); } else { status.setText("Sorry, applet version can't save files."); } return true; } else if (e.target == selected) { scenarioList.setEnabled(true); return true; } else if (e.target == all || e.target == unexecuted) { int[] selected = scenarioList.getSelectedIndexes(); for (int i = 0; i < selected.length; i++) { scenarioList.deselect(selected[i]); } scenarioList.setEnabled(false); return true; } return super.action(e, junk); } private void executeScenarios() { status.setText("Executing " + cg.getSelectedCheckbox().getLabel().toLowerCase() + " scenarios in Jack not implemented yet."); // CYCLE THROUGH JACK EXECUTIONS } public void viewScenarios() { TextEditDialog fed = new TextEditDialog( VerbLearn.frame(), "View Scenario Set", VerbLearn.context().scenarios().toString(), false); fed.show(); } public void saveScenarios() { if (!VerbLearn.isApplication()) return; boolean success = VerbLearn.context().scenarios().save(); if (success) { status.setText("Saved scenarios (if it was needed)."); } else { status.setText("Failed to save scenarios."); } } } /////////////////////////////////////////////////////////////////////// // Labelling of actions Panel class LabelPanel extends Panel { private Panel east, center, west, south; private Label title; private LangChoice langChoice; private Label cgLabel; private CheckboxGroup cg; private Checkbox unlabelled, all, selected; private Button doIt; private Label slLabel; private ScenarioList scenarioList; private TextField status; private Button view; private Button save; LabelPanel() { setLayout(new GridBagLayout()); title = new Label("Action Labelling Window"); title.setFont(new Font("Helvetica", Font.BOLD, 18)); Constrain.add(this, title, 0, 0, 3, 1, 10, 10, 20, 10); langChoice = new LangChoice(); Constrain.add(this, langChoice, 0, 1, 3, 1, 10, 10, 20, 10); west = new Panel(); west.setLayout(new GridBagLayout()); cgLabel = new Label("Label:"); Constrain.add(west, cgLabel, 0, 0, 1, 1, 10, 10, 10, 10); cg = new CheckboxGroup(); unlabelled = new Checkbox("Unlabelled", cg, true); Constrain.add(west, unlabelled, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 10, 10, 3, 10); all = new Checkbox("All", cg, false); Constrain.add(west, all, 0, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 3, 10, 3, 10); selected = new Checkbox("Selected", cg, false); Constrain.add(west, selected, 0, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 3, 10, 10, 10); doIt = new Button("Do It !"); doIt.setFont(new Font("Helvetica", Font.PLAIN, 18)); Constrain.add(west, doIt, 0, 4, 1, 1, 20, 10, 10, 10); Constrain.add(this, west, 0, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); center = new Panel(); center.setLayout(new GridBagLayout()); slLabel = new Label("Scenario List:"); Constrain.add(center, slLabel, 0, 0, 1, 1, 10, 10, 10, 10); scenarioList = new ScenarioList(true, false, false); Constrain.add(center, scenarioList, 0, 1, 1, 1, 10, 10, 10, 10); Constrain.add(this, center, 1, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); east = new Panel(); east.setLayout(new GridBagLayout()); // SET UP ANIMATION WINDOW HERE... // (SHOULD ALTERNATELY SHOW FSTRUCT FOR FAKE SCENARIOS) Constrain.add(east, new Label("(Movie plays here)"), 0, 0, 1, 1, 10, 10, 10, 10); Constrain.add(this, east, 2, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); south = new Panel(); south.setLayout(new GridBagLayout()); status = new TextField("Make your selections above and then Do It !", 50); status.setEditable(false); Constrain.add(south, status, 0, 1, 1, 1, 10, 10, 10, 10); view = new Button("View Scenarios"); Constrain.add(south, view, 1, 1, 1, 1, 10, 10, 10, 10); save = new Button("Save Labels"); Constrain.add(south, save, 2, 1, 1, 1, 10, 10, 10, 10); Constrain.add(this, south, 0, 3, 3, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); } public boolean action(Event e, Object junk) { if (e.target == doIt) { labelScenarios(); return true; } else if (e.target == view) { VerbLearn.generatePanel().viewScenarios(); return true; } else if (e.target == save) { if (VerbLearn.isApplication()) { saveLabels(); } else { status.setText("Sorry, applet version can't save files."); } return true; } else if (e.target == selected) { scenarioList.setEnabled(true); return true; } else if (e.target == all || e.target == unlabelled) { int[] selected = scenarioList.getSelectedIndexes(); for (int i = 0; i < selected.length; i++) { scenarioList.deselect(selected[i]); } scenarioList.setEnabled(false); return true; } return super.action(e, junk); } private void labelScenarios() { if (VerbLearn.context().langslot() == null) { status.setText("You must choose a language before labelling.\n"); return; } // First, set up a Vector containing the desired scenarios: Vector scList = new Vector(); if (cg.getSelectedCheckbox() == selected) { String[] arr = scenarioList.getSelectedItems(); for (int i = 0; i < arr.length; i++) { scList.addElement(VerbLearn.context().scenarios().get(arr[i])); } } else if (cg.getSelectedCheckbox() == unlabelled) { Enumeration scnEnum = VerbLearn.context().scenarios().names(); while (scnEnum.hasMoreElements()) { Scenario sc = VerbLearn.context().scenarios() .get((String)scnEnum.nextElement()); if (!sc.isLabelled()) { scList.addElement(sc); } } } else { Enumeration scnEnum = VerbLearn.context().scenarios().names(); while (scnEnum.hasMoreElements()) { scList.addElement(VerbLearn.context().scenarios() .get((String)scnEnum.nextElement())); } } // Next, go through the desired scenarios, displaying them either as // text or a movie, and collecting labels: Enumeration scEnum = scList.elements(); while (scEnum.hasMoreElements()) { Scenario sc = (Scenario)scEnum.nextElement(); if (sc.isGrounded()) { // PLAY MOVIE status.setText("Labelling not implemented with movies yet " + "(hit enter)."); } else { // Display final linking Fstruct in window. String str = sc.id() + " " + sc.finalLink(); ScrollingTextArea text = new ScrollingTextArea(str, 20, 30); east.removeAll(); Constrain.add(east, text, 0, 0, 1, 1, 10, 10, 10, 10); validate(); } status.setText("Enter label and press enter."); TextFieldDialog tfd = new TextFieldDialog(VerbLearn.frame(), "Label entry", "Enter label, or blank to skip:"); if (sc.isLabelled()) { tfd.setText(sc.label().toString()); } tfd.setLocation(100, 100); tfd.show(); VerbComplex vc = new VerbComplex(tfd.getText()); while (vc.size() > 0 && vc.size() != VerbLearn.context().numSlots()) { // Try again until a proper label is entered: status.setText("TRY AGAIN - AND REMEMBER YOUR DASHES!"); tfd = new TextFieldDialog(VerbLearn.frame(), "Label entry", "Enter label, or blank to skip:"); if (sc.isLabelled()) { tfd.setText(sc.label().toString()); } tfd.setLocation(100, 100); tfd.show(); vc = new VerbComplex(tfd.getText()); } if (vc.size() > 0 ) { VerbLearn.context().scenarios().setLabel(sc, vc); } status.setText(""); east.removeAll(); } status.setText("Labelling done."); } public void saveLabels() { if (!VerbLearn.isApplication()) return; boolean success = VerbLearn.context().scenarios().saveLabels(); if (success) { status.setText("Saved labels (if it was needed)."); } else { status.setText("Failed to save labels."); } } } /////////////////////////////////////////////////////////////////////// // Training Panel class TrainPanel extends Panel { private static final String INITIAL_TEXT = "Training log window\n\n"; private Label title; private Panel buttonPanel1, buttonPanel2; private LangChoice langChoice; private ParamChoice paramChoice; private DatasetChoice datasetChoice; private LexiconChoice lexiconChoice; private Button paramEdit, datasetEdit, doIt; private ScrollingTextArea log; private Button clear, save; public TextArea log() { return log; } TrainPanel() { setLayout(new GridBagLayout()); title = new Label("Training Window"); title.setFont(new Font("Helvetica", Font.BOLD, 18)); Constrain.add(this, title, 0, 0, 3, 1, 10, 10, 10, 10); buttonPanel1 = new Panel(); buttonPanel1.setLayout(new GridBagLayout()); langChoice = new LangChoice(); Constrain.add(buttonPanel1, langChoice, 0, 0, 1, 1, 0, 0, 0, 10); paramChoice = new ParamChoice(); Constrain.add(buttonPanel1, paramChoice, 1, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST, 0, 10, 0, 10); paramEdit = new Button("View/Edit"); Constrain.add(buttonPanel1, paramEdit, 2, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 10, 0, 0); Constrain.add(this, buttonPanel1, 0, 1, 3, 1, 10, 5, 10, 5); buttonPanel2 = new Panel(); buttonPanel2.setLayout(new GridBagLayout()); lexiconChoice = new LexiconChoice(); Constrain.add(buttonPanel2, lexiconChoice, 0, 0, 1, 1, 0, 0, 0, 10); datasetChoice = new DatasetChoice(); Constrain.add(buttonPanel2, datasetChoice, 1, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST, 0, 10, 0, 10); datasetEdit = new Button("View/Edit"); Constrain.add(buttonPanel2, datasetEdit, 2, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 10, 0, 0); Constrain.add(this, buttonPanel2, 0, 2, 3, 1, 10, 5, 10, 5); doIt = new Button("Do It !"); doIt.setFont(new Font("Helvetica", Font.PLAIN, 18)); Constrain.add(this, doIt, 0, 3, 3, 1, 10, 10, 10, 10); log = new ScrollingTextArea(INITIAL_TEXT, 30, 80); Constrain.add(this, log, 0, 4, 4, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 5, 10, 5, 10); clear = new Button("Clear Log"); Constrain.add(this, clear, 0, 5, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 5, 10, 10, 10); save = new Button("Save Log..."); Constrain.add(this, save, 2, 5, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST, 5, 10, 10, 10); } public boolean action(Event e, Object junk) { if (e.target == doIt) { train(); return true; } else if (e.target == paramEdit) { editParams(); return true; } else if (e.target == datasetEdit) { editDataset(); return true; } else if (e.target == save) { if (VerbLearn.isApplication()) { saveLog(); } else { log.append("Sorry, applet version can't save files.\n"); } return true; } else if (e.target == clear) { log.setText(INITIAL_TEXT); return true; } return super.action(e, junk); } private void train() { if (VerbLearn.context().params() == null) { log.append("You must choose a parameter file before training.\n"); return; } if (VerbLearn.context().dataset() == null) { log.append("You must choose a dataset before training.\n"); return; } if (VerbLearn.context().lexicon() == null) { log.append("You must choose an initial lexicon before training.\n"); return; } log.append(VerbLearn.context().params().toString()); log.append("\nBeginning training...\n"); for (int i = 0; i < VerbLearn.context().params().trainingPasses(); i++) { Enumeration scEnum = VerbLearn.context().dataset().trainEnum(); while (scEnum.hasMoreElements()) { Scenario sc = VerbLearn.context().scenarios().get( (String)scEnum.nextElement()); if (sc.isLabelled()) { log.append("Incorporating scenario " + sc.id() + ":\n"); VerbLearn.context().lexicon().incorporate(sc.finalLink(), sc.label()); // SOME CODE TO HELP DIAGNOSE LINUX MEMORY LEAK PROBLEMS: // System.gc(); // long mTotal = Runtime.getRuntime().totalMemory(); // long mFree = Runtime.getRuntime().freeMemory(); // System.err.println("total=" + mTotal + ", free=" + mFree + // ", thus used=" + (mTotal - mFree)); } else { log.append("WARNING: scenario " + sc.id() + " is unlabelled.\n"); } } } VerbLearn.context().lexicon().synch(); log.append("Done training.\n"); // Tell the LexiconPanel and ObeyPanel that the lexicon has changed: VerbLearn.lexiconPanel().rebuildLexiconGrid(); VerbLearn.obeyPanel().rebuildCommandEntryGrid(); getToolkit().beep(); } private void editParams() { if (VerbLearn.context().params() == null) { log.append("Can't view/edit parameter set until one's been chosen!\n"); return; } TextEditDialog fed = new TextEditDialog( VerbLearn.frame(), "Edit Parameters", VerbLearn.context().params().toString(), true); fed.show(); if (fed.apply()) { VerbLearn.context().setParams(new Params( new NestedStreamTokenizer(new StringReader(fed.getText())), VerbLearn.context().params().id())); log.append("Changes to parameters noted.\n"); if (fed.save()) { if (VerbLearn.isApplication()) { FileDialog fd = new FileDialog(VerbLearn.frame(), "Save New Parameters As...", FileDialog.SAVE); String dirname = VerbLearn.BASE_DIR + "param/"; fd.setDirectory(dirname); fd.setFile(VerbLearn.context().params().id()); fd.show(); String fname = fd.getFile(); if (fname != null) { VerbLearn.context().params().setId(fname); String pathname = dirname + fname; try { PrintWriter pw = new PrintWriter(new FileWriter(pathname)); pw.print(VerbLearn.context().params().toString()); pw.close(); VerbLearn.context().rereadLists(); log.append("Saved parameters in " + pathname + ".\n"); } catch (IOException e) { log.append("Failed to save parameters in " + pathname + ".\n"); } } } else { log.append("Sorry, applet version can't save files.\n"); } } } else { log.append("Viewed parameters but made no changes.\n"); } } private void editDataset() { if (VerbLearn.context().dataset() == null) { log.append("Can't view/edit dataset until one's been chosen!\n"); return; } TextEditDialog fed = new TextEditDialog( VerbLearn.frame(), "Edit Dataset", VerbLearn.context().dataset().toString(), true); fed.show(); if (fed.apply()) { VerbLearn.context().setDataset(new Dataset( new NestedStreamTokenizer(new StringReader(fed.getText())), VerbLearn.context().dataset().id())); log.append("Changes to dataset noted.\n"); if (fed.save()) { if (VerbLearn.isApplication()) { FileDialog fd = new FileDialog(VerbLearn.frame(), "Save New Dataset As...", FileDialog.SAVE); String dirname = VerbLearn.BASE_DIR + "dataset/"; fd.setDirectory(dirname); fd.setFile(VerbLearn.context().dataset().id()); fd.show(); String fname = fd.getFile(); if (fname != null) { VerbLearn.context().dataset().setId(fname); String pathname = dirname + fname; try { PrintWriter pw = new PrintWriter(new FileWriter(pathname)); pw.print(VerbLearn.context().dataset().toString()); pw.close(); VerbLearn.context().rereadLists(); log.append("Saved dataset in " + pathname + ".\n"); } catch (IOException e) { log.append("Failed to save dataset in " + pathname + ".\n"); } } } else { log.append("Sorry, applet version can't save files.\n"); } } } else { log.append("Viewed dataset but made no changes.\n"); } } private void saveLog() { if (VerbLearn.context().langslot() != null) { String dirname = VerbLearn.BASE_DIR + "lang/" + VerbLearn.context().langslot() + "/output/"; File f = new File(dirname); if (!f.exists()) { f.mkdirs(); } FileDialog fd = new FileDialog(VerbLearn.frame(), "Save Training Log As...", FileDialog.SAVE); fd.setDirectory(dirname); fd.show(); String fname = fd.getFile(); if (fname != null) { String pathname = fd.getDirectory() + fname; try { PrintWriter pw = new PrintWriter(new FileWriter(pathname)); pw.print(log.getText()); pw.close(); log.append("This log saved in file " + pathname + "\n"); } catch (IOException e) { log.append("Could not save this log in " + pathname + ": " + e.getMessage() + "\n"); } } } } } /////////////////////////////////////////////////////////////////////// // Lexicon Panel class LexiconPanel extends Panel { private static final String INITIAL_TEXT = "Word display window\n\n"; private Label title, slotTitle; private LangLabel langLabel; private ScrollingTextArea slotInfo; private LexiconGrid lexiconGrid; private TextArea text; private Label wordTitle; private Button formatSwitch; private Button saveLog; private Button saveLex; private String currentWord; LexiconPanel() { setLayout(new GridBagLayout()); title = new Label("Lexicon Inspection Window"); title.setFont(new Font("Helvetica", Font.BOLD, 18)); Constrain.add(this, title, 0, 0, 4, 1, 10, 10, 10, 10); langLabel = new LangLabel(); Constrain.add(this, langLabel, 0, 1, 4, 1, 10, 10, 10, 10); slotTitle = new Label("Slot Info:"); Constrain.add(this, slotTitle, 0, 2, 4, 1, 10, 10, 0, 10); slotInfo = new ScrollingTextArea(null, 5, 90); Constrain.add(this, slotInfo, 0, 3, 4, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 10, 10, 10, 10); wordTitle = new Label("Word Info:"); Constrain.add(this, wordTitle, 0, 4, 4, 1, 10, 10, 0, 10); lexiconGrid = new LexiconGrid(); Constrain.add(this, lexiconGrid, 0, 5, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); text = new TextArea(INITIAL_TEXT, 27, 70); Constrain.add(this, text, 1, 5, 3, 1, 10, 10, 10, 10); formatSwitch = new Button("Toggle Verbosity"); Constrain.add(this, formatSwitch, 1, 6, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 10, 10, 10, 10); saveLog = new Button("Save Text Version of Lexicon..."); Constrain.add(this, saveLog, 2, 6, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST, 10, 40, 10, 10); saveLex = new Button("Save Lexicon..."); Constrain.add(this, saveLex, 3, 6, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST, 10, 10, 10, 10); } // Called whenever lexicon changes (e.g., by TrainPanel). // Also updates the slot info window. public void rebuildLexiconGrid() { lexiconGrid.rebuild(); text.setText(INITIAL_TEXT); currentWord = null; if (VerbLearn.context().lexicon() != null) { slotInfo.setText(VerbLearn.context().lexicon().toString()); } else { slotInfo.setText("No lexicon exists yet."); } } public boolean action(Event e, Object junk) { if (e.target == formatSwitch) { switchFormats(); return true; } if (e.target == saveLex) { if (VerbLearn.isApplication()) { saveLexicon(); } else { text.setText("Sorry, applet version can't save files.\n"); } return true; } if (e.target == saveLog) { if (VerbLearn.isApplication()) { saveLexiconLog(); } else { text.setText("Sorry, applet version can't save files.\n"); } return true; } int i = lexiconGrid.indexOfButton(e.target); if (i >= 0) { showWord(((Button)e.target).getLabel()); return true; } return super.action(e, junk); } private void switchFormats() { Params p = VerbLearn.context().params(); if (p != null) { p.setVerboseSenses(!p.verboseSenses()); if (currentWord != null) { showWord(currentWord); } } } private void showWord(String w) { // Assumes words don't occur in multiple slots... text.setText(VerbLearn.context().lexicon().getWord(w).toString()); currentWord = w; } public void saveLexicon() { if (VerbLearn.context().lexicon() != null) { String dirname = VerbLearn.BASE_DIR + "lang/" + VerbLearn.context().langslot() + "/lexicon/"; File f = new File(dirname); if (!f.exists()) { f.mkdirs(); } FileDialog fd = new FileDialog(VerbLearn.frame(), "Save Lexicon As...", FileDialog.SAVE); fd.setDirectory(dirname); if (!VerbLearn.context().lexicon().id().equals("New/Unnamed")) { fd.setFile(VerbLearn.context().lexicon().id()); } fd.show(); String fname = fd.getFile(); if (fname != null) { VerbLearn.context().lexicon().setId(fname); String pathname = fd.getDirectory() + fname; try { ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(pathname)); os.writeObject(VerbLearn.context().lexicon()); text.setText("Lexicon saved in file " + pathname + "\n"); VerbLearn.context().rereadLists(); } catch (IOException e) { text.setText("Could not save lexicon in " + pathname + ": " + e.getMessage() + "\n"); } } } } public void saveLexiconLog() { if ((VerbLearn.context().langslot() != null) && (VerbLearn.context().lexicon() != null)) { String dirname = VerbLearn.BASE_DIR + "lang/" + VerbLearn.context().langslot() + "/output/"; File f = new File(dirname); if (!f.exists()) { f.mkdirs(); } FileDialog fd = new FileDialog(VerbLearn.frame(), "Save Text Version of Lexicon As...", FileDialog.SAVE); fd.setDirectory(dirname); fd.show(); String fname = fd.getFile(); if (fname != null) { String pathname = fd.getDirectory() + fname; try { PrintWriter pw = new PrintWriter(new FileWriter(pathname)); pw.print("Language: " + VerbLearn.context().lang() + " Slots: " + VerbLearn.context().numSlots() + "\n\n" + slotInfo.getText() + "\n"); for (int si = 0; si < VerbLearn.context().numSlots(); si++) { pw.print("-------------------------------\n\n" + "Slot " + si + " words:\n\n"); String[] wl = VerbLearn.context().lexicon().slot(si).wordList(); for (int wi = 0; wi < wl.length; wi++) { pw.print(VerbLearn.context().lexicon().slot(si) .get(wl[wi]).toString() + "\n"); } } pw.close(); text.setText("Text version of lexicon saved in file " + pathname + "\n"); } catch (IOException e) { text.setText("Could not save text version of lexicon in " + pathname + ": " + e.getMessage() + "\n"); } } } } } /////////////////////////////////////////////////////////////////////// // Recognition testing Panel class RecognizePanel extends Panel { private static final String INITIAL_TEXT = "Recognition test log window\n\n"; private Label title; private LangLabel langLabel; private Panel west, east; private Label cgLabel; private CheckboxGroup cg; private Checkbox all, selected; private Button doIt; private Label slLabel; private ScenarioList scenarioList; private ScrollingTextArea log; private Button clear, save; RecognizePanel() { setLayout(new GridBagLayout()); title = new Label("Recognition Testing Window"); title.setFont(new Font("Helvetica", Font.BOLD, 18)); Constrain.add(this, title, 0, 0, 2, 1, 10, 10, 20, 10); langLabel = new LangLabel(); Constrain.add(this, langLabel, 0, 1, 2, 1, 10, 10, 10, 10); west = new Panel(); west.setLayout(new GridBagLayout()); cgLabel = new Label("Recognize:"); Constrain.add(west, cgLabel, 0, 0, 1, 1, 10, 10, 10, 10); cg = new CheckboxGroup(); all = new Checkbox("All Recognition Test Examples", cg, true); Constrain.add(west, all, 0, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 3, 10, 3, 10); selected = new Checkbox("Selected (w/ more detail)", cg, false); Constrain.add(west, selected, 0, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 3, 10, 10, 10); doIt = new Button("Do It !"); doIt.setFont(new Font("Helvetica", Font.PLAIN, 18)); Constrain.add(west, doIt, 0, 4, 1, 1, 20, 10, 10, 10); Constrain.add(this, west, 0, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); east = new Panel(); east.setLayout(new GridBagLayout()); slLabel = new Label("Scenario List:"); Constrain.add(east, slLabel, 0, 0, 1, 1, 10, 10, 10, 10); scenarioList = new ScenarioList(false, false, true); Constrain.add(east, scenarioList, 0, 1, 1, 1, 10, 10, 10, 10); Constrain.add(this, east, 1, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); log = new ScrollingTextArea(INITIAL_TEXT, 24, 80); Constrain.add(this, log, 0, 3, 2, 1, 10, 10, 10, 10); clear = new Button("Clear Log"); Constrain.add(this, clear, 0, 4, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 5, 10, 10, 10); save = new Button("Save Log..."); Constrain.add(this, save, 1, 4, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST, 5, 10, 10, 10); } public boolean action(Event e, Object junk) { if (e.target == selected) { scenarioList.setEnabled(true); return true; } if (e.target == all) { scenarioList.setEnabled(false); int[] selected = scenarioList.getSelectedIndexes(); for (int i = 0; i < selected.length; i++) { scenarioList.deselect(selected[i]); } return true; } if (e.target == doIt) { recognize(); return true; } if (e.target == clear) { log.setText(INITIAL_TEXT); return true; } if (e.target == save) { if (VerbLearn.isApplication()) { saveLog(); } else { log.append("Sorry, applet version can't save files.\n"); } return true; } return super.action(e, junk); } private void recognize() { if (VerbLearn.context().params() == null) { log.append("You must choose a parameter file before testing " + "recognition.\n"); return; } if (cg.getSelectedCheckbox() == all && VerbLearn.context().dataset() == null) { log.append("You haven't chosen a dataset!\n"); return; } if (VerbLearn.context().lexicon() == null) { log.append("You must load or train a lexicon before testing " + "recognition.\n"); return; } String[] scArray = null; if (cg.getSelectedCheckbox() == selected) { scArray = scenarioList.getSelectedItems(); if (scArray == null || scArray.length == 0) { log.append("You haven't chosen any scenarios to test!\n"); return; } } log.append("Beginning recognition test...\n"); int numCorrect = 0; int numTested = 0; Enumeration scEnum; if (cg.getSelectedCheckbox() == selected) { Vector v = new Vector(); for (int i = 0; i < scArray.length; i++) { v.addElement(scArray[i]); } scEnum = v.elements(); } else { scEnum = VerbLearn.context().dataset().recognizeEnum(); } while (scEnum.hasMoreElements()) { Scenario sc = VerbLearn.context().scenarios().get( (String)scEnum.nextElement()); if (sc.isLabelled()) { String actual = VerbLearn.context().lexicon().label(sc.finalLink()) .toString(); log.append("Scenario " + sc.id() + ": desired=" + sc.label() + ", output=" + actual); numTested++; if (actual.equals(sc.label().toString())) { numCorrect++; } else { log.append(" *ERROR*"); } log.append("\n"); // Print out full word probability vector only if in "selected" mode: if (cg.getSelectedCheckbox() == selected) { log.append(VerbLearn.context().lexicon() .allSensePosteriors(sc.finalLink())); } } else { log.append("WARNING: scenario " + sc.id() + " is unlabelled.\n"); } } log.append("Correctly labelled " + numCorrect + " of " + numTested + " test scenarios"); if (numTested > 0) { log.append(" (" + ((int)((double)numCorrect / (double)numTested * 100)) + "%).\n"); } log.append("Recognition test done.\n\n"); } private void saveLog() { if (VerbLearn.context().langslot() != null) { String dirname = VerbLearn.BASE_DIR + "lang/" + VerbLearn.context().langslot() + "/output/"; File f = new File(dirname); if (!f.exists()) { f.mkdirs(); } FileDialog fd = new FileDialog(VerbLearn.frame(), "Save Recognition Test Log As...", FileDialog.SAVE); fd.setDirectory(dirname); fd.show(); String fname = fd.getFile(); if (fname != null) { String pathname = fd.getDirectory() + fname; try { PrintWriter pw = new PrintWriter(new FileWriter(pathname)); pw.print(log.getText()); pw.close(); log.append("This log saved in file " + pathname + "\n"); } catch (IOException e) { log.append("Could not save this log in " + pathname + ": " + e.getMessage() + "\n"); } } } } } /////////////////////////////////////////////////////////////////////// // Obeying testing Panel class ObeyPanel extends Panel { private static final String INITIAL_TEXT = "Obey test log window\n\n"; private Label title; private LangLabel langLabel; private Panel west, center, east; private Label cgLabel; private CheckboxGroup cg; private Checkbox all, selected, movie; private Label slLabel; private ScenarioList scenarioList; private Button doIt; private CommandEntryGrid commandEntryGrid; private ScrollingTextArea log; private Button clear, save; ObeyPanel() { setLayout(new GridBagLayout()); title = new Label("Command Obeying Testing Window"); title.setFont(new Font("Helvetica", Font.BOLD, 18)); Constrain.add(this, title, 0, 0, 3, 1, 10, 10, 20, 10); langLabel = new LangLabel(); Constrain.add(this, langLabel, 0, 1, 3, 1, 10, 10, 10, 10); west = new Panel(); west.setLayout(new GridBagLayout()); cgLabel = new Label("Obey:"); Constrain.add(west, cgLabel, 0, 0, 1, 1, 10, 10, 10, 10); cg = new CheckboxGroup(); all = new Checkbox("All (eval by recognition)", cg, true); Constrain.add(west, all, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 3, 10, 3, 10); selected = new Checkbox("Selected (w/ more detail)", cg, false); Constrain.add(west, selected, 0, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 3, 10, 3, 10); movie = new Checkbox("Selected (eval by movie)", cg, false); Constrain.add(west, movie, 0, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 3, 10, 10, 10); Constrain.add(this, west, 0, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); doIt = new Button("Do It !"); doIt.setFont(new Font("Helvetica", Font.PLAIN, 18)); Constrain.add(west, doIt, 0, 4, 1, 1, 10, 10, 10, 10); center = new Panel(); center.setLayout(new GridBagLayout()); slLabel = new Label("Scenario List:"); Constrain.add(center, slLabel, 0, 0, 1, 1, 10, 10, 10, 10); scenarioList = new ScenarioList(false, false, true); scenarioList.setEnabled(false); Constrain.add(center, scenarioList, 0, 1, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); Constrain.add(this, center, 1, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); east = new Panel(); east.setLayout(new GridBagLayout()); commandEntryGrid = new CommandEntryGrid(); commandEntryGrid.setEnabled(false); Constrain.add(east, commandEntryGrid, 0, 0, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); Constrain.add(this, east, 2, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 10, 10, 10, 10); // SET UP ANIMATION WINDOW HERE... Constrain.add(this, new Label("(Movie plays here)"), 0, 3, 1, 1, 10, 10, 10, 10); log = new ScrollingTextArea(INITIAL_TEXT, 20, 65); Constrain.add(this, log, 1, 3, 2, 1, 10, 10, 10, 10); clear = new Button("Clear Log"); Constrain.add(this, clear, 0, 4, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 5, 10, 10, 10); save = new Button("Save Log..."); Constrain.add(this, save, 2, 4, 1, 1, GridBagConstraints.NONE, GridBagConstraints.EAST, 5, 10, 10, 10); } // Called whenever lexicon changes (e.g., by TrainPanel). public void rebuildCommandEntryGrid() { commandEntryGrid.rebuild(); } public boolean action(Event e, Object junk) { // SHOULD CATCH SCENARIO-SELECTION AND DISPLAY FIRST FRAME IF AVAIL. if (e.target == selected) { scenarioList.setEnabled(true); int[] selected = scenarioList.getSelectedIndexes(); for (int i = 0; i < selected.length; i++) { scenarioList.deselect(selected[i]); } scenarioList.setMasks(false, false, true); commandEntryGrid.setEnabled(false); commandEntryGrid.clear(); return true; } if (e.target == all) { scenarioList.setEnabled(false); int[] selected = scenarioList.getSelectedIndexes(); for (int i = 0; i < selected.length; i++) { scenarioList.deselect(selected[i]); } commandEntryGrid.setEnabled(false); commandEntryGrid.clear(); return true; } if (e.target == movie) { scenarioList.setEnabled(true); int[] selected = scenarioList.getSelectedIndexes(); for (int i = 0; i < selected.length; i++) { scenarioList.deselect(selected[i]); } scenarioList.setMasks(true, true, false); commandEntryGrid.setEnabled(true); return true; } if (e.target == doIt) { obey(); return true; } if (e.target == clear) { log.setText(INITIAL_TEXT); return true; } if (e.target == save) { if (VerbLearn.isApplication()) { saveLog(); } else { log.append("Sorry, applet version can't save files.\n"); } return true; } return super.action(e, junk); } private void obey() { if (cg.getSelectedCheckbox() == movie && !VerbLearn.isApplication()) { log.append("Sorry, applet version can't run Jack.\n"); return; } if (VerbLearn.context().params() == null) { log.append("You must choose a parameter file before obey testing.\n"); return; } if (cg.getSelectedCheckbox() == all && VerbLearn.context().dataset() == null) { log.append("You must choose a dataset before (automated) obey " + "testing.\n"); return; } if (VerbLearn.context().lexicon() == null) { log.append("You must choose or train a lexicon before obey testing.\n"); return; } if (cg.getSelectedCheckbox() == movie && scenarioList.getSelectedItem() == null) { log.append("You must choose a scenario before (movie-based) obey " + "testing.\n"); return; } String[] scArray = null; if (cg.getSelectedCheckbox() == selected) { scArray = scenarioList.getSelectedItems(); if (scArray == null || scArray.length == 0) { log.append("You haven't chosen any scenarios to test!\n"); return; } } if (cg.getSelectedCheckbox() == movie && commandEntryGrid.getCommand().equals("")) { log.append("You must enter a command before (movie-based) obey " + "testing.\n"); return; } log.append("Beginning obey test.\n"); if (cg.getSelectedCheckbox() == all || cg.getSelectedCheckbox() == selected) { int numCorrect = 0; int numTested = 0; Enumeration scEnum; if (cg.getSelectedCheckbox() == selected) { Vector v = new Vector(); for (int i = 0; i < scArray.length; i++) { v.addElement(scArray[i]); } scEnum = v.elements(); } else { scEnum = VerbLearn.context().dataset().obeyEnum(); } while (scEnum.hasMoreElements()) { Scenario sc = VerbLearn.context().scenarios().get( (String)scEnum.nextElement()); if (sc.isLabelled()) { // Derive linking f-struct from the command and world state: Fstruct f1 = VerbLearn.context().lexicon() .obey(sc.label(), sc.initialLink()); // Simulate execution by filling in any needed defaults: Fstruct f2 = execute(f1, sc.initialLink()); // Pass it back through recognizer: String actual = VerbLearn.context().lexicon().label(f2).toString(); log.append("Scenario " + sc.id() + ": command=" + sc.label() + ", output=" + actual); numTested++; if (actual.equals(sc.label().toString())) { numCorrect++; } else { log.append(" *ERROR*"); } log.append("\n"); // Print out full word probability vector only if in "selected" mode: if (cg.getSelectedCheckbox() == selected) { log.append("Linking f-struct (pre-execution):\n\t" + f1.toString4()); log.append("Linking f-struct (post-execution):\n\t" + f2.toString4()); log.append(VerbLearn.context().lexicon() .allSensePosteriors(f2)); } } else { log.append("WARNING: scenario " + sc.id() + " is unlabelled.\n"); } } log.append("Correctly recognized " + numCorrect + " of " + numTested + " obeyed commands"); if (numTested > 0) { log.append(" (" + ((int)((double)numCorrect / (double)numTested * 100)) + "%).\n"); } } else { // Play a movie to allow evaluation String cmd = commandEntryGrid.getCommand(); Scenario sc = VerbLearn.context().scenarios().get( scenarioList.getSelectedItem()); log.append("Command is " + cmd + " with initial state from scenario " + sc.id() + ".\n"); Fstruct f = VerbLearn.context().lexicon().obey( new VerbComplex(cmd), sc.initialLink()); // WRITE f TO FILE, TELL JACK TO EXECUTE IT, THEN PLAY MOVIE log.append("Actual execution and movie playing not implemented yet.\n"); } log.append("Obey test done.\n\n"); } private Fstruct execute(Fstruct f1, Fstruct w) { Fstruct f2 = new Fstruct(f1); // Fill in needed defaults in f2: // TBD... return f2; } private void saveLog() { if (VerbLearn.context().langslot() != null) { String dirname = VerbLearn.BASE_DIR + "lang/" + VerbLearn.context().langslot() + "/output/"; File f = new File(dirname); if (!f.exists()) { f.mkdirs(); } FileDialog fd = new FileDialog(VerbLearn.frame(), "Save Obeying Test Log As...", FileDialog.SAVE); fd.setDirectory(dirname); fd.show(); String fname = fd.getFile(); if (fname != null) { String pathname = fd.getDirectory() + fname; try { PrintWriter pw = new PrintWriter(new FileWriter(pathname)); pw.print(log.getText()); pw.close(); log.append("This log saved in file " + pathname + "\n"); } catch (IOException e) { log.append("Could not save this log in " + pathname + ": " + e.getMessage() + "\n"); } } } } } /////////////////////////////////////////////////////////////////////// // Choice box for choosing a language. // WANT TO ADD AN ENTRY FOR CREATING A NEW LANGUAGE. class LangChoice extends Panel implements Observer { private Choice choice; LangChoice() { add(new Label("Pick a language and # of slots:")); VerbLearn.context().addObserver(this); update(VerbLearn.context(), null); } public boolean action(Event e, Object arg) { if (arg.equals("None yet")) VerbLearn.context().setLang(null); else VerbLearn.context().setLang((String)arg); return true; } public void update(Observable o, Object junk) { Context c = (Context)o; rebuild(); String sel; if (c.langslot() != null) sel = c.langslot(); else sel = "None yet"; choice.select(sel); } private void rebuild() { if (choice != null) { remove(choice); } choice = new Choice(); choice.addItem("None yet"); for (int i = 0; i < VerbLearn.context().langslotList().length; i++) { choice.addItem(VerbLearn.context().langslotList()[i]); } add(choice); // validate(); // refreshes panel -- got idea from netnews } } /////////////////////////////////////////////////////////////////////// // Display current language but don't allow changing it. class LangLabel extends Label implements Observer { private static final String prefix = "Current language and # of slots: "; LangLabel() { VerbLearn.context().addObserver(this); update(VerbLearn.context(), null); } public void update(Observable o, Object junk) { Context c = (Context)o; if (c.langslot() != null) setText(prefix + c.langslot()); else setText(prefix + "None yet"); } } /////////////////////////////////////////////////////////////////////// // Display current list of Scenarios for selection. // Optionally restrict the list to Scenarios with linking fstructs // (these may include "fakes") or to grounded Scenarios (no "fakes"), // or to labelled Scenarios. class ScenarioList extends List implements Observer { private static final int ROWS = 8; // Number of rows to use. private boolean linkedOnly, groundedOnly, labelledOnly; // By default, allows multiple selections and is initially disabled. ScenarioList(boolean linkedOnly, boolean groundedOnly, boolean labelledOnly) { super(ROWS, true); setEnabled(false); VerbLearn.context().addObserver(this); setMasks(linkedOnly, groundedOnly, labelledOnly); } public void setMasks(boolean linkedOnly, boolean groundedOnly, boolean labelledOnly) { this.linkedOnly = linkedOnly; this.groundedOnly = groundedOnly; this.labelledOnly = labelledOnly; update(VerbLearn.context().scenarios(), null); } public void update(Observable o, Object junk) { removeAll(); Enumeration scnEnum = VerbLearn.context().scenarios().names(); while (scnEnum.hasMoreElements()) { Scenario sc = VerbLearn.context().scenarios() .get((String)scnEnum.nextElement()); if ((!linkedOnly || sc.hasLinks()) && (!groundedOnly || sc.isGrounded()) && (!labelledOnly || sc.isLabelled())) { addItem(sc.id()); } } } } /////////////////////////////////////////////////////////////////////// // Choice box for choosing a parameter file. class ParamChoice extends Panel implements Observer { private Choice choice; ParamChoice() { add(new Label("Pick a parameter file:")); VerbLearn.context().addObserver(this); update(VerbLearn.context(), null); } public boolean action(Event e, Object arg) { if (arg.equals("None yet")) VerbLearn.context().setParams((String)null); else VerbLearn.context().setParams((String)arg); return true; } public void update(Observable o, Object junk) { Context c = (Context)o; rebuild(); String sel; if (c.params() != null) sel = c.params().id(); else sel = "None yet"; choice.select(sel); } private void rebuild() { if (choice != null) { remove(choice); } choice = new Choice(); choice.addItem("None yet"); for (int i = 0; i < VerbLearn.context().paramList().length; i++) { choice.addItem(VerbLearn.context().paramList()[i]); } add(choice); // validate(); // refreshes panel -- got idea from netnews } } /////////////////////////////////////////////////////////////////////// // Choice box for choosing a dataset file. class DatasetChoice extends Panel implements Observer { private Choice choice; private String currentLangslot; DatasetChoice() { add(new Label("Pick a dataset file:")); VerbLearn.context().addObserver(this); update(VerbLearn.context(), null); } public boolean action(Event e, Object arg) { if (arg.equals("None yet")) VerbLearn.context().setDataset((String)null); else VerbLearn.context().setDataset((String)arg); return true; } public void update(Observable o, Object junk) { Context c = (Context)o; rebuild(); String sel; if (c.dataset() != null) sel = c.dataset().id(); else sel = "None yet"; choice.select(sel); } private void rebuild() { if (choice != null) { remove(choice); } choice = new Choice(); choice.addItem("None yet"); for (int i = 0; i < VerbLearn.context().datasetList().length; i++) { choice.addItem(VerbLearn.context().datasetList()[i]); } add(choice); // validate(); // refreshes panel -- got idea from netnews } } /////////////////////////////////////////////////////////////////////// // Choice box for choosing a stored lexicon file. class LexiconChoice extends Panel implements Observer { private Choice choice; private String currentLangslot; private Params currentParams; LexiconChoice() { add(new Label("Pick a lexicon to start with:")); rebuild(); VerbLearn.context().addObserver(this); update(VerbLearn.context(), null); } public boolean action(Event e, Object arg) { if (arg.equals("None yet")) VerbLearn.context().setLexicon(null); else VerbLearn.context().setLexicon((String)arg); VerbLearn.lexiconPanel().rebuildLexiconGrid(); VerbLearn.obeyPanel().rebuildCommandEntryGrid(); return true; } public void update(Observable o, Object junk) { Context c = (Context)o; rebuild(); String sel; if (c.lexicon() != null) sel = c.lexicon().id(); else sel = "None yet"; choice.select(sel); } private void rebuild() { if (choice != null) { remove(choice); } choice = new Choice(); choice.addItem("None yet"); if (VerbLearn.context().lang() != null && VerbLearn.context().params() != null) { choice.addItem("New/Unnamed"); if (VerbLearn.context().lexiconList() != null) { for (int i = 0; i < VerbLearn.context().lexiconList().length; i++) { choice.addItem(VerbLearn.context().lexiconList()[i]); } } } add(choice); currentLangslot = VerbLearn.context().langslot(); currentParams = VerbLearn.context().params(); // validate(); // refreshes panel -- got idea from netnews if (VerbLearn.lexiconPanel() != null) { VerbLearn.lexiconPanel().rebuildLexiconGrid(); VerbLearn.obeyPanel().rebuildCommandEntryGrid(); } } } /////////////////////////////////////////////////////////////////////// // A panel containing a grid of buttons, one for each word in the // lexicon, arranged as one column per slot. class LexiconGrid extends Panel { public static final int MAX_PER_COLUMN = 10; private Label warn; private Vector buttonVec; LexiconGrid() { setLayout(new GridBagLayout()); rebuild(); } public void rebuild() { // Get rid of any old components: if (warn != null) { remove(warn); warn = null; } else if (buttonVec != null) { Enumeration bEnum = buttonVec.elements(); while (bEnum.hasMoreElements()) { remove((Button)bEnum.nextElement()); } buttonVec = null; } // Insert a new set of components: if (VerbLearn.context().lexicon() == null) { warn = new Label("(No lexicon exists yet)"); Constrain.add(this, warn, 0, 0, 1, 1, 10, 10, 10, 10); } else { buttonVec = new Vector(); int curCol = 0; for (int s = 0; s < VerbLearn.context().numSlots(); s++) { String[] words = VerbLearn.context().lexicon().slot(s).wordList(); int curRow = 0; for (int w = 0; w < words.length; w++) { Button b = new Button(words[w]); b.setFont(new Font("Helvetica", Font.PLAIN, 10)); Constrain.add(this, b, curCol, curRow, 1, 1, 3, 5, 3, 5); buttonVec.addElement(b); curRow++; if (curRow == MAX_PER_COLUMN) { curCol++; curRow = 0; } } curCol++; } } // validate(); } // Returns -1 if o is not a member button. public int indexOfButton(Object o) { return buttonVec.indexOf(o); } } /////////////////////////////////////////////////////////////////////// // A panel containing a grid of buttons, one for each word in the // lexicon, arranged as one column per slot, plus text fields across // the top which reflect the most recently selected word in each slot. class CommandEntryGrid extends Panel { public static final int MAX_PER_COLUMN = 6; private Label warn; private TextField[] command; private Hashtable buttonTable; // maps Buttons to slotNums CommandEntryGrid() { setLayout(new GridBagLayout()); rebuild(); } public void rebuild() { // Get rid of any old components: if (warn != null) { remove(warn); warn = null; } else if (buttonTable != null) { Enumeration bEnum = buttonTable.keys(); while (bEnum.hasMoreElements()) { remove((Button)bEnum.nextElement()); } for (int i = 0; i < command.length; i++) { remove(command[i]); } buttonTable = null; command = null; } // Insert a new set of components: if (VerbLearn.context().lexicon() == null) { warn = new Label("(No lexicon exists yet)"); Constrain.add(this, warn, 0, 0, 1, 1, 10, 10, 10, 10); } else { buttonTable = new Hashtable(); command = new TextField[VerbLearn.context().numSlots()]; int curCol = 0; for (int s = 0; s < VerbLearn.context().numSlots(); s++) { command[s] = new TextField(8); command[s].setEditable(false); Constrain.add(this, command[s], curCol, 0, 1, 1, 5, 5, 5, 5); Button b = new Button(" "); b.setFont(new Font("Helvetica", Font.PLAIN, 10)); Constrain.add(this, b, curCol, 1, 1, 1, 3, 5, 3, 5); buttonTable.put(b, new Integer(s)); String[] words = VerbLearn.context().lexicon().slot(s).wordList(); int curRow = 1; for (int w = 0; w < words.length; w++) { b = new Button(words[w]); b.setFont(new Font("Helvetica", Font.PLAIN, 10)); Constrain.add(this, b, curCol, curRow + 1, 1, 1, 3, 5, 3, 5); buttonTable.put(b, new Integer(s)); curRow++; if (curRow == MAX_PER_COLUMN) { curCol++; curRow = 0; } } curCol++; } } // validate(); } public void clear() { if (warn == null) { for (int s = 0; s < VerbLearn.context().numSlots(); s++) { command[s].setText(""); } } } public boolean action(Event e, Object arg) { int s = ((Integer)buttonTable.get(e.target)).intValue(); if (s >= 0) { command[s].setText(((Button)e.target).getLabel().trim()); return true; } return super.action(e, arg); } public String getCommand() { String str = ""; for (int i = 0; i < command.length; i++) { if (i > 0) str += "-"; str += command[i].getText(); } return str; } }