File size: 2,242 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
86
87
88
89
90
91
92
#pragma once

#include "moses/Syntax/BoundedPriorityContainer.h"
#include "moses/Syntax/PHyperedge.h"
#include "moses/Syntax/PVertex.h"
#include "moses/Syntax/SHyperedgeBundle.h"
#include "moses/Syntax/SHyperedgeBundleScorer.h"

#include "PHyperedgeToSHyperedgeBundle.h"

namespace Moses
{
namespace Syntax
{
namespace S2T
{

class StandardParserCallback
{
private:
  typedef BoundedPriorityContainer<SHyperedgeBundle> Container;

public:
  StandardParserCallback(const SChart &schart, std::size_t ruleLimit)
    : m_schart(schart)
    , m_container(ruleLimit) {}

  void operator()(const PHyperedge &hyperedge) {
    PHyperedgeToSHyperedgeBundle(hyperedge, m_schart, m_tmpBundle);
    float score = SHyperedgeBundleScorer::Score(m_tmpBundle);
    m_container.SwapIn(m_tmpBundle, score);
  }

  void InitForRange(const Range &range) {
    m_container.LazyClear();
  }

  const Container &GetContainer() {
    return m_container;
  }

private:
  const SChart &m_schart;
  SHyperedgeBundle m_tmpBundle;
  BoundedPriorityContainer<SHyperedgeBundle> m_container;
};

class EagerParserCallback
{
private:
  typedef BoundedPriorityContainer<SHyperedgeBundle> Container;

public:
  EagerParserCallback(const SChart &schart, std::size_t ruleLimit)
    : m_schart(schart)
    , m_containers(schart.GetWidth(), Container(ruleLimit))
    , m_prevStart(std::numeric_limits<std::size_t>::max()) {}

  void operator()(const PHyperedge &hyperedge, std::size_t end) {
    PHyperedgeToSHyperedgeBundle(hyperedge, m_schart, m_tmpBundle);
    float score = SHyperedgeBundleScorer::Score(m_tmpBundle);
    m_containers[end].SwapIn(m_tmpBundle, score);
  }

  void InitForRange(const Range &range) {
    const std::size_t start = range.GetStartPos();
    m_end = range.GetEndPos();
    if (start != m_prevStart) {
      for (std::vector<Container>::iterator p = m_containers.begin();
           p != m_containers.end(); ++p) {
        p->LazyClear();
      }
      m_prevStart = start;
    }
  }

  const Container &GetContainer() {
    return m_containers[m_end];
  }

private:
  const SChart &m_schart;
  SHyperedgeBundle m_tmpBundle;
  std::vector<Container> m_containers;
  std::size_t m_end;
  std::size_t m_prevStart;
};

}  // S2T
}  // Syntax
}  // Moses