File size: 1,679 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "ExternalFeature.h"
#include <dlfcn.h>
#include <cstdlib>
#include <iostream>
#include "util/exception.hh"

using namespace std;

namespace Moses
{
ExternalFeatureState::ExternalFeatureState(int stateSize, void *data)
{
  m_stateSize = stateSize;
  m_data = malloc(stateSize);
  memcpy(m_data, data, stateSize);
}

void ExternalFeature::Load(AllOptions const& opts)
{
  string nparam = "testing";

  if (m_path.size() < 1) {
    UTIL_THROW2("External requires a path to a dynamic library");
  }
  lib_handle = dlopen(m_path.c_str(), RTLD_LAZY);
  if (!lib_handle) {
    UTIL_THROW2("dlopen reports: " << dlerror() << ". Did you provide a full path to the dynamic library?";);
  }
  CdecFF* (*fn)(const string&) =
    (CdecFF* (*)(const string&))(dlsym(lib_handle, "create_ff"));
  if (!fn) {
    UTIL_THROW2("dlsym reports: " << dlerror());
  }
  ff_ext = (*fn)(nparam);
  m_stateSize = ff_ext->StateSize();

}

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

void ExternalFeature::SetParameter(const std::string& key, const std::string& value)
{
  if (key == "path") {
    m_path = value;
  } else {
    StatefulFeatureFunction::SetParameter(key, value);
  }
}

FFState* ExternalFeature::EvaluateWhenApplied(
  const Hypothesis& cur_hypo,
  const FFState* prev_state,
  ScoreComponentCollection* accumulator) const
{
  return new ExternalFeatureState(m_stateSize);
}

FFState* ExternalFeature::EvaluateWhenApplied(
  const ChartHypothesis& /* cur_hypo */,
  int /* featureID - used to index the state in the previous hypotheses */,
  ScoreComponentCollection* accumulator) const
{
  return new ExternalFeatureState(m_stateSize);
}


}