当前位置: 首页>>代码示例>>C++>>正文


C++ Hypothesis::GetSize方法代码示例

本文整理汇总了C++中Hypothesis::GetSize方法的典型用法代码示例。如果您正苦于以下问题:C++ Hypothesis::GetSize方法的具体用法?C++ Hypothesis::GetSize怎么用?C++ Hypothesis::GetSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Hypothesis的用法示例。


在下文中一共展示了Hypothesis::GetSize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Evaluate

void GlobalLexicalModelUnlimited::Evaluate(const Hypothesis& cur_hypo, ScoreComponentCollection* accumulator) const
{
  const Sentence& input = *(m_local->input);
  const TargetPhrase& targetPhrase = cur_hypo.GetCurrTargetPhrase();

  for(size_t targetIndex = 0; targetIndex < targetPhrase.GetSize(); targetIndex++ ) {
    StringPiece targetString = targetPhrase.GetWord(targetIndex).GetString(0); // TODO: change for other factors

    if (m_ignorePunctuation) {
      // check if first char is punctuation
      char firstChar = targetString[0];
      CharHash::const_iterator charIterator = m_punctuationHash.find( firstChar );
      if(charIterator != m_punctuationHash.end())
        continue;
    }

    if (m_biasFeature) {
      stringstream feature;
      feature << "glm_";
      feature << targetString;
      feature << "~";
      feature << "**BIAS**";
      accumulator->SparsePlusEquals(feature.str(), 1);
    }

    boost::unordered_set<uint64_t> alreadyScored;
    for(size_t sourceIndex = 0; sourceIndex < input.GetSize(); sourceIndex++ ) {
      const StringPiece sourceString = input.GetWord(sourceIndex).GetString(0);
      // TODO: change for other factors

      if (m_ignorePunctuation) {
        // check if first char is punctuation
        char firstChar = sourceString[0];
        CharHash::const_iterator charIterator = m_punctuationHash.find( firstChar );
        if(charIterator != m_punctuationHash.end())
          continue;
      }
      const uint64_t sourceHash = util::MurmurHashNative(sourceString.data(), sourceString.size());

      if ( alreadyScored.find(sourceHash) == alreadyScored.end()) {
        bool sourceExists, targetExists;
        if (!m_unrestricted) {
          sourceExists = FindStringPiece(m_vocabSource, sourceString ) != m_vocabSource.end();
          targetExists = FindStringPiece(m_vocabTarget, targetString) != m_vocabTarget.end();
        }

        // no feature if vocab is in use and both words are not in restricted vocabularies
        if (m_unrestricted || (sourceExists && targetExists)) {
          if (m_sourceContext) {
            if (sourceIndex == 0) {
              // add <s> trigger feature for source
              stringstream feature;
              feature << "glm_";
              feature << targetString;
              feature << "~";
              feature << "<s>,";
              feature << sourceString;
              accumulator->SparsePlusEquals(feature.str(), 1);
              alreadyScored.insert(sourceHash);
            }

            // add source words to the right of current source word as context
            for(int contextIndex = sourceIndex+1; contextIndex < input.GetSize(); contextIndex++ ) {
              StringPiece contextString = input.GetWord(contextIndex).GetString(0); // TODO: change for other factors
              bool contextExists;
              if (!m_unrestricted)
                contextExists = FindStringPiece(m_vocabSource, contextString ) != m_vocabSource.end();

              if (m_unrestricted || contextExists) {
                stringstream feature;
                feature << "glm_";
                feature << targetString;
                feature << "~";
                feature << sourceString;
                feature << ",";
                feature << contextString;
                accumulator->SparsePlusEquals(feature.str(), 1);
                alreadyScored.insert(sourceHash);
              }
            }
          } else if (m_biphrase) {
            // --> look backwards for constructing context
            int globalTargetIndex = cur_hypo.GetSize() - targetPhrase.GetSize() + targetIndex;

            // 1) source-target pair, trigger source word (can be discont.) and adjacent target word (bigram)
            StringPiece targetContext;
            if (globalTargetIndex > 0)
              targetContext = cur_hypo.GetWord(globalTargetIndex-1).GetString(0); // TODO: change for other factors
            else
              targetContext = "<s>";

            if (sourceIndex == 0) {
              StringPiece sourceTrigger = "<s>";
              AddFeature(accumulator, sourceTrigger, sourceString,
                         targetContext, targetString);
            } else
              for(int contextIndex = sourceIndex-1; contextIndex >= 0; contextIndex-- ) {
                StringPiece sourceTrigger = input.GetWord(contextIndex).GetString(0); // TODO: change for other factors
                bool sourceTriggerExists = false;
                if (!m_unrestricted)
//.........这里部分代码省略.........
开发者ID:akartbayev,项目名称:mosesdecoder,代码行数:101,代码来源:GlobalLexicalModelUnlimited.cpp

示例2: Evaluate

size_t LM::Evaluate(
  const Hypothesis& hypo,
  size_t prevState,
  Scores &scores) const
{
  if (m_order <= 1) {
    return 0; // not sure if returning NULL is correct
  }

  if (hypo.targetPhrase.GetSize() == 0) {
    return 0; // not sure if returning NULL is correct
  }

    PhraseVec m_phraseVec(m_order);

  const size_t currEndPos = hypo.targetRange.endPos;
  const size_t startPos = hypo.targetRange.startPos;

  size_t index = 0;
  for (int currPos = (int) startPos - (int) m_order + 1 ; currPos <= (int) startPos ; currPos++) {
    if (currPos >= 0)
      m_phraseVec[index++] = &hypo.GetWord(currPos);
    else {
      m_phraseVec[index++] = &m_bos;
    }
  }

  SCORE lmScore = GetValueCache(m_phraseVec);

  // main loop
  size_t endPos = std::min(startPos + m_order - 2
                           , currEndPos);
  for (size_t currPos = startPos + 1 ; currPos <= endPos ; currPos++) {
    // shift all args down 1 place
    for (size_t i = 0 ; i < m_order - 1 ; i++)
      m_phraseVec[i] = m_phraseVec[i + 1];

    // add last factor
    m_phraseVec.back() = &hypo.GetWord(currPos);

    lmScore	+= GetValueCache(m_phraseVec);
  }

  // end of sentence
  if (hypo.GetCoverage().IsComplete()) {
    const size_t size = hypo.GetSize();
    m_phraseVec.back() = &m_eos;

    for (size_t i = 0 ; i < m_order - 1 ; i ++) {
      int currPos = (int)(size - m_order + i + 1);
      if (currPos < 0)
        m_phraseVec[i] = &m_bos;
      else
        m_phraseVec[i] = &hypo.GetWord((size_t)currPos);
    }
    lmScore += GetValueCache(m_phraseVec);
  } else {
    if (endPos < currEndPos) {
      //need to get the LM state (otherwise the last LM state is fine)
      for (size_t currPos = endPos+1; currPos <= currEndPos; currPos++) {
        for (size_t i = 0 ; i < m_order - 1 ; i++)
          m_phraseVec[i] = m_phraseVec[i + 1];
        m_phraseVec.back() = &hypo.GetWord(currPos);
      }
    }
  }

  size_t state = GetLastState();
  return state;
}
开发者ID:arvs,项目名称:mosesdecoder,代码行数:70,代码来源:LM.cpp


注:本文中的Hypothesis::GetSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。