File size: 1,753 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
74
75
76
77
78
79
80
81
82
#pragma once

#include <string>
#include <cstring>
#include <cstdlib>
#include "StatefulFeatureFunction.h"
#include "FFState.h"

namespace Moses
{
class CdecFF;

class ExternalFeatureState : public FFState
{
protected:
  int m_stateSize;
  void *m_data;
public:
  ExternalFeatureState(int stateSize)
    :m_stateSize(stateSize)
    ,m_data(NULL) {
  }
  ExternalFeatureState(int stateSize, void *data);

  ~ExternalFeatureState() {
    free(m_data);
  }

  int Compare(const FFState& other) const {
    const ExternalFeatureState &otherFF = static_cast<const ExternalFeatureState&>(other);
    int ret = memcmp(m_data, otherFF.m_data, m_stateSize);
    return ret;
  }
};

// copied from cdec
class ExternalFeature : public StatefulFeatureFunction
{
public:
  ExternalFeature(const std::string &line)
    :StatefulFeatureFunction(line) {
    ReadParameters();
  }
  ~ExternalFeature();

  void Load(AllOptions const& opts);

  bool IsUseable(const FactorMask &mask) const {
    return true;
  }

  void SetParameter(const std::string& key, const std::string& value);

  FFState* EvaluateWhenApplied(const Hypothesis& cur_hypo, const FFState* prev_state,
                               ScoreComponentCollection* accumulator) const;

  FFState* EvaluateWhenApplied(
    const ChartHypothesis& /* cur_hypo */,
    int /* featureID - used to index the state in the previous hypotheses */,
    ScoreComponentCollection* accumulator) const;

  virtual const FFState* EmptyHypothesisState(const InputType &input) const {
    return new ExternalFeatureState(m_stateSize);
  }

protected:
  std::string m_path;
  void* lib_handle;
  CdecFF *ff_ext;
  int m_stateSize;
};

class CdecFF
{
public:
  virtual ~CdecFF() {}
  virtual int StateSize() const = 0;
};

}