// file: dec_project_1.cc
//
 
// isip include files
//
#include "decoder.h"
#include "decoder_constants.h"
 
// method: project_sp_word_cc
//
// arguments:
//  Trace* trace: (input) the input model-level trace
//  Lex_node* lexnode: (input) lexical node for the parent trace
//  int_4 phone: (input) the current cd model index
//  
// return: a logical_1 indicating status
//
// this method handles sp model skips for non-crossword decoding modes
// by creating the corresponding word-end model-level traces
//
logical_1 Decoder::project_sp_word_cc(Trace* trace_a, Lex_node* lexnode_a,
				       int_4 phone_a) {

  // dummy variables
  //
  float_4 lmscore =(float_4)0;
  float_4 lmahead =(float_4)0;
  Trace* new_tr = (Trace*)NULL;
  Lex_node* lexnew = (Lex_node*)NULL;
  Link_list* wrd_list = (Link_list*)NULL;
  
  // path history of the trace
  //
  History* hist = trace_a->get_hist_cc();

  // instance information variables
  //
  Instance* inst = trace_a->get_inst_cc();
  
  // get the lm look-ahead score
  //
  lmahead = inst->get_lmscore_cc();
  lmahead *= lmscale_d;
  inst = (Instance*)NULL;
  
  // get the list of next phones from the current lexical tree
  //
  Link_list* lex_list = lexnode_a->get_child_cc();
  for (Link_node* nlx = lex_list->get_head_cc();
       nlx != (Link_node*)NULL; nlx = nlx->get_next_cc()) {
    
    // get the new lex node
    //
    lexnew = (Lex_node*)(nlx->get_item_cc());
    
    // get the instance here
    //
    get_instance_cc(inst, hist, lexnew, phone_a);

    // get the scaled lm score to update the word-level score
    //
    lmscore = inst->get_lmscore_cc();
    lmscore *= lmscale_d;
    
    // list of words that end at this point
    //
    wrd_list = lexnew->get_words_cc();
    
    // find the nodes corresponding to the words that end here
    //
    for (Link_node* lln = wrd_list->get_head_cc();
	 lln != (Link_node*)NULL; lln = lln->get_next_cc()) {
      
      // create the new word-end phone trace
      //
      new_tr = manager_d->new_trace_cc();
      new_tr->project_trace_cc(trace_a, lmscore - lmahead + sp_score_d);
      new_tr->set_inst_cc(inst);
      new_tr->set_max_hist_cc(DEC_PHONE_BACKPTR);

      // if the alignment mode is phone-level or state-level
      //
      if ((align_mode_d == DEC_STATE_ALIGN_MODE) ||
	  (align_mode_d == DEC_MODEL_ALIGN_MODE)) {
	
	// update the reference count
	//
	new_tr->incr_ref_cc();
      }
      
      // update the instance score
      //
      inst->compare_score_cc(new_tr->get_score_cc());

      // for end of phone update the phone-level score and put this in
      // the active traces list
      //
      insert_model_cc(model_trlist_d[phone_a], new_tr);
            
    } // end loop over all words that end here
  } // end loop over all child link list nodes

  // exit gracefully
  //
  return ISIP_TRUE;
}
