summaryrefslogtreecommitdiff
path: root/decoder/apply_fsa_models.cc
blob: 416b9323e39e862b5ae9585d84a75cf85045f14c (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
100
101
102
103
104
105
106
107
108
109
110
111
#include "apply_fsa_models.h"
#include "hg.h"
#include "ff_fsa_dynamic.h"
#include "ff_from_fsa.h"
#include "feature_vector.h"
#include "stringlib.h"
#include "apply_models.h"
#include <stdexcept>
#include <cassert>
#include "cfg.h"

using namespace std;

struct ApplyFsa {
  ApplyFsa(const Hypergraph& ih,
           const SentenceMetadata& smeta,
           const FsaFeatureFunction& fsa,
           DenseWeightVector const& weights,
           ApplyFsaBy const& by,
           Hypergraph* oh)
    :ih(ih),smeta(smeta),fsa(fsa),weights(weights),by(by),oh(oh)
  {
//    sparse_to_dense(weight_vector,&weights);
    Init();
  }
  void Init() {
    if (by.IsBottomUp())
      ApplyBottomUp();
    else
      ApplyEarley();
  }
  void ApplyBottomUp() {
    assert(by.IsBottomUp());
    FeatureFunctionFromFsa<FsaFeatureFunctionFwd> buff(&fsa);
    buff.Init(); // mandatory to call this (normally factory would do it)
    vector<const FeatureFunction*> ffs(1,&buff);
    ModelSet models(weights, ffs);
    IntersectionConfiguration i(by.BottomUpAlgorithm(),by.pop_limit);
    ApplyModelSet(ih,smeta,models,i,oh);
  }
  void ApplyEarley() {
    CFG cfg(ih,true,false,true);
  }
private:
  const Hypergraph& ih;
  const SentenceMetadata& smeta;
  const FsaFeatureFunction& fsa;
//  WeightVector weight_vector;
  DenseWeightVector weights;
  ApplyFsaBy by;
  Hypergraph* oh;
};


void ApplyFsaModels(const Hypergraph& ih,
                    const SentenceMetadata& smeta,
                    const FsaFeatureFunction& fsa,
                    DenseWeightVector const& weight_vector,
                    ApplyFsaBy const& by,
                    Hypergraph* oh)
{
  ApplyFsa a(ih,smeta,fsa,weight_vector,by,oh);
}


namespace {
char const* anames[]={
  "BU_CUBE",
  "BU_FULL",
  "EARLEY",
  0
};
}

//TODO: named enum type in boost?

std::string ApplyFsaBy::name() const {
  return anames[algorithm];
}

std::string ApplyFsaBy::all_names() {
  std::ostringstream o;
  for (int i=0;i<N_ALGORITHMS;++i) {
    assert(anames[i]);
    if (i) o<<' ';
    o<<anames[i];
  }
  return o.str();
}

ApplyFsaBy::ApplyFsaBy(std::string const& n, int pop_limit) : pop_limit(pop_limit){
  algorithm=0;
  std::string uname=toupper(n);
  while(anames[algorithm] && anames[algorithm] != uname) ++algorithm;
  if (!anames[algorithm])
    throw std::runtime_error("Unknown ApplyFsaBy type: "+n+" - legal types: "+all_names());
}

ApplyFsaBy::ApplyFsaBy(int i, int pop_limit) : pop_limit(pop_limit) {
  assert (i>=0);
  assert (i<N_ALGORITHMS);
  algorithm=i;
}

int ApplyFsaBy::BottomUpAlgorithm() const {
  assert(IsBottomUp());
  return algorithm==BU_CUBE ?
    IntersectionConfiguration::CUBE
    :IntersectionConfiguration::FULL;
}