// file: $isip/class/mmedia/NGramParser/ngmp_02.cc
// version: $Id: ngmp_02.cc 8246 2002-06-27 16:36:58Z zheng $
//

// isip include files
//
#include "NGramParser.h"
#include <Console.h>

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean NGramParser::diagnose(Integral::DEBUG level_a) {

  level_a = Integral::ALL;

  //---------------------------------------------------------------------
  //
  // 0. preliminaries
  //
  //---------------------------------------------------------------------

  // output the class name
  //
  if (level_a > Integral::NONE) {
    String output(L"diagnosing class ");
    output.concat(CLASS_NAME);
    output.concat(L": ");
    Console::put(output);
    Console::increaseIndention();
  } 

  //--------------------------------------------------------------------
  //
  // 1. required public methods
  //
  //--------------------------------------------------------------------

  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing required public methods...\n");
    Console::increaseIndention();
  }

  // test destructor/constructor(s)
  //
  NGramParser np0;
  NGramParser np1(np0);
  
  if (!np1.eq(np0)) {
    return Error::handle(name(), L"copy constructor", Error::TEST,
			 __FILE__, __LINE__);
  }

  // test large allocation construction and deletion
  //
  if (level_a > Integral::BRIEF) {
    
    Console::put(L"testing large chunk memory allocation and deletion:\n");
    
    // set the memory to a strange block size so we can hopefully catch any
    // frame overrun errors
    //
    NGramParser::setGrowSize((long)500);
    
    NGramParser* pft = new NGramParser();
    
    for (long j = 1; j <= 100; j++) {
      NGramParser** pfts = new NGramParser*[j * 100];
      
      // create the objects
      //
      for (long i = 0; i < j * 100; i++) {
	pfts[i] = new NGramParser();
      }
    
      // delete objects
      //
      for (long i = (j * 100) - 1; i >= 0; i--) {
	delete pfts[i];
      }
    
      delete [] pfts;
    } 
    delete pft;
  }

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 2. class-specific public methods:
  //     algorithm methods
  //
  //--------------------------------------------------------------------------

  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing class-specific public methods: computational methods...\n");
    Console::increaseIndention();
  }

  // declare local variables
  //
  NGramParser np(3);
  HashTable<Long, NGramNode> gram_hash;
  Sof sof;

  // convert SearchLevel's SearchSymbol hash table to String hash table
  //
  String all_symbol(L"!SENT_START !SENT_END ZERO ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE OH");
  Vector<String> symbol_table;
  String symbol;
  long pos = 0;
  Long index(0);
  String file_name;
  
  while(all_symbol.tokenize(symbol, pos)) {
    symbol.trim();
    if (!symbol.eq(String::DEF_VALUE)) {
      symbol_table.setLength((long)index + 1);
      symbol_table(index).assign(symbol);
      index++;
    }
  }
  
  // open the language model file
  //
  file_name.assign(L"../../../doc/examples/data/models/tidigits_trigram.arpa");
  if (!sof.open(file_name, File::READ_ONLY)) {
    return Error::handle(file_name, L"open", Error::FILE_NOTFND,
			 __FILE__, __LINE__);
  }

  // load a trigram file into hash table
  //
  np.load(sof, gram_hash, symbol_table, 1, L"NGramModel");

  // close file
  //
  sof.close();
  
  // write the instance of the object into the Sof file
  //
  file_name.assign(L"trigram.sof");
  if (!sof.open(file_name, File::WRITE_ONLY)) {
        return Error::handle(file_name, L"open", Error::FILE_NOTFND,
			 __FILE__, __LINE__);
  }

  // store hash table to a trigram file
  //  
  np.store(sof, gram_hash, symbol_table, 1, L"NGramModel");
  File::remove(file_name);

  // close file
  //
  sof.close();
  
  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }

  // --------------------------------------------------------------------
  //
  // 3. print completion message
  //
  // --------------------------------------------------------------------

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  if (level_a > Integral::NONE) {
    String output(L"diagnostics passed for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }

  // exit gracefully
  //
  return true;
}
