mosesdecoder/moses/FF/ExternalFeature.cpp

76 lines
1.6 KiB
C++
Raw Normal View History

2013-09-12 11:48:17 +04:00
#include "ExternalFeature.h"
#include <dlfcn.h>
2013-11-21 22:58:28 +04:00
#include <stdlib.h>
#include <iostream>
2013-09-12 11:48:17 +04:00
using namespace std;
namespace Moses
{
ExternalFeatureState::ExternalFeatureState(int stateSize, void *data)
{
2013-09-27 12:35:24 +04:00
m_stateSize = stateSize;
m_data = malloc(stateSize);
memcpy(m_data, data, stateSize);
2013-09-12 11:48:17 +04:00
}
void ExternalFeature::Load()
{
string nparam = "testing";
if (m_path.size() < 1) {
2013-09-27 12:35:24 +04:00
cerr << "External requires a path to a dynamic library!\n";
abort();
2013-09-12 11:48:17 +04:00
}
lib_handle = dlopen(m_path.c_str(), RTLD_LAZY);
if (!lib_handle) {
2013-09-27 12:35:24 +04:00
cerr << "dlopen reports: " << dlerror() << endl;
cerr << "Did you provide a full path to the dynamic library?\n";
abort();
2013-09-12 11:48:17 +04:00
}
CdecFF* (*fn)(const string&) =
2013-09-27 12:35:24 +04:00
(CdecFF* (*)(const string&))(dlsym(lib_handle, "create_ff"));
2013-09-12 11:48:17 +04:00
if (!fn) {
2013-09-27 12:35:24 +04:00
cerr << "dlsym reports: " << dlerror() << endl;
abort();
2013-09-12 11:48:17 +04:00
}
ff_ext = (*fn)(nparam);
m_stateSize = ff_ext->StateSize();
}
2013-09-27 12:35:24 +04:00
ExternalFeature::~ExternalFeature()
{
2013-09-12 11:48:17 +04:00
delete ff_ext;
dlclose(lib_handle);
}
void ExternalFeature::SetParameter(const std::string& key, const std::string& value)
{
if (key == "path") {
2013-09-27 12:35:24 +04:00
m_path = value;
} else {
2013-09-12 11:48:17 +04:00
StatefulFeatureFunction::SetParameter(key, value);
}
}
FFState* ExternalFeature::Evaluate(
const Hypothesis& cur_hypo,
const FFState* prev_state,
ScoreComponentCollection* accumulator) const
{
2013-09-27 12:35:24 +04:00
return new ExternalFeatureState(m_stateSize);
2013-09-12 11:48:17 +04:00
}
FFState* ExternalFeature::EvaluateChart(
const ChartHypothesis& /* cur_hypo */,
int /* featureID - used to index the state in the previous hypotheses */,
ScoreComponentCollection* accumulator) const
{
2013-09-27 12:35:24 +04:00
return new ExternalFeatureState(m_stateSize);
2013-09-12 11:48:17 +04:00
}
}