|
#include <stdlib.h> |
|
#include <iostream> |
|
#include "ReorderingConstraint.h" |
|
#include "Sentence.h" |
|
#include "../TypeDef.h" |
|
#include "../legacy/Bitmap.h" |
|
|
|
using namespace std; |
|
|
|
namespace Moses2 |
|
{ |
|
|
|
ReorderingConstraint::~ReorderingConstraint() |
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
void ReorderingConstraint::InitializeWalls(size_t size, int max_distortion) |
|
{ |
|
m_size = size; |
|
|
|
m_wall = m_pool.Allocate<bool>(size); |
|
m_localWall = m_pool.Allocate<size_t>(size); |
|
|
|
m_max_distortion = max_distortion; |
|
|
|
for (size_t pos = 0 ; pos < m_size ; pos++) { |
|
m_wall[pos] = false; |
|
m_localWall[pos] = NOT_A_ZONE; |
|
} |
|
} |
|
|
|
|
|
void ReorderingConstraint::FinalizeWalls() |
|
{ |
|
for(size_t z = 0; z < m_zone.size(); z++ ) { |
|
const size_t startZone = m_zone[z].first; |
|
const size_t endZone = m_zone[z].second; |
|
for( size_t pos = startZone; pos < endZone; pos++ ) { |
|
if (m_wall[ pos ]) { |
|
m_localWall[ pos ] = z; |
|
m_wall[ pos ] = false; |
|
|
|
} |
|
|
|
else if (m_localWall[ pos ] != NOT_A_ZONE) { |
|
size_t assigned_z = m_localWall[ pos ]; |
|
if ((m_zone[assigned_z].first < startZone) || |
|
(m_zone[assigned_z].second > endZone)) { |
|
m_localWall[ pos ] = z; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
void ReorderingConstraint::SetWall( size_t pos, bool value ) |
|
{ |
|
|
|
UTIL_THROW_IF2(pos >= m_size, "Wall over length of sentence: " << pos << " >= " << m_size); |
|
m_wall[pos] = value; |
|
m_active = true; |
|
} |
|
|
|
|
|
void ReorderingConstraint::SetZone( size_t startPos, size_t endPos ) |
|
{ |
|
|
|
std::pair<size_t,size_t> newZone; |
|
newZone.first = startPos; |
|
newZone.second = endPos; |
|
m_zone.push_back( newZone ); |
|
m_active = true; |
|
} |
|
|
|
|
|
void ReorderingConstraint::SetMonotoneAtPunctuation( const Sentence &sentence ) |
|
{ |
|
for( size_t i=0; i<sentence.GetSize(); i++ ) { |
|
const Word& word = sentence[i]; |
|
if (word[0]->GetString() == "," || |
|
word[0]->GetString() == "." || |
|
word[0]->GetString() == "!" || |
|
word[0]->GetString() == "?" || |
|
word[0]->GetString() == ":" || |
|
word[0]->GetString() == ";" || |
|
word[0]->GetString() == "\"") { |
|
|
|
if (i>0 && i<m_size-1) SetWall( i, true ); |
|
if (i>1) SetWall( i-1, true ); |
|
} |
|
} |
|
} |
|
|
|
|
|
bool ReorderingConstraint::Check( const Bitmap &bitmap, size_t startPos, size_t endPos ) const |
|
{ |
|
|
|
if (! IsActive() ) return true; |
|
|
|
|
|
|
|
|
|
size_t firstGapPos = bitmap.GetFirstGapPos(); |
|
|
|
if (firstGapPos != startPos) { |
|
|
|
|
|
|
|
for( size_t pos = firstGapPos; pos < endPos; pos++ ) { |
|
if( GetWall( pos ) ) { |
|
|
|
return false; |
|
} |
|
} |
|
} |
|
|
|
|
|
size_t lastPos = bitmap.GetLastPos(); |
|
if ((lastPos == NOT_FOUND && startPos == 0) || |
|
(firstGapPos > lastPos && |
|
firstGapPos == startPos)) { |
|
|
|
return true; |
|
} |
|
|
|
|
|
for(size_t z = 0; z < m_zone.size(); z++ ) { |
|
const size_t startZone = m_zone[z].first; |
|
const size_t endZone = m_zone[z].second; |
|
|
|
|
|
if (lastPos < startZone && ( endPos < startZone || startPos > endZone ) ) { |
|
continue; |
|
} |
|
|
|
|
|
if (firstGapPos > endZone) { |
|
continue; |
|
} |
|
|
|
|
|
|
|
size_t numWordsInZoneTranslated = 0; |
|
if (lastPos >= startZone) { |
|
for(size_t pos = startZone; pos <= endZone; pos++ ) { |
|
if( bitmap.GetValue( pos ) ) { |
|
numWordsInZoneTranslated++; |
|
} |
|
} |
|
} |
|
|
|
|
|
if (numWordsInZoneTranslated == endZone-startZone+1) { |
|
continue; |
|
} |
|
|
|
|
|
bool activeZone = (numWordsInZoneTranslated > 0); |
|
|
|
|
|
if (!activeZone && ( endPos < startZone || startPos > endZone ) ) { |
|
continue; |
|
} |
|
|
|
|
|
if (activeZone && ( endPos < startZone || startPos > endZone ) ) { |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t distortionLimit = m_max_distortion; |
|
if (startPos != firstGapPos && endZone-firstGapPos >= distortionLimit) { |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
if (startPos <= startZone) { |
|
continue; |
|
} |
|
|
|
|
|
if (endPos > endZone) { |
|
if (endZone-startPos+1 < |
|
endZone-startZone+1-numWordsInZoneTranslated) { |
|
|
|
return false; |
|
} else { |
|
continue; |
|
} |
|
} |
|
|
|
|
|
|
|
bool seenUntranslatedBeforeStartPos = false; |
|
for(size_t pos = startZone; pos < endZone && pos < endPos; pos++ ) { |
|
|
|
if( !bitmap.GetValue( pos ) |
|
&& pos < startPos ) { |
|
seenUntranslatedBeforeStartPos = true; |
|
} |
|
if( seenUntranslatedBeforeStartPos && GetLocalWall( pos, z ) ) { |
|
|
|
return false; |
|
} |
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
} |
|
|
|
std::ostream &ReorderingConstraint::Debug(std::ostream &out, const System &system) const |
|
{ |
|
out << "Zones:"; |
|
for (size_t i = 0; i < m_zone.size(); ++i) { |
|
const std::pair<size_t,size_t> &zone1 = m_zone[i]; |
|
out << zone1.first << "-" << zone1.second << " "; |
|
} |
|
|
|
out << "Walls:"; |
|
for (size_t i = 0; i < m_size; ++i) { |
|
out << m_wall[i]; |
|
} |
|
|
|
out << " Local walls:"; |
|
for (size_t i = 0; i < m_size; ++i) { |
|
out << m_localWall[i] << " "; |
|
} |
|
|
|
return out; |
|
} |
|
|
|
} |
|
|
|
|