00001 #include "TreeStructureFeature.h" 00002 #include "moses/StaticData.h" 00003 #include "moses/ScoreComponentCollection.h" 00004 #include "moses/ChartHypothesis.h" 00005 #include <vector> 00006 #include "moses/PP/TreeStructurePhraseProperty.h" 00007 00008 namespace Moses 00009 { 00010 00011 void TreeStructureFeature::Load(AllOptions::ptr const& opts) 00012 { 00013 m_options = opts; 00014 00015 // syntactic constraints can be hooked in here. 00016 m_constraints = NULL; 00017 00018 StaticData &staticData = StaticData::InstanceNonConst(); 00019 staticData.SetTreeStructure(this); 00020 } 00021 00022 00023 FFState* TreeStructureFeature::EvaluateWhenApplied(const ChartHypothesis& cur_hypo 00024 , int featureID /* used to index the state in the previous hypotheses */ 00025 , ScoreComponentCollection* accumulator) const 00026 { 00027 if (const PhraseProperty *property = cur_hypo.GetCurrTargetPhrase().GetProperty("Tree")) { 00028 const std::string *tree = property->GetValueString(); 00029 TreePointer mytree (boost::make_shared<InternalTree>(*tree)); 00030 00031 //get subtrees (in target order) 00032 std::vector<TreePointer> previous_trees; 00033 for (size_t pos = 0; pos < cur_hypo.GetCurrTargetPhrase().GetSize(); ++pos) { 00034 const Word &word = cur_hypo.GetCurrTargetPhrase().GetWord(pos); 00035 if (word.IsNonTerminal()) { 00036 size_t nonTermInd = cur_hypo.GetCurrTargetPhrase().GetAlignNonTerm().GetNonTermIndexMap()[pos]; 00037 const ChartHypothesis *prevHypo = cur_hypo.GetPrevHypo(nonTermInd); 00038 const TreeState* prev = static_cast<const TreeState*>(prevHypo->GetFFState(featureID)); 00039 const TreePointer prev_tree = prev->GetTree(); 00040 previous_trees.push_back(prev_tree); 00041 } 00042 } 00043 00044 if (m_constraints) { 00045 m_constraints->SyntacticRules(mytree, previous_trees, this, accumulator); 00046 } 00047 mytree->Combine(previous_trees); 00048 00049 bool full_sentence = (mytree->GetChildren().back()->GetLabel() == m_send || (mytree->GetChildren().back()->GetLabel() == m_send_nt && mytree->GetChildren().back()->GetChildren().back()->GetLabel() == m_send)); 00050 if (m_binarized && full_sentence) { 00051 mytree->Unbinarize(); 00052 } 00053 00054 return new TreeState(mytree); 00055 } else { 00056 UTIL_THROW2("Error: TreeStructureFeature active, but no internal tree structure found"); 00057 } 00058 00059 } 00060 00061 void TreeStructureFeature::SetParameter(const std::string& key, const std::string& value) 00062 { 00063 std::cerr << "setting: " << this->GetScoreProducerDescription() << " - " << key << "\n"; 00064 if (key == "tuneable") { 00065 m_tuneable = Scan<bool>(value); 00066 } else if (key == "filterable") { //ignore 00067 } else if (key == "binarized") { // if trees have been binarized before learning translation model; output unbinarized trees 00068 m_binarized = true; 00069 } else { 00070 UTIL_THROW(util::Exception, "Unknown argument " << key << "=" << value); 00071 } 00072 } 00073 00074 }