00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #pragma once
00021
00022 #include <set>
00023 #include <vector>
00024
00025 #include "Node.h"
00026
00027 namespace MosesTraining
00028 {
00029 namespace Syntax
00030 {
00031 namespace GHKM
00032 {
00033
00034 class Node;
00035
00036 class Subgraph
00037 {
00038 public:
00039 Subgraph(const Node *root)
00040 : m_root(root)
00041 , m_depth(0)
00042 , m_size(root->GetType() == TREE ? 1 : 0)
00043 , m_nodeCount(1)
00044 , m_pcfgScore(0.0f) {}
00045
00046 Subgraph(const Node *root, const std::set<const Node *> &leaves)
00047 : m_root(root)
00048 , m_leaves(leaves)
00049 , m_depth(-1)
00050 , m_size(-1)
00051 , m_nodeCount(-1)
00052 , m_pcfgScore(0.0f) {
00053 m_depth = CalcDepth(m_root);
00054 m_size = CalcSize(m_root);
00055 m_nodeCount = CountNodes(m_root);
00056 m_pcfgScore = CalcPcfgScore();
00057 }
00058
00059 Subgraph(const Subgraph &other, bool targetOnly=false)
00060 : m_root(other.m_root)
00061 , m_leaves(other.m_leaves)
00062 , m_depth(other.m_depth)
00063 , m_size(other.m_size)
00064 , m_nodeCount(other.m_nodeCount)
00065 , m_pcfgScore(other.m_pcfgScore) {
00066 if (targetOnly && m_root->GetType() != SOURCE) {
00067
00068
00069
00070 std::set<const Node *> targetLeaves;
00071 for (std::set<const Node *>::const_iterator p = m_leaves.begin();
00072 p != m_leaves.end(); ++p) {
00073 const Node *leaf = *p;
00074 if (leaf->GetType() != SOURCE) {
00075 targetLeaves.insert(leaf);
00076 } else {
00077 const std::vector<Node*> &parents = leaf->GetParents();
00078 for (std::vector<Node*>::const_iterator q = parents.begin();
00079 q != parents.end(); ++q) {
00080 const Node *parent = *q;
00081
00082
00083
00084
00085 if (parent->GetType() == TARGET) {
00086 targetLeaves.insert(*q);
00087 }
00088 }
00089 }
00090 }
00091 m_leaves.swap(targetLeaves);
00092 }
00093 }
00094
00095 const Node *GetRoot() const {
00096 return m_root;
00097 }
00098 const std::set<const Node *> &GetLeaves() const {
00099 return m_leaves;
00100 }
00101 int GetDepth() const {
00102 return m_depth;
00103 }
00104 int GetSize() const {
00105 return m_size;
00106 }
00107 int GetNodeCount() const {
00108 return m_nodeCount;
00109 }
00110 float GetPcfgScore() const {
00111 return m_pcfgScore;
00112 }
00113
00114 bool IsTrivial() const {
00115 return m_leaves.empty();
00116 }
00117
00118 void GetTargetLeaves(std::vector<const Node *> &) const;
00119 void PrintTree(std::ostream &out) const;
00120 void PrintPartsOfSpeech(std::ostream &out) const;
00121 void GetPartsOfSpeech(std::vector<std::string> &out) const;
00122
00123 private:
00124 void GetTargetLeaves(const Node *, std::vector<const Node *> &) const;
00125 int CalcDepth(const Node *) const;
00126 int CalcSize(const Node *) const;
00127 float CalcPcfgScore() const;
00128 int CountNodes(const Node *) const;
00129 void RecursivelyPrintTree(const Node *n, std::ostream &out) const;
00130 void RecursivelyPrintPartsOfSpeech(const Node *n, std::ostream &out) const;
00131 void RecursivelyGetPartsOfSpeech(const Node *n, std::vector<std::string> &out) const;
00132
00133 const Node *m_root;
00134 std::set<const Node *> m_leaves;
00135 int m_depth;
00136 int m_size;
00137 int m_nodeCount;
00138 float m_pcfgScore;
00139 };
00140
00141 }
00142 }
00143 }