summaryrefslogtreecommitdiff
path: root/decoder/ff_wordset.h
blob: e78cd2fbdb441c3723fc579e42af78966cbaf7ff (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
#ifndef _FF_WORDSET_H_
#define _FF_WORDSET_H_

#include "ff.h"
#include "tdict.h"

#include <vector>
#include <string>
#include <iostream>
#include <fstream>

#ifndef HAVE_OLD_CPP
# include <unordered_set>
#else
# include <tr1/unordered_set>
namespace std { using std::tr1::unordered_set; }
#endif

class WordSet : public FeatureFunction {
 public:
// we depend on the order of the initializer list
// to call member constructurs in the proper order
// modify this carefully!
//
// Usage: "WordSet -v vocab.txt [--oov]"
 WordSet(const std::string& param) {
    std::string vocabFile;
    std::string featName;
    parseArgs(param, &featName, &vocabFile, &oovMode_);

    fid_ = FD::Convert(featName);

    std::cerr << "Loading vocab for " << param << " from " << vocabFile << std::endl;
    loadVocab(vocabFile, &vocab_);
  }

  ~WordSet() {
  }

 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:

  static void parseArgs(const std::string& args, std::string* featName, std::string* vocabFile, bool* oovMode);
  static void loadVocab(const std::string& vocabFile, std::unordered_set<WordID>* vocab);

  int fid_;
  bool oovMode_;
  std::unordered_set<WordID> vocab_;
};

#endif