|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once |
|
|
|
#include <set> |
|
#include <vector> |
|
|
|
#include "Node.h" |
|
|
|
namespace MosesTraining |
|
{ |
|
namespace Syntax |
|
{ |
|
namespace GHKM |
|
{ |
|
|
|
class Node; |
|
|
|
class Subgraph |
|
{ |
|
public: |
|
Subgraph(const Node *root) |
|
: m_root(root) |
|
, m_depth(0) |
|
, m_size(root->GetType() == TREE ? 1 : 0) |
|
, m_nodeCount(1) |
|
, m_pcfgScore(0.0f) {} |
|
|
|
Subgraph(const Node *root, const std::set<const Node *> &leaves) |
|
: m_root(root) |
|
, m_leaves(leaves) |
|
, m_depth(-1) |
|
, m_size(-1) |
|
, m_nodeCount(-1) |
|
, m_pcfgScore(0.0f) { |
|
m_depth = CalcDepth(m_root); |
|
m_size = CalcSize(m_root); |
|
m_nodeCount = CountNodes(m_root); |
|
m_pcfgScore = CalcPcfgScore(); |
|
} |
|
|
|
Subgraph(const Subgraph &other, bool targetOnly=false) |
|
: m_root(other.m_root) |
|
, m_leaves(other.m_leaves) |
|
, m_depth(other.m_depth) |
|
, m_size(other.m_size) |
|
, m_nodeCount(other.m_nodeCount) |
|
, m_pcfgScore(other.m_pcfgScore) { |
|
if (targetOnly && m_root->GetType() != SOURCE) { |
|
|
|
|
|
|
|
std::set<const Node *> targetLeaves; |
|
for (std::set<const Node *>::const_iterator p = m_leaves.begin(); |
|
p != m_leaves.end(); ++p) { |
|
const Node *leaf = *p; |
|
if (leaf->GetType() != SOURCE) { |
|
targetLeaves.insert(leaf); |
|
} else { |
|
const std::vector<Node*> &parents = leaf->GetParents(); |
|
for (std::vector<Node*>::const_iterator q = parents.begin(); |
|
q != parents.end(); ++q) { |
|
const Node *parent = *q; |
|
|
|
|
|
|
|
|
|
if (parent->GetType() == TARGET) { |
|
targetLeaves.insert(*q); |
|
} |
|
} |
|
} |
|
} |
|
m_leaves.swap(targetLeaves); |
|
} |
|
} |
|
|
|
const Node *GetRoot() const { |
|
return m_root; |
|
} |
|
const std::set<const Node *> &GetLeaves() const { |
|
return m_leaves; |
|
} |
|
int GetDepth() const { |
|
return m_depth; |
|
} |
|
int GetSize() const { |
|
return m_size; |
|
} |
|
int GetNodeCount() const { |
|
return m_nodeCount; |
|
} |
|
float GetPcfgScore() const { |
|
return m_pcfgScore; |
|
} |
|
|
|
bool IsTrivial() const { |
|
return m_leaves.empty(); |
|
} |
|
|
|
void GetTargetLeaves(std::vector<const Node *> &) const; |
|
void PrintTree(std::ostream &out) const; |
|
void PrintPartsOfSpeech(std::ostream &out) const; |
|
void GetPartsOfSpeech(std::vector<std::string> &out) const; |
|
|
|
private: |
|
void GetTargetLeaves(const Node *, std::vector<const Node *> &) const; |
|
int CalcDepth(const Node *) const; |
|
int CalcSize(const Node *) const; |
|
float CalcPcfgScore() const; |
|
int CountNodes(const Node *) const; |
|
void RecursivelyPrintTree(const Node *n, std::ostream &out) const; |
|
void RecursivelyPrintPartsOfSpeech(const Node *n, std::ostream &out) const; |
|
void RecursivelyGetPartsOfSpeech(const Node *n, std::vector<std::string> &out) const; |
|
|
|
const Node *m_root; |
|
std::set<const Node *> m_leaves; |
|
int m_depth; |
|
int m_size; |
|
int m_nodeCount; |
|
float m_pcfgScore; |
|
}; |
|
|
|
} |
|
} |
|
} |
|
|