#!/usr/bin/perl

#============================================================
#use statements
#============================================================
use constant LIB_PATH => "/usr/lib/perlbox-voice";
use Tk;
use lib LIB_PATH;
use strict;
use Perlbox::VoiceServer;
use Perlbox::ThirdParty::Tk::MListbox;
use Perlbox::perlboxTK::pDialog::pSimpleDialog;
use constant WINDOW_WIDTH	=> 500;
use constant WINDOW_HEIGHT	=> 350;
use constant WINDOW_TITLE	=> "Perlbox Voice ";
use constant PBOX_VERSION	=> "0.07-9";

use constant TRUE		=> 1;                    #boolean true
use constant FALSE		=> 0;                    #boolean false


use constant MSG_NO_NEW_STATE 		=> 0;

my $SHOW_GRAPHICAL_INTERFACE = 1;

$SIG{INT} = \&catch_int;  # best strategy
use Getopt::Long;
#============================================================
#Command line options
#============================================================
#first things first: get opt
my $silent_opt = undef;
my $nogui_opt = undef;
my $opts = GetOptions  ("silent" => \$silent_opt,    
                        "nogui"   =>   \$nogui_opt); 

if( $nogui_opt ){
   print "Found no gui\n";
   $SHOW_GRAPHICAL_INTERFACE = 0;
}

my $verbosity_level  =6;
my $current_msg      ="The listener has not started";

my $voice_server     =Perlbox::VoiceServer->new;

#============================================================
#Variable initialization
#============================================================
my $mainwindow       =undef;
my $mainwindow_color =undef;
my $panel_canvas     =undef;
my $widget_canvas    =undef;
my $this_form        =undef;


my $lst_speech       =undef;

