|
|
|
|
|
|
|
|
|
|
|
|
|
#include "ReorderingStack.h" |
|
#include <vector> |
|
|
|
namespace Moses |
|
{ |
|
size_t ReorderingStack::hash() const |
|
{ |
|
std::size_t ret = boost::hash_range(m_stack.begin(), m_stack.end()); |
|
return ret; |
|
} |
|
|
|
bool ReorderingStack::operator==(const ReorderingStack& o) const |
|
{ |
|
const ReorderingStack& other = static_cast<const ReorderingStack&>(o); |
|
return m_stack == other.m_stack; |
|
} |
|
|
|
|
|
int ReorderingStack::ShiftReduce(Range input_span) |
|
{ |
|
int distance; |
|
|
|
|
|
if(m_stack.empty()) { |
|
m_stack.push_back(input_span); |
|
return input_span.GetStartPos() + 1; |
|
} |
|
|
|
|
|
Range prev_span = m_stack.back(); |
|
|
|
|
|
if(input_span.GetStartPos() > prev_span.GetStartPos()) { |
|
distance = input_span.GetStartPos() - prev_span.GetEndPos(); |
|
} else { |
|
distance = input_span.GetEndPos() - prev_span.GetStartPos(); |
|
} |
|
|
|
if(distance == 1) { |
|
m_stack.pop_back(); |
|
Range new_span(prev_span.GetStartPos(), input_span.GetEndPos()); |
|
Reduce(new_span); |
|
} else if(distance == -1) { |
|
m_stack.pop_back(); |
|
Range new_span(input_span.GetStartPos(), prev_span.GetEndPos()); |
|
Reduce(new_span); |
|
} else { |
|
m_stack.push_back(input_span); |
|
} |
|
|
|
return distance; |
|
} |
|
|
|
|
|
void ReorderingStack::Reduce(Range current) |
|
{ |
|
bool cont_loop = true; |
|
|
|
while (cont_loop && m_stack.size() > 0) { |
|
|
|
Range previous = m_stack.back(); |
|
|
|
if(current.GetStartPos() - previous.GetEndPos() == 1) { |
|
m_stack.pop_back(); |
|
Range t(previous.GetStartPos(), current.GetEndPos()); |
|
current = t; |
|
} else if(previous.GetStartPos() - current.GetEndPos() == 1) { |
|
m_stack.pop_back(); |
|
Range t(current.GetStartPos(), previous.GetEndPos()); |
|
current = t; |
|
} else { |
|
cont_loop=false; |
|
} |
|
} |
|
|
|
|
|
m_stack.push_back(current); |
|
} |
|
|
|
} |
|
|
|
|