/* * LatticeMBR.h * moses-cmd * * Created by Abhishek Arun on 26/01/2010. * Copyright 2010 __MyCompanyName__. All rights reserved. * */ #ifndef moses_cmd_LatticeMBR_h #define moses_cmd_LatticeMBR_h #include #include #include #include "moses/Hypothesis.h" #include "moses/Manager.h" #include "moses/TrellisPathList.h" namespace Moses { class Edge; typedef std::vector< const Moses::Hypothesis *> Lattice; typedef std::vector Path; typedef std::map PathCounts; typedef std::map NgramHistory; class Edge { const Moses::Hypothesis* m_tailNode; const Moses::Hypothesis* m_headNode; float m_score; Moses::TargetPhrase m_targetPhrase; NgramHistory m_ngrams; public: Edge(const Moses::Hypothesis* from, const Moses::Hypothesis* to, float score, const Moses::TargetPhrase& targetPhrase) : m_tailNode(from), m_headNode(to), m_score(score), m_targetPhrase(targetPhrase) { //cout << "Creating new edge from Node " << from->GetId() << ", to Node : " << to->GetId() << ", score: " << score << " phrase: " << targetPhrase << endl; } const Moses::Hypothesis* GetHeadNode() const { return m_headNode; } const Moses::Hypothesis* GetTailNode() const { return m_tailNode; } float GetScore() const { return m_score; } size_t GetWordsSize() const { return m_targetPhrase.GetSize(); } const Moses::Phrase& GetWords() const { return m_targetPhrase; } friend std::ostream& operator<< (std::ostream& out, const Edge& edge); const NgramHistory& GetNgrams( std::map > & incomingEdges) ; bool operator < (const Edge & compare) const; void GetPhraseSuffix(const Moses::Phrase& origPhrase, size_t lastN, Moses::Phrase& targetPhrase) const; void storeNgramHistory(const Moses::Phrase& phrase, Path & path, size_t count = 1) { m_ngrams[phrase][path]+= count; } }; /** * Data structure to hold the ngram scores as we traverse the lattice. Maps (hypo,ngram) to score */ class NgramScores { public: NgramScores() {} /** logsum this score to the existing score */ void addScore(const Moses::Hypothesis* node, const Moses::Phrase& ngram, float score); /** Iterate through ngrams for selected node */ typedef std::map::const_iterator NodeScoreIterator; NodeScoreIterator nodeBegin(const Moses::Hypothesis* node); NodeScoreIterator nodeEnd(const Moses::Hypothesis* node); private: std::set m_ngrams; std::map > m_scores; }; /** Holds a lattice mbr solution, and its scores */ class LatticeMBRSolution { public: /** Read the words from the path */ LatticeMBRSolution(const Moses::TrellisPath& path, bool isMap); const std::vector& GetNgramScores() const { return m_ngramScores; } const std::vector& GetWords() const { return m_words; } float GetMapScore() const { return m_mapScore; } float GetScore() const { return m_score; } /** Initialise ngram scores */ void CalcScore(std::map& finalNgramScores, const std::vector& thetas, float mapWeight); private: std::vector m_words; float m_mapScore; std::vector m_ngramScores; float m_score; }; struct LatticeMBRSolutionComparator { bool operator()(const LatticeMBRSolution& a, const LatticeMBRSolution& b) { return a.GetScore() > b.GetScore(); } }; void pruneLatticeFB(Lattice & connectedHyp, std::map < const Moses::Hypothesis*, std::set > & outgoingHyps, std::map >& incomingEdges, const std::vector< float> & estimatedScores, const Moses::Hypothesis*, size_t edgeDensity,float scale); //Use the ngram scores to rerank the nbest list, return at most n solutions void getLatticeMBRNBest(const Moses::Manager& manager, const Moses::TrellisPathList& nBestList, std::vector& solutions, size_t n); //calculate expectated ngram counts, clipping at 1 (ie calculating posteriors) if posteriors==true. void calcNgramExpectations(Lattice & connectedHyp, std::map >& incomingEdges, std::map& finalNgramScores, bool posteriors); void GetOutputFactors(const Moses::TrellisPath &path, std::vector &translation); void extract_ngrams(const std::vector& sentence, std::map < Moses::Phrase, int > & allngrams); bool ascendingCoverageCmp(const Moses::Hypothesis* a, const Moses::Hypothesis* b); std::vector doLatticeMBR(const Moses::Manager& manager, const Moses::TrellisPathList& nBestList); const Moses::TrellisPath doConsensusDecoding(const Moses::Manager& manager, const Moses::TrellisPathList& nBestList); //std::vector doConsensusDecoding(Moses::Manager& manager, Moses::TrellisPathList& nBestList); } #endif