Exemplo n.º 1
0
/**
 * Calculate score of a given UYiV. The score is based on the formulas written in the inform.
 * @param  uyv
 * @return score
 */
double Dencoder::score(shared_ptr<UYiV> uyv) const
{
  if (uyv == NULL) return -1;

  WordList* y = uyv->getYList();
  //if the yList is NULL return score 0
  
  assert(y != NULL);
  if (y == NULL) return 0;
  // if (y->getSize() == 0) return -1;

  int p, q, k, l, pipes, I, encoding, total;
  p = uyv->getWordsPosPairs()->size();
  //This is the complete encoding... And I just to count the encoding of the new uyv...
  encoding = 0;
  q = y->getWordVec().size();
  k = uyv->getU()->getSize();
  l = uyv->getV()->getSize();
  pipes = (q-1) ;
  I = 1;


  // the terms with the + are our gains, the - our penalties

  // total = - getProdElectionSize() + p*k + p*l + y->sumWordsLengthWithoutRepeats() - y->sumWordsLengthWithRepeats() - p - 1 - k - l - 1 - 1 - (q - 1);
  total =  p*k + p*l + y->sumWordsLengthWithRepeats() -
                          y->sumWordsLengthWithoutRepeats() - 
                          encoding - 
                          p - 1 - k - l - 1 - I - pipes ;
  assert(y->sumWordsLengthWithRepeats()>=y->sumWordsLengthWithoutRepeats());

  return total;
}
Exemplo n.º 2
0
/**
 * replace the given shield with a NonTerminal in the Sequence
 * @param uyv the yield to be replaced
 * @param nonTerminal
 */
void Dencoder::replaceNonTerminal(shared_ptr<UYiV> uyv, int nonTerminal)
{

  int I = nonTerminal-1;

  vector<int> delimiterPos;
  shared_ptr<Word> u = uyv->getU();
  shared_ptr<Word> v = uyv->getV();


  // int uSize = u->getSize();
  int vSize = v->getSize();

  int uPos = -1;
  int vPos = -1;
  int yieldLength = -1;
  int diff = 0;

  //I'm gonna use this just to know which y i'm adding and its position. 
  WordList* y = uyv->getYList();

  list< shared_ptr<Word> > * wordList = y->getWordList();
  list< shared_ptr<Word> >::iterator yIt = wordList->begin();

  int p, k, l, total;
  p = uyv->getWordsPosPairs()->size();
  k = uyv->getU()->getSize();
  l = uyv->getV()->getSize();

  // if(opt == 0)
  // total = getSeq()->getSize() - (p*k + p*l + y->sumWordsLengthWithRepeats() - p);
  // else
  
  total = getSeq()->getSize() - (p*k + p*l + y->sumWordsLengthWithRepeats() - p) ;


  int * newSeq = new int[total];

  shared_ptr<Sequence> oldSequence = getSeq();
  int realSize = oldSequence->getSize();

  int* oldSeq = oldSequence->getIntSequence();


  list<std::pair<int, int> > * wordsPosPairs = uyv->getWordsPosPairs();
  list<std::pair<int,int> >::iterator posIt = wordsPosPairs->begin();

  int lastEncode = -2;
  assert(lastEncode!=-15); 
  int yWordPos;

  //Rewrite
    
  for(int i = 0; i < realSize; i++)
  {
    assert(i<oldSequence->getSize());
    assert(i-diff<total);

    if (posIt!=wordsPosPairs->end() && i == (*posIt).first)
    {
      uPos = (*posIt).first;
      vPos = (*posIt).second;


      //add which production option I chose here
      yWordPos = y->wordPos(*yIt);
      prodElectionOrder_[I].push(yWordPos);
      totalElectionSize_++;
      // Changes for the new encode:
      ++yIt; 
      

      yieldLength = vPos + vSize - uPos;
      
      newSeq[i - diff] = nonTerminal;
      diff += yieldLength-1;
      i += yieldLength-1;

      //increase pos iterator
      posIt++;
    }
    else
    {
      if(oldSeq[i] == 1)
        {
          delimiterPos.push_back(i-diff);
        }
      newSeq[i-diff] = oldSeq[i];
    }
  }
  //Create Productions:
  list<shared_ptr<Production> >* lp;
  lp = rebuildGrammar(newSeq, delimiterPos, total);
  delete[] newSeq;
  grammar_ = make_shared<Grammar>(lp);
}