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
|
#pragma once
#include "grammar.hh"
#include "semiring.hh"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iterator>
#include <fstream>
#include "dummyvector.h"
#include <msgpack.hpp>
using namespace std;
typedef double score_t;
typedef double weight_t;
namespace Hg {
struct Node;
struct Edge {
Node* head;
vector<Node*> tails;
score_t score;
string rule; //FIXME
DummyVector f; //FIXME
unsigned int arity;
unsigned int mark;
bool is_marked();
friend std::ostream& operator<<(std::ostream& os, const Edge& s);
size_t head_id_;
vector<size_t> tails_ids_; // node ids
MSGPACK_DEFINE(head_id_, tails_ids_, score, f, arity);
};
struct Node {
size_t id;
string symbol;
unsigned short left;
unsigned short right;
score_t score;
vector<Edge*> incoming;
vector<Edge*> outgoing;
unsigned int mark;
bool is_marked();
friend std::ostream& operator<<(std::ostream& os, const Node& n);
vector<size_t> incoming_ids_; // edge ids
vector<size_t> outgoing_ids_; // edge ids
MSGPACK_DEFINE(id, symbol, left, right, score, incoming_ids_, outgoing_ids_);
};
struct Hypergraph {
list<Node*> nodes;
vector<Edge*> edges;
unordered_map<size_t, Node*> nodes_by_id;
unsigned int arity;
void add_node(Node* n) { nodes.push_back(n); nodes_by_id[n->id] = n; }
};
void
reset();
template<typename Semiring> void
init(list<Node*>& nodes, list<Node*>::iterator root, Semiring& semiring);
void
topological_sort(list<Node*>& nodes, list<Node*>::iterator root);
void
viterbi(Hypergraph& hg);
namespace io {
void
read(Hypergraph& hg, string fn);
void
write(Hypergraph& hg, string fn);
void
manual(Hypergraph& hg);
} // namespace
} // namespace
|