Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
model-merge-reducer-main.C
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
39 
40 #include <cstdio>
41 #include <iostream>
42 #include <string>
43 #include <unistd.h>
44 #include "../utils/kdebug.h"
45 #include "model-merge-reducer.H"
46 
47 using namespace std;
48 using reranker::Reducer;
52 
53 int main(int argc, char* argv[]) {
54  int option_char;
55  bool uniform_mix = false;
56  int mix_denominator = 1.0;
57  bool reduce_symbols = false;
58 
59  // Invokes member function `int operator ()(void);'
60  while ((option_char = getopt(argc, argv, "ud:S")) != EOF) {
61  switch (option_char) {
62  case 'S':
63  reduce_symbols = true;
64  case 'u':
65  uniform_mix = true;
66  break;
67  case 'd':
68  mix_denominator = atoi(optarg);
69  break;
70  case '?':
71  cerr << "usage: " << argv[0] << "[-K] [-u] [-d denom]" << endl;
72  cerr << "-u - mix the features uniformly (overrides -d)" << endl;
73  cerr << "-d - normalize mixture with this value" << endl;
74  cerr << "-S - Run this in symbol reducer mode (unique)" << endl;
75  return -1;
76  break;
77  }
78  }
79 
80  FeatureReducer feat_reducer(uniform_mix, mix_denominator);
81  ModelInfoReducer model_reducer;
82  SymbolReducer sym_reducer;
83  string empty_string;
84 
85  // Process each of the input records. This reducer assumes that the input is
86  // <FeatureId, encoded FeatureMessage> pair per line in the following format:
87  // FeatureString \t EncodedMsg \n
88  while (cin) {
89  // Process input.
90  string input_data;
91  getline(cin, input_data);
92  if (input_data.empty()) {
93  break;
94  }
95  if (reduce_symbols) {
96  sym_reducer.Reduce(input_data, empty_string);
97  } else {
98  int delim_pos = input_data.find('\t');
99  string feat_id = input_data.substr(0, delim_pos);
100  string value = input_data.substr(delim_pos + 1);
101 
102  Reducer* reducer;
103  if (feat_id.compare(ModelInfoReducer::kModelMessageFeatureName) == 0) {
104  reducer = &model_reducer;
105  } else {
106  reducer = &feat_reducer;
107  }
108  reducer->Reduce(feat_id, value);
109  }
110  }
111  if (!reduce_symbols) {
112  feat_reducer.Flush();
113  model_reducer.Flush();
114  } else {
115  sym_reducer.Flush();
116  }
117  return 0;
118 }
virtual int Reduce(const string &key, const string &value)
A reducer class which processes FeatureMessage proto buffers.
int main(int argc, char *argv[])
Reducer classes for trainer.
virtual int Reduce(const string &key, const string &value)=0
Abstract base-class for a streaming reducer.
A reducer class which processes SymbolMessage messages and returns a set of unique them into a single...
A reducer class which processes ModelMessage protocol messages and merges them into a single message...