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-combine-symbols.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 //
35 
36 #include <cstdio>
37 #include <iostream>
38 #include <string>
39 #include <memory>
40 #include <unistd.h>
41 #include "../proto/dataio.h"
42 #include "../proto/model.pb.h"
43 #include "../utils/kdebug.h"
44 
45 using namespace std;
46 using confusion_learning::SymbolMessage;;
47 
48 
49 int main(int argc, char* argv[]) {
50  int option_char;
51  bool output_compressed = true;
52  string output_name;
53 
54  // Invokes member function `int operator ()(void);'
55  while ((option_char = getopt(argc, argv, "Uo:")) != EOF) {
56  switch (option_char) {
57  case 'U':
58  output_compressed = false;
59  break;
60  case 'o':
61  output_name = optarg;
62  break;
63  case '?':
64  cerr << "usage: " << argv[0]
65  << " [-U] [-o <output file>]"
66  << endl;
67  cerr << "-U - output to uncompressed file" << endl;
68  cerr << "-o - output to filename (otherwise to uncompressed stdout" << endl;
69  return -1;
70  break;
71  }
72  }
73 
74  // Process each of the input records. This reducer assumes that the input is
75  // a stream of feature strings;
76  ConfusionProtoIO reader;
77  ConfusionProtoIO* writer;
78  if (output_name.empty()) {
79  writer = new ConfusionProtoIO("", ConfusionProtoIO::WRITESTD, false, true);
80  } else {
81  writer = new ConfusionProtoIO(output_name, ConfusionProtoIO::WRITE,
82  output_compressed, true);
83  }
84  int feature_id = 0;
85  SymbolMessage sym_msg;
86  while (cin) {
87  // Process input.
88  string input_data;
89  getline(cin, input_data);
90  if (input_data.empty()) {
91  break;
92  }
93  size_t delim_pos = input_data.find('\t');
94  if (delim_pos != string::npos) {
95  input_data.erase(delim_pos);
96  }
97  sym_msg.set_symbol(input_data);
98  sym_msg.set_index(feature_id);
99  feature_id++;
100  writer->Write(sym_msg);
101  }
102  cerr << "Wrote " << feature_id << " feature messages to file: ";
103  if (output_name.empty()) {
104  cerr << " STDOUT " << endl;
105  } else {
106  cerr << output_name.c_str() << " " << endl;
107  }
108  delete writer;
109  return 0;
110 }
int main(int argc, char *argv[])