Fleet  0.0.9
Inference in the LOT
BindingTree.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <assert.h>
6 #include <set>
7 #include <map>
8 #include <functional>
9 
10 #include "BaseNode.h"
11 #include "SExpression.h"
12 
13 // Some parts of speech here as a class
14 enum class POS { None, S, SBAR, NP, NPS, NPO, NPPOSS, VP, MD, PP, CP, CC, N, V, P, A};
15 
16 std::map<std::string,POS> posmap = { // just for decoding strings to tags -- ugh
17  {"S", POS::S},
18  {"NP", POS::NP},
19  {"NP-S", POS::NPS},
20  {"NP-O", POS::NPO},
21  {"VP", POS::VP},
22  {"PP", POS::PP},
23  {"CP", POS::CP},
24  {"CC", POS::CC},
25  {"SBAR", POS::SBAR},
26  {"MD", POS::MD},
27  {"N", POS::N},
28  {"V", POS::V},
29  {"P", POS::P},
30  {"A", POS::A},
31  {"NP-POSS", POS::NPPOSS},
32  };
33 
34 
35 
46 class BindingTree : public BaseNode<BindingTree> {
47 public:
48  int referent; // parsed out of the string
49  bool target; // and I the target?
50  int linear_order; // order of leaves
52  std::string label;
53  std::string word;
54 
55  BindingTree() : referent(-1), target(false), linear_order(0), pos(POS::None), word("") {
56 
57  }
58 
64  referent(-1), target(false), linear_order(0), pos(POS::None), word(""){
65 
66  int copy_from = 0; // where do we start copying the children from?
67 
68  if(n.nchildren()>0 and n.child(0).label.has_value()) {
69  const std::string& s = n.child(0).label.value();
70  copy_from = 1;
71 
72  // set up the referent if we can
73  // NOTE: There referent is on the POS
74  auto p = s.find(".");
75  if(p != std::string::npos) {
76  referent = stoi(s.substr(p+1)); // only single digits!!
77  label = s.substr(0,p);
78  }
79  else {
80  label = s;
81  }
82 
83 
84  // and then fix label if it has a star
85  if(label.length() > 0 and label.at(0) == '>') {
86  target = true;
87  label = label.substr(1,std::string::npos);
88  }
89 
90  // and set the POS accordingly
91  if(posmap.count(label) != 0){
92  pos = posmap[label];
93  }
94 
95 
96  // and check if there is a word next
97  if(n.nchildren() > 1 and n.child(1).label.has_value()) {
98 
99  copy_from = 2;
100 
101  word = n.child(1).label.value();
102  }
103 
104  for(size_t i=copy_from;i<n.nchildren();i++) {
105 
106  push_back(BindingTree{n.child(i)});
107 
108  }
109 
110  }
111 
112 
113 
114 
115  }
116 
118  // I need these because I need to call basenode, which overrides
119  // so that children are correctly set
121 
123  label = t.label;
124  target = t.target;
125  referent = t.referent;
126  word = t.word;
127  linear_order = t.linear_order;
128  pos = t.pos;
129  }
131  label = t.label;
132  target = t.target;
133  referent = t.referent;
134  word = t.word;
135  linear_order = t.linear_order;
136  pos = t.pos;
137  }
138  void operator=(const BindingTree& t) {
140  label = t.label;
141  target = t.target;
142  referent = t.referent;
143  word = t.word;
144  linear_order = t.linear_order;
145  pos = t.pos;
146  }
147  void operator=(const BindingTree&& t) {
149  label = t.label;
150  target = t.target;
151  referent = t.referent;
152  word = t.word;
153  linear_order = t.linear_order;
154  pos = t.pos;
155  }
156 
158  std::function f = [&](BindingTree& t) { return t.target; };
159  return get_via(f);
160  }
161 
163  BindingTree* x = this; // local ref for f
164 
165  std::function f = [&](BindingTree& t) -> bool {
166  return (t.referent != -1) and
167  (t.referent == x->referent) and
168  (&t != x);
169  };
170 
171  return root()->get_via(f);
172  }
173 
174  std::string string(bool usedot=true) const override {
175  std::string out = "[" + (target ? std::string(">") : "") +
176  label +
177  (referent > -1 ? "."+str(referent) : "") +
178  ";" +
179  word;
180  for(auto& c : children) {
181  out += " " + c.string();
182  }
183  out += "]";
184  return out;
185  }
186 
187  std::string my_string() const override {
188  // string for just me but not my kids
189  return (target ? ">" : "") + label + " " + std::to_string(referent);
190  }
191 
192 };
193 
194 
195 std::string str(BindingTree* t){
196  if(t == nullptr) return "nullptr";
197  else return t->string();
198 }
This is the return type of parsing S-expressions. It contains an optional label and typically we be c...
POS
Definition: BindingTree.h:14
This is a general tree class, which we are adding because there are currently at least 3 different tr...
bool target
Definition: BindingTree.h:49
BindingTree(const SExpression::SExpNode &n)
This is a convertion from S-expression parsing.
Definition: BindingTree.h:63
std::string label
Definition: BindingTree.h:52
BindingTree()
Definition: BindingTree.h:55
std::function children
Definition: MyGrammar.h:25
Definition: BindingTree.h:46
int referent
Definition: BindingTree.h:48
Definition: SExpression.h:20
std::string word
Definition: BindingTree.h:53
BindingTree * coreferent()
Definition: BindingTree.h:162
BindingTree(const BindingTree &&t)
Definition: BindingTree.h:130
std::string str(BindingTree *t)
Definition: BindingTree.h:195
Definition: BaseNode.h:20
void operator=(const BindingTree &&t)
Definition: BindingTree.h:147
BindingTree(const BindingTree &t)
Definition: BindingTree.h:122
this_t & child(const size_t i)
Definition: BaseNode.h:175
int linear_order
Definition: BindingTree.h:50
void operator=(const this_t &t)
Definition: BaseNode.h:51
BindingTree * get_target()
Definition: BindingTree.h:157
std::string string(bool usedot=true) const override
Definition: BindingTree.h:174
std::string my_string() const override
Definition: BindingTree.h:187
POS pos
Definition: BindingTree.h:51
size_t nchildren() const
Definition: BaseNode.h:208
void operator=(const BindingTree &t)
Definition: BindingTree.h:138
std::map< std::string, POS > posmap
Definition: BindingTree.h:16