/* hub.cpp
 *
 * $Id: hub.cpp,v 1.6 2008/05/19 01:22:35 zhanghao Exp $
 */

#include "globals.h"
#include "itggrmr.h"

using namespace std;

//the hub that sums up the expected counts from 
//distributed parts
Vocab l2vocab;

FILE *logfile;

int main(int argc, char *argv[]){
  // configfile parameters are mostly as for itg trainer.
  // parameter countfilelist is the name of a file 
  //  containing the names of the files to combine.

  if (argc < 1)  {
    printf("Usage: hub <config file>\n");
    exit(-1);
  }

  logfile = NULL;
  Globals::Load(argc, argv);

//   configFile->add<int>("verbosity", 0);
//   configFile->add<bool>("logging", false);

  string rules = Globals::Get<string>("grammarfile");
  string probs = Globals::Get<string>("probfile");
  string runid = Globals::Get<string>("runid");
  string counts_file = runid + ".cnt";    

  string rule_probs_file = runid + ".rules.prb";  // name of the file of probabilities
  string t_probs_file = runid + ".t.prb";  // name of the file of probabilities
  //  string probs_file  = runid + ".prb";

  cout << "HUB:\n";
  cout << "  rules = " << rules << endl;
  cout << "  t-table = " << probs << endl;
  cout << "  output counts = " << counts_file << endl;
  cout << "  output rule probs = " << rule_probs_file << endl;
  cout << "  output t probs = " << t_probs_file << endl;
  cout << "  list of count files = " << Globals::Get<string>("countfilelist") << endl;

  ITGGrammar *grammar = new ITGGrammar(rules, probs);

  printf("Loading the grammar...");
  fflush(stdout);
  grammar->load();
  printf("done.\n");

  printf("Combining the counts...");
  fflush(stdout);
  grammar->combinecnt(Globals::Get<string>("countfilelist").data());
  //grammar->dumpcnt(counts_file);
  printf("done.\n");

//   printf("Dumping the combined counts...");
//   fflush(stdout);
//   grammar->dumpcnt(output_counts);
//   printf("done.\n");

  printf("Normalizing...");
  fflush(stdout);

  if (Globals::Get<bool>("mixture_DP")){ 
    printf("mixture of dp.\n");
    grammar->mdpnormalize();
  }
  else
    grammar->normalize(); 

  //  grammar->normalize();
  printf("done.\n");

  printf("Writing new table...");
  fflush(stdout);
  grammar->dumpprb(rule_probs_file, t_probs_file);
  printf("done.\n");
}
