00001 #pragma once
00002 
00003 #include <istream>
00004 #include <string>
00005 #include <vector>
00006 #include <utility>
00007 
00008 #include <boost/shared_ptr.hpp>
00009 #include <boost/unordered_set.hpp>
00010 
00011 #include "util/string_piece.hh"
00012 
00013 #include "StringForest.h"
00014 
00015 namespace MosesTraining
00016 {
00017 namespace Syntax
00018 {
00019 namespace FilterRuleTable
00020 {
00021 
00022 class StringForestParser
00023 {
00024 public:
00025   struct Entry {
00026     std::size_t sentNum;
00027     std::string sentence;
00028     boost::shared_ptr<StringForest> forest;
00029   };
00030 
00031   StringForestParser();
00032   StringForestParser(std::istream &);
00033 
00034   Entry &operator*() {
00035     return m_entry;
00036   }
00037   Entry *operator->() {
00038     return &m_entry;
00039   }
00040 
00041   StringForestParser &operator++();
00042 
00043   friend bool operator==(const StringForestParser &,
00044                          const StringForestParser &);
00045   friend bool operator!=(const StringForestParser &,
00046                          const StringForestParser &);
00047 
00048 private:
00049   struct VertexSetHash {
00050     std::size_t operator()(const StringForest::Vertex *v) const {
00051       std::size_t seed = 0;
00052       boost::hash_combine(seed, v->value.symbol);
00053       boost::hash_combine(seed, v->value.start);
00054       boost::hash_combine(seed, v->value.end);
00055       return seed;
00056     }
00057   };
00058 
00059   struct VertexSetPred {
00060     bool operator()(const StringForest::Vertex *v,
00061                     const StringForest::Vertex *w) const {
00062       return v->value.symbol == w->value.symbol &&
00063              v->value.start == w->value.start &&
00064              v->value.end == w->value.end;
00065     }
00066   };
00067 
00068   typedef boost::unordered_set<StringForest::Vertex *, VertexSetHash,
00069           VertexSetPred> VertexSet;
00070 
00071   
00072   StringForestParser(const StringForestParser &);
00073   StringForestParser &operator=(const StringForestParser &);
00074 
00075   StringForest::Vertex *AddOrDeleteVertex(StringForest::Vertex *);
00076   void ParseHyperedgeLine(const std::string &, StringForest &);
00077   void ParseSentenceNumLine(const std::string &, std::size_t &);
00078   StringForest::Vertex *ParseVertex(const StringPiece &);
00079 
00080   Entry m_entry;
00081   std::istream *m_input;
00082   std::string m_tmpLine;
00083   VertexSet m_vertexSet;
00084 };
00085 
00086 }  
00087 }  
00088 }