// file: lxt_tree_1.cc
//
// function to count number of lex nodes in a tree
//

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

// method: build_tree_cc
//
// arguments:
//  int_4 num : (input) the number of unigram words at this tree
//  Ngram_node** ngmnodes : (input) the list of unigram words to be added
//
// return: a logical_1 to indicate success
//
// this method loops over all the input words and for each
// pronunciations of the word, it checks phone-by-phone if the current
// phone is already on the correct branch of the tree and inserts the
// word in the corresponding node
//
logical_1 Lex_tree::build_tree_cc(int_4 num_a, Ngram_node** ngmnodes_a) {
  
  // phone / word parameters
  //
  int_4 phone = (int_4)-1;
  int_4 num_prons = (int_4)0;
  int_4* num_ph = (int_4*)NULL;
  int_4** phlist = (int_4**)NULL;
  
  // dummy lexical nodes
  //
  Lex_node* curr_node = (Lex_node*)NULL;
  Lex_node* next_node = (Lex_node*)NULL;
  Lex_node* stop_node = (Lex_node*)NULL;

  // memory manager
  //
  Memory_manager* manager = Link_list::get_manager_cc();

  // set the head node
  //
  if (head_d == (Lex_node*)NULL) {
    head_d = manager->new_lex_cc();
    head_d->set_phone_cc(LXN_START_PHONE);
    head_d->set_type_cc(LXN_NGRAM);
  }

  // mark status as true i.e. active
  //
  status_d = ISIP_TRUE;

  // get each word in the input word list
  //
  Word* word = (Word*)NULL;

  // loop over each word in the list
  //
  for (int_4 k = 0; k < num_a; k++) {

    // get the word
    //
    word = ngmnodes_a[k]->get_word_cc();

    // create a stop node
    //
    stop_node = manager->new_lex_cc();
    stop_node->set_phone_cc(LXN_STOP_PHONE);
    stop_node->set_type_cc(LXN_NGRAM);
    stop_node->add_word_cc(ngmnodes_a[k]);
    
    // get the pronunciations for this word
    //
    num_prons = word->get_num_prons_cc();
    phlist = word->get_phone_list_cc();
    num_ph = word->get_num_phones_cc();
    
    // loop over all the pronunciations for this word
    //
    for (int_4 i = 0; i < num_prons; i++) {
      
      // set the next node as current
      //
      next_node = head_d;
      
      // loop over all phones in this pronunciations
      //
      for (int_4 j = 0; j < num_ph[i]; j++) {
	
	// set the current node
	//
	curr_node = next_node;
	
	// the jth phone of this ith pronunciation
	//
	phone = phlist[i][j];
	
	// get the node in the tree that contains this phone
	//
	next_node = curr_node->make_node_cc(phone);
	next_node->add_word_cc(ngmnodes_a[k]);
	next_node->set_type_cc(LXN_NGRAM);
	
      } // end for j
      
      // add the stop node at the end of word and mark word end
      //
      next_node->add_child_cc(stop_node);
      next_node->set_word_end_cc(ISIP_TRUE);

    } // end loop over all pronunciations
  } // end for loop over k
  
  // exit gracefully
  //
  return ISIP_TRUE;  
}
