// file: ht_dm_lat_0.cc
//

// system include files
//
#include <memory.h>
#include <string.h>
#include <ctype.h>

// isip include files
//
#include "hmm_train.h"
#include "hmm_train_constants.h"

// method: built_dm_lat_cc
//
// arguments:
//  FILE* fp_in_a: (input) input transcription file
//  int_4* word_table : (input) the hash table of word indices
//
// return: a logical_1 indicating status
//
// this method reads in the lattice from a file
//
logical_1 build_dm_lat_cc(Train_Lattice* lattice_a, Train_Hash_table* words_a) {

 // define some local variables
  //
  
  char_1** nodes = new char_1*[TRAIN_LATTICE_MAX_NUM_WORD];
  int_4 num_nodes = (int_4)0;
  int_4 num_arcs = (int_4)0;
  int_4 start_node = (int_4)0;
  int_4 end_node = (int_4)0;
  float_8 lm_score = (float_4)0;
  float_8 ac_score = (float_4)0;

  Train_Lattice_node** lnodes = (Train_Lattice_node**)NULL;
  Train_Word* word = (Train_Word*)NULL;
  Train_Hash_cell* hcell = (Train_Hash_cell*)NULL;
  
 
  // set the start node
  //
  nodes[1] = new char_1[strlen((char*)("!SENT_START"))+1];
  strcpy((char*)nodes[1], (char*)("!SENT_START"));
 
  nodes[2] = new char_1[strlen((char*)("!SENT_END"))+1];
  strcpy((char*)nodes[2], (char*)("!SENT_END"));

  // count number of nodes and arcs
  //
  num_nodes = 3;
  num_arcs = 2;
  
  // use above information to generate a lattice
  //
  lattice_a->set_num_nodes_cc(num_nodes);
  lattice_a->set_num_arcs_cc(num_arcs);

  
  lnodes = new Train_Lattice_node*[num_nodes];
  // memory manager
  //
  Train_Memory_manager* manager = Train_Link_list::get_manager_cc();
  
  // declare memory for each node and add index
  //
  for (int_4 i = 0; i < num_nodes; i++) {
    lnodes[i] = manager->new_lat_cc();
    lnodes[i]->set_node_index_cc(i);
    lattice_a->set_lat_node_cc(lnodes[i]);
  }
  
  // create the lattice start node --- this is a dummy node that is
  // not evaluated, it just serves as a starting point for all the
  // sentence start nodes
  //
  lattice_a->set_start_node_cc(lnodes[0]);

  // loop over all nodes
  //
  for (int i = 0; i < num_nodes - 1; i++) {

    // set the endpoints for each arc in the lattice
    //
    start_node = i;
    end_node = i + 1;
    
    // find the corresponding word in the lexicon
    //
    hcell = words_a->hash_lookup_cc((char_1*)nodes[end_node]);
    
    // error if word not found in lexicon
    //
    if (hcell == (Train_Hash_cell*)NULL) {
      printf ("%s\n", (char*)nodes[end_node]);
      lattice_a->error_handler_cc((char_1*)"lat_read_0.cc",
		       (char_1*)"Train_Word does not exist in lexicon");
    }	
    
    // set the item index of the node
    //
    word = (Train_Word*)(hcell->get_item_cc());
    lnodes[end_node]->set_word_cc(word);
    
    // update the prev node and next node pointers for the
    // two nodes corresponding to this arc
    //
    lnodes[start_node]->add_next_node_cc(lnodes[end_node]);
    lnodes[end_node]->add_prev_node_cc(lnodes[start_node]);
    
    // set the acoustic and language model scores
    //
    lnodes[start_node]->add_lm_score_cc(lm_score);
    lnodes[start_node]->add_ac_score_cc(ac_score);
  }
  
  // free memory
  //
  for (int i = 1; i < num_nodes; i++) {
    delete [] nodes[i];
  }
  delete [] nodes;
  delete [] lnodes;
  
  // exit gracefully
  //
  return ISIP_TRUE;
}

