summaryrefslogtreecommitdiff
path: root/extools/filter_grammar.cc
blob: de052e49cea96f03c838b346eb2827b0bf649827 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Build suffix tree representation of a data set for grammar filtering
 * ./filter_grammar <test set> < unfiltered.grammar > filter.grammar
 *
 */
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
#include <cstdlib>
#include <fstream>
#include <tr1/unordered_map>

#include "filelib.h"
#include "sentence_pair.h"
#include "suffix_tree.h"
#include "extract.h"
#include "fdict.h"
#include "tdict.h"

#include <boost/functional/hash.hpp>
#include <boost/program_options.hpp>
#include <boost/program_options/variables_map.hpp>


using namespace std;
using namespace std::tr1;

static const size_t MAX_LINE_LENGTH = 64000000;

typedef unordered_map<vector<WordID>, RuleStatistics, boost::hash<vector<WordID> > > ID2RuleStatistics;


namespace {
  inline bool IsWhitespace(char c) { return c == ' ' || c == '\t'; }
  inline bool IsBracket(char c){return c == '[' || c == ']';}
  inline void SkipWhitespace(const char* buf, int* ptr) {
    while (buf[*ptr] && IsWhitespace(buf[*ptr])) { ++(*ptr); }
  }
}



int ReadPhraseUntilDividerOrEnd(const char* buf, const int sstart, const int end, vector<WordID>* p) {
  static const WordID kDIV = TD::Convert("|||");

  int ptr = sstart;
  while(ptr < end) {
    while(ptr < end && IsWhitespace(buf[ptr])) { ++ptr; }
    int start = ptr;
    while(ptr < end && !IsWhitespace(buf[ptr])) { ++ptr; }
    if (ptr == start) {cerr << "Warning! empty token.\n"; return ptr; }
    //look in the buffer and see if its a nonterminal marker before integerizing it to wordID-anything with [...] or |||

    const WordID w = TD::Convert(string(buf, start, ptr - start));

    if((IsBracket(buf[start]) and IsBracket(buf[ptr-1])) or( w == kDIV))
      p->push_back(-1);
    else {
        if (w == kDIV) return ptr;
        p->push_back(w);
    }
  }
  return ptr;
}



void ParseLine(const char* buf, vector<WordID>* cur_key, ID2RuleStatistics* counts) {
  static const WordID kDIV = TD::Convert("|||");
  counts->clear();
  int ptr = 0;
  while(buf[ptr] != 0 && buf[ptr] != '\t') { ++ptr; }
  if (buf[ptr] != '\t') {
    cerr << "Missing tab separator between key and value!\n INPUT=" << buf << endl;
    exit(1);
  }
  cur_key->clear();
  // key is: "[X] ||| word word word"
  int tmpp = ReadPhraseUntilDividerOrEnd(buf, 0, ptr, cur_key);
  cur_key->push_back(kDIV);
  ReadPhraseUntilDividerOrEnd(buf, tmpp, ptr, cur_key);
  ++ptr;
  int start = ptr;
  int end = ptr;
  int state = 0; // 0=reading label, 1=reading count
  vector<WordID> name;
  while(buf[ptr] != 0) {
    while(buf[ptr] != 0 && buf[ptr] != '|') { ++ptr; }
    if (buf[ptr] == '|') {
      ++ptr;
      if (buf[ptr] == '|') {
        ++ptr;
        if (buf[ptr] == '|') {
          ++ptr;
          end = ptr - 3;
          while (end > start && IsWhitespace(buf[end-1])) { --end; }
          if (start == end) {
            cerr << "Got empty token!\n  LINE=" << buf << endl;
            exit(1);
          }
          switch (state) {
            case 0: ++state; name.clear(); ReadPhraseUntilDividerOrEnd(buf, start, end, &name); break;
            case 1: --state; (*counts)[name].ParseRuleStatistics(buf, start, end); break;
            default: cerr << "Can't happen\n"; abort();
          }
          SkipWhitespace(buf, &ptr);
          start = ptr;
        }
      }
    }
  }
  end=ptr;
  while (end > start && IsWhitespace(buf[end-1])) { --end; }
  if (end > start) {
    switch (state) {
      case 0: ++state; name.clear(); ReadPhraseUntilDividerOrEnd(buf, start, end, &name); break;
      case 1: --state; (*counts)[name].ParseRuleStatistics(buf, start, end); break;
      default: cerr << "Can't happen\n"; abort();
    }
  }
}







int main(int argc, char* argv[]){
  if (argc != 2) {
    cerr << "Usage: " << argv[0] << " testset.txt < unfiltered.grammar\n";
    return 1;
  }

  assert(FileExists(argv[1]));
  ReadFile rfts(argv[1]);
  istream& testSet = *rfts.stream();
  ofstream filter_grammar_;
  bool DEBUG = false;

  AnnotatedParallelSentence sent;
  char* buf = new char[MAX_LINE_LENGTH];
  cerr << "Build suffix tree from test set in " << argv[1] << endl;
  //root of the suffix tree
  Node<int> root;
  int line=0;

  /* process the data set to build suffix tree
   */
  while(!testSet.eof()) {
    ++line;
    testSet.getline(buf, MAX_LINE_LENGTH);
    if (buf[0] == 0) continue;

    //hack to read in the test set using the alignedparallelsentence methods
    strcat(buf," ||| fake ||| 0-0");   
    sent.ParseInputLine(buf);

    if (DEBUG)cerr << line << "||| " << buf << " -- " << sent.f_len << endl;

    //add each successive suffix to the tree
    for(int i =0;i<sent.f_len;i++)
        root.InsertPath(sent.f, i, sent.f_len - 1);
    if(DEBUG)cerr<<endl;

  }  

  cerr << "Filtering grammar..." << endl;
  //process the unfiltered, unscored grammar    

  ID2RuleStatistics cur_counts;
  vector<WordID> cur_key;
  line = 0;

  while(cin) {
    ++line;
    cin.getline(buf, MAX_LINE_LENGTH);
    if (buf[0] == 0) continue;
    ParseLine(buf, &cur_key, &cur_counts);
    const Node<int>* curnode = &root;        
    for(int i=0;i<cur_key.size() - 1; i++) {
      if (DEBUG) cerr << line << " " << cur_key[i] << " ::: ";
      if (cur_key[i] == -1) { // non-terminal
        curnode = &root;
      } else if (curnode) {
        curnode = curnode->Extend(cur_key[i]);
        if (!curnode) break;
      }
    }
    if(curnode) cout << buf << endl;
  }

  return 0;
}