my $txt_speech       =undef;
if( $SHOW_GRAPHICAL_INTERFACE ){

   #============================================================
   #Main window initialization
   #============================================================
      $mainwindow=MainWindow->new;
      $mainwindow->title(WINDOW_TITLE.PBOX_VERSION);
      $mainwindow->geometry(WINDOW_WIDTH."x".WINDOW_HEIGHT."+10+10");
      $mainwindow->title(WINDOW_TITLE);
      $mainwindow->maxsize(WINDOW_WIDTH,WINDOW_HEIGHT*3);
      $mainwindow->minsize(WINDOW_WIDTH,WINDOW_HEIGHT);
   
      $mainwindow_color=$mainwindow->cget(-bg);
   #============================================================
   #Canvas (panels)
   #============================================================
   
         $panel_canvas=$mainwindow->Canvas( -relief=>"groove", -borderwidth=>"2", -bg=>'#ffffff',)->
               place(-y=>10,-x=>10,-width=>90,-relheight=>'.95');
         $widget_canvas=$mainwindow->Canvas(-relief=>"groove", -borderwidth=>"2")->
               place(-y=>10,-x=>110,-width=>380,-relheight=>'.95');
   
   #============================================================
   #images:
   #============================================================
   
   my $splash=$panel_canvas->Photo(-file=>LIB_PATH."/Perlbox/pixels/splash.ppm");
   
   $widget_canvas->createImage(190,160,-image=>$splash);
   
   $widget_canvas->createText(190,310,-text=>"Press the icon labeled \'Control\' on left to begin");
   $widget_canvas->createText(190,290,-text=>"Contact: me\@perlbox.org");
   
   
   my ($img_config,$img_control,$img_help,$img_vocabulary);
   
      $img_config=$panel_canvas->Photo(-file=>LIB_PATH."/Perlbox/pixels/config.gif");
      $img_control=$panel_canvas->Photo(-file=>LIB_PATH."/Perlbox/pixels/control.gif");
      $img_help=$panel_canvas->Photo(-file=>LIB_PATH."/Perlbox/pixels/help.gif");
      $img_vocabulary=$panel_canvas->Photo(-file=>LIB_PATH."/Perlbox/pixels/vocabulary.gif");
   
   #============================================================
   #panel canvas buttons
   #============================================================
   my ($cmd_config, $cmd_control, $cmd_help, $cmd_vocabulary);
   my ($lbl_config, $lbl_control, $lbl_help, $lbl_vocabulary);
   my ($id_config, $id_control,$id_help,$id_vocabulary);
   
      $cmd_control=$panel_canvas->Button(-image=>$img_control,-bg=>'#ffffff' ,-relief=>'flat',-command => sub{&cmd_control_clicked;});
      $id_control=$panel_canvas->createWindow(15,10, -window=>$cmd_control, -anchor=>'nw',-width=>54,-height=>54);
   
      $cmd_vocabulary=$panel_canvas->Button(-image=>$img_vocabulary,-bg=>'#ffffff' ,-relief=>'flat',-command => sub{&cmd_vocabulary_clicked;});
      $id_vocabulary=$panel_canvas->createWindow(15,90, -window=>$cmd_vocabulary, -anchor=>'nw',-width=>54,-height=>54);
   
      $cmd_config=$panel_canvas->Button(-image=>$img_config,-bg=>'#ffffff' ,-relief=>'flat', -command => sub{&cmd_config_clicked;});
      $id_config=$panel_canvas->createWindow(15,170, -window=>$cmd_config, -anchor=>'nw',-width=>54,-height=>54);
   
      $cmd_help=$panel_canvas->Button(-image=>$img_help,-bg=>'#ffffff' ,-relief=>'flat', -command => sub{&cmd_help_clicked;});
      $id_help=$panel_canvas->createWindow(15,250, -window=>$cmd_help, -anchor=>'nw',-width=>54,-height=>54);
   
   
   
      #left canvas labels
      $lbl_control=$panel_canvas->Label(-text=>"Control",-bg=>'#ffffff', -fg=>'#2c6997' ,-relief=>'flat');
                  $panel_canvas->createWindow(15,65, -window=>$lbl_control, -anchor=>'nw',-width=>54,-height=>15);
   
      $lbl_vocabulary=$panel_canvas->Label(-text=>"Vocab",-bg=>'#ffffff', -fg=>'#2c6997' ,-relief=>'flat');
                  $panel_canvas->createWindow(15,145, -window=>$lbl_vocabulary, -anchor=>'nw',-width=>54,-height=>15);
   
      $lbl_config=$panel_canvas->Label(-text=>"Config",-bg=>'#ffffff', -fg=>'#2c6997' ,-relief=>'flat');
                  $panel_canvas->createWindow(15,225, -window=>$lbl_config, -anchor=>'nw',-width=>54,-height=>15);
   
   
      $lbl_help=$panel_canvas->Label(-text=>"Help",-bg=>'#ffffff', -fg=>'#2c6997' ,-relief=>'flat');
                  $panel_canvas->createWindow(15,305, -window=>$lbl_help, -anchor=>'nw',-width=>54,-height=>15);

   #initialize our timing chain callback:
   $mainwindow->repeat(1000,\&timing_chain_callback);
   
    $voice_server->say("Welcome to perlbox voice for perl T K",2);
   MainLoop;
  
}#end if( $SHOW_GRAPHICAL_INTERFACE ){
else{
   start_listener();
   while( 1 ){
      timing_chain_callback();

      if ( !defined($silent_opt)){
         system("clear");
         print $current_msg."\n"        
      }

      sleep 1;
   }
}


#here we clean up!


sub catch_int{
   my $listener_response = $voice_server->stop_listener;
   die "Caught signal, bye\n";
}

#sub is called once every second to chech for new messages
sub timing_chain_callback {

   my $new_message = $voice_server->check_messages();
   if( $new_message =~ /^SAY:/ ){
      $new_message =~ s/^SAY://;
      $voice_server->say($new_message,1);
   }
   if( $new_message ne MSG_NO_NEW_STATE and $new_message ne ""){
       $current_msg=$new_message;
       $voice_server->say($new_message,4);
   }


   return TRUE;
}

