
#include "moses/StaticData.h"
#include "moses/FactorCollection.h"
#include <boost/functional/hash.hpp>
#include "RNNLMWrapper.h"
#include "rnnlmlib.h"

using namespace std;

namespace Moses
{
RNNLMWrapper::RNNLMWrapper(const std::string &line)
  :LanguageModelSingleFactor(line)
{
  ReadParameters();
}


RNNLMWrapper::~RNNLMWrapper()
{
  delete rnnlm_shared;
}


void RNNLMWrapper::Load()
{

  // Set parameters required by ancestor classes
  FactorCollection &factorCollection = FactorCollection::Instance();
  m_sentenceStart = factorCollection.AddFactor(Output, m_factorType, BOS_);
  m_sentenceStartWord[m_factorType] = m_sentenceStart;
  m_sentenceEnd		= factorCollection.AddFactor(Output, m_factorType, EOS_);
  m_sentenceEndWord[m_factorType] = m_sentenceEnd;

  rnnlm_shared = new CRnnLM();
  char *path = strdup(m_filePath.c_str());
  rnnlm_shared->setRnnLMFile(path);
      
  rnnlm_shared->restoreNet();

}


LMResult RNNLMWrapper::GetValue(const vector<const Word*> &contextFactor, State* finalState) const
{

	size_t hashCode = 0, n= contextFactor.size();
	int words[n];

 
  for (size_t i=0; i<n; i++) {
    const Word* word = contextFactor[i];
    const Factor* factor = word->GetFactor(m_factorType);
    const std::string string = factor->GetString().as_string();
    char *wd = strdup(string.c_str());
    int neuralLM_wordID = rnnlm_shared->searchVocab(wd);
    words[i] = neuralLM_wordID;
    boost::hash_combine(hashCode, neuralLM_wordID);
  }

  double value = rnnlm_shared->getLogP(words, n);

  // Create a new struct to hold the result
  LMResult ret;
  ret.score = FloorScore(value);
  //ret.unknown = (words.back() == m_unk);

  (*finalState) = (State*) hashCode;

  return ret;
}

}


