// file: dec_wer_0.cc
//

// isip include files
//
#include "decoder.h"
#include "decoder_constants.h"
 
// method: lattice_wer_cc
//
// arguments:
//  FILE* flat: (input) file with list of lattice files
//  FILE* fref: (input) file with list of transcription
//  FILE* fout: (input) file with the output
//
// return: logical_1 indicating status
//
// this method computes the lattice wer of each lattice in the input
// lattice file with the corresponding utterance transcription in the
// input reference file and outputs individual as well as cumulative
// WER statistics
//
logical_1 Decoder::lattice_wer_cc(FILE* flat_a, FILE* fref_a,
				  FILE* fout_a) {

  // strings to read filenames and transcriptions
  //
  char_1* lattice_in = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* transcription = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* alignment = new char_1[ISIP_MAX_STRING_LENGTH];

  // error values
  //
  int_4 nref = (int_4)0;
  int_4 corr = (int_4)0;
  int_4 subs = (int_4)0;
  int_4 dels = (int_4)0;
  int_4 ins = (int_4)0;
  int_4 total_nref = (int_4)0;
  int_4 total_corr = (int_4)0;
  int_4 total_subs = (int_4)0;
  int_4 total_dels = (int_4)0;
  int_4 total_ins = (int_4)0;

  // file count
  //
  int_4 count = (int_4)0;
  
  // file pointer for lattice file
  //
  FILE* fp = (FILE*)NULL;

  // make sure there is a corresponding lattice file for each file
  //
  while (fgets((char*)lattice_in, ISIP_MAX_STRING_LENGTH, flat_a) !=
	 (char*)NULL) {
    
    // read lattice into the decoder and close file
    //
    expand_filename_cc(lattice_in);
    fp = fopen((char*)lattice_in, "r");
    fprintf(fout_a, "id: %s\n", lattice_in);
    read_lattice_cc(fp);
    fclose(fp);

    // make sure there is a corresponding transcription string for
    // each file
    //
    if (fgets((char*)transcription, ISIP_MAX_STRING_LENGTH, fref_a) ==
	(char*)NULL) {
      error_handler_cc((char_1*)"lattice_wer_cc",
		       (char_1*)"transcription not found");
    }
    expand_filename_cc(transcription);

    // set the transcription in the lattice
    //
    lattice_d->set_utterance_cc(transcription);

    // compute the wer of the lattice
    //
    count++;
    lattice_d->error_rate_cc(alignment, nref, corr, subs, dels, ins,
			     lexicon_d);

    // print this to output file
    //
    fprintf(fout_a, "Scores: (#C #S #D #I) %ld %ld %ld %ld\n",
	    corr, subs, dels, ins);
    fprintf(fout_a, "REF: %s\n", transcription);
    fprintf(fout_a, "HYP: %s\n", alignment);
    fprintf(fout_a, "\n");
    fflush(fout_a);
    
    // update cumulative counts
    //
    total_nref += nref;
    total_corr += corr;
    total_subs += subs;
    total_dels += dels;
    total_ins += ins;

    // free memory
    //
    delete lattice_d;
    lattice_d = (Lattice*)NULL;
    
  } // end while loop
  
  // print final statistics to output file
  //
  if (count > (int_4)0) {
    
    int_4 total_err = total_subs + total_dels + total_ins;

    fprintf(fout_a, "WORD RECOGNITION PERFORMANCE\n\n");
    fprintf(fout_a, "%-32s = %.1f%%   (%ld)\n\n",
	    "Percent Total Error",
	    (float_4)total_err * 100 / (float_4)total_nref,
	    total_err);
    
    fprintf(fout_a, "%-32s = %.1f%%   (%ld)\n\n",
	    "Percent Correct",
	    (float_4)total_corr * 100 / (float_4)total_nref,
	    total_corr);
    
    fprintf(fout_a, "%-32s = %.1f%%   (%ld)\n",
	    "Percent Substitutions",
	    (float_4)total_subs * 100 / (float_4)total_nref,
	    total_subs);
    
    fprintf(fout_a, "%-32s = %.1f%%   (%ld)\n",
	    "Percent Deletions",
	    (float_4)total_dels * 100 / (float_4)total_nref,
	    total_dels);
    
    fprintf(fout_a, "%-32s = %.1f%%   (%ld)\n",
	    "Percent Insertions",
	    (float_4)total_ins * 100 / (float_4)total_nref,
	    total_ins);

    fprintf(fout_a, "%-32s = %.1f%%   \n\n",
	    "Percent Word Accuracy",
	    (float_4)(total_err - total_ins) * 100 / (float_4)total_nref);

    fprintf(fout_a, "%-32s =        (%ld)\n",
	    "Ref. words", total_nref);

    fprintf(fout_a, "%-32s =        (%ld)\n\n",
	    "Hyp. words", total_corr + total_subs + total_ins);
  }
  else {
    fprintf(stdout, "No files processed.\n");
  }
  
  // free memory
  //
  delete [] lattice_in;
  delete [] transcription;
  delete [] alignment;
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
