1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#include "hypergraph.hh"
namespace Hg {
/*
* Node
*
*/
bool
Node::is_marked()
{
return mark >= incoming.size();
}
std::ostream&
operator<<(std::ostream& os, const Node& n)
{
os << \
"Node<id=" << n.id << \
", symbol=" << n.symbol << \
", span=(" << n.left << "," << n.right << ")" \
", score=" << n.score << \
", incoming:" << n.incoming.size() << \
", outgoing:" << n.outgoing.size() << \
", mark=" << n.mark << ">";
}
/*
* Edge
*
*/
bool
Edge::is_marked()
{
return mark >= arity;
}
std::ostream&
operator<<(std::ostream& os, const Edge& e)
{
ostringstream _;
for (auto it = e.tails.begin(); it != e.tails.end(); ++it) {
_ << (*it)->id; if (*it != e.tails.back()) _ << ",";
}
os << \
"Edge<head=" << e.head->id << \
"', tails=[" << _.str() << "]" \
", score=" << e.score << \
", rule:'" << "TODO" << \
" , f=" << "TODO" << \
", arity=" << e.arity << \
", mark=" << e.mark << ">";
return os;
}
/*
* Hypergraph
* methods
*
*/
void
Hypergraph::reset()
{
}
/*
* functions
*
*/
void
topological_sort(list<Node*>& nodes, list<Node*>::iterator root)
{
auto p = root;
(**p).mark = 0; // is_marked()==true
auto to = nodes.begin();
while (to != nodes.end()) {
for (auto it = nodes.begin(); it != nodes.end(); ++it) cout << (**it).id << " ";
cout << endl;
if ((**p).is_marked()) {
// explore edges
for (auto e = (**p).outgoing.begin(); e!=(**p).outgoing.end(); ++e) {
(**e).mark++;
if ((**e).is_marked()) {
(**e).head->mark++;
}
}
}
if ((**p).is_marked()) {
nodes.splice(to, nodes, p);
to = next(p);
p = to;
} else {
++p;
/*if (p == nodes.end()) {
for (auto e = (**to).outgoing.begin(); e!=(**to).outgoing.end(); ++e) {
// explore edges
(**e).mark++;
if ((**e).is_marked()) {
(**e).head->mark++;
}
}
to++;
p = to;
}*/
}
}
}
/*void
init(vector<Node*>& nodes, ViterbiSemiring<double>& semiring, Node* root)
{
for (auto it = nodes.begin(); it != nodes.end(); ++it)
(*it)->score = semiring.null;
root->score = semiring.one;
}
void
viterbi(vector<Node*>& nodes, map<unsigned int, Hg::Node*> nodes_by_id, Node* root)
{
vector<Node*> sorted = topological_sort(nodes);
ViterbiSemiring<double> semiring;
init(sorted, semiring, root);
for (auto n_it = sorted.begin(); n_it != sorted.end(); ++n_it) {
for (auto e_it = (*n_it)->incoming.begin(); e_it != (*n_it)->incoming.end(); ++e_it) {
cout << (*e_it)->s() << endl;
double s = semiring.one;
for (auto m_it = (*e_it)->tails.begin(); m_it != (*e_it)->tails.end(); m_it++) {
s = semiring.multiply(s, (*m_it)->score);
}
(*n_it)->score = semiring.add((*n_it)->score, semiring.multiply(s, (*e_it)->score));
}
}
for (auto it = sorted.begin(); it != sorted.end(); ++it) {
cout << (*it)->id << " " << (*it)->score << endl;
}
}*/
} // namespace
|