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

TailLatticeSearcher.h « Scope3Parser « Parsers « S2T « Syntax « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 407f04d5b4c7e47a2329464e7fb6698c3cd70d44 (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
#pragma once

#include <algorithm>
#include <vector>

#include "moses/Syntax/PHyperedge.h"

#include "TailLattice.h"
#include "moses/TargetPhraseCollection.h"
namespace Moses
{
namespace Syntax
{
namespace S2T
{

template<typename Callback>
class TailLatticeSearcher
{
public:
  TailLatticeSearcher(const TailLattice &lattice,
                      const PatternApplicationKey &key,
                      const std::vector<SymbolRange> &ranges)
    : m_lattice(lattice)
    , m_key(key)
    , m_ranges(ranges) {}

  void Search(const std::vector<int> &labels, 
	      const TargetPhraseCollection::shared_ptr tpc,
              Callback &callback) {
    m_labels = &labels;
    m_matchCB = &callback;
    m_hyperedge.head = 0;
    m_hyperedge.tail.clear();
    m_hyperedge.label.translations = tpc;
    SearchInner(0, 0, 0);
  }

private:
  void SearchInner(int offset, std::size_t i, std::size_t nonTermIndex) {
    assert(m_hyperedge.tail.size() == i);

    const PatternApplicationTrie *patNode = m_key[i];
    const SymbolRange &range = m_ranges[i];

    if (patNode->IsTerminalNode()) {
      const int width = range.minEnd - range.minStart + 1;
      const PVertex *v = m_lattice[offset][0][width][0];
      // FIXME Sort out const-ness
      m_hyperedge.tail.push_back(const_cast<PVertex*>(v));
      if (i == m_key.size()-1) {
        (*m_matchCB)(m_hyperedge);
      } else {
        SearchInner(offset+width, i+1, nonTermIndex);
      }
      m_hyperedge.tail.pop_back();
      return;
    }

    const int absStart = m_ranges[0].minStart + offset;
    const int minWidth = std::max(1, range.minEnd - absStart + 1);
    const std::size_t maxWidth = range.maxEnd - absStart + 1;

    const std::vector<std::vector<const PVertex *> > &innerVec =
      m_lattice[offset][nonTermIndex+1];

    std::size_t labelIndex = (*m_labels)[nonTermIndex];

    // Loop over all possible widths for this offset and index.
    for (std::size_t width = minWidth; width <= maxWidth; ++width) {
      const PVertex *v = innerVec[width][labelIndex];
      if (!v) {
        continue;
      }
      // FIXME Sort out const-ness
      m_hyperedge.tail.push_back(const_cast<PVertex*>(v));
      if (i == m_key.size()-1) {
        (*m_matchCB)(m_hyperedge);
      } else {
        SearchInner(offset+width, i+1, nonTermIndex+1);
      }
      m_hyperedge.tail.pop_back();
    }
  }

  const TailLattice &m_lattice;
  const PatternApplicationKey &m_key;
  const std::vector<SymbolRange> &m_ranges;
  const std::vector<int> *m_labels;
  Callback *m_matchCB;
  PHyperedge m_hyperedge;
};

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