File size: 10,647 Bytes
fd49381 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
#include "dsgHyp.h"
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <algorithm>
#include <cstdlib>
#include <math.h>
#include <map>
using namespace std;
using namespace lm::ngram;
namespace Moses
{
dsgState::dsgState(const State & val)
{
lmState = val;
}
void dsgState::saveState( std::vector<std::string> danglingTok, std::vector<int> srcSpans,float deltaValue)
{
buffer = danglingTok;
span=srcSpans;
delta=deltaValue;
}
size_t dsgState::hash() const
{
size_t ret = 0;
boost::hash_combine(ret, lmState);
/*size_t ret = delta;
boost::hash_combine(ret, buffer);
boost::hash_combine(ret, span);
boost::hash_combine(ret, lmState.length);
return ret;*/
}
bool dsgState::operator==(const FFState& otherBase) const //CHECK
{
const dsgState &other = static_cast<const dsgState&>(otherBase);
if (lmState < other.lmState) return false;
if (lmState == other.lmState) return true;
return false;
}
// ----------------------------------------
std::string dsgState :: getName() const
{
return "done";
}
dsgHypothesis :: dsgHypothesis()
{
lmProb = 0;
discontig0 = 0;
discontig1 = 0;
discontig2 = 0;
UnsegWP = 0;
m_buffer.clear();//="";
}
void dsgHypothesis :: setState(const FFState* prev_state)
{
if(prev_state != NULL) {
m_buffer = static_cast <const dsgState *> (prev_state)->getBuffer();
m_span = static_cast <const dsgState *> (prev_state)->getSpan();
lmState = static_cast <const dsgState *> (prev_state)->getLMState();
delta = static_cast <const dsgState *> (prev_state)->getDelta(); //NEW
}
}
dsgState * dsgHypothesis :: saveState()
{
dsgState * statePtr = new dsgState(lmState);
statePtr->saveState(m_buffer, m_span, delta);
return statePtr;
}
void dsgHypothesis :: populateScores(vector <float> & scores , const int numFeatures)
{
scores.clear();
scores.push_back(lmProb);
if (numFeatures == 1)
return;
scores.push_back(discontig0);
scores.push_back(discontig1);
scores.push_back(discontig2);
scores.push_back(UnsegWP);
}
bool dsgHypothesis::isPrefix(const std::string &tok)
{
if ((tok.at(tok.size() - 1) == '+' )&& (tok != "+")) {
return true;
} else {
return false;
};
}
bool dsgHypothesis::isSuffix(const std::string &tok)
{
if ((tok.at(0) == '+' )&& (tok != "+")) {
return true;
} else {
return false;
};
}
bool dsgHypothesis::isStem(const std::string &tok)
{
if ((tok.at(0) != '+') && (tok.at(tok.size() - 1) != '+')) {
return true;
} else {
return false;
};
}
/**
* chain stores segmented tokens that are in process of building a word
* The function checks if tok contributes to the word being formed in chain
*
*/
bool dsgHypothesis::isValidChain(const std::string &tok, std::vector<std::string> &chain)
{
std::string last_tok;
if (chain.size() >= 1) {
last_tok = chain[chain.size() - 1];
} else {
last_tok = "NULL";
}
if(tok=="+") {
return false;
}
if (isPrefix(tok) && (chain.size() == 0 || isPrefix(last_tok))) {
return true;
} else if (isSuffix(tok) && (chain.size() != 0 && ( isStem(last_tok) || isPrefix(last_tok)))) {
return true; // allows one suffix ONLY
}
//else if (isSuffix(tok) && (chain.size() != 0 && ( isStem(last_tok) || isPrefix(last_tok) || isSuffix(last_tok) ))) { return true; } // allows multiple suffixes
else if (isStem(tok) && (chain.size() == 0 || isPrefix(last_tok))) {
return true;
} else {
return false;
}
}
/**
* grouper function groups tokens that form a word together
*/
vector<string> dsgHypothesis::grouper(std::vector<std::string> &phr_vec,vector<vector<int> > &allchain_ids, int sourceOffset,const AlignmentInfo &align, bool isolation)
{
std::vector<std::string> chain;
std::vector<int> chain_ids;
std::vector<std::string> allchains;
chain_ids=m_span;
if (!m_buffer.empty() && !isolation) { // if evaluate in isolation is called, then do not add buffer content
for (int i = 0; i < m_buffer.size(); i++) { // initialize chain with the content of the buffer
chain.push_back(m_buffer[i]);
}
}
for (int i = 0; i < phr_vec.size(); i++) {
std::set<std::size_t> sourcePosSet = align.GetAlignmentsForTarget(i);
if (isValidChain(phr_vec[i], chain)) {
chain.push_back(phr_vec[i]);
if (sourcePosSet.empty()==false) {
for (std::set<size_t>::iterator it(sourcePosSet.begin()); it != sourcePosSet.end(); it++) {
int cur=*it;
chain_ids.push_back(cur+sourceOffset);
}
}
}
else if (chain.size() == 0) { // start of a suffix at hypothesis0
allchains.push_back(phr_vec[i]);
allchain_ids.push_back(chain_ids);
chain_ids.clear();//={};
}
else { // tokens formed a complete word; add tokens segmented by space to allchains
std::string joined = boost::algorithm::join(chain, " ");
allchains.push_back(joined);
allchain_ids.push_back(chain_ids);
chain.clear();// = {};
chain_ids.clear();//={};
chain.push_back(phr_vec[i]);
if (sourcePosSet.empty()==false) {
for (std::set<size_t>::iterator it(sourcePosSet.begin()); it != sourcePosSet.end(); it++) {
int cur=*it;
chain_ids.push_back(cur+sourceOffset);
}
}
}
}
if (!chain.empty()) {
std::string joined = boost::algorithm::join(chain, " ");
allchains.push_back(joined);
allchain_ids.push_back(chain_ids);
}
return allchains;
}
void dsgHypothesis :: calculateDsgProbinIsol(DsgLM & ptrDsgLM, Desegmenter &desegT, const AlignmentInfo &align )
{
lmProb = 0;
State currState = lmState;
State temp;
string desegmented="";
vector <string> words;
vector <string> currFVec;
discontig0=0;
discontig1=0;
discontig2=0;
UnsegWP=0;
currFVec = m_buffer;
currFVec.insert( currFVec.end(), m_curr_phr.begin(), m_curr_phr.end() );
int vecSize=currFVec.size();
// phrases with suffix-starts and prefix-end
if (currFVec.size()>0 && isPrefix (currFVec.back())) {
UnsegWP-=0.5;
}
if (currFVec.size()>0 && isSuffix (currFVec.front())) {
UnsegWP-=0.5;
}
/* //Dropping prefix-end and suffix-start
while (currFVec.size()>0 && isPrefix (currFVec.back())){
currFVec.pop_back(); //drop prefix appearing at end of phrase
}
while (currFVec.size()>0 && isSuffix (currFVec.front())){
currFVec.erase (currFVec.begin()); //drop suffix appearning at start of a phrase
} */
vector<vector<int> > chain_ids;
words = grouper(currFVec,chain_ids,0,align,1);
for (int i = 0; i<words.size(); i++) {
UnsegWP+=1;
temp = currState;
if (words[i].find(" ")!=std::string::npos) {
desegmented=desegT.Search(words[i])[0];
lmProb += ptrDsgLM.Score(temp,desegmented,currState);
} else {
boost::replace_all(words[i], "-LRB-", "(");
boost::replace_all(words[i], "-RRB-", ")");
lmProb += ptrDsgLM.Score(temp,words[i],currState);
}
}
lmState = currState;
}
void dsgHypothesis :: calculateDsgProb(DsgLM& ptrDsgLM, Desegmenter &desegT, bool isCompleted , const AlignmentInfo &align, int sourceOffset, bool optimistic)
{
lmProb = 0;
discontig0=0;
discontig1=0;
discontig2=0;
UnsegWP=0;
State currState = lmState;
State temp;
string desegmented="";
vector <string> words;
vector <string> currFVec;
bool completePhraseSuffixEnd = false;
vector<vector<int> > all_chain_ids;
double pscore;
currFVec=m_curr_phr;
// Check if the the phrase ends in a suffix, which means that it completes a full word;Make sure to change the isValidChain
if (isSuffix (currFVec.back()) && (currFVec.back()!="+")) {
completePhraseSuffixEnd=true;
}
words = grouper(currFVec,all_chain_ids,sourceOffset,align,0);
for (int i = 0; i < words.size(); i++) {
temp = currState;
if (i==words.size()-1) {
if (completePhraseSuffixEnd) { //i.e if phrase ends with suffix, which marks an end of a word
m_buffer.clear();// ="";
m_span.clear();// ={};
} else if (!isCompleted) { // not end of sentence( or final hypothesis), and probably the last token is not a complete word
m_buffer.clear();
if (optimistic == 1) {
if ( isPrefix (currFVec.back())) { // this will delay scoring of prefix in prefix-ending phrases until the next hypothesis arrives
//pscore = ptrDsgLM.Score(temp,desegmented,currState);
lmProb -= delta;
delta = 0.0;
}
else if (words[i].find(" ")!=std::string::npos) {
desegmented=desegT.Search(words[i])[0];
pscore=ptrDsgLM.Score(temp,desegmented,currState);
lmProb = lmProb + pscore - delta;
delta=pscore;
currState=temp;
} else {
boost::replace_all(words[i], "-LRB-", "(");
boost::replace_all(words[i], "-RRB-", ")");
pscore=ptrDsgLM.Score(temp,words[i],currState);
lmProb = lmProb + pscore - delta;
delta=pscore;
currState=temp;
}
}
m_buffer.push_back(words.back());
m_span=all_chain_ids.back();
break;
}
}
//temp = currState;
if (words[i].find(" ")!=std::string::npos) {
UnsegWP+=1;
desegmented=desegT.Search(words[i])[0];
std::set<int> cur_chain_ids(all_chain_ids[i].begin(),all_chain_ids[i].end());
if (cur_chain_ids.size()>1) {
vector<int> dsc;
for (std::set<int>::iterator it(cur_chain_ids.begin()), next(it); it != cur_chain_ids.end() && ++next != cur_chain_ids.end(); it = next) {
int cur=*it;
int mynext=*next;
if (std::abs(cur - mynext)>= 3) {
dsc.push_back(3);
} else if (std::abs(cur - mynext)== 2) {
dsc.push_back(2);
} else if (std::abs(cur - mynext)<= 1) {
dsc.push_back(1);
}
}
int mymax=*std::max_element(dsc.begin(),dsc.end());
if (mymax==3) {
discontig2+=1;
} else if (mymax==2) {
discontig1+=1;
} else {
discontig0+=1;
}
} else {
discontig0 += 1;
}
lmProb += ptrDsgLM.Score(temp,desegmented,currState);
} else {
UnsegWP+=1;
boost::replace_all(words[i], "-LRB-", "(");
boost::replace_all(words[i], "-RRB-", ")");
lmProb += ptrDsgLM.Score(temp,words[i],currState);
}
}
if (isCompleted) {
temp = currState;
lmProb = lmProb + ptrDsgLM.ScoreEndSentence(temp,currState) - delta;
}
lmState = currState;
}
void dsgHypothesis :: print()
{}
} // namespace
|