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
|
#pragma once
#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <msgpack.hpp>
#include <msgpack/fbuffer.hpp>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "grammar.hh"
#include "semiring.hh"
#include "sparse_vector.hh"
#include "weaver.hh"
using namespace std;
namespace Hg {
struct Node;
struct Edge {
Node* head;
vector<Node*> tails;
score_t score;
G::Rule* rule;
unsigned int arity = 0;
unsigned int mark = 0;
inline bool is_marked() { return mark >= arity; }
friend ostream& operator<<(ostream& os, const Edge& s);
size_t head_id_;
vector<size_t> tails_ids_; // node ids
size_t rule_id_;
MSGPACK_DEFINE(head_id_, tails_ids_, rule_id_, score, arity);
};
struct Node {
size_t id;
string symbol;
short left;
short right;
score_t score;
vector<Edge*> incoming;
vector<Edge*> outgoing;
unsigned int mark;
inline bool is_marked() { return mark >= incoming.size(); };
friend ostream& operator<<(ostream& os, const Node& n);
MSGPACK_DEFINE(id, symbol, left, right, score);
};
struct Hypergraph {
list<Node*> nodes;
vector<Edge*> edges;
unordered_map<size_t, Node*> nodes_by_id;
unsigned int arity;
};
template<typename Semiring> void
init(const list<Node*>& nodes, const list<Node*>::iterator root, const Semiring& semiring);
void
reset(const list<Node*> nodes, const vector<Edge*> edges);
void
topological_sort(list<Node*>& nodes, const list<Node*>::iterator root);
void
viterbi(Hypergraph& hg);
typedef vector<Edge*> Path;
void
viterbi_path(Hypergraph& hg, Path& p);
void
derive(const Path& p, const Node* cur, vector<string>& carry);
namespace io {
void
read(Hypergraph& hg, vector<G::Rule*>& rules, const string& fn); // FIXME
void
write(Hypergraph& hg, vector<G::Rule*>& rules, const string& fn); // FIXME
void
manual(Hypergraph& hg, vector<G::Rule*>& rules);
} // namespace
} // namespace
|