summaryrefslogtreecommitdiff
path: root/fast/hypergraph.hh
blob: ea940ad3fdd83bfc4408efd3ecf86a6d7057e651 (plain)
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
#pragma once

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <msgpack.hpp>
#include <msgpack/fbuffer.hpp>

#include "grammar.hh"
#include "semiring.hh"
#include "dummyvector.h"

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 = 0;
      unsigned int mark = 0;

  inline bool is_marked() { return mark >= arity; }
  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;
           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 std::ostream& operator<<(std::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;

  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