File size: 1,684 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 |
#include "HyperTree.h"
namespace Moses
{
namespace Syntax
{
namespace F2S
{
void HyperTree::Node::Prune(std::size_t tableLimit)
{
// Recusively prune child nodes.
for (Map::iterator p = m_map.begin(); p != m_map.end(); ++p) {
p->second.Prune(tableLimit);
}
// Prune TargetPhraseCollection at this node.
m_targetPhraseCollection->Prune(true, tableLimit);
}
void HyperTree::Node::Sort(std::size_t tableLimit)
{
// Recusively sort child nodes.
for (Map::iterator p = m_map.begin(); p != m_map.end(); ++p) {
p->second.Sort(tableLimit);
}
// Sort TargetPhraseCollection at this node.
m_targetPhraseCollection->Sort(true, tableLimit);
}
HyperTree::Node *HyperTree::Node::GetOrCreateChild(
const HyperPath::NodeSeq &nodeSeq)
{
return &m_map[nodeSeq];
}
const HyperTree::Node *HyperTree::Node::GetChild(
const HyperPath::NodeSeq &nodeSeq) const
{
Map::const_iterator p = m_map.find(nodeSeq);
return (p == m_map.end()) ? NULL : &p->second;
}
TargetPhraseCollection::shared_ptr HyperTree::GetOrCreateTargetPhraseCollection(
const HyperPath &hyperPath)
{
Node &node = GetOrCreateNode(hyperPath);
return node.GetTargetPhraseCollection();
}
HyperTree::Node &HyperTree::GetOrCreateNode(const HyperPath &hyperPath)
{
const std::size_t height = hyperPath.nodeSeqs.size();
Node *node = &m_root;
for (std::size_t i = 0; i < height; ++i) {
const HyperPath::NodeSeq &nodeSeq = hyperPath.nodeSeqs[i];
node = node->GetOrCreateChild(nodeSeq);
}
return *node;
}
void HyperTree::SortAndPrune(std::size_t tableLimit)
{
if (tableLimit) {
m_root.Sort(tableLimit);
}
}
} // namespace F2S
} // namespace Syntax
} // namespace Moses
|