// Generate the target tree of the derivation d. TreePointer KBestExtractor::GetOutputTree(const Derivation &d) { const TargetPhrase &phrase = *(d.edge->shyperedge.label.translation); if (const PhraseProperty *property = phrase.GetProperty("Tree")) { const std::string *tree = property->GetValueString(); TreePointer mytree (boost::make_shared<InternalTree>(*tree)); //get subtrees (in target order) std::vector<TreePointer> previous_trees; for (size_t pos = 0; pos < phrase.GetSize(); ++pos) { const Word &word = phrase.GetWord(pos); if (word.IsNonTerminal()) { size_t nonTermInd = phrase.GetAlignNonTerm().GetNonTermIndexMap()[pos]; const Derivation &subderivation = *d.subderivations[nonTermInd]; const TreePointer prev_tree = GetOutputTree(subderivation); previous_trees.push_back(prev_tree); } } mytree->Combine(previous_trees); return mytree; } else { UTIL_THROW2("Error: TreeStructureFeature active, but no internal tree structure found"); } }
FFState* SyntaxConstraintFeature::EvaluateChart(const ChartHypothesis& cur_hypo , int featureID /* used to index the state in the previous hypotheses */ , ScoreComponentCollection* accumulator) const { std::string tree; bool found = 0; cur_hypo.GetCurrTargetPhrase().GetProperty("Tree", tree, found); TreePointer mytree (new InternalTree(tree)); //get subtrees (in target order) std::vector<TreePointer> previous_trees; for (size_t pos = 0; pos < cur_hypo.GetCurrTargetPhrase().GetSize(); ++pos) { const Word &word = cur_hypo.GetCurrTargetPhrase().GetWord(pos); if (word.IsNonTerminal()) { size_t nonTermInd = cur_hypo.GetCurrTargetPhrase().GetAlignNonTerm().GetNonTermIndexMap()[pos]; const ChartHypothesis *prevHypo = cur_hypo.GetPrevHypo(nonTermInd); const TreeState* prev = dynamic_cast<const TreeState*>(prevHypo->GetFFState(featureID)); const TreePointer prev_tree = prev->GetTree(); previous_trees.push_back(prev_tree); } } mytree->Combine(previous_trees); return new TreeState(mytree); }
FFState* TreeStructureFeature::EvaluateWhenApplied(const ChartHypothesis& cur_hypo , int featureID /* used to index the state in the previous hypotheses */ , ScoreComponentCollection* accumulator) const { if (const PhraseProperty *property = cur_hypo.GetCurrTargetPhrase().GetProperty("Tree")) { const std::string *tree = property->GetValueString(); TreePointer mytree (new InternalTree(*tree)); if (m_labelset) { AddNTLabels(mytree); } //get subtrees (in target order) std::vector<TreePointer> previous_trees; for (size_t pos = 0; pos < cur_hypo.GetCurrTargetPhrase().GetSize(); ++pos) { const Word &word = cur_hypo.GetCurrTargetPhrase().GetWord(pos); if (word.IsNonTerminal()) { size_t nonTermInd = cur_hypo.GetCurrTargetPhrase().GetAlignNonTerm().GetNonTermIndexMap()[pos]; const ChartHypothesis *prevHypo = cur_hypo.GetPrevHypo(nonTermInd); const TreeState* prev = dynamic_cast<const TreeState*>(prevHypo->GetFFState(featureID)); const TreePointer prev_tree = prev->GetTree(); previous_trees.push_back(prev_tree); } } std::vector<std::string> sparse_features; if (m_constraints) { sparse_features = m_constraints->SyntacticRules(mytree, previous_trees); } mytree->Combine(previous_trees); //sparse scores for (std::vector<std::string>::const_iterator feature=sparse_features.begin(); feature != sparse_features.end(); ++feature) { accumulator->PlusEquals(this, *feature, 1); } return new TreeState(mytree); } else { UTIL_THROW2("Error: TreeStructureFeature active, but no internal tree structure found"); } }
FFState* TreeStructureFeature::EvaluateWhenApplied(const ChartHypothesis& cur_hypo , int featureID /* used to index the state in the previous hypotheses */ , ScoreComponentCollection* accumulator) const { if (const PhraseProperty *property = cur_hypo.GetCurrTargetPhrase().GetProperty("Tree")) { const std::string *tree = property->GetValueString(); TreePointer mytree (boost::make_shared<InternalTree>(*tree)); //get subtrees (in target order) std::vector<TreePointer> previous_trees; for (size_t pos = 0; pos < cur_hypo.GetCurrTargetPhrase().GetSize(); ++pos) { const Word &word = cur_hypo.GetCurrTargetPhrase().GetWord(pos); if (word.IsNonTerminal()) { size_t nonTermInd = cur_hypo.GetCurrTargetPhrase().GetAlignNonTerm().GetNonTermIndexMap()[pos]; const ChartHypothesis *prevHypo = cur_hypo.GetPrevHypo(nonTermInd); const TreeState* prev = dynamic_cast<const TreeState*>(prevHypo->GetFFState(featureID)); const TreePointer prev_tree = prev->GetTree(); previous_trees.push_back(prev_tree); } } if (m_constraints) { m_constraints->SyntacticRules(mytree, previous_trees, this, accumulator); } mytree->Combine(previous_trees); bool full_sentence = (mytree->GetChildren().back()->GetLabel() == m_send || (mytree->GetChildren().back()->GetLabel() == m_send_nt && mytree->GetChildren().back()->GetChildren().back()->GetLabel() == m_send)); if (m_binarized && full_sentence) { mytree->Unbinarize(); } return new TreeState(mytree); } else { UTIL_THROW2("Error: TreeStructureFeature active, but no internal tree structure found"); } }