00001 #pragma once 00002 00003 #include <vector> 00004 00005 #include <boost/unordered_map.hpp> 00006 00007 #include "moses/Syntax/NonTerminalMap.h" 00008 #include "moses/Syntax/PVertex.h" 00009 #include "moses/Syntax/SymbolEqualityPred.h" 00010 #include "moses/Syntax/SymbolHasher.h" 00011 #include "moses/Word.h" 00012 00013 namespace Moses 00014 { 00015 namespace Syntax 00016 { 00017 namespace S2T 00018 { 00019 00020 class PChart 00021 { 00022 public: 00023 struct Cell { 00024 typedef boost::unordered_map<Word, PVertex, SymbolHasher, 00025 SymbolEqualityPred> TMap; 00026 typedef NonTerminalMap<PVertex> NMap; 00027 // Collection of terminal vertices (keyed by terminal symbol). 00028 TMap terminalVertices; 00029 // Collection of non-terminal vertices (keyed by non-terminal symbol). 00030 NMap nonTerminalVertices; 00031 }; 00032 00033 struct CompressedItem { 00034 std::size_t end; 00035 const PVertex *vertex; 00036 }; 00037 00038 typedef std::vector<std::vector<CompressedItem> > CompressedMatrix; 00039 00040 PChart(std::size_t width, bool maintainCompressedChart); 00041 00042 ~PChart(); 00043 00044 std::size_t GetWidth() const { 00045 return m_cells.size(); 00046 } 00047 00048 const Cell &GetCell(std::size_t start, std::size_t end) const { 00049 return m_cells[start][end]; 00050 } 00051 00052 // Insert the given PVertex and return a reference to the inserted object. 00053 PVertex &AddVertex(const PVertex &v) { 00054 const std::size_t start = v.span.GetStartPos(); 00055 const std::size_t end = v.span.GetEndPos(); 00056 Cell &cell = m_cells[start][end]; 00057 // If v is a terminal vertex add it to the cell's terminalVertices map. 00058 if (!v.symbol.IsNonTerminal()) { 00059 Cell::TMap::value_type x(v.symbol, v); 00060 std::pair<Cell::TMap::iterator, bool> ret = 00061 cell.terminalVertices.insert(x); 00062 return ret.first->second; 00063 } 00064 // If v is a non-terminal vertex add it to the cell's nonTerminalVertices 00065 // map and update the compressed chart (if enabled). 00066 std::pair<Cell::NMap::Iterator, bool> result = 00067 cell.nonTerminalVertices.Insert(v.symbol, v); 00068 if (result.second && m_compressedChart) { 00069 CompressedItem item; 00070 item.end = end; 00071 item.vertex = &(result.first->second); 00072 (*m_compressedChart)[start][v.symbol[0]->GetId()].push_back(item); 00073 } 00074 return result.first->second; 00075 } 00076 00077 const CompressedMatrix &GetCompressedMatrix(std::size_t start) const { 00078 return (*m_compressedChart)[start]; 00079 } 00080 00081 private: 00082 typedef std::vector<CompressedMatrix> CompressedChart; 00083 00084 std::vector<std::vector<Cell> > m_cells; 00085 CompressedChart *m_compressedChart; 00086 }; 00087 00088 } // S2T 00089 } // Syntax 00090 } // Moses