File size: 2,715 Bytes
fd49381 |
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 |
#pragma once
#include <map>
#include <vector>
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <boost/version.hpp>
#include "moses/Syntax/SymbolEqualityPred.h"
#include "moses/Syntax/SymbolHasher.h"
#include "moses/TargetPhrase.h"
#include "moses/TargetPhraseCollection.h"
#include "moses/Util.h"
#include "moses/Word.h"
#include "RuleTrie.h"
namespace Moses
{
namespace Syntax
{
namespace S2T
{
class RuleTrieScope3 : public RuleTrie
{
public:
class Node
{
public:
typedef std::vector<std::vector<Word> > LabelTable;
typedef boost::unordered_map<Word, Node, SymbolHasher,
SymbolEqualityPred> TerminalMap;
typedef boost::unordered_map<std::vector<int>,
TargetPhraseCollection::shared_ptr> LabelMap;
~Node() {
delete m_gapNode;
}
const LabelTable &GetLabelTable() const {
return m_labelTable;
}
const LabelMap &GetLabelMap() const {
return m_labelMap;
}
const TerminalMap &GetTerminalMap() const {
return m_terminalMap;
}
const Node *GetNonTerminalChild() const {
return m_gapNode;
}
Node *GetOrCreateTerminalChild(const Word &sourceTerm);
Node *GetOrCreateNonTerminalChild(const Word &targetNonTerm);
TargetPhraseCollection::shared_ptr
GetOrCreateTargetPhraseCollection(const TargetPhrase &);
bool IsLeaf() const {
return m_terminalMap.empty() && m_gapNode == NULL;
}
bool HasRules() const {
return !m_labelMap.empty();
}
void Prune(std::size_t tableLimit);
void Sort(std::size_t tableLimit);
private:
friend class RuleTrieScope3;
Node() : m_gapNode(NULL) {}
int InsertLabel(int i, const Word &w) {
std::vector<Word> &inner = m_labelTable[i];
for (std::size_t j = 0; j < inner.size(); ++j) {
if (inner[j] == w) {
return j;
}
}
inner.push_back(w);
return inner.size()-1;
}
LabelTable m_labelTable;
LabelMap m_labelMap;
TerminalMap m_terminalMap;
Node *m_gapNode;
};
RuleTrieScope3(const RuleTableFF *ff) : RuleTrie(ff) {}
const Node &GetRootNode() const {
return m_root;
}
bool HasPreterminalRule(const Word &) const;
private:
TargetPhraseCollection::shared_ptr
GetOrCreateTargetPhraseCollection(const Phrase &source,
const TargetPhrase &target,
const Word *sourceLHS);
Node &GetOrCreateNode(const Phrase &source, const TargetPhrase &target,
const Word *sourceLHS);
void SortAndPrune(std::size_t);
Node m_root;
};
} // namespace S2T
} // namespace Syntax
} // namespace Moses
|