00001 #pragma once
00002
00003 #include <memory>
00004 #include <vector>
00005
00006 #include "moses/ChartParser.h"
00007 #include "moses/ChartTranslationOptionList.h"
00008 #include "moses/InputType.h"
00009 #include "moses/NonTerminal.h"
00010 #include "moses/StaticData.h"
00011 #include "moses/Syntax/S2T/Parsers/Parser.h"
00012 #include "moses/Syntax/S2T/PChart.h"
00013
00014 #include "TailLatticeSearcher.h"
00015
00016 namespace Moses
00017 {
00018 namespace Syntax
00019 {
00020 namespace S2T
00021 {
00022
00023 template<typename Callback>
00024 Scope3Parser<Callback>::Scope3Parser(PChart &chart, const RuleTrie &trie,
00025 std::size_t maxChartSpan)
00026 : Parser<Callback>(chart)
00027 , m_ruleTable(trie)
00028 , m_maxChartSpan(maxChartSpan)
00029 , m_latticeBuilder(chart)
00030 {
00031 Init();
00032 }
00033
00034 template<typename Callback>
00035 Scope3Parser<Callback>::~Scope3Parser()
00036 {
00037 delete m_patRoot;
00038 }
00039
00040 template<typename Callback>
00041 void Scope3Parser<Callback>::
00042 EnumerateHyperedges(const Range &range, Callback &callback)
00043 {
00044 const std::size_t start = range.GetStartPos();
00045 const std::size_t end = range.GetEndPos();
00046
00047 const std::vector<const PatternApplicationTrie *> &patNodes =
00048 m_patSpans[start][end-start+1];
00049
00050 for (std::vector<const PatternApplicationTrie *>::const_iterator
00051 p = patNodes.begin(); p != patNodes.end(); ++p) {
00052 const PatternApplicationTrie *patNode = *p;
00053
00054
00055 patNode->ReadOffPatternApplicationKey(m_patKey);
00056
00057
00058 m_symbolRangeCalculator.Calc(m_patKey, start, end, m_symbolRanges);
00059
00060
00061
00062 m_latticeBuilder.Build(m_patKey, m_symbolRanges, m_lattice,
00063 m_quickCheckTable);
00064
00065
00066
00067 const RuleTrie::Node::LabelMap &labelMap = patNode->m_node->GetLabelMap();
00068
00069
00070
00071 TailLatticeSearcher<Callback> searcher(m_lattice, m_patKey, m_symbolRanges);
00072 RuleTrie::Node::LabelMap::const_iterator q = labelMap.begin();
00073 for (; q != labelMap.end(); ++q) {
00074 const std::vector<int> &labelSeq = q->first;
00075 TargetPhraseCollection::shared_ptr tpc = q->second;
00076
00077
00078
00079 bool failCheck = false;
00080 std::size_t nonTermIndex = 0;
00081 for (std::size_t i = 0; i < m_patKey.size(); ++i) {
00082 if (m_patKey[i]->IsTerminalNode()) {
00083 continue;
00084 }
00085 if (!m_quickCheckTable[nonTermIndex][labelSeq[nonTermIndex]]) {
00086 failCheck = true;
00087 break;
00088 }
00089 ++nonTermIndex;
00090 }
00091 if (failCheck) {
00092 continue;
00093 }
00094 searcher.Search(labelSeq, tpc, callback);
00095 }
00096 }
00097 }
00098
00099 template<typename Callback>
00100 void Scope3Parser<Callback>::Init()
00101 {
00102
00103 SentenceMap sentMap;
00104 FillSentenceMap(sentMap);
00105
00106
00107 const RuleTrie::Node &root = m_ruleTable.GetRootNode();
00108 m_patRoot = new PatternApplicationTrie(-1, -1, root, 0, 0);
00109 m_patRoot->Extend(root, -1, sentMap, false);
00110
00111
00112 InitRuleApplicationVector();
00113 RecordPatternApplicationSpans(*m_patRoot);
00114 }
00115
00116
00117 template<typename Callback>
00118 void Scope3Parser<Callback>::InitRuleApplicationVector()
00119 {
00120 std::size_t length = Base::m_chart.GetWidth();
00121 m_patSpans.resize(length);
00122 for (std::size_t start = 0; start < length; ++start) {
00123 std::size_t maxSpan = length-start;
00124 m_patSpans[start].resize(maxSpan+1);
00125 }
00126 }
00127
00128 template<typename Callback>
00129 void Scope3Parser<Callback>::FillSentenceMap(SentenceMap &sentMap)
00130 {
00131 typedef PChart::Cell Cell;
00132
00133 const std::size_t width = Base::m_chart.GetWidth();
00134 for (std::size_t i = 0; i < width; ++i) {
00135 for (std::size_t j = i; j < width; ++j) {
00136 const Cell::TMap &map = Base::m_chart.GetCell(i, j).terminalVertices;
00137 for (Cell::TMap::const_iterator p = map.begin(); p != map.end(); ++p) {
00138 const Word &terminal = p->first;
00139 const PVertex &v = p->second;
00140 sentMap[terminal].push_back(&v);
00141 }
00142 }
00143 }
00144 }
00145
00146 template<typename Callback>
00147 void Scope3Parser<Callback>::RecordPatternApplicationSpans(
00148 const PatternApplicationTrie &patNode)
00149 {
00150 if (patNode.m_node->HasRules()) {
00151 int s1 = -1;
00152 int s2 = -1;
00153 int e1 = -1;
00154 int e2 = -1;
00155 patNode.DetermineStartRange(Base::m_chart.GetWidth(), s1, s2);
00156 patNode.DetermineEndRange(Base::m_chart.GetWidth(), e1, e2);
00157
00158 int minSpan = patNode.Depth();
00159
00160
00161 for (int i = s1; i <= s2; ++i) {
00162 for (int j = std::max(e1, i+minSpan-1); j <= e2; ++j) {
00163 std::size_t span = j-i+1;
00164 assert(span >= 1);
00165 if (span < std::size_t(minSpan)) {
00166 continue;
00167 }
00168 if (m_maxChartSpan && span > m_maxChartSpan) {
00169 break;
00170 }
00171 m_patSpans[i][span].push_back(&patNode);
00172 }
00173 }
00174 }
00175
00176 for (std::vector<PatternApplicationTrie*>::const_iterator p =
00177 patNode.m_children.begin(); p != patNode.m_children.end(); ++p) {
00178 RecordPatternApplicationSpans(**p);
00179 }
00180 }
00181
00182 }
00183 }
00184 }