File size: 2,390 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * PhraseLR.cpp
 *
 *  Created on: 22 Mar 2016
 *      Author: hieu
 */

#include "PhraseBasedReorderingState.h"
#include "LexicalReordering.h"
#include "../../PhraseBased/Hypothesis.h"
#include "../../InputPathBase.h"
#include "../../PhraseBased/Manager.h"

using namespace std;

namespace Moses2
{

PhraseBasedReorderingState::PhraseBasedReorderingState(const LRModel &config,
    LRModel::Direction dir, size_t offset) :
  LRState(config, dir, offset)
{
  // uninitialised
  prevPath = NULL;
  prevTP = NULL;
}

void PhraseBasedReorderingState::Init(const LRState *prev,
                                      const TargetPhrase<Moses2::Word> &topt, const InputPathBase &path, bool first,
                                      const Bitmap *coverage)
{
  prevTP = &topt;
  prevPath = &path;
  m_first = first;
}

size_t PhraseBasedReorderingState::hash() const
{
  size_t ret;
  ret = (size_t) &prevPath->range;
  boost::hash_combine(ret, m_direction);

  return ret;
}

bool PhraseBasedReorderingState::operator==(const FFState& o) const
{
  if (&o == this) return true;

  const PhraseBasedReorderingState &other =
    static_cast<const PhraseBasedReorderingState&>(o);
  if (&prevPath->range == &other.prevPath->range) {
    if (m_direction == LRModel::Forward) {
      int compareScore = ComparePrevScores(other.prevTP);
      return compareScore == 0;
    } else {
      return true;
    }
  } else {
    return false;
  }
}

void PhraseBasedReorderingState::Expand(const ManagerBase &mgr,
                                        const LexicalReordering &ff, const Hypothesis &hypo, size_t phraseTableInd,
                                        Scores &scores, FFState &state) const
{
  if ((m_direction != LRModel::Forward) || !m_first) {
    LRModel const& lrmodel = m_configuration;
    Range const &cur = hypo.GetInputPath().range;
    LRModel::ReorderingType reoType = (
                                        m_first ?
                                        lrmodel.GetOrientation(cur) :
                                        lrmodel.GetOrientation(prevPath->range, cur));
    CopyScores(mgr.system, scores, hypo.GetTargetPhrase(), reoType);
  }

  PhraseBasedReorderingState &stateCast =
    static_cast<PhraseBasedReorderingState&>(state);
  stateCast.Init(this, hypo.GetTargetPhrase(), hypo.GetInputPath(), false,
                 NULL);
}

} /* namespace Moses2 */