Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TrellisPath.h « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89efb32e4551ecf5a95262fab4d6c315aa332865 (plain)
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
// $Id$

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
***********************************************************************/

#pragma once

#include <iostream>
#include <vector>
#include <limits>
#include "Hypothesis.h"
#include "TypeDef.h"
#include <boost/shared_ptr.hpp>

namespace Moses
{

class TrellisPathCollection;
class TrellisPathList;

/** Encapsulate the set of hypotheses/arcs that goes from decoding 1 phrase to all the source phrases
 *	to reach a final translation. For the best translation, this consist of all hypotheses, for the other
 *	n-best paths, the node on the path can consist of hypotheses or arcs.
 *  Used by phrase-based decoding
 */
class TrellisPath
{
  friend std::ostream& operator<<(std::ostream&, const TrellisPath&);
  friend class Manager;

protected:
  std::vector<const Hypothesis *> m_path; //< list of hypotheses/arcs
  size_t		m_prevEdgeChanged; /**< the last node that was wiggled to create this path
																	, or NOT_FOUND if this path is the best trans so consist of only hypos
															 */

  float m_totalScore;
  mutable boost::shared_ptr<ScoreComponentCollection> m_scoreBreakdown;

  //Used by Manager::LatticeSample()
  explicit TrellisPath(const std::vector<const Hypothesis*> edges);

  void InitTotalScore();

public:
  TrellisPath(); // not implemented

  //! create path OF pure hypo
  TrellisPath(const Hypothesis *hypo);

  /** create path from another path, deviate at edgeIndex by using arc instead,
  	* which may change other hypo back from there
  	*/
  TrellisPath(const TrellisPath &copy, size_t edgeIndex, const Hypothesis *arc);

  //! get score for this path throught trellis
  inline float GetTotalScore() const {
    return m_totalScore;
  }

  /** list of each hypo/arcs in path. For anything other than the best hypo, it is not possible just to follow the
  	* m_prevHypo variable in the hypothesis object
  	*/
  inline const std::vector<const Hypothesis *> &GetEdges() const {
    return m_path;
  }

  inline size_t GetSize() const {
    return m_path.size();
  }

  //! create a set of next best paths by wiggling 1 of the node at a time.
  void CreateDeviantPaths(TrellisPathCollection &pathColl) const;

  //! create a list of next best paths by wiggling 1 of the node at a time.
  void CreateDeviantPaths(TrellisPathList &pathColl) const;

  const boost::shared_ptr<ScoreComponentCollection> GetScoreBreakdown() const;

  //! get target words range of the hypo within n-best trellis. not necessarily the same as hypo.GetCurrTargetWordsRange()
  WordsRange GetTargetWordsRange(const Hypothesis &hypo) const;

  Phrase GetTargetPhrase() const;
  Phrase GetSurfacePhrase() const;

  TO_STRING();

};

// friend
inline std::ostream& operator<<(std::ostream& out, const TrellisPath& path)
{
  const size_t sizePath = path.m_path.size();
  for (int pos = (int) sizePath - 1 ; pos >= 0 ; pos--) {
    const Hypothesis *edge = path.m_path[pos];
    const WordsRange &sourceRange = edge->GetCurrSourceWordsRange();
    out << edge->GetId() << " " << sourceRange.GetStartPos() << "-" << sourceRange.GetEndPos() << ", ";
  }
  // scores
  out << " total=" << path.GetTotalScore()
      << " " << path.GetScoreBreakdown()
      << std::endl;

  return out;
}

}