summaryrefslogtreecommitdiff
path: root/decoder/ff_wordset.h
blob: 256d54bb52f3c67774997bb8f34066bc6bd7646f (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef _FF_WORDSET_H_
#define _FF_WORDSET_H_

#include "ff.h"

#include <boost/unordered/unordered_set.hpp>
#include <boost/algorithm/string.hpp>

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

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_);
  }


 protected:
  virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta,
                                     const Hypergraph::Edge& edge,
                                     const std::vector<const void*>& ant_contexts,
                                     SparseVector<double>* features,
                                     SparseVector<double>* estimated_features,
                                     void* context) const;
 private:

  static void loadVocab(const std::string& vocabFile, boost::unordered_set<WordID>* vocab) {

      std::ifstream file;
      std::string line;

      file.open(vocabFile.c_str(), std::fstream::in);
      if (file.is_open()) {
	unsigned lineNum = 0;
	while (!file.eof()) {
	  ++lineNum;
	  getline(file, line);
	  boost::trim(line);
	  if(line.empty()) {
	    continue;
	  }
	  
	  WordID vocabId = TD::Convert(line);
	  vocab->insert(vocabId);
	}
	file.close();
      } else {
	std::cerr << "Unable to open file: " << vocabFile; 
	exit(1);
      }
  }

  static void parseArgs(const std::string& args, std::string* featName, std::string* vocabFile, bool* oovMode) {

    std::vector<std::string> toks(10);
    boost::split(toks, args, boost::is_any_of(" "));

    *oovMode = false;

    // skip initial feature name
    for(std::vector<std::string>::const_iterator it = toks.begin(); it != toks.end(); ++it) {
      if(*it == "-v") {
	*vocabFile = *++it; // copy

      } else if(*it == "-N") {
	*featName = *++it;

      } else if(*it == "--oov") {
	*oovMode = true;

      } else {
	std::cerr << "Unrecognized argument: " << *it << std::endl;
	exit(1);
      }
    }

    if(*featName == "") {
      std::cerr << "featName (-N) not specified for WordSet" << std::endl;
      exit(1);
    }
    if(*vocabFile == "") {
      std::cerr << "vocabFile (-v) not specified for WordSet" << std::endl;
      exit(1);
    }
  }

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

#endif