// $Id$ // vim:tabstop=2 #include "PhraseDictionaryTree.h" #include #include "util/check.hh" #include #include #include #include #include namespace Moses { template std::ostream& operator<<(std::ostream& out,const std::vector& x) { out<::const_iterator iend=x.end(); for(typename std::vector::const_iterator i=x.begin(); i!=iend; ++i) out<<*i<<' '; return out; } class TgtCand { IPhrase e; Scores sc; std::string m_alignment; public: TgtCand() {} TgtCand(const IPhrase& a, const Scores& b , const std::string& alignment) : e(a) , sc(b) , m_alignment(alignment) {} TgtCand(const IPhrase& a,const Scores& b) : e(a),sc(b) {} TgtCand(FILE* f) { readBin(f); } void writeBin(FILE* f) const { fWriteVector(f,e); fWriteVector(f,sc); } void readBin(FILE* f) { fReadVector(f,e); fReadVector(f,sc); } void writeBinWithAlignment(FILE* f) const { fWriteVector(f,e); fWriteVector(f,sc); fWriteString(f, m_alignment.c_str(), m_alignment.size()); } void readBinWithAlignment(FILE* f) { fReadVector(f,e); fReadVector(f,sc); fReadString(f, m_alignment); } const IPhrase& GetPhrase() const { return e; } const Scores& GetScores() const { return sc; } const std::string& GetAlignment() const { return m_alignment; } }; class TgtCands : public std::vector { typedef std::vector MyBase; public: TgtCands() : MyBase() {} void writeBin(FILE* f) const { unsigned s=size(); fWrite(f,s); for(size_t i=0; iisValid(); } typedef LVoc WordVoc; static WordVoc* ReadVoc(const std::string& filename) { static std::map vocs; #ifdef WITH_THREADS boost::mutex mutex; boost::mutex::scoped_lock lock(mutex); #endif std::map::iterator vi = vocs.find(filename); if (vi == vocs.end()) { WordVoc* voc = new WordVoc(); voc->Read(filename); vocs[filename] = voc; } return vocs[filename]; } class PDTimp { public: typedef PrefixTreeF PTF; typedef FilePtr CPT; typedef std::vector Data; Data data; std::vector srcOffsets; FILE *os,*ot; WordVoc* sv; WordVoc* tv; ObjectPool pPool; // a comparison with the Boost MemPools might be useful bool usewordalign; bool printwordalign; PDTimp() : os(0),ot(0), usewordalign(false), printwordalign(false) { PTF::setDefault(InvalidOffT); } ~PDTimp() { if(os) fClose(os); if(ot) fClose(ot); FreeMemory(); } inline void UseWordAlignment(bool a) { usewordalign=a; } inline bool UseWordAlignment() { return usewordalign; }; inline void PrintWordAlignment(bool a) { printwordalign=a; }; inline bool PrintWordAlignment() { return printwordalign; }; void FreeMemory() { for(Data::iterator i=data.begin(); i!=data.end(); ++i) (*i).free(); pPool.reset(); } int Read(const std::string& fn); void GetTargetCandidates(const IPhrase& f,TgtCands& tgtCands) { if(f.empty()) return; if(f[0]>=data.size()) return; if(!data[f[0]]) return; CHECK(data[f[0]]->findKey(f[0])size()); OFF_T tCandOffset=data[f[0]]->find(f); if(tCandOffset==InvalidOffT) return; fSeek(ot,tCandOffset); if (UseWordAlignment()) tgtCands.readBinWithAlignment(ot); else tgtCands.readBin(ot); } typedef PhraseDictionaryTree::PrefixPtr PPtr; void GetTargetCandidates(PPtr p,TgtCands& tgtCands) { CHECK(p); if(p.imp->isRoot()) return; OFF_T tCandOffset=p.imp->ptr()->getData(p.imp->idx); if(tCandOffset==InvalidOffT) return; fSeek(ot,tCandOffset); if (UseWordAlignment()) tgtCands.readBinWithAlignment(ot); else tgtCands.readBin(ot); } void PrintTgtCand(const TgtCands& tcands,std::ostream& out) const; // convert target candidates from internal data structure to the external one void ConvertTgtCand(const TgtCands& tcands,std::vector& rv) const { for(TgtCands::const_iterator i=tcands.begin(); i!=tcands.end(); ++i) { const IPhrase& iphrase=i->GetPhrase(); std::vector vs; vs.reserve(iphrase.size()); for(size_t j=0; jsymbol(iphrase[j])); rv.push_back(StringTgtCand(vs,i->GetScores())); } } // convert target candidates from internal data structure to the external one void ConvertTgtCand(const TgtCands& tcands,std::vector& rv, std::vector& wa) const { for(TgtCands::const_iterator i=tcands.begin(); i!=tcands.end(); ++i) { const IPhrase& iphrase=i->GetPhrase(); std::vector vs; vs.reserve(iphrase.size()); for(size_t j=0; jsymbol(iphrase[j])); rv.push_back(StringTgtCand(vs,i->GetScores())); wa.push_back(i->GetAlignment()); } } PPtr GetRoot() { return PPtr(pPool.get(PPimp(0,0,1))); } PPtr Extend(PPtr p,const std::string& w) { CHECK(p); if(w.empty() || w==EPSILON) return p; LabelId wi=sv->index(w); if(wi==InvalidLabelId) return PPtr(); // unknown word else if(p.imp->isRoot()) { if(wifindKeyPtr(wi); CHECK(ptr); return PPtr(pPool.get(PPimp(data[wi],data[wi]->findKey(wi),0))); } } else if(PTF const* nextP=p.imp->ptr()->getPtr(p.imp->idx)) { return PPtr(pPool.get(PPimp(nextP,nextP->findKey(wi),0))); } return PPtr(); } }; //////////////////////////////////////////////////////////// // // member functions of PDTimp // //////////////////////////////////////////////////////////// int PDTimp::Read(const std::string& fn) { std::string ifs, ift, ifi, ifsv, iftv; if (UseWordAlignment()) { //asking for word-to-word alignment if (!FileExists(fn+".binphr.srctree.wa") || !FileExists(fn+".binphr.tgtdata.wa")) { // ERROR std::stringstream strme; strme << "You are asking for word alignment but the binary phrase table does not contain any alignment info. Please check if you had generated the correct phrase table with word alignment (.wa)\n"; UserMessage::Add(strme.str()); return false; } ifs=fn+".binphr.srctree.wa"; ift=fn+".binphr.tgtdata.wa"; ifi=fn+".binphr.idx"; ifsv=fn+".binphr.srcvoc"; iftv=fn+".binphr.tgtvoc"; } else { if (!FileExists(fn+".binphr.srctree") || !FileExists(fn+".binphr.tgtdata")) { // ERROR std::stringstream strme; strme << "You are asking binary phrase table without word alignments but the file do not exist. Please check if you had generated the correct phrase table without word alignment (" << (fn+".binphr.srctree") << "," << (fn+".binphr.tgtdata")<< ")\n"; UserMessage::Add(strme.str()); return false; } ifs=fn+".binphr.srctree"; ift=fn+".binphr.tgtdata"; ifi=fn+".binphr.idx"; ifsv=fn+".binphr.srcvoc"; iftv=fn+".binphr.tgtvoc"; } FILE *ii=fOpen(ifi.c_str(),"rb"); fReadVector(ii,srcOffsets); fClose(ii); os=fOpen(ifs.c_str(),"rb"); ot=fOpen(ift.c_str(),"rb"); data.resize(srcOffsets.size()); for(size_t i=0; isymbol(iphr[j])<<" "; out<< " -- " << trgAlign; out << std::endl; } } //////////////////////////////////////////////////////////// // // member functions of PhraseDictionaryTree // //////////////////////////////////////////////////////////// PhraseDictionaryTree::PhraseDictionaryTree(size_t numScoreComponent) : Dictionary(numScoreComponent),imp(new PDTimp) { if(sizeof(OFF_T)!=8) { TRACE_ERR("ERROR: size of type 'OFF_T' has to be 64 bit!\n" "In gcc, use compiler settings '-D_FILE_OFFSET_BITS=64 -D_LARGE_FILES'\n" " -> abort \n\n"); abort(); } } PhraseDictionaryTree::~PhraseDictionaryTree() { delete imp; } void PhraseDictionaryTree::UseWordAlignment(bool a) { imp->UseWordAlignment(a); }; bool PhraseDictionaryTree::UseWordAlignment() { return imp->UseWordAlignment(); }; void PhraseDictionaryTree::PrintWordAlignment(bool a) { imp->PrintWordAlignment(a); }; bool PhraseDictionaryTree::PrintWordAlignment() { return imp->PrintWordAlignment(); }; void PhraseDictionaryTree::FreeMemory() const { imp->FreeMemory(); } void PhraseDictionaryTree:: GetTargetCandidates(const std::vector& src, std::vector& rv) const { IPhrase f(src.size()); for(size_t i=0; isv->index(src[i]); if(f[i]==InvalidLabelId) return; } TgtCands tgtCands; imp->GetTargetCandidates(f,tgtCands); imp->ConvertTgtCand(tgtCands,rv); } void PhraseDictionaryTree:: GetTargetCandidates(const std::vector& src, std::vector& rv, std::vector& wa) const { IPhrase f(src.size()); for(size_t i=0; isv->index(src[i]); if(f[i]==InvalidLabelId) return; } TgtCands tgtCands; imp->GetTargetCandidates(f,tgtCands); imp->ConvertTgtCand(tgtCands,rv,wa); } void PhraseDictionaryTree:: PrintTargetCandidates(const std::vector& src, std::ostream& out) const { IPhrase f(src.size()); for(size_t i=0; isv->index(src[i]); if(f[i]==InvalidLabelId) { TRACE_ERR("the source phrase '"<GetTargetCandidates(f,tcand); imp->PrintTgtCand(tcand,out); } int PhraseDictionaryTree::Create(std::istream& inFile,const std::string& out) { std::string line; size_t count = 0; std::string ofn(out+".binphr.srctree"), oft(out+".binphr.tgtdata"), ofi(out+".binphr.idx"), ofsv(out+".binphr.srcvoc"), oftv(out+".binphr.tgtvoc"); if (PrintWordAlignment()) { ofn+=".wa"; oft+=".wa"; } FILE *os=fOpen(ofn.c_str(),"wb"), *ot=fOpen(oft.c_str(),"wb"); typedef PrefixTreeSA PSA; PSA *psa=new PSA; PSA::setDefault(InvalidOffT); LabelId currFirstWord=InvalidLabelId; IPhrase currF; TgtCands tgtCands; std::vector vo; size_t lnc=0; size_t numElement = NOT_FOUND; // 3=old format, 5=async format which include word alignment info imp->sv = new WordVoc(); imp->tv = new WordVoc(); while(getline(inFile, line)) { ++lnc; std::vector tokens = TokenizeMultiCharSeparator( line , "|||" ); if (numElement == NOT_FOUND) { // init numElement numElement = tokens.size(); CHECK(numElement >= 3); } if (tokens.size() != numElement) { std::stringstream strme; strme << "Syntax error at line " << lnc << " : " << line; UserMessage::Add(strme.str()); abort(); } const std::string &sourcePhraseString =tokens[0] ,&targetPhraseString=tokens[1] ,&scoreString = tokens[2]; const std::string empty; const std::string &alignmentString = PrintWordAlignment() ? tokens[3] : empty; IPhrase f,e; Scores sc; std::vector wordVec = Tokenize(sourcePhraseString); for (size_t i = 0 ; i < wordVec.size() ; ++i) f.push_back(imp->sv->add(wordVec[i])); wordVec = Tokenize(targetPhraseString); for (size_t i = 0 ; i < wordVec.size() ; ++i) e.push_back(imp->tv->add(wordVec[i])); // while(is>>w && w!="|||") sc.push_back(atof(w.c_str())); // Mauro: to handle 0 probs in phrase tables std::vector scoreVector = Tokenize(scoreString); for (size_t i = 0 ; i < scoreVector.size() ; ++i) { float tmp = scoreVector[i]; sc.push_back(((tmp>0.0)?tmp:(float)1.0e-38)); } if(f.empty()) { TRACE_ERR("WARNING: empty source phrase in line '"<insert(f); if(d==InvalidOffT) d=fTell(ot); else { TRACE_ERR("ERROR: source phrase already inserted (A)!\nline(" << lnc << "): '" <=vo.size()) vo.resize(currFirstWord+1,InvalidOffT); vo[currFirstWord]=fTell(os); pf.create(*psa,os); // clear delete psa; psa=new PSA; currFirstWord=f[0]; } // insert src phrase in prefix tree CHECK(psa); PSA::Data& d=psa->insert(f); if(d==InvalidOffT) d=fTell(ot); else { TRACE_ERR("ERROR: xsource phrase already inserted (B)!\nline(" << lnc << "): '" <=vo.size()) vo.resize(currFirstWord+1,InvalidOffT); vo[currFirstWord]=fTell(os); pf.create(*psa,os); delete psa; psa=0; TRACE_ERR("distinct source phrases: "< inv; for(size_t i=0; isv->Write(ofsv); imp->tv->Write(oftv); return 1; } int PhraseDictionaryTree::Read(const std::string& fn) { TRACE_ERR("size of OFF_T "<Read(fn); } PhraseDictionaryTree::PrefixPtr PhraseDictionaryTree::GetRoot() const { return imp->GetRoot(); } PhraseDictionaryTree::PrefixPtr PhraseDictionaryTree::Extend(PrefixPtr p, const std::string& w) const { return imp->Extend(p,w); } void PhraseDictionaryTree::PrintTargetCandidates(PrefixPtr p,std::ostream& out) const { TgtCands tcand; imp->GetTargetCandidates(p,tcand); out<<"there are "<PrintTgtCand(tcand,out); } void PhraseDictionaryTree:: GetTargetCandidates(PrefixPtr p, std::vector& rv) const { TgtCands tcands; imp->GetTargetCandidates(p,tcands); imp->ConvertTgtCand(tcands,rv); } void PhraseDictionaryTree:: GetTargetCandidates(PrefixPtr p, std::vector& rv, std::vector& wa) const { TgtCands tcands; imp->GetTargetCandidates(p,tcands); imp->ConvertTgtCand(tcands,rv,wa); } std::string PhraseDictionaryTree::GetScoreProducerDescription(unsigned) const { return "PhraseDictionaryTree"; } }