// file: dec_read_5.cc
//
 
// system include files
//
#include <string.h>

// isip include files
//
#include "decoder.h"
#include "decoder_constants.h"
 
// method: read_lexicon_cc
//
// arguments:
//  FILE* fp: (input) file pointer
//  
// return: a logical_1 indicating status
//
// this method reads in the word data from file into a hash table
//
logical_1 Decoder::read_lexicon_cc(FILE* fp_a) {
  
  // check file
  //
  if (fp_a == (FILE*)NULL) {
    error_handler_cc((char_1*)"read_lexicon_cc",
		     (char_1*)"NULL input file pointer");
  }
  
  // variables to read data
  //
  int_4 num_ph = (int_4)0;
  int_4* phones = new int_4[ISIP_MAX_STRING_LENGTH];

  char_1* ph = (char_1*)NULL;
  char_1* wdstr = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* phstr = new char_1[ISIP_MAX_STRING_LENGTH];
  
  Word* word = (Word*)NULL;
  Hash_cell* wrd_cell = (Hash_cell*)NULL;
  
  // initialize the number of words
  //
  num_words_d = (int_4)0;
  
  // allocate space for the word hash table
  //
  lexicon_d = new Hash_table(HASH_TABLE_2EXP10);
  
  // read data from file
  //		
  while (fscanf(fp_a, "%s", wdstr) != EOF) {
    
    // ignore comment lines
    //
    if (wdstr[0] == (char_1)'#') {
      
      // do nothing
      //
      fscanf(fp_a, "%[^\n]", wdstr);
      fscanf(fp_a, "%[\n]", wdstr);
    }
    
    // otherwise this is a word entry --- read the pronunciations as
    // well
    //
    else {
      
      // read the pronunciation string
      //
      memset(phstr, 0, ISIP_MAX_STRING_LENGTH);
      fscanf(fp_a, "%[\t\b]", phstr);
      fscanf(fp_a, "%[^\n]", phstr);
      
      // variables to store the pronunciation
      //
      num_ph = (int_4)0;
      memset((void_p)phones, 0, sizeof(int_4)*ISIP_MAX_STRING_LENGTH);

      // now break it into phone components and list them
      //
      ph = (char_1*)strtok((char*)phstr, " ");
      if (ph != (char_1*)NULL) {
	for (int_4 k = 0; k < num_ci_d; k++) {
	  if (strcmp((char*)ph, (char*)ci_models_d[k]) == 0) {
	    phones[num_ph++] = k;
	    break;
	  }
	}
	while ((ph = (char_1*)strtok(NULL, " "))) {
	  for (int_4 k = 0; k < num_ci_d; k++) {
	    if (strcmp((char*)ph, (char*)ci_models_d[k]) == 0) {
	      phones[num_ph++] = k;
	      break;
	    }
	  } // end for
	} // end while
      } // end if ph is not null

      // see if this word already exists
      //
      wrd_cell = lexicon_d->hash_lookup_cc(wdstr);

      // if it exists add this pronunciation to it
      //
      if (wrd_cell != (Hash_cell*)NULL) {

	// get the word
	//
	word = (Word*)(wrd_cell->get_item_cc());
	
	// add the pronunciation
	//
	word->add_pron_cc(num_ph, phones);
      }

      // otherwise create a word model and put it in the hash table
      //
      else {

	// make a new word
	//
	word = new Word(num_words_d, wdstr, num_ph, phones);
      
	// put it in the hash table
	//
	wrd_cell = manager_d->new_hash_cc();
	wrd_cell->set_cc(wdstr, (void_p)word);
	lexicon_d->hash_insert_cc(wrd_cell);
	
	// increment word count
	//
	num_words_d++;
      }
    }
  }

  // delete memory
  //
  delete [] wdstr;
  delete [] phstr;
  delete [] phones;
  wdstr = (char_1*)NULL;
  phstr = (char_1*)NULL;
  phones = (int_4*)NULL;
  
  // if no word !NULL in the lexicon, add the word !NULL which has no
  // pronunciations
  //
  wrd_cell = lexicon_d->hash_lookup_cc(WRD_NULL);
  if (wrd_cell == (Hash_cell*)NULL) {

    // reset counts
    //
    num_ph = (int_4)0;
    phones = (int_4*)NULL;
    
    // create a !NULL word
    //
    word = new Word(num_words_d, WRD_NULL, num_ph, phones);
    wrd_cell = manager_d->new_hash_cc();
    wrd_cell->set_cc(WRD_NULL, (void_p)word);
    lexicon_d->hash_insert_cc(wrd_cell);
    num_words_d++;
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
