void ChartRuleLookupManagerMemory::GetChartRuleCollection( const WordsRange &range, ChartParserCallback &outColl) { size_t relEndPos = range.GetEndPos() - range.GetStartPos(); size_t absEndPos = range.GetEndPos(); // MAIN LOOP. create list of nodes of target phrases // get list of all rules that apply to spans at same starting position DottedRuleColl &dottedRuleCol = *m_dottedRuleColls[range.GetStartPos()]; const DottedRuleList &expandableDottedRuleList = dottedRuleCol.GetExpandableDottedRuleList(); const ChartCellLabel &sourceWordLabel = GetSourceAt(absEndPos); // loop through the rules // (note that expandableDottedRuleList can be expanded as the loop runs // through calls to ExtendPartialRuleApplication()) for (size_t ind = 0; ind < expandableDottedRuleList.size(); ++ind) { // rule we are about to extend const DottedRuleInMemory &prevDottedRule = *expandableDottedRuleList[ind]; // we will now try to extend it, starting after where it ended size_t startPos = prevDottedRule.IsRoot() ? range.GetStartPos() : prevDottedRule.GetWordsRange().GetEndPos() + 1; // search for terminal symbol // (if only one more word position needs to be covered) if (startPos == absEndPos) { // look up in rule dictionary, if the current rule can be extended // with the source word in the last position const Word &sourceWord = sourceWordLabel.GetLabel(); const PhraseDictionaryNodeSCFG *node = prevDottedRule.GetLastNode().GetChild(sourceWord); // if we found a new rule -> create it and add it to the list if (node != NULL) { // create the rule #ifdef USE_BOOST_POOL DottedRuleInMemory *dottedRule = m_dottedRulePool.malloc(); new (dottedRule) DottedRuleInMemory(*node, sourceWordLabel, prevDottedRule); #else DottedRuleInMemory *dottedRule = new DottedRuleInMemory(*node, sourceWordLabel, prevDottedRule); #endif dottedRuleCol.Add(relEndPos+1, dottedRule); } } // search for non-terminals size_t endPos, stackInd; // span is already complete covered? nothing can be done if (startPos > absEndPos) continue; else if (startPos == range.GetStartPos() && range.GetEndPos() > range.GetStartPos()) { // We're at the root of the prefix tree so won't try to cover the full // span (i.e. we don't allow non-lexical unary rules). However, we need // to match non-unary rules that begin with a non-terminal child, so we // do that in two steps: during this iteration we search for non-terminals // that cover all but the last source word in the span (there won't // already be running nodes for these because that would have required a // non-lexical unary rule match for an earlier span). Any matches will // result in running nodes being appended to the list and on subsequent // iterations (for this same span), we'll extend them to cover the final // word. endPos = absEndPos - 1; stackInd = relEndPos; } else { endPos = absEndPos; stackInd = relEndPos + 1; } ExtendPartialRuleApplication(prevDottedRule, startPos, endPos, stackInd, dottedRuleCol); } // list of rules that that cover the entire span DottedRuleList &rules = dottedRuleCol.Get(relEndPos + 1); // look up target sides for the rules DottedRuleList::const_iterator iterRule; for (iterRule = rules.begin(); iterRule != rules.end(); ++iterRule) { const DottedRuleInMemory &dottedRule = **iterRule; const PhraseDictionaryNodeSCFG &node = dottedRule.GetLastNode(); // look up target sides const TargetPhraseCollection *tpc = node.GetTargetPhraseCollection(); // add the fully expanded rule (with lexical target side) if (tpc != NULL) { AddCompletedRule(dottedRule, *tpc, range, outColl); } } dottedRuleCol.Clear(relEndPos+1); }
void ChartRuleLookupManagerMemory::GetChartRuleCollection( const WordsRange &range, bool adhereTableLimit, ChartTranslationOptionList &outColl) { size_t relEndPos = range.GetEndPos() - range.GetStartPos(); size_t absEndPos = range.GetEndPos(); // MAIN LOOP. create list of nodes of target phrases ProcessedRuleColl &processedRuleCol = *m_processedRuleColls[range.GetStartPos()]; const ProcessedRuleList &runningNodes = processedRuleCol.GetRunningNodes(); // Note that runningNodes can be expanded as the loop runs (through calls to // ExtendPartialRuleApplication()). for (size_t ind = 0; ind < runningNodes.size(); ++ind) { const ProcessedRule &prevProcessedRule = *runningNodes[ind]; const PhraseDictionaryNodeSCFG &prevNode = prevProcessedRule.GetLastNode(); const WordConsumed *prevWordConsumed = prevProcessedRule.GetLastWordConsumed(); size_t startPos = (prevWordConsumed == NULL) ? range.GetStartPos() : prevWordConsumed->GetWordsRange().GetEndPos() + 1; // search for terminal symbol if (startPos == absEndPos) { const Word &sourceWord = GetSentence().GetWord(absEndPos); const PhraseDictionaryNodeSCFG *node = prevNode.GetChild(sourceWord); if (node != NULL) { WordConsumed *newWordConsumed = new WordConsumed(absEndPos, absEndPos , sourceWord , prevWordConsumed); ProcessedRule *processedRule = new ProcessedRule(*node, newWordConsumed); processedRuleCol.Add(relEndPos+1, processedRule); } } // search for non-terminals size_t endPos, stackInd; if (startPos > absEndPos) continue; else if (startPos == range.GetStartPos() && range.GetEndPos() > range.GetStartPos()) { // start. endPos = absEndPos - 1; stackInd = relEndPos; } else { endPos = absEndPos; stackInd = relEndPos + 1; } const NonTerminalSet &sourceNonTerms = GetSentence().GetLabelSet(startPos, endPos); const NonTerminalSet &targetNonTerms = GetCellCollection().GetHeadwords(WordsRange(startPos, endPos)); ExtendPartialRuleApplication(prevNode, prevWordConsumed, startPos, endPos, stackInd, sourceNonTerms, targetNonTerms, processedRuleCol); } // return list of target phrases ProcessedRuleList &nodes = processedRuleCol.Get(relEndPos + 1); size_t rulesLimit = StaticData::Instance().GetRuleLimit(); ProcessedRuleList::const_iterator iterNode; for (iterNode = nodes.begin(); iterNode != nodes.end(); ++iterNode) { const ProcessedRule &processedRule = **iterNode; const PhraseDictionaryNodeSCFG &node = processedRule.GetLastNode(); const WordConsumed *wordConsumed = processedRule.GetLastWordConsumed(); assert(wordConsumed); const TargetPhraseCollection *targetPhraseCollection = node.GetTargetPhraseCollection(); if (targetPhraseCollection != NULL) { outColl.Add(*targetPhraseCollection, *wordConsumed, adhereTableLimit, rulesLimit); } } outColl.CreateChartRules(rulesLimit); }