// file: lxt_cstr_2.cc
//

// isip include files
//
#include "lex_tree.h"
#include "lex_tree_constants.h"

// method: constructor
//
// arguments:
//  void_p history: (input) the word node
//
// return: none
//
// this is an overloaded constructor to build the tree for a lattice
// node
//
Lex_tree::Lex_tree(void_p history_a) {
  
  // initialize the word history
  //
  history_d = history_a;

  // the word history is a lattice node here
  //
  Lattice_node* latnode = (Lattice_node*)history_d;
  head_d = (Lex_node*)NULL;
  
  // mark status as true i.e. active
  //
  status_d = ISIP_TRUE;
  
  // get all possible next words for this history lattice node
  //
  Link_list* next_list = latnode->get_next_nodes_cc();
  
  // if there are no next words return
  //
  if (next_list != (Link_list*)NULL) {
    
    // the memory manager
    //
    Memory_manager* manager = Link_list::get_manager_cc();
    Lattice_node* lnode = (Lattice_node*)NULL;
    float_4* lscore = latnode->get_lm_scores_cc();
    int_4 idx = (int_4)0;
    
    // create a start node for this tree
    //
    head_d = manager->new_lex_cc();
    head_d->set_phone_cc(LXN_START_PHONE);
    head_d->set_type_cc(LXN_WORDGRAPH);

    // loop over all words and add them to the tree
    //
    for (Link_node* nd = next_list->get_head_cc(); nd != (Link_node*)NULL;
	 nd = nd->get_next_cc()) {
      
      // get the next lattice node
      //
      lnode = (Lattice_node*)(nd->get_item_cc());
      
      // add this word to the tree
      //
      grow_tree_cc(lnode, lscore[idx++], (int_4)0);
    }
  }
  
  // exit gracefully
  //
}
