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

HypothesisStackCubePruning.h « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c9c9057b2c54099d8a55f50497e1fd05dd61460a (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
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
// $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
***********************************************************************/

#ifndef moses_HypothesisStackCubePruning_h
#define moses_HypothesisStackCubePruning_h

#include <limits>
#include <set>
#include <boost/unordered_map.hpp>
#include "Hypothesis.h"
#include "BitmapContainer.h"
#include "HypothesisStack.h"
#include "Util.h"

namespace Moses
{

class BitmapContainer;
class TranslationOptionList;
class Manager;

//typedef boost::unordered_map<Bitmap, BitmapContainer*, UnorderedComparer<Bitmap>, UnorderedComparer<Bitmap> > _BMType;
typedef boost::unordered_map<const Bitmap*, BitmapContainer*> _BMType;
// can compare Bitmap* 'cos all bitmaps are created from bitmaps factory class. MUST ensure this is the case

/** A stack for phrase-based decoding with cube-pruning. */
class HypothesisStackCubePruning : public HypothesisStack
{
public:
  friend std::ostream& operator<<(std::ostream&, const HypothesisStackCubePruning&);

protected:
  _BMType m_bitmapAccessor;

  float m_bestScore; /**< score of the best hypothesis in collection */
  float m_worstScore; /**< score of the worse hypthesis in collection */
  float m_beamWidth; /**< minimum score due to threashold pruning */
  size_t m_maxHypoStackSize; /**< maximum number of hypothesis allowed in this stack */
  bool m_nBestIsEnabled; /**< flag to determine whether to keep track of old arcs */
  bool m_deterministic; /**< flag to determine whether to sort hypotheses deterministically */

  /** add hypothesis to stack. Prune if necessary.
   * Returns false if equiv hypo exists in collection, otherwise returns true
   */
  std::pair<HypothesisStackCubePruning::iterator, bool> Add(Hypothesis *hypothesis);

  /** destroy all instances of Hypothesis in this collection */
  void RemoveAll();

  BitmapContainer *AddBitmapContainer(const Bitmap &bitmap, HypothesisStackCubePruning &stack);

public:
  HypothesisStackCubePruning(Manager& manager);
  ~HypothesisStackCubePruning() {
    RemoveAll();
    m_bitmapAccessor.clear();
  }

  /** adds the hypo, but only if within thresholds (beamThr, stackSize).
  *	This function will recombine hypotheses silently!  There is no record
  * (could affect n-best list generation...TODO)
  * Call stack for adding hypothesis is
  		AddPrune()
  			Add()
  				AddNoPrune()
  */
  bool AddPrune(Hypothesis *hypothesis);

  void AddInitial(Hypothesis *hypo);

  /** set maximum number of hypotheses in the collection
   * \param maxHypoStackSize maximum number (typical number: 100)
   */
  inline void SetMaxHypoStackSize(size_t maxHypoStackSize) {
    m_maxHypoStackSize = maxHypoStackSize;
  }

  inline size_t GetMaxHypoStackSize() const {
    return m_maxHypoStackSize;
  }

  /** set beam threshold, hypotheses in the stack must not be worse than
    * this factor times the best score to be allowed in the stack
   * \param beamThreshold minimum factor (typical number: 0.03)
   */
  inline void SetBeamWidth(float beamWidth) {
    m_beamWidth = beamWidth;
  }

  /** return score of the best hypothesis in the stack */
  inline float GetBestScore() const {
    return m_bestScore;
  }

  /** return worst score allowed for the stack */
  inline float GetWorstScore() const {
    return m_worstScore;
  }

  void AddHypothesesToBitmapContainers();

  const _BMType& GetBitmapAccessor() const {
    return m_bitmapAccessor;
  }

  void SetBitmapAccessor(const Bitmap &newBitmap
                         , HypothesisStackCubePruning &stack
                         , const Range &range
                         , BitmapContainer &bitmapContainer
                         , const SquareMatrix &estimatedScores
                         , const TranslationOptionList &transOptList);

  /** pruning, if too large.
   * Pruning algorithm: find a threshold and delete all hypothesis below it.
   * The threshold is chosen so that exactly newSize top items remain on the
   * stack in fact, in situations where some of the hypothesis fell below
   * m_beamWidth, the stack will contain less items.
   * \param newSize maximum size */
  void PruneToSize(size_t newSize);

  //! return the hypothesis with best score. Used to get the translated at end of decoding
  const Hypothesis *GetBestHypothesis() const;
  //! return all hypothesis, sorted by descending score. Used in creation of N best list
  std::vector<const Hypothesis*> GetSortedList() const;

  /** make all arcs in point to the equiv hypothesis that contains them.
  * Ie update doubly linked list be hypo & arcs
  */
  void CleanupArcList();

  TO_STRING();
};

}
#endif