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

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

#include <map>
#include <vector>

#include <boost/unordered_map.hpp>

#include "moses/Syntax/RuleTable.h"
#include "moses/TargetPhraseCollection.h"

#include "HyperPath.h"

namespace Moses
{
namespace Syntax
{
namespace F2S
{

// A HyperTree for representing a tree-to-string rule table.  See this paper:
//
//  Hui Zhang, Min Zhang, Haizhou Li, and Chew Lim Tan
//  "Fast Translation Rule Matching for Syntax-based Statistical Machine
//   Translation"
//  In proceedings of EMNLP 2009
//
class HyperTree : public RuleTable
{
public:
  class Node
  {
  public:
    typedef boost::unordered_map<HyperPath::NodeSeq, Node> Map;

    bool IsLeaf() const {
      return m_map.empty();
    }

    bool HasRules() const {
      return !m_targetPhraseCollection->IsEmpty();
    }

    void Prune(std::size_t tableLimit);
    void Sort(std::size_t tableLimit);

    Node *GetOrCreateChild(const HyperPath::NodeSeq &);

    const Node *GetChild(const HyperPath::NodeSeq &) const;

    TargetPhraseCollection::shared_ptr 
    GetTargetPhraseCollection() const {
      return m_targetPhraseCollection;
    }

    TargetPhraseCollection::shared_ptr 
    GetTargetPhraseCollection() {
      return m_targetPhraseCollection;
    }

    const Map &GetMap() const {
      return m_map;
    }

    Node() : m_targetPhraseCollection(new TargetPhraseCollection) { }

  private:
    Map m_map;
    TargetPhraseCollection::shared_ptr m_targetPhraseCollection;
  };

  HyperTree(const RuleTableFF *ff) : RuleTable(ff) { }

  const Node &GetRootNode() const {
    return m_root;
  }

private:
  friend class HyperTreeCreator;

  TargetPhraseCollection::shared_ptr 
  GetOrCreateTargetPhraseCollection(const HyperPath &);

  Node &GetOrCreateNode(const HyperPath &);

  void SortAndPrune(std::size_t);

  Node m_root;
};

}  // namespace F2S
}  // namespace Syntax
}  // namespace Moses