summaryrefslogtreecommitdiff
path: root/decoder/ff_ruleshape.h
blob: 66914f5d860dd5c24a0f50d7974c815b4f50264a (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
#ifndef FF_RULESHAPE_H_
#define FF_RULESHAPE_H_

#include <vector>
#include <map>
#include "ff.h"

class RuleShapeFeatures : public FeatureFunction {
 public:
  RuleShapeFeatures(const std::string& param);
 protected:
  virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta,
                                     const HG::Edge& edge,
                                     const std::vector<const void*>& ant_contexts,
                                     SparseVector<double>* features,
                                     SparseVector<double>* estimated_features,
                                     void* context) const;
 private:
  struct Node {
    int fid_;
    Node() : fid_(-1) {}
    std::map<bool, Node> next_;
  };
  Node fidtree_;
  static const Node* Advance(const Node* cur, bool val) {
    std::map<bool, Node>::const_iterator it = cur->next_.find(val);
    if (it == cur->next_.end()) return NULL;
    return &it->second;
  }
};

class RuleShapeFeatures2 : public FeatureFunction {
 public:
  ~RuleShapeFeatures2();
  RuleShapeFeatures2(const std::string& param);
 protected:
  virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta,
                                     const HG::Edge& edge,
                                     const std::vector<const void*>& ant_contexts,
                                     SparseVector<double>* features,
                                     SparseVector<double>* estimated_features,
                                     void* context) const;
 private:
  struct Node {
    int fid_;
    Node() : fid_() {}
    std::map<WordID, Node> next_;
  };
  mutable Node fidtree_;

  inline WordID MapE(WordID w) const {
    if (w <= 0) return kNT;
    unsigned res = 0;
    if (w < e2class_.size()) res = e2class_[w];
    if (!res) res = kUNK;
    return res;
  }

  inline WordID MapF(WordID w) const {
    if (w <= 0) return kNT;
    unsigned res = 0;
    if (w < f2class_.size()) res = f2class_[w];
    if (!res) res = kUNK;
    return res;
  }

  // prfx_size=0 => use full word classes otherwise truncate to specified length
  void LoadWordClasses(const std::string& fname, unsigned pfxsize, std::vector<WordID>* pv);
  const WordID kNT;
  const WordID kUNK;
  std::vector<WordID> e2class_;
  std::vector<WordID> f2class_;
  bool has_src_;
  bool has_trg_;
};

#endif