sub cmd_control_clicked{
   my $t1="This is the control pane.";
   my $t2="From here you can start and stop the listener as well as specify text for me to speak";
   $voice_server->say($t1 ,6);
   $voice_server->say($t2 ,8);


   $widget_canvas->configure(-bg=>$mainwindow_color);
   $widget_canvas->delete("all");
   my ( $lbl_msg, $cmd_speak, $cmd_startlistener,$cmd_stoplistener, $cmd_cleartxt, $cmd_exit);

   $txt_speech=$widget_canvas->Scrolled('Text',-bg=>'#ffffff', -scrollbars=>'oe',-relief=>'groove');
   $widget_canvas->createWindow(20,10, -window=>$txt_speech, -anchor=>'nw',-width=>340,-height=>200);

   $cmd_speak=$widget_canvas->Button(-text=>"Speak this Text",-relief=>'groove', -command => sub{&speak_text});
   $widget_canvas->createWindow(20,215, -window=>$cmd_speak, -anchor=>'nw',-width=>165,-height=>22);

   $cmd_cleartxt=$widget_canvas->Button(-text=>"Clear Text",-relief=>'groove', -command => sub{&clear_text;});
   $widget_canvas->createWindow(195,215, -window=>$cmd_cleartxt, -anchor=>'nw',-width=>165,-height=>22);


   $lbl_msg=$widget_canvas->Label(-textvariable=>\$current_msg,-relief=>'flat',-fg=>'#2f93c5');
   $widget_canvas->createWindow(10,240, -window=>$lbl_msg, -anchor=>'nw',-width=>360,-height=>30);

   $cmd_startlistener=$widget_canvas->Button(-text=>"Start Listener",-relief=>'groove', -command => sub{&start_listener;});
   $widget_canvas->createWindow(20,275, -window=>$cmd_startlistener, -anchor=>'nw',-width=>165,-height=>22);

   $cmd_stoplistener=$widget_canvas->Button(-text=>"Stop Listener",-relief=>'groove', -command => sub{&stop_listener;});
   $widget_canvas->createWindow(195,275, -window=>$cmd_stoplistener, -anchor=>'nw',-width=>165,-height=>22);


   $cmd_exit=$widget_canvas->Button(-text=>"Exit",-relief=>'groove', -command => sub{&exit_app});
   $widget_canvas->createWindow(20,300, -window=>$cmd_exit, -anchor=>'nw',-width=>340,-height=>22);



}

sub cmd_vocabulary_clicked{

   my $t1="This is the vocabulary pane.";
   my $t2="From here you can create and modify vocabularies for the listener";
   $voice_server->say($t1 ,6);
   $voice_server->say($t2 ,8);

   $widget_canvas->configure(-bg=>$mainwindow_color);
   $widget_canvas->delete("all");
   my ($lbl_lstspeech, $lst_command,$lbl_lstcommand, $scroll);
   my ($txt_yousay, $lbl_yousay, $txt_computerdoes,$lbl_computerdoes);
   my($cmd_addentry,$cmd_deleteentry,$cmd_loadcurrent,$cmd_createnew);
   my $yousay="";
   my $computerdoes="";


   $lst_speech=$widget_canvas->Scrolled("MListbox",-selectmode=>'single',-relief=>'groove',-bg=>'#ffffff', -scrollbars=>'oe');
   $widget_canvas->createWindow(20,10, -window=>$lst_speech, -anchor=>'nw',-width=>340,-height=>200);

   $lst_speech->Subwidget("yscrollbar")->configure(bg=>'#ffffff');
   $lst_speech->columnInsert(0,-text=>"Computer Does");
   $lst_speech->columnInsert(0,-text=>"When You Say");
   my @packorder=("0:170","1:170");
   $lst_speech->columnPack(@packorder);


   $lbl_yousay=$widget_canvas->Label(-text=>"When You Say:", -relief=>"flat", -anchor=>'nw',-justify=>'left',-wraplength=>210);
   $widget_canvas->createWindow(20,220, -window=>$lbl_yousay, -anchor=>'nw',-width=>100,-height=>22);

   $txt_yousay=$widget_canvas->Entry(-textvariable=>\$yousay, -relief=>'groove',-bg=>'#ffffff');
   $widget_canvas->createWindow(20,240, -window=>$txt_yousay, -anchor=>'nw',-width=>165,-height=>22);
   

   $lbl_computerdoes=$widget_canvas->Label(-text=>"Computer Does:", -relief=>"flat", -anchor=>'nw',-justify=>'left',-wraplength=>210);
   $widget_canvas->createWindow(195,220, -window=>$lbl_computerdoes, -anchor=>'nw',-width=>165,-height=>22);

   $txt_computerdoes=$widget_canvas->Entry(-textvariable=>\$computerdoes, -relief=>'groove',-bg=>'#ffffff');
   $widget_canvas->createWindow(195,240, -window=>$txt_computerdoes, -anchor=>'nw',-width=>165,-height=>22);


   $cmd_addentry=$widget_canvas->Button(-text=>"Add Entry",-relief=>'groove', -command => sub{&vocab_add_entry($yousay,$computerdoes);$yousay="";$computerdoes="";});
   $widget_canvas->createWindow(20,275, -window=>$cmd_addentry, -anchor=>'nw',-width=>165,-height=>22);

   $cmd_deleteentry=$widget_canvas->Button(-text=>"Delete Entry",-relief=>'groove', -command => sub{&vocab_delete_entry;});
   $widget_canvas->createWindow(195,275, -window=>$cmd_deleteentry, -anchor=>'nw',-width=>165,-height=>22);

   $cmd_loadcurrent=$widget_canvas->Button(-text=>"Reset Fields",-relief=>'groove', -command => sub{&vocab_load_current;});
   $widget_canvas->createWindow(20,300, -window=>$cmd_loadcurrent, -anchor=>'nw',-width=>165,-height=>22);

   $cmd_createnew=$widget_canvas->Button(-text=>"Apply Changes",-relief=>'groove', -command => sub{&vocab_create_new});
   $widget_canvas->createWindow(195,300, -window=>$cmd_createnew, -anchor=>'nw',-width=>165,-height=>22);

   vocab_load_current();

}

sub cmd_config_clicked{

   my $t1="This is the configuration pane.";
   my $t2="From here you can specify parameters such as my verbosity level and helper applications";
   $voice_server->say($t1 ,6);
   $voice_server->say($t2 ,8);

   $widget_canvas->configure(-bg=>$mainwindow_color);
   $widget_canvas->delete("all");
   my($scale_verbosity);
   my( $txt_browser, $help_browser, $lbl_browser );
   my( $cmd_apply, $cmd_reset );
   my( $opt_desktop, $lbl_desktop, $lbl_loaded_desktop, $selected_desktop, $available_desktops, $loaded_desktop );
   #magic word
   my( $chk_magic, $txt_magic, $magic_word, $use_magic);
   
   
   #verbosity level of speaker
   $verbosity_level = $voice_server->get_verbosity();
   $scale_verbosity = $widget_canvas->Scale( -orient=>'horizontal',-label=>"Set Talker's Verbosity Level",-from=>0,-to=>10,-tickinterval=>2,-variable=>\$verbosity_level);
   $widget_canvas->createWindow(20,20, -window=>$scale_verbosity, -anchor=>'nw',-width=>220,-height=>60);


   #default browser to use for browsing documentation
   $help_browser = $voice_server->get_helpbrowser();
   $txt_browser = $widget_canvas->Entry( -textvariable=>\$help_browser);
   $widget_canvas->createWindow( 20,100, -window=>$txt_browser, -anchor=>'nw',-width=>120,-height=>20);


   $lbl_browser = $widget_canvas->Label(-text=>"Browser to view help documentation", -relief=>"flat", -anchor=>'nw',-justify=>'left',-wraplength=>210);
   $widget_canvas->createWindow(150,100, -window=>$lbl_browser, -anchor=>'nw',-width=>220,-height=>60);

   $loaded_desktop = $voice_server->get_currrent_desktop_plugin();
   $available_desktops = $voice_server->get_desktop_plugins();

   $opt_desktop = $widget_canvas->Optionmenu(-options=>["No Change",@$available_desktops],-textvariable=>\$selected_desktop);
   $widget_canvas->createWindow( 20,170, -window=>$opt_desktop, -anchor=>'nw',-width=>120,-height=>30);

   $lbl_desktop = $widget_canvas->Label(-text=>"Desktop Plugins", -relief=>"flat", -anchor=>'nw',-justify=>'left',-wraplength=>210);
   $widget_canvas->createWindow(20,150, -window=>$lbl_desktop, -anchor=>'nw',-width=>220,-height=>20);

   $lbl_loaded_desktop = $widget_canvas->Label(-text=>"Current Plugin: ".$loaded_desktop, -relief=>"flat", -anchor=>'nw',-justify=>'left',-wraplength=>210);
   $widget_canvas->createWindow(150,170, -window=>$lbl_loaded_desktop, -anchor=>'nw',-width=>220,-height=>60);

   #now for the magic word:

   $magic_word = $voice_server->get_magicword();
   $txt_magic = $widget_canvas->Entry( -textvariable=>\$magic_word);
   $widget_canvas->createWindow( 20,220, -window=>$txt_magic, -anchor=>'nw',-width=>120,-height=>20);
      
   $use_magic = $voice_server->get_use_magicword();
   $chk_magic = $widget_canvas->Checkbutton(-text=>"Use magic word?", -variable=>\$use_magic);
   $widget_canvas->createWindow(150,220, -window=>$chk_magic, -anchor=>'nw',-width=>120,-height=>20);
   
   $chk_magic->select if $use_magic;
   $chk_magic->deselect if not $use_magic;
   
   #bottom buttons (Apply)
   $cmd_reset=$widget_canvas->Button(-text=>"Reset Values",-relief=>'groove', -command => sub{$help_browser=$voice_server->get_helpbrowser();$verbosity_level=$voice_server->get_verbosity();});
   $widget_canvas->createWindow(20,300, -window=>$cmd_reset, -anchor=>'nw',-width=>100,-height=>25);

   $cmd_apply=$widget_canvas->Button(-text=>"Apply Changes",-relief=>'groove', -command => sub{&apply_configuration($help_browser,$verbosity_level,$selected_desktop, $use_magic, $magic_word);});
   $widget_canvas->createWindow(260,300, -window=>$cmd_apply, -anchor=>'nw',-width=>100,-height=>25);

}

sub cmd_help_clicked{

   my $t1="This is the help pane.";
   my $t2="Here you can launch help documents in your favorite web browser";
   $voice_server->say($t1 ,6);
   $voice_server->say($t2 ,8);

   $widget_canvas->configure(-bg=>$mainwindow_color);
   $widget_canvas->delete("all");
   #so fill in the 'help' buttons.
   my($cmd_startfaq,$cmd_starttutorial,$cmd_startabout,$cmd_startapi,$cmd_startplug);
   my($lbl_startfaq,$lbl_starttutorial,$lbl_startabout,$lbl_startapi,$lbl_startplug);
   my($id_startfaq,$id_starttutorial,$id_startabout,$id_startapi, $id_startplug);

   $cmd_starttutorial=$widget_canvas->Button(-text=>"Open Tutorial",-relief=>'groove', -command => sub{&open_doc(1);});
   $id_starttutorial=$widget_canvas->createWindow(20,30, -window=>$cmd_starttutorial, -anchor=>'nw',-width=>100,-height=>30);

   $lbl_starttutorial=$widget_canvas->Label(-text=>"View the Perlbox Voice step by step tutorial in your web browser",-anchor=>'nw',-justify=>'left',-wraplength=>220);
   $widget_canvas->createWindow(130,30, -window=>$lbl_starttutorial, -anchor=>'nw',-width=>220,-height=>60);

   $cmd_startfaq=$widget_canvas->Button(-text=>"Open FAQ",-relief=>'groove', -command => sub{&open_doc(2);});
   $id_startfaq=$widget_canvas->createWindow(20,90, -window=>$cmd_startfaq, -anchor=>'nw',-width=>100,-height=>30);

   $id_startfaq=$widget_canvas->Label(-text=>"View the Frequently Asked Questions in your web browser",-anchor=>'nw',-justify=>'left',-wraplength=>220);
   $widget_canvas->createWindow(130,90, -window=>$id_startfaq, -anchor=>'nw',-width=>220,-height=>60);

   $cmd_startapi=$widget_canvas->Button(-text=>"Open API",-relief=>'groove', -command => sub{&open_doc(3);});
   $id_startapi=$widget_canvas->createWindow(20,150, -window=>$cmd_startapi, -anchor=>'nw',-width=>100,-height=>30);

   $lbl_startapi=$widget_canvas->Label(-text=>"View the Perlbox Voice Application Framework API",-anchor=>'nw',-justify=>'left',-wraplength=>220);
   $widget_canvas->createWindow(130,150, -window=>$lbl_startapi, -anchor=>'nw',-width=>220,-height=>60);

   $cmd_startplug=$widget_canvas->Button(-text=>"Plugin Docs",-relief=>'groove', -command => sub{&open_doc(4);});
   $id_startplug=$widget_canvas->createWindow(20,210, -window=>$cmd_startplug, -anchor=>'nw',-width=>100,-height=>30);

   $lbl_startplug=$widget_canvas->Label(-text=>"View the Plugin Documentation",-anchor=>'nw',-justify=>'left',-wraplength=>220);
   $widget_canvas->createWindow(130,210, -window=>$lbl_startplug, -anchor=>'nw',-width=>220,-height=>60);

   $cmd_startabout=$widget_canvas->Button(-text=>"About",-relief=>'groove', -command => sub{&open_doc(5);});
   $id_startapi=$widget_canvas->createWindow(20,270, -window=>$cmd_startabout, -anchor=>'nw',-width=>100,-height=>30);

   $lbl_startabout=$widget_canvas->Label(-text=>"About Perlbox Voice for TK",-anchor=>'nw',-justify=>'left',-wraplength=>220);
   $widget_canvas->createWindow(130,270, -window=>$lbl_startabout, -anchor=>'nw',-width=>220,-height=>60);
}

sub open_doc{
 my $purpose=shift;
 if($purpose==1){
   $voice_server->start_tutorial;
 }
 elsif($purpose==2){
   $voice_server->start_help;
 }
 elsif($purpose==3){
   $voice_server->start_api;
 }
 elsif($purpose==4){
   $voice_server->start_plugdoc;
 }
 elsif($purpose==5){
       my $text="Perlbox Voice ".PBOX_VERSION."\nDecember 2004\nby Shane Mason\n(me\@perlbox.org)\nhttp://perlbox.org";
       my $d=$mainwindow->pSimpleDialog(-title => "perlbox",-text =>$text,-OKonly=>'yes');
       my $button = $d->Show;
       if(!$button){
         &myCANCEL;
       }
       else{
        return;
       }
 }
}

sub apply_configuration{

   $voice_server->set_helpbrowser(shift);
   $voice_server->set_verbosity(shift);
   my $requested_dtop = shift;
   my $usemagic = shift;
   my $magic = shift;
      
   my $create_new_model = FALSE;
   
   #we don't want to load our placeholder  
   if( $requested_dtop ne "No Change" ){
      $voice_server->set_currrent_desktop_plugin( $requested_dtop );
      $create_new_model = TRUE;
   }
   
   
   if( $usemagic != $voice_server->get_use_magicword() or $magic ne $voice_server->get_magicword()){
      $voice_server->set_use_magicword($usemagic);
      $voice_server->set_magicword($magic); 
      $create_new_model = TRUE;  
   }
   
   if( $create_new_model ){
     #load the current lm
      #create two arrays to pass by reference to voice server
      my @human_says;
      my @computer_does;
      #pass to $voice_server->commands_toarrays, which will fill
      #them with commands/Actions
      $voice_server->commands_toarrays(\@human_says,\@computer_does);
      my $text=$voice_server->create_language_model(\@human_says, \@computer_does);      
   }
   
   #now we want to redraw the config screen 
   cmd_config_clicked();
}

sub speak_text{
   my $this_text=$txt_speech->get("1.0","end");
   $voice_server->say($this_text,1);
}

sub clear_text{
   $txt_speech->delete("1.0","end");
}

sub exit_app{
    my $listener_response = $voice_server->killall;
        $current_msg="I am exiting";
    exit;
}

sub start_listener{
    my $listener_response = $voice_server->start_listener;
    $current_msg="$listener_response";
}

sub stop_listener{
    my $listener_response = $voice_server->stop_listener;
        $current_msg = $listener_response;
}



sub vocab_delete_entry{
   $lst_speech->delete($lst_speech->curselection());
}

sub vocab_add_entry{
   my ($yousay,$computerdoes);
   $yousay = shift;
   $computerdoes=shift;
    if(($voice_server->validate_vocabulary_entry($yousay)==TRUE)){
       my @toadd=($yousay,$computerdoes);
       $lst_speech->insert('end',\@toadd);

    }
    else{
       my $text="ERROR: Only alpha and whitespace allowed in field \"When You Say\".";
       my $d=$mainwindow->pSimpleDialog(-title => "perlbox",-text =>$text,-OKonly=>'yes');
       my $button = $d->Show;
       if(!$button){
         &myCANCEL;
       }
       else{
         return;
       }
    }
}

sub vocab_load_current{
   $lst_speech->delete('0.0','end');
    #load the current lm
    #create two arrays to pass by reference to voice server
    my @human_says;
    my @computer_does;
    #pass to $voice_server->commands_toarrays, which will fill
    #them with commands/Actions
    $voice_server->commands_toarrays(\@human_says,\@computer_does);

    #now fill the list with the current model
    #get the number of commands
    my $num_commands=@computer_does;
    my $i;
    for($i=0;$i<$num_commands;$i++){
       my @toadd=($human_says[$i],$computer_does[$i]);
       $lst_speech->insert('end',\@toadd);
    }

}

sub vocab_create_new{
    my (@phrase_array, @execute_array);
    my $numrows = $lst_speech->size();

    #loop through, collecting each row getting the human phrase and execute statement
    for(my $row=0;$row<$numrows;$row++){
       ($phrase_array[$row],$execute_array[$row])=$lst_speech->getRow($row);
    }
       #call the language model, and give it the array
       my $text=$voice_server->create_language_model(\@phrase_array, \@execute_array);
       my $d=$mainwindow->pSimpleDialog(-title => "perlbox",-text =>$text,-OKonly=>'yes');
       my $button = $d->Show;
       if(!$button){
         &myCANCEL;
       }
       else{
        return;
       }
}

