summaryrefslogtreecommitdiff
path: root/decoder/ff_external.cc
blob: 6ee4b2cf0ace766954216edd9a68aedcb821d4a5 (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
#include "ff_external.h"

#include <dlfcn.h>

#include "stringlib.h"
#include "hg.h"

using namespace std;

ExternalFeature::ExternalFeature(const string& param) {
  size_t pos = param.find(' ');
  string nparam;
  string file = param;
  if (pos < param.size()) {
    nparam = Trim(param.substr(pos + 1));
    file = param.substr(0, pos);
  }
  if (file.size() < 1) {
    cerr << "External requires a path to a dynamic library!\n";
    abort();
  }
  lib_handle = dlopen(file.c_str(), RTLD_LAZY | RTLD_GLOBAL);
  if (!lib_handle) {
    cerr << "dlopen reports: " << dlerror() << endl;
    cerr << "Did you provide a full path to the dynamic library?\n";
    abort();
  }
  FeatureFunction* (*fn)(const string&) =
    (FeatureFunction* (*)(const string&))(dlsym(lib_handle, "create_ff"));
  if (!fn) {
    cerr << "dlsym reports: " << dlerror() << endl;
    abort();
  }
  ff_ext = (*fn)(nparam);
  SetStateSize(ff_ext->StateSize());
}

ExternalFeature::~ExternalFeature() {
  delete ff_ext;
  dlclose(lib_handle);
}

void ExternalFeature::PrepareForInput(const SentenceMetadata& smeta) {
  ff_ext->PrepareForInput(smeta);
}

void ExternalFeature::FinalTraversalFeatures(const void* context,
                                             SparseVector<double>* features) const {
  ff_ext->FinalTraversalFeatures(context, features);
}

void ExternalFeature::TraversalFeaturesImpl(const SentenceMetadata& smeta,
                                     const Hypergraph::Edge& edge,
                                     const std::vector<const void*>& ant_contexts,
                                     SparseVector<double>* features,
                                     SparseVector<double>* estimated_features,
                                     void* context) const {
  ff_ext->TraversalFeaturesImpl(smeta, edge, ant_contexts, features, estimated_features, context);
}