summaryrefslogtreecommitdiff
path: root/decoder/apply_fsa_models.h
blob: 0227664a9d47b8af1c6a5786129a7b719ce7e4ec (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
#ifndef _APPLY_FSA_MODELS_H_
#define _APPLY_FSA_MODELS_H_

#include <string>
#include <iostream>
#include "feature_vector.h"
#include "cfg.h"

struct FsaFeatureFunction;
struct Hypergraph;
struct SentenceMetadata;

struct ApplyFsaBy {
  enum {
    BU_CUBE,
    BU_FULL,
    EARLEY,
    N_ALGORITHMS
  };
  int pop_limit; // only applies to BU_FULL so far
  bool IsBottomUp() const {
    return algorithm==BU_FULL || algorithm==BU_CUBE;
  }
  int BottomUpAlgorithm() const;
  int algorithm;
  std::string name() const;
  friend inline std::ostream &operator << (std::ostream &o,ApplyFsaBy const& c) {
    o << c.name();
    if (c.algorithm==BU_CUBE)
      o << "("<<c.pop_limit<<")";
    return o;
  }
  explicit ApplyFsaBy(int alg, int poplimit=200);
  ApplyFsaBy(std::string const& name, int poplimit=200);
  ApplyFsaBy(const ApplyFsaBy &o) : algorithm(o.algorithm) {  }
  static std::string all_names(); // space separated
};

// in case you might want the CFG whether or not you apply FSA models:
struct HgCFG {
  HgCFG(Hypergraph const& ih) : ih(ih) { have_cfg=false; }
  Hypergraph const& ih;
  CFG cfg;
  bool have_cfg;
  void InitCFG(CFG &to) {
    to.Init(ih,true,false,true);
  }

  CFG &GetCFG()
  {
    if (!have_cfg) {
      have_cfg=true;
      InitCFG(cfg);
    }
    return cfg;
  }
  void GiveCFG(CFG &to) {
    if (!have_cfg)
      InitCFG(to);
    else {
      have_cfg=false;
      to.Clear();
      to.Swap(cfg);
    }
  }
  CFG const& GetCFG() const {
    assert(have_cfg);
    return cfg;
  }
};


void ApplyFsaModels(HgCFG &hg_or_cfg_in,
                    const SentenceMetadata& smeta,
                    const FsaFeatureFunction& fsa,
                    DenseWeightVector const& weights, // pre: in is weighted by these (except with fsa featval=0 before this)
                    ApplyFsaBy const& cfg,
                    Hypergraph* out);

inline void ApplyFsaModels(Hypergraph const& ih,
                    const SentenceMetadata& smeta,
                    const FsaFeatureFunction& fsa,
                    DenseWeightVector const& weights, // pre: in is weighted by these (except with fsa featval=0 before this)
                    ApplyFsaBy const& cfg,
                    Hypergraph* out) {
  HgCFG i(ih);
  ApplyFsaModels(i,smeta,fsa,weights,cfg,out);
}


#endif