// $Id$ // vim:tabstop=2 #ifndef moses_PDTAimp_h #define moses_PDTAimp_h #include "StaticData.h" // needed for factor splitter #include "PhraseDictionaryTree.h" #include "UniqueObject.h" #include "InputFileStream.h" #include "PhraseDictionaryTreeAdaptor.h" #include "Util.h" namespace Moses { inline double addLogScale(double x,double y) { if(x>y) return addLogScale(y,x); else return x+log(1.0+exp(y-x)); } inline double Exp(double x) { return exp(x); } class PDTAimp { // only these classes are allowed to instantiate this class friend class PhraseDictionaryTreeAdaptor; protected: PDTAimp(PhraseDictionaryTreeAdaptor *p,unsigned nis) : m_languageModels(0),m_weightWP(0.0),m_dict(0), m_obj(p),useCache(1),m_numInputScores(nis),totalE(0),distinctE(0) {} public: std::vector m_weights; LMList const* m_languageModels; float m_weightWP; std::vector m_input,m_output; PhraseDictionaryTree *m_dict; typedef std::vector vTPC; mutable vTPC m_tgtColls; typedef std::map MapSrc2Tgt; mutable MapSrc2Tgt m_cache; PhraseDictionaryTreeAdaptor *m_obj; int useCache; std::vector m_rangeCache; unsigned m_numInputScores; UniqueObjectManager uniqSrcPhr; size_t totalE,distinctE; std::vector path1Best,pathExplored; std::vector pathCN; ~PDTAimp() { CleanUp(); delete m_dict; if (StaticData::Instance().GetVerboseLevel() >= 2) { TRACE_ERR("tgt candidates stats: total="<(std::cerr," \t")); TRACE_ERR("\n"); } if(pathCN.size()) { TRACE_ERR("CN (full): "); std::transform(pathCN.begin()+1 ,pathCN.end() ,std::ostream_iterator(std::cerr," \t") ,Exp); TRACE_ERR("\n"); } if(pathExplored.size()) { TRACE_ERR("CN (explored): "); std::copy(pathExplored.begin()+1,pathExplored.end(), std::ostream_iterator(std::cerr," \t")); TRACE_ERR("\n"); } } } void Factors2String(Word const& w,std::string& s) const { s=w.GetString(m_input,false); } void CleanUp() { CHECK(m_dict); m_dict->FreeMemory(); for(size_t i=0; i piter; if(useCache) { piter=m_cache.insert(std::make_pair(src,static_cast(0))); if(!piter.second) return piter.first->second; } else if (m_cache.size()) { MapSrc2Tgt::const_iterator i=m_cache.find(src); return (i!=m_cache.end() ? i->second : 0); } std::vector srcString(src.GetSize()); // convert source Phrase into vector of strings for(size_t i=0; i cands; std::vector wacands; m_dict->GetTargetCandidates(srcString,cands,wacands); if(cands.empty()) { return 0; } std::vector tCands; tCands.reserve(cands.size()); std::vector > costs; costs.reserve(cands.size()); // convert into TargetPhrases for(size_t i=0; i scoreVector(probVector.size()); std::transform(probVector.begin(),probVector.end(),scoreVector.begin(), TransformScore); std::transform(scoreVector.begin(),scoreVector.end(),scoreVector.begin(), FloorScore); //CreateTargetPhrase(targetPhrase,factorStrings,scoreVector,&src); CreateTargetPhrase(targetPhrase,factorStrings,scoreVector,wacands[i],&src); costs.push_back(std::make_pair(-targetPhrase.GetFutureScore(),tCands.size())); tCands.push_back(targetPhrase); } TargetPhraseCollection *rv; rv=PruneTargetCandidates(tCands,costs); if(rv->IsEmpty()) { delete rv; return 0; } else { if(useCache) piter.first->second=rv; m_tgtColls.push_back(rv); return rv; } } void Create(const std::vector &input , const std::vector &output , const std::string &filePath , const std::vector &weight , const LMList &languageModels , float weightWP ) { // set my members m_dict=new PhraseDictionaryTree(weight.size()-m_numInputScores); m_input=input; m_output=output; m_languageModels=&languageModels; m_weightWP=weightWP; m_weights=weight; const StaticData &staticData = StaticData::Instance(); m_dict->UseWordAlignment(staticData.UseAlignmentInfo()); std::string binFname=filePath+".binphr.idx"; if(!FileExists(binFname.c_str())) { UserMessage::Add( "bin ttable does not exist\n"); abort(); //TRACE_ERR( "bin ttable does not exist -> create it\n"); //InputFileStream in(filePath); //m_dict->Create(in,filePath); } TRACE_ERR( "reading bin ttable\n"); // m_dict->Read(filePath); bool res=m_dict->Read(filePath); if (!res) { std::stringstream strme; strme << "bin ttable was read in a wrong way\n"; UserMessage::Add(strme.str()); exit(1); } } typedef PhraseDictionaryTree::PrefixPtr PPtr; typedef unsigned short Position; typedef std::pair Range; struct State { PPtr ptr; Range range; std::vector scores; Phrase src; State() : range(0,0),scores(0),src(ARRAY_SIZE_INCR) {} State(Position b,Position e,const PPtr& v,const std::vector& sv=std::vector(0)) : ptr(v),range(b,e),scores(sv),src(ARRAY_SIZE_INCR) {} State(Range const& r,const PPtr& v,const std::vector& sv=std::vector(0)) : ptr(v),range(r),scores(sv),src(ARRAY_SIZE_INCR) {} Position begin() const { return range.first; } Position end() const { return range.second; } std::vector GetScores() const { return scores; } friend std::ostream& operator<<(std::ostream& out,State const& s) { out<<" R=("<::const_iterator scoreIterator = s.GetScores().begin(); scoreIterator factors=TokenizeMultiCharSeparator(*factorStrings[k],StaticData::Instance().GetFactorDelimiter()); CHECK(factors.size()==m_output.size()); Word& w=targetPhrase.AddWord(); for(size_t l=0; lGetFeature(), scoreVector, m_weights, m_weightWP, *m_languageModels); targetPhrase.SetSourcePhrase(srcPtr); } TargetPhraseCollection* PruneTargetCandidates(std::vector const & tCands, std::vector >& costs) const { // convert into TargetPhraseCollection TargetPhraseCollection *rv=new TargetPhraseCollection; // set limit to tableLimit or actual size, whatever is smaller std::vector >::iterator nth = costs.begin() + ((m_obj->m_tableLimit>0 && // 0 indicates no limit m_obj->m_tableLimit < costs.size()) ? m_obj->m_tableLimit : costs.size()); // find the nth phrase according to future cost std::nth_element(costs.begin(),nth ,costs.end()); // add n top phrases to the return list for(std::vector >::iterator it = costs.begin(); it != nth; ++it) rv->Add(new TargetPhrase(tCands[it->second])); return rv; } // POD for target phrase scores struct TScores { float total; StringTgtCand::second_type trans; Phrase const* src; TScores() : total(0.0),src(0) {} }; void CacheSource(ConfusionNet const& src) { CHECK(m_dict); const size_t srcSize=src.GetSize(); std::vector exploredPaths(srcSize+1,0); std::vector exPathsD(srcSize+1,-1.0); // collect some statistics std::vector cnDepths(srcSize,0); for(size_t i=0; i=0.0 ? addLogScale(pd,exPathsD[len]) : pd); } // update global statistics if(pathCN.size()<=srcSize) pathCN.resize(srcSize+1,-1.0); for(size_t len=1; len<=srcSize; ++len) pathCN[len]=pathCN[len]>=0.0 ? addLogScale(pathCN[len],exPathsD[len]) : exPathsD[len]; if(path1Best.size()<=srcSize) path1Best.resize(srcSize+1,0); for(size_t len=1; len<=srcSize; ++len) path1Best[len]+=srcSize-len+1; if (StaticData::Instance().GetVerboseLevel() >= 2 && exPathsD.size()) { TRACE_ERR("path stats for current CN: \nCN (full): "); std::transform(exPathsD.begin()+1 ,exPathsD.end() ,std::ostream_iterator(std::cerr," ") ,Exp); TRACE_ERR("\n"); } typedef StringTgtCand::first_type sPhrase; typedef std::map E2Costs; std::map cov2cand; std::vector stack; for(Position i=0 ; i < srcSize ; ++i) stack.push_back(State(i, i, m_dict->GetRoot(), std::vector(m_numInputScores,0.0))); while(!stack.empty()) { State curr(stack.back()); stack.pop_back(); CHECK(curr.end()= m_numInputScores); // do not start with epsilon (except at first position) if(isEpsilon && curr.begin()==curr.end() && curr.begin()>0) continue; // At a given node in the prefix tree, look to see if w defines an edge to // another node (Extend). Stay at the same node if w==EPSILON PPtr nextP = (isEpsilon ? curr.ptr : m_dict->Extend(curr.ptr,s)); if(nextP) { // w is a word that should be considered Range newRange(curr.begin(),curr.end()+src.GetColumnIncrement(curr.end(),colidx)); //add together the link scores from the current state and the new arc float inputScoreSum = 0; std::vector newInputScores(m_numInputScores,0.0); if (m_numInputScores) { std::transform(currCol[colidx].second.begin(), currCol[colidx].second.end(), curr.GetScores().begin(), newInputScores.begin(), std::plus()); //we need to sum up link weights (excluding realWordCount, which isn't in numLinkParams) //if the sum is too low, then we won't expand this. //TODO: dodgy! shouldn't we consider weights here? what about zero-weight params? inputScoreSum = std::accumulate(newInputScores.begin(),newInputScores.begin()+m_numInputScores,0.0); } Phrase newSrc(curr.src); if(!isEpsilon) newSrc.AddWord(w); if(newRange.secondLOWEST_SCORE) { // if there is more room to grow, add a new state onto the queue // to be explored that represents [begin, curEnd+) stack.push_back(State(newRange,nextP,newInputScores)); stack.back().src=newSrc; } std::vector tcands; // now, look up the target candidates (aprx. TargetPhraseCollection) for // the current path through the CN m_dict->GetTargetCandidates(nextP,tcands); if(newRange.second>=exploredPaths.size()+newRange.first) exploredPaths.resize(newRange.second-newRange.first+1,0); ++exploredPaths[newRange.second-newRange.first]; totalE+=tcands.size(); if(tcands.size()) { E2Costs& e2costs=cov2cand[newRange]; Phrase const* srcPtr=uniqSrcPhr(newSrc); for(size_t i=0; i nscores(newInputScores); //resize to include phrase table scores nscores.resize(m_numInputScores+tcands[i].second.size(),0.0f); //put in phrase table scores, logging as we insert std::transform(tcands[i].second.begin(),tcands[i].second.end(),nscores.begin() + m_numInputScores,TransformScore); CHECK(nscores.size()==m_weights.size()); //tally up float score=std::inner_product(nscores.begin(), nscores.end(), m_weights.begin(), 0.0f); //count word penalty score-=tcands[i].first.size() * m_weightWP; std::pair p=e2costs.insert(std::make_pair(tcands[i].first,TScores())); if(p.second) ++distinctE; TScores & scores=p.first->second; if(p.second || scores.total= 2 && exploredPaths.size()) { TRACE_ERR("CN (explored): "); std::copy(exploredPaths.begin()+1,exploredPaths.end(), std::ostream_iterator(std::cerr," ")); TRACE_ERR("\n"); } if(pathExplored.size()::const_iterator i=cov2cand.begin(); i!=cov2cand.end(); ++i) { CHECK(i->first.firstfirst.second>0); CHECK(static_cast(i->first.second-1)first.first].size()); CHECK(m_rangeCache[i->first.first][i->first.second-1]==0); std::vector tCands; tCands.reserve(i->second.size()); std::vector > costs; costs.reserve(i->second.size()); for(E2Costs::const_iterator j=i->second.begin(); j!=i->second.end(); ++j) { TScores const & scores=j->second; TargetPhrase targetPhrase(Output); CreateTargetPhrase(targetPhrase,j->first,scores.trans,scores.src); costs.push_back(std::make_pair(-targetPhrase.GetFutureScore(),tCands.size())); tCands.push_back(targetPhrase); //std::cerr << i->first.first << "-" << i->first.second << ": " << targetPhrase << std::endl; } TargetPhraseCollection *rv=PruneTargetCandidates(tCands,costs); if(rv->IsEmpty()) delete rv; else { m_rangeCache[i->first.first][i->first.second-1]=rv; m_tgtColls.push_back(rv); } } // free memory m_dict->FreeMemory(); } size_t GetNumInputScores() const { return m_numInputScores; } }; } #endif