summaryrefslogtreecommitdiff
path: root/gi/pf/cfg_wfst_composer.h
blob: cf47f4592d78563e63aed4972d9abb81f559735d (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
#ifndef _CFG_WFST_COMPOSER_H_
#define _CFG_WFST_COMPOSER_H_

#include <iostream>
#include <vector>
#include <utility>

#include "trule.h"
#include "wordid.h"

class CFG_WFSTComposerImpl;
class Hypergraph;

struct WFSTNode {
  virtual ~WFSTNode();
  // returns the next states reachable by consuming srcindex (which identifies a word)
  // paired with the output string generated by taking that transition.
  virtual std::vector<std::pair<const WFSTNode*,TRulePtr> > ExtendInput(unsigned srcindex) const = 0;
};

struct WFST {
  virtual ~WFST();
  virtual const WFSTNode* Final() const = 0;
  virtual const WFSTNode* Initial() const = 0;
};

class CFG_WFSTComposer {
 public:
  ~CFG_WFSTComposer();
  explicit CFG_WFSTComposer(const WFST& wfst);
  bool Compose(const Hypergraph& in_forest, Hypergraph* trg_forest);

  // reads the grammar from a file. There must be a single top-level
  // S -> X rule.  Anything else is possible. Format is:
  // [S] ||| [SS,1]
  // [SS] ||| [NP,1] [VP,2] ||| Feature1=0.2 Feature2=-2.3
  // [SS] ||| [VP,1] [NP,2] ||| Feature1=0.8
  // [NP] ||| [DET,1] [N,2] ||| Feature3=2
  // ...
  bool Compose(std::istream* grammar_file, Hypergraph* trg_forest);

 private:
  CFG_WFSTComposerImpl* pimpl_;
};

#endif