// file: $isip/class/stat/NGramModel/NGramModel.h
// version: $Id: NGramModel.h 8546 2002-08-08 21:53:44Z zheng $
//

// make sure definitions are only made once
//
#ifndef ISIP_NGRAM_MODEL
#define ISIP_NGRAM_MODEL

#ifndef ISIP_SOF
#include <Sof.h>
#endif

#ifndef ISIP_VECTOR
#include <Vector.h>
#endif

#ifndef ISIP_STRING
#include <String.h>
#endif

#ifndef ISIP_HASH_TABLE
#include <HashTable.h>
#endif

#ifndef ISIP_NGRAM_NODE
#include <NGramNode.h>
#endif

#ifndef ISIP_NGRAM_PARSER
#include <NGramParser.h>
#endif

// NGram: a class that manages N-gram probabilities, such as bigram,
// trigram, etc. This class can be used to load an input N-gram file,
// such as NIST_ARPA grammar file, and then store it to HashTable for
// lookup.
//
class NGramModel {
  
  //-------------------------------------------------------------
  //
  // public constants
  //
  //-------------------------------------------------------------
public:

  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------  
  
  static const String PARAM_ORDER;
  static const String PARAM_KEYS;
  static const String PARAM_VALUES;
  static const String PARAM_GRAM_HASH;
  
  //--------------------------------------------------------------
  //
  // other important constants
  //
  //--------------------------------------------------------------

  // define the default value(s) of the class data
  //
  static const String DEF_PARAM;
  static const Long DEF_ORDER;
  static const Float DEF_LM_SCORE;  
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  

  static const long ERR = (long)60600;  
  
  //-------------------------------------------------------------
  //
  // protected data
  //
  //-------------------------------------------------------------

protected:

  // the order of N_gram
  //
  Long order_d;

  // hashtable of symbol indices and ngram nodes for unigram.
  //  here, higher order gram will be expanded from each node.
  //
  HashTable<Long, NGramNode> gram_hash_d;

  // symbol table
  //
  Vector<String> symbol_table_d;

  // declare a static debug level for all class instantiations
  //
  static Integral::DEBUG debug_level_d;

  // static memory manager
  //
  static MemoryManager mgr_d;
  
  //-------------------------------------------------------------
  //
  // required public methods
  //
  //-------------------------------------------------------------
public:

  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }

  // other static methods
  //
  static boolean diagnose(Integral::DEBUG debug_level);

  // method: setDebug
  //
  static boolean setDebug(Integral::DEBUG debug_level) {
    debug_level_d = debug_level;
    return true;
  }

  // debug methods:
  //
  boolean debug(const unichar* msg) const;
  
  // method: destructor
  //
  ~NGramModel() {
    clear(Integral::FREE);
  }

  // method: default constructor
  //
  NGramModel(Long order = DEF_ORDER) {
    order_d = order;
  }

  // method: assign
  //
  boolean assign(const NGramModel& arg) {
    order_d = arg.order_d;
    gram_hash_d.assign(arg.gram_hash_d);
    return true;
  }
  
  // method: operator=
  //
  NGramModel& operator= (const NGramModel& arg) {
    assign(arg);
    return *this;
  }

  // i/o methods
  //
  
  // method: sofSize
  //
  long sofSize() const;
  
  // method: read
  //
  boolean read(Sof& sof, const long tag, const String& cname = CLASS_NAME);

  // method: write
  //
  boolean write(Sof& sof, long tag, const String& cname = CLASS_NAME) const;

  // method: readData
  //
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
                   long size = SofParser::FULL_OBJECT,
                   boolean param = true,
                   boolean nested = false);
  
  // method: writeData
  //
  boolean writeData(Sof& sof_a, const String& pname_a = DEF_PARAM) const;

  // equality methods
  //
  boolean eq(const NGramModel& arg) const;

  // method: new
  //
  static void* operator new(size_t size) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t size) {
    return mgr_d.getBlock(size);
  }

  // method: delete
  //
  static void operator delete(void* ptr) {
    mgr_d.release(ptr);
  }

  // method: delete[]
  //
  static void operator delete[](void* ptr) {
    mgr_d.releaseBlock(ptr);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long grow_size) {
    return mgr_d.setGrow(grow_size);
  }
  
  // method: clear
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  set methods
  //
  //---------------------------------------------------------------------------

  // method: setOrder
  //
  boolean setOrder(long order) {
    order_d = order;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  get methods
  //
  //---------------------------------------------------------------------------

  // method: getOrder
  //
  long getOrder() const {
    return order_d;
  }

  // method: getGramHash
  //
  HashTable<Long, NGramNode>* getGramHash() {
    return &gram_hash_d;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  operation related methods
  //
  //---------------------------------------------------------------------------

  // method: load
  //
  boolean load(Sof& sof, const long& tag,
	       const Vector<String>& symbol_table,
	       const String& name = CLASS_NAME);

  // method: store
  //
  boolean store(Sof& sof, const long& tag,
		const String& name = CLASS_NAME) const;

  // method: getScore
  //
  float getScore(const VectorLong& symbol_indices);
  boolean getScore(const VectorLong& index_a, float& score_a);
  
  //-------------------------------------------------------------
  //
  // private methods
  //
  //-------------------------------------------------------------
private:

};

// end of include file
//
#endif
