00001 #include "GlueRuleSynthesizer.h"
00002 
00003 #include <sstream>
00004 
00005 #include "moses/FF/UnknownWordPenaltyProducer.h"
00006 #include "util/string_stream.hh"
00007 #include "moses/parameters/AllOptions.h"
00008 namespace Moses
00009 {
00010 namespace Syntax
00011 {
00012 namespace F2S
00013 {
00014 
00015 GlueRuleSynthesizer::
00016 GlueRuleSynthesizer(Moses::AllOptions const& opts, HyperTree &trie)
00017   : m_input_default_nonterminal(opts.syntax.input_default_non_terminal)
00018   , m_output_default_nonterminal(opts.syntax.output_default_non_terminal)
00019   , m_hyperTree(trie)
00020 {
00021   Word *lhs = NULL;
00022   m_dummySourcePhrase.CreateFromString(Input, opts.input.factor_order, "hello", &lhs);
00023   delete lhs;
00024 }
00025 
00026 void GlueRuleSynthesizer::SynthesizeRule(const Forest::Hyperedge &e)
00027 {
00028   HyperPath source;
00029   SynthesizeHyperPath(e, source);
00030   TargetPhrase *tp = SynthesizeTargetPhrase(e);
00031   TargetPhraseCollection::shared_ptr tpc
00032   = GetOrCreateTargetPhraseCollection(m_hyperTree, source);
00033   tpc->Add(tp);
00034 }
00035 
00036 void GlueRuleSynthesizer::SynthesizeHyperPath(const Forest::Hyperedge &e,
00037     HyperPath &path)
00038 {
00039   path.nodeSeqs.clear();
00040   path.nodeSeqs.resize(2);
00041   path.nodeSeqs[0].push_back(e.head->pvertex.symbol[0]->GetId());
00042   for (std::vector<Forest::Vertex*>::const_iterator p = e.tail.begin();
00043        p != e.tail.end(); ++p) {
00044     const Forest::Vertex &child = **p;
00045     path.nodeSeqs[1].push_back(child.pvertex.symbol[0]->GetId());
00046   }
00047 }
00048 
00049 TargetPhrase*
00050 GlueRuleSynthesizer::
00051 SynthesizeTargetPhrase(const Forest::Hyperedge &e)
00052 {
00053   const UnknownWordPenaltyProducer &unknownWordPenaltyProducer =
00054     UnknownWordPenaltyProducer::Instance();
00055 
00056   TargetPhrase *targetPhrase = new TargetPhrase();
00057 
00058   util::StringStream alignmentSS;
00059   for (std::size_t i = 0; i < e.tail.size(); ++i) {
00060     const Word &symbol = e.tail[i]->pvertex.symbol;
00061     if (symbol.IsNonTerminal()) {
00062       targetPhrase->AddWord(m_output_default_nonterminal);
00063     } else {
00064       
00065       Word &targetWord = targetPhrase->AddWord();
00066       targetWord.CreateUnknownWord(symbol);
00067     }
00068     alignmentSS << i << "-" << i << " ";
00069   }
00070 
00071   
00072   
00073   float score = LOWEST_SCORE;
00074   targetPhrase->GetScoreBreakdown().Assign(&unknownWordPenaltyProducer, score);
00075   targetPhrase->EvaluateInIsolation(m_dummySourcePhrase);
00076   Word *targetLhs = new Word(m_output_default_nonterminal);
00077   targetPhrase->SetTargetLHS(targetLhs);
00078   targetPhrase->SetAlignmentInfo(alignmentSS.str());
00079 
00080   return targetPhrase;
00081 }
00082 
00083 }  
00084 }  
00085 }