00001
00002 #ifndef moses_ForestInput_h
00003 #define moses_ForestInput_h
00004
00005 #include <string>
00006 #include <vector>
00007
00008 #include <boost/shared_ptr.hpp>
00009
00010 #include <util/string_piece.hh>
00011
00012 #include "moses/Syntax/F2S/Forest.h"
00013
00014 #include "Sentence.h"
00015
00016 namespace Moses
00017 {
00018 class TranslationTask;
00019 class ForestInput : public Sentence
00020 {
00021 public:
00022 friend std::ostream &operator<<(std::ostream&, const ForestInput &);
00023
00024 ForestInput(AllOptions::ptr const& opts) : Sentence(opts), m_rootVertex(NULL) {}
00025
00026 InputTypeEnum GetType() const {
00027 return ForestInputType;
00028 }
00029
00031 virtual int
00032 Read(std::istream& in);
00033
00035 virtual void Print(std::ostream&) const;
00036
00038 virtual TranslationOptionCollection*
00039 CreateTranslationOptionCollection() const;
00040
00041 boost::shared_ptr<const Syntax::F2S::Forest> GetForest() const {
00042 return m_forest;
00043 }
00044
00045 const Syntax::F2S::Forest::Vertex *GetRootVertex() const {
00046 return m_rootVertex;
00047 }
00048
00049 private:
00050 typedef Syntax::F2S::Forest Forest;
00051
00052 struct VertexSetHash {
00053 std::size_t operator()(const Forest::Vertex *v) const {
00054 std::size_t seed = 0;
00055 boost::hash_combine(seed, v->pvertex.symbol);
00056 boost::hash_combine(seed, v->pvertex.span.GetStartPos());
00057 boost::hash_combine(seed, v->pvertex.span.GetEndPos());
00058 return seed;
00059 }
00060 };
00061
00062 struct VertexSetPred {
00063 bool operator()(const Forest::Vertex *v, const Forest::Vertex *w) const {
00064 return v->pvertex == w->pvertex;
00065 }
00066 };
00067
00068 typedef boost::unordered_set<Forest::Vertex *, VertexSetHash,
00069 VertexSetPred> VertexSet;
00070
00071 Forest::Vertex *AddOrDeleteVertex(Forest::Vertex *);
00072
00073 std::size_t FindMaxEnd(const Forest &);
00074
00075 void FindTopVertices(Forest &, std::vector<Forest::Vertex *> &);
00076
00077 void ParseHyperedgeLine(const std::string &);
00078
00079 Forest::Vertex *ParseVertex(const StringPiece &);
00080
00081 boost::shared_ptr<Forest> m_forest;
00082 Forest::Vertex *m_rootVertex;
00083 VertexSet m_vertexSet;
00084 };
00085
00086 }
00087
00088 #endif