template <class Model> void Fill<Model>::Add(const TargetPhraseCollection &targets, const StackVec &nts, const WordsRange &) { std::vector<search::PartialVertex> vertices; vertices.reserve(nts.size()); float below_score = 0.0; for (StackVec::const_iterator i(nts.begin()); i != nts.end(); ++i) { vertices.push_back((*i)->GetStack().incr->RootAlternate()); if (vertices.back().Empty()) return; below_score += vertices.back().Bound(); } std::vector<lm::WordIndex> words; for (TargetPhraseCollection::const_iterator p(targets.begin()); p != targets.end(); ++p) { words.clear(); const TargetPhrase &phrase = **p; const AlignmentInfo::NonTermIndexMap &align = phrase.GetAlignNonTerm().GetNonTermIndexMap(); search::PartialEdge edge(edges_.AllocateEdge(nts.size())); search::PartialVertex *nt = edge.NT(); for (size_t i = 0; i < phrase.GetSize(); ++i) { const Word &word = phrase.GetWord(i); if (word.IsNonTerminal()) { *(nt++) = vertices[align[i]]; words.push_back(search::kNonTerminal); } else { words.push_back(Convert(word)); } } edge.SetScore(phrase.GetFutureScore() + below_score); // prob and oov were already accounted for. search::ScoreRule(context_.LanguageModel(), words, edge.Between()); search::Note note; note.vp = &phrase; edge.SetNote(note); edges_.AddEdge(edge); } }
void ChartTranslationOptionList::Add(const TargetPhraseCollection &tpc, const StackVec &stackVec, const WordsRange &range) { if (tpc.IsEmpty()) { return; } for (size_t i = 0; i < stackVec.size(); ++i) { const ChartCellLabel &chartCellLabel = *stackVec[i]; size_t numHypos = chartCellLabel.GetStack().cube->size(); if (numHypos == 0) { return; // empty stack. These rules can't be used } } float score = ChartTranslationOptions::CalcEstimateOfBestScore(tpc, stackVec); // If the rule limit has already been reached then don't add the option // unless it is better than at least one existing option. if (m_size > m_ruleLimit && score < m_scoreThreshold) { return; } // Add the option to the list. if (m_size == m_collection.size()) { // m_collection has reached capacity: create a new object. m_collection.push_back(new ChartTranslationOptions(tpc, stackVec, range, score)); } else { // Overwrite an unused object. *(m_collection[m_size]) = ChartTranslationOptions(tpc, stackVec, range, score); } ++m_size; // If the rule limit hasn't been exceeded then update the threshold. if (m_size <= m_ruleLimit) { m_scoreThreshold = (score < m_scoreThreshold) ? score : m_scoreThreshold; } // Prune if bursting if (m_size == m_ruleLimit * 2) { NTH_ELEMENT4(m_collection.begin(), m_collection.begin() + m_ruleLimit - 1, m_collection.begin() + m_size, ChartTranslationOptionOrderer()); m_scoreThreshold = m_collection[m_ruleLimit-1]->GetEstimateOfBestScore(); m_size = m_ruleLimit; } }
void StackedSet::update() { clear(); // Since overlap should be calculated using the precision scaled dimensions // we need to precision scale the bounding box for the screen. PixelBox screenBox = m_projection.getPixelBoundingBox(); MC2Point topLeft(screenBox.getTopLeft().getX() * SCALING_CONSTANT, screenBox.getTopLeft().getY() * SCALING_CONSTANT); MC2Point bottomRight(screenBox.getBottomRight().getX() * SCALING_CONSTANT, screenBox.getBottomRight().getY() * SCALING_CONSTANT); PixelBox screenBoxScaled(topLeft, bottomRight); for( OverlayViewLayerInterface::LayerMap::iterator itr = m_layerInterface.begin(); itr != m_layerInterface.end(); itr++) { Layer& l = *itr->second; if(!l.getVisible()) { // We don't need to detect any overlaps with invisible layers. continue; } for(Layer::const_iterator itemItr = l.items_begin(); itemItr != l.items_end(); itemItr++) { OverlayItemInternal* item = *itemItr; initItem(item); if (!screenBoxScaled.overlaps(item->getPrecisionScaledPixelBox())) { // The item lies outside the screen, do NOT add it to the stacks. continue; } addToStackSet(item); } } #ifdef USE_SMOOTHING // Smooth the positions of the stacks to represent the average // of the contained items' positions. smoothPositions(); #endif #ifdef USE_SMOOTHING #ifdef USE_ITERATIVE_SMOOTHING // Since we have changed the position of the stacks, we run another // pass of overlap detection. unsigned int prevSize = 0; unsigned int iterationCap = ITERATIVE_SMOOTHING_CAP; // If the number of stacks has not changed, no stacks overlapped // so the iteration should terminate. while (prevSize != m_stacks.size() && iterationCap > 0) { StackVec tempVec = m_stacks; prevSize = m_stacks.size(); clear(); for(StackVec::iterator itr = tempVec.begin(); itr != tempVec.end(); itr++) { addToStackSet(*itr); } smoothPositions(); --iterationCap; } #endif #endif OverlayItemInternal* currClosest = NULL; if ( m_stacks.size() != 0 && m_automaticHighlightMode){ // Find out which item is closest to the center of the screen MC2Point screenCenter; m_projection.transform(screenCenter, m_projection.getCenter()); WFAPI::wf_float64 closestDistSq = 0; currClosest = *m_stacks.begin(); closestDistSq = getSquareScreenDistance(screenCenter, currClosest); for(StackedSet::StackVec::const_iterator itr = m_stacks.begin(); itr != m_stacks.end(); itr++) { WFAPI::wf_float64 currDistSq = getSquareScreenDistance(screenCenter, *itr); if (currDistSq < closestDistSq){ currClosest = *itr; closestDistSq = currDistSq; } } // If the currently closest item is further away than RADIUS pixels, // disregard it. const unsigned int RADIUS = 75; if (closestDistSq > RADIUS * RADIUS){ currClosest = NULL; closestDistSq = 0; } } updateClosest(currClosest); if (m_closest.item && automaticHighlightEnabled()){ m_closest.item->m_isHighlighted = true; // Since we don't care pixel scale the item is on, we // use 0 as the current pixel scale. const WFAPI::OverlayItemVisualSpec* highlighted = OverlayItemUtil::getCurrentVisualSpec(m_closest.item, 0); if(highlighted) { updateItemPixelBox(m_closest.item, highlighted, m_closest.item->m_adjustedPosition); } } }