00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "ComposedRule.h"
00021
00022 #include <set>
00023 #include <vector>
00024 #include <queue>
00025
00026 #include "Node.h"
00027 #include "Options.h"
00028 #include "Subgraph.h"
00029
00030 namespace MosesTraining
00031 {
00032 namespace Syntax
00033 {
00034 namespace GHKM
00035 {
00036
00037 ComposedRule::ComposedRule(const Subgraph &baseRule)
00038 : m_baseRule(baseRule)
00039 , m_depth(baseRule.GetDepth())
00040 , m_size(baseRule.GetSize())
00041 , m_nodeCount(baseRule.GetNodeCount())
00042 {
00043 const std::set<const Node *> &leaves = baseRule.GetLeaves();
00044 for (std::set<const Node *>::const_iterator p = leaves.begin();
00045 p != leaves.end(); ++p) {
00046 if ((*p)->GetType() == TREE) {
00047 m_openAttachmentPoints.push(*p);
00048 }
00049 }
00050 }
00051
00052 ComposedRule::ComposedRule(const ComposedRule &other, const Subgraph &rule,
00053 int depth)
00054 : m_baseRule(other.m_baseRule)
00055 , m_attachedRules(other.m_attachedRules)
00056 , m_openAttachmentPoints(other.m_openAttachmentPoints)
00057 , m_depth(depth)
00058 , m_size(other.m_size+rule.GetSize())
00059 , m_nodeCount(other.m_nodeCount+rule.GetNodeCount()-1)
00060 {
00061 m_attachedRules.push_back(&rule);
00062 m_openAttachmentPoints.pop();
00063 }
00064
00065 const Node *ComposedRule::GetOpenAttachmentPoint()
00066 {
00067 return m_openAttachmentPoints.empty() ? 0 : m_openAttachmentPoints.front();
00068 }
00069
00070 void ComposedRule::CloseAttachmentPoint()
00071 {
00072 assert(!m_openAttachmentPoints.empty());
00073 m_attachedRules.push_back(0);
00074 m_openAttachmentPoints.pop();
00075 }
00076
00077 ComposedRule *ComposedRule::AttemptComposition(const Subgraph &rule,
00078 const Options &options) const
00079 {
00080
00081
00082 assert(rule.GetRoot()->GetType() == TREE);
00083
00084
00085 if (m_nodeCount+rule.GetNodeCount()-1 > options.maxNodes) {
00086 return 0;
00087 }
00088
00089
00090 if (m_size+rule.GetSize() > options.maxRuleSize) {
00091 return 0;
00092 }
00093
00094
00095
00096 int attachmentPointDepth = 0;
00097 const Node *n = rule.GetRoot();
00098 while (n != m_baseRule.GetRoot()) {
00099 assert(n->GetParents().size() == 1);
00100 n = n->GetParents()[0];
00101 ++attachmentPointDepth;
00102 }
00103 int newDepth = std::max(m_depth, attachmentPointDepth+rule.GetDepth());
00104 if (newDepth > options.maxRuleDepth) {
00105 return 0;
00106 }
00107
00108 return new ComposedRule(*this, rule, newDepth);
00109 }
00110
00111 Subgraph ComposedRule::CreateSubgraph()
00112 {
00113 std::set<const Node *> leaves;
00114 const std::set<const Node *> &baseLeaves = m_baseRule.GetLeaves();
00115 size_t i = 0;
00116 for (std::set<const Node *>::const_iterator p = baseLeaves.begin();
00117 p != baseLeaves.end(); ++p) {
00118 const Node *baseLeaf = *p;
00119 if (baseLeaf->GetType() == TREE && i < m_attachedRules.size()) {
00120 const Subgraph *attachedRule = m_attachedRules[i++];
00121 if (attachedRule) {
00122 leaves.insert(attachedRule->GetLeaves().begin(),
00123 attachedRule->GetLeaves().end());
00124 continue;
00125 }
00126 }
00127 leaves.insert(baseLeaf);
00128 }
00129 return Subgraph(m_baseRule.GetRoot(), leaves);
00130 }
00131
00132 }
00133 }
00134 }