00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "ReorderingConstraint.h"
00024 #include "InputType.h"
00025 #include "StaticData.h"
00026 #include "Bitmap.h"
00027
00028 namespace Moses
00029 {
00030
00032 void ReorderingConstraint::InitializeWalls(size_t size)
00033 {
00034 m_size = size;
00035 m_wall = (bool*) malloc(sizeof(bool) * size);
00036 m_localWall = (size_t*) malloc(sizeof(size_t) * size);
00037
00038 for (size_t pos = 0 ; pos < m_size ; pos++) {
00039 m_wall[pos] = false;
00040 m_localWall[pos] = NOT_A_ZONE;
00041 }
00042 }
00043
00044
00046 void ReorderingConstraint::SetWall( size_t pos, bool value )
00047 {
00048 VERBOSE(3,"SETTING reordering wall at position " << pos << std::endl);
00049 m_wall[pos] = value;
00050 m_active = true;
00051 }
00052
00054 void ReorderingConstraint::FinalizeWalls()
00055 {
00056 for(size_t z = 0; z < m_zone.size(); z++ ) {
00057 const size_t startZone = m_zone[z].first;
00058 const size_t endZone = m_zone[z].second;
00059 for( size_t pos = startZone; pos < endZone; pos++ ) {
00060 if (m_wall[ pos ]) {
00061 m_localWall[ pos ] = z;
00062 m_wall[ pos ] = false;
00063 VERBOSE(3,"SETTING local wall " << pos << std::endl);
00064 }
00065
00066 else if (m_localWall[ pos ] != NOT_A_ZONE) {
00067 size_t assigned_z = m_localWall[ pos ];
00068 if ((m_zone[assigned_z].first < startZone) ||
00069 (m_zone[assigned_z].second > endZone)) {
00070 m_localWall[ pos ] = z;
00071 }
00072 }
00073 }
00074 }
00075 }
00076
00078 void ReorderingConstraint::SetMonotoneAtPunctuation( const Phrase &sentence )
00079 {
00080 for( size_t i=0; i<sentence.GetSize(); i++ ) {
00081 const Word& word = sentence.GetWord(i);
00082 if (word[0]->GetString() == "," ||
00083 word[0]->GetString() == "." ||
00084 word[0]->GetString() == "!" ||
00085 word[0]->GetString() == "?" ||
00086 word[0]->GetString() == ":" ||
00087 word[0]->GetString() == ";" ||
00088 word[0]->GetString() == "\"") {
00089
00090 if (i>0 && i<m_size-1) SetWall( i, true );
00091 if (i>1) SetWall( i-1, true );
00092 }
00093 }
00094 }
00095
00097 void ReorderingConstraint::SetZone( size_t startPos, size_t endPos )
00098 {
00099 VERBOSE(3,"SETTING zone " << startPos << "-" << endPos << std::endl);
00100 std::pair<size_t,size_t> newZone;
00101 newZone.first = startPos;
00102 newZone.second = endPos;
00103 m_zone.push_back( newZone );
00104 m_active = true;
00105 }
00106
00108 bool ReorderingConstraint::Check( const Bitmap &bitmap, size_t startPos, size_t endPos ) const
00109 {
00110
00111 if (! IsActive() ) return true;
00112
00113 VERBOSE(3,"Check " << bitmap << " " << startPos << "-" << endPos);
00114
00115
00116 size_t firstGapPos = bitmap.GetFirstGapPos();
00117
00118 if (firstGapPos != startPos) {
00119
00120
00121
00122 for( size_t pos = firstGapPos; pos < endPos; pos++ ) {
00123 if( GetWall( pos ) ) {
00124 VERBOSE(3," hitting wall " << pos << std::endl);
00125 return false;
00126 }
00127 }
00128 }
00129
00130
00131 size_t lastPos = bitmap.GetLastPos();
00132 if ((lastPos == NOT_FOUND && startPos == 0) ||
00133 (firstGapPos > lastPos &&
00134 firstGapPos == startPos)) {
00135 VERBOSE(3," montone, fine." << std::endl);
00136 return true;
00137 }
00138
00139
00140 for(size_t z = 0; z < m_zone.size(); z++ ) {
00141 const size_t startZone = m_zone[z].first;
00142 const size_t endZone = m_zone[z].second;
00143
00144
00145 if (lastPos < startZone && ( endPos < startZone || startPos > endZone ) ) {
00146 continue;
00147 }
00148
00149
00150 if (firstGapPos > endZone) {
00151 continue;
00152 }
00153
00154
00155
00156 size_t numWordsInZoneTranslated = 0;
00157 if (lastPos >= startZone) {
00158 for(size_t pos = startZone; pos <= endZone; pos++ ) {
00159 if( bitmap.GetValue( pos ) ) {
00160 numWordsInZoneTranslated++;
00161 }
00162 }
00163 }
00164
00165
00166 if (numWordsInZoneTranslated == endZone-startZone+1) {
00167 continue;
00168 }
00169
00170
00171 bool activeZone = (numWordsInZoneTranslated > 0);
00172
00173
00174 if (!activeZone && ( endPos < startZone || startPos > endZone ) ) {
00175 continue;
00176 }
00177
00178
00179 if (activeZone && ( endPos < startZone || startPos > endZone ) ) {
00180 VERBOSE(3," outside active zone" << std::endl);
00181 return false;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 size_t distortionLimit = m_max_distortion;
00193 if (startPos != firstGapPos && endZone-firstGapPos >= distortionLimit) {
00194 VERBOSE(3," dead end due to distortion limit" << std::endl);
00195 return false;
00196 }
00197
00198
00199
00200
00201 if (startPos <= startZone) {
00202 continue;
00203 }
00204
00205
00206 if (endPos > endZone) {
00207 if (endZone-startPos+1 <
00208 endZone-startZone+1-numWordsInZoneTranslated) {
00209 VERBOSE(3," overlap end, but not completing" << std::endl);
00210 return false;
00211 } else {
00212 continue;
00213 }
00214 }
00215
00216
00217
00218 bool seenUntranslatedBeforeStartPos = false;
00219 for(size_t pos = startZone; pos < endZone && pos < endPos; pos++ ) {
00220
00221 if( !bitmap.GetValue( pos )
00222 && pos < startPos ) {
00223 seenUntranslatedBeforeStartPos = true;
00224 }
00225 if( seenUntranslatedBeforeStartPos && GetLocalWall( pos, z ) ) {
00226 VERBOSE(3," local wall violation" << std::endl);
00227 return false;
00228 }
00229 }
00230
00231
00232 }
00233
00234
00235 VERBOSE(3," fine." << std::endl);
00236 return true;
00237 }
00238
00239 std::ostream& operator<<(std::ostream& out, const ReorderingConstraint &obj)
00240 {
00241 out << "Zones:";
00242 for (size_t i = 0; i < obj.m_zone.size(); ++i) {
00243 const std::pair<size_t,size_t> &zone1 = obj.m_zone[i];
00244 out << zone1.first << "-" << zone1.second << " ";
00245 }
00246
00247 out << "Walls:";
00248 for (size_t i = 0; i < obj.m_size; ++i) {
00249 out << obj.m_wall[i];
00250 }
00251
00252 out << " Local walls:";
00253 for (size_t i = 0; i < obj.m_size; ++i) {
00254 out << obj.m_localWall[i] << " ";
00255 }
00256
00257 return out;
00258 }
00259
00260 }