00001 #pragma once
00002
00003 #include <iostream>
00004 #include <string>
00005 #include <map>
00006 #include <vector>
00007 #include "FFState.h"
00008 #include "moses/Word.h"
00009 #include "moses/StaticData.h"
00010 #include <boost/shared_ptr.hpp>
00011 #include <boost/make_shared.hpp>
00012 #include "util/generator.hh"
00013 #include "util/exception.hh"
00014 #include "util/string_piece.hh"
00015
00016 namespace Moses
00017 {
00018
00019 class InternalTree;
00020 typedef boost::shared_ptr<InternalTree> TreePointer;
00021
00022 class InternalTree
00023 {
00024 Word m_value;
00025 std::vector<TreePointer> m_children;
00026 public:
00027 InternalTree(const std::string & line, size_t start, size_t len, const bool terminal);
00028 InternalTree(const std::string & line, const bool nonterminal = true);
00029 InternalTree(const InternalTree & tree):
00030 m_value(tree.m_value) {
00031 const std::vector<TreePointer> & children = tree.m_children;
00032 for (std::vector<TreePointer>::const_iterator it = children.begin(); it != children.end(); it++) {
00033 m_children.push_back(boost::make_shared<InternalTree>(**it));
00034 }
00035 }
00036 size_t AddSubTree(const std::string & line, size_t start);
00037
00038 std::string GetString(bool start = true) const;
00039 void Combine(const std::vector<TreePointer> &previous);
00040 void Unbinarize();
00041 void GetUnbinarizedChildren(std::vector<TreePointer> &children) const;
00042 const Word & GetLabel() const {
00043 return m_value;
00044 }
00045
00046 size_t GetLength() const {
00047 return m_children.size();
00048 }
00049 std::vector<TreePointer> & GetChildren() {
00050 return m_children;
00051 }
00052
00053 bool IsTerminal() const {
00054 return !m_value.IsNonTerminal();
00055 }
00056
00057 bool IsLeafNT() const {
00058 return (m_value.IsNonTerminal() && m_children.size() == 0);
00059 }
00060
00061
00062
00063
00064
00065 bool FlatSearch(const Word & label, std::vector<TreePointer>::const_iterator & it) const;
00066 bool RecursiveSearch(const Word & label, std::vector<TreePointer>::const_iterator & it) const;
00067
00068
00069 bool RecursiveSearch(const Word & label, std::vector<TreePointer>::const_iterator & it, InternalTree const* &parent) const;
00070
00071
00072 $generator(leafNT) {
00073 std::vector<TreePointer>::iterator it;
00074 InternalTree* tree;
00075 leafNT(InternalTree* root = 0): tree(root) {}
00076 $emit(std::vector<TreePointer>::iterator)
00077 for (it = tree->GetChildren().begin(); it !=tree->GetChildren().end(); ++it) {
00078 if (!(*it)->IsTerminal() && (*it)->GetLength() == 0) {
00079 $yield(it);
00080 } else if ((*it)->GetLength() > 0) {
00081 if ((*it).get()) {
00082 $restart(tree = (*it).get());
00083 }
00084 }
00085 }
00086 $stop;
00087 };
00088
00089
00090
00091 $generator(leafNTParent) {
00092 std::vector<TreePointer>::iterator it;
00093 InternalTree* tree;
00094 leafNTParent(InternalTree* root = 0): tree(root) {}
00095 $emit(InternalTree*)
00096 for (it = tree->GetChildren().begin(); it !=tree->GetChildren().end(); ++it) {
00097 if (!(*it)->IsTerminal() && (*it)->GetLength() == 0) {
00098 $yield(tree);
00099 } else if ((*it)->GetLength() > 0) {
00100 if ((*it).get()) {
00101 $restart(tree = (*it).get());
00102 }
00103 }
00104 }
00105 $stop;
00106 };
00107
00108
00109 $generator(leafNTPath) {
00110 std::vector<TreePointer>::iterator it;
00111 InternalTree* tree;
00112 std::vector<InternalTree*> * path;
00113 leafNTPath(InternalTree* root = NULL, std::vector<InternalTree*> * orig = NULL): tree(root), path(orig) {}
00114 $emit(std::vector<TreePointer>::iterator)
00115 path->push_back(tree);
00116 for (it = tree->GetChildren().begin(); it !=tree->GetChildren().end(); ++it) {
00117 if (!(*it)->IsTerminal() && (*it)->GetLength() == 0) {
00118 path->push_back((*it).get());
00119 $yield(it);
00120 path->pop_back();
00121 } else if ((*it)->GetLength() > 0) {
00122 if ((*it).get()) {
00123 $restart(tree = (*it).get());
00124 }
00125 }
00126 }
00127 path->pop_back();
00128 $stop;
00129 };
00130
00131 };
00132
00133 class TreeState : public FFState
00134 {
00135 TreePointer m_tree;
00136 public:
00137 TreeState(TreePointer tree)
00138 :m_tree(tree) {
00139 }
00140
00141 TreePointer GetTree() const {
00142 return m_tree;
00143 }
00144
00145 virtual size_t hash() const {
00146 return 0;
00147 }
00148 virtual bool operator==(const FFState& other) const {
00149 return true;
00150 }
00151
00152 };
00153
00154 }