00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <fstream>
00024 #include <sstream>
00025 #include <string>
00026 #include <vector>
00027
00028 #include <boost/algorithm/string/predicate.hpp>
00029 #include <boost/filesystem.hpp>
00030 #include <boost/iostreams/device/file.hpp>
00031 #include <boost/iostreams/filter/bzip2.hpp>
00032 #include <boost/iostreams/filter/gzip.hpp>
00033 #include <boost/iostreams/filtering_stream.hpp>
00034
00035 #include <util/exception.hh>
00036
00037 #include "ChartHypothesisCollection.h"
00038 #include "ChartManager.h"
00039 #include "HypergraphOutput.h"
00040 #include "Manager.h"
00041
00042 using namespace std;
00043
00044 namespace Moses
00045 {
00046
00047 template class HypergraphOutput<Manager>;
00048 template class HypergraphOutput<ChartManager>;
00049
00050 void
00051 ChartSearchGraphWriterMoses::
00052 WriteHypos(const ChartHypothesisCollection& hypos,
00053 const map<unsigned, bool> &reachable) const
00054 {
00055
00056 ChartHypothesisCollection::const_iterator iter;
00057 for (iter = hypos.begin() ; iter != hypos.end() ; ++iter) {
00058 ChartHypothesis &mainHypo = **iter;
00059 if (m_options->output.DontPruneSearchGraph ||
00060 reachable.find(mainHypo.GetId()) != reachable.end()) {
00061 (*m_out) << m_lineNumber << " " << mainHypo << endl;
00062 }
00063
00064 const ChartArcList *arcList = mainHypo.GetArcList();
00065 if (arcList) {
00066 ChartArcList::const_iterator iterArc;
00067 for (iterArc = arcList->begin(); iterArc != arcList->end(); ++iterArc) {
00068 const ChartHypothesis &arc = **iterArc;
00069 if (reachable.find(arc.GetId()) != reachable.end())
00070 (*m_out) << m_lineNumber << " " << arc << endl;
00071 }
00072 }
00073 }
00074 }
00075
00076 void
00077 ChartSearchGraphWriterHypergraph::
00078 WriteHeader(size_t winners, size_t losers) const
00079 {
00080 (*m_out) << "# target ||| features ||| source-covered" << endl;
00081 (*m_out) << winners << " " << (winners+losers) << endl;
00082 }
00083
00084 void
00085 ChartSearchGraphWriterHypergraph::
00086 WriteHypos(const ChartHypothesisCollection& hypos,
00087 const map<unsigned, bool> &reachable) const
00088 {
00089
00090 ChartHypothesisCollection::const_iterator iter;
00091 for (iter = hypos.begin() ; iter != hypos.end() ; ++iter) {
00092 const ChartHypothesis* mainHypo = *iter;
00093 if (!m_options->output.DontPruneSearchGraph &&
00094 reachable.find(mainHypo->GetId()) == reachable.end()) {
00095
00096 continue;
00097 }
00098 (*m_out) << "# node " << m_nodeId << endl;
00099 m_hypoIdToNodeId[mainHypo->GetId()] = m_nodeId;
00100 ++m_nodeId;
00101 vector<const ChartHypothesis*> edges;
00102 edges.push_back(mainHypo);
00103 const ChartArcList *arcList = (*iter)->GetArcList();
00104 if (arcList) {
00105 ChartArcList::const_iterator iterArc;
00106 for (iterArc = arcList->begin(); iterArc != arcList->end(); ++iterArc) {
00107 const ChartHypothesis* arc = *iterArc;
00108 if (reachable.find(arc->GetId()) != reachable.end()) {
00109 edges.push_back(arc);
00110 }
00111 }
00112 }
00113 (*m_out) << edges.size() << endl;
00114 for (vector<const ChartHypothesis*>::const_iterator ei = edges.begin();
00115 ei != edges.end(); ++ei) {
00116 const ChartHypothesis* hypo = *ei;
00117 const TargetPhrase& target = hypo->GetCurrTargetPhrase();
00118 size_t ntIndex = 0;
00119 for (size_t i = 0; i < target.GetSize(); ++i) {
00120 const Word& word = target.GetWord(i);
00121 if (word.IsNonTerminal()) {
00122 size_t hypoId = hypo->GetPrevHypos()[ntIndex++]->GetId();
00123 (*m_out) << "[" << m_hypoIdToNodeId[hypoId] << "]";
00124 } else {
00125 (*m_out) << word.GetFactor(0)->GetString();
00126 }
00127 (*m_out) << " ";
00128 }
00129 (*m_out) << " ||| ";
00130 ScoreComponentCollection scores = hypo->GetScoreBreakdown();
00131 HypoList::const_iterator hi;
00132 for (hi = hypo->GetPrevHypos().begin(); hi != hypo->GetPrevHypos().end(); ++hi) {
00133 scores.MinusEquals((*hi)->GetScoreBreakdown());
00134 }
00135 scores.Save(*m_out, false);
00136 (*m_out) << " ||| ";
00137 (*m_out) << hypo->GetCurrSourceRange().GetNumWordsCovered();
00138 (*m_out) << endl;
00139
00140 }
00141 }
00142 }
00143
00144
00145 }
00146