// (c) MIT 2003.  All rights reserved.

// -*-Mode: Java-*- 
// $Id: GrammarTest.java,v 1.7 2003/03/06 00:40:40 moltmans Exp $
// GrammarTest.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Wed Feb  2 22:52:54 2000> 
// Time-stamp: <2001-09-11 15:18:46 moltmans> 
package rec.speech;

import java.util.*;
import java.awt.*;
import java.awt.event.*;

import rec.util.*;
import rec.sys.*;
import rec.speech.*;


/**
 **/
public class GrammarTest implements TagsListener
{
  public GrammarTest( String grammar)
  {
    if( grammar.toLowerCase().equals( "dict" ) ) {
      Runnable dictLooper = new Runnable() {
	public void run() {
	  String text = getDictation();
	  while( text == null || text.toLowerCase().indexOf( "quit" ) < 0 ) {
	    DebugWindow.println( "GramTest", text );
	    text = getDictation();
	  }
	  System.exit( 0 );
	}
      };
      Thread t = new Thread( dictLooper );
      t.start();
    }
    else {
      RecGrammar gram = RecGrammar.makeRecGrammar( grammar );
      gram.activate();
      gram.addTagsListener( this );
    }
  }

  public String getDictation( ) 
  {
    return RecListen.getListen().getDictation();
  }

  public void sayPhrase( String phrase )
  {
    Say.say( phrase );
  }

  /**
   */
  public void resultAccepted( Tag tags )
  {
    Console.writeln( "Got tags: " + tags.getText() );
    Console.writeln( tags.toString() );

    DebugWindow.println( "GramTest", tags.getText() );
    DebugWindow.println( "GramTest", tags.toString() );
    
    try {
      Thread.sleep(200);
    }
    catch (InterruptedException ie ) {
    }
  }

  public static void main( String[] argv ) 
  {
    new GrammarTest( argv[0] );
    ActionListener l = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  System.exit(0);
	}
      };

    ActionListener clear = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	  DebugWindow.clear( "GramTest" );
	}
      };
    DebugWindow.addButton( "GramTest", "Quit", l );
    DebugWindow.addButton( "GramTest", "Clear", clear );

  }
  
    
}

