Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
symbol-table.H
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 //
36 
37 #ifndef RERANKER_SYMBOL_TABLE_H_
38 #define RERANKER_SYMBOL_TABLE_H_
39 
40 #include <iostream>
41 #include <string>
42 #include <unordered_map>
43 #include <vector>
44 
45 namespace reranker {
46 
47 using std::cerr;
48 using std::endl;
49 using std::ostream;
50 using std::string;
51 using std::unordered_map;
52 using std::vector;
53 
57 class Symbols {
58  public:
59  virtual ~Symbols() { }
60  typedef unordered_map<string, int>::const_iterator const_iterator;
61 
62  virtual const_iterator begin() = 0;
63  virtual const_iterator end() = 0;
64 
66  virtual size_t size() const = 0;
67 
73  virtual int GetIndex(const string &symbol) = 0;
74 
77  virtual const string &GetSymbol(int index) const = 0;
78 
79  virtual void SetIndex(const string &symbol, int index) = 0;
80 
82  virtual void Clear() = 0;
83 
90  virtual Symbols *Clone() const = 0;
91 
104  virtual ostream &Output(ostream &os) = 0;
105  protected:
106  static string null_symbol;
107 };
108 
120 class StaticSymbolTable : public Symbols {
121  public:
122  virtual const_iterator begin() {return symbols_.begin(); }
123  virtual const_iterator end() { return symbols_.end(); }
124 
126  virtual size_t size() const { return symbols_.size(); }
127 
135  virtual int GetIndex(const string &symbol);
136 
139  virtual const string &GetSymbol(int index) const;
140 
141  virtual void SetIndex(const string &symbol, int index) {
142  unordered_map<string, int>::const_iterator it = symbols_.find(symbol);
143  if (it != symbols_.end()) {
144  // TODO(dbikel): Emit warning message?
145  int index = GetIndex(symbol);
146  symbols_.erase(symbol);
147  indices_to_symbols_.erase(index);
148  }
149  symbols_[symbol] = index;
150  indices_to_symbols_[index] = symbol;
151  }
152 
154  virtual void Clear() {
155  symbols_.clear();
156  indices_to_symbols_.clear();
157  }
158 
160  virtual Symbols *Clone() const {
161  return new StaticSymbolTable();
162  }
163 
165  virtual ostream &Output(ostream &os) {
166  for (unordered_map<string, int>::const_iterator it = symbols_.begin();
167  it != symbols_.end();
168  ++it) {
169  os << it->first << "\t" << it->second << "\n";
170  }
171  os.flush();
172  return os;
173  }
174 
175  private:
176  // static data members
177  static unordered_map<string, int> symbols_;
178  static unordered_map<int, string> indices_to_symbols_;
179 };
180 
185 class LocalSymbolTable : public Symbols {
186  public:
187  virtual const_iterator begin() {return symbols_.begin(); }
188  virtual const_iterator end() { return symbols_.end(); }
189 
191  virtual size_t size() const { return symbols_.size(); }
192 
196  virtual int GetIndex(const string &symbol);
197 
199  virtual const string &GetSymbol(int index) const;
200 
201  virtual void SetIndex(const string &symbol, int index) {
202  unordered_map<string, int>::const_iterator it = symbols_.find(symbol);
203  if (it != symbols_.end()) {
204  cerr << "LocalSymbolTable::SetIndex: warning: symbol \"" << symbol
205  << "\" already has an index " << it->second << "; new index: "
206  << index << endl;
207  int index = GetIndex(symbol);
208  symbols_.erase(symbol);
209  indices_to_symbols_.erase(index);
210  }
211  symbols_[symbol] = index;
212  indices_to_symbols_[index] = symbol;
213  }
214 
216  virtual void Clear() {
217  symbols_.clear();
218  indices_to_symbols_.clear();
219  }
220 
222  virtual Symbols *Clone() const {
223  return new LocalSymbolTable(*this);
224  }
225 
227  virtual ostream &Output(ostream &os) {
228  for (unordered_map<string, int>::const_iterator it = symbols_.begin();
229  it != symbols_.end();
230  ++it) {
231  os << it->first << "\t" << it->second << "\n";
232  }
233  os.flush();
234  return os;
235  }
236 
237  private:
238  // data members
239  unordered_map<string, int> symbols_;
240  unordered_map<int, string> indices_to_symbols_;
241 };
242 
243 } // namespace reranker
244 
245 #endif
virtual const_iterator begin()
Definition: symbol-table.H:122
virtual size_t size() const =0
Returns the number of symbols in this table.
virtual Symbols * Clone() const
Creates a newly-constructed clone of this Symbols instance that has the same runtime type...
Definition: symbol-table.H:160
virtual Symbols * Clone() const
Creates a newly-constructed clone of this Symbols instance that has the same runtime type...
Definition: symbol-table.H:222
virtual Symbols * Clone() const =0
Creates a newly-constructed clone of this Symbols instance that has the same runtime type...
virtual void SetIndex(const string &symbol, int index)
Definition: symbol-table.H:141
virtual int GetIndex(const string &symbol)
Returns the unique index for the specified symbol.
Definition: symbol-table.C:74
virtual void Clear()=0
Clears all symbols from this symbol table.
virtual ostream & Output(ostream &os)
Outputs the symbol table to the specified output stream in a simple format, one symbol-to-index mappi...
Definition: symbol-table.H:227
virtual const_iterator end()=0
virtual const_iterator end()
Definition: symbol-table.H:188
A symbol table that stores the mapping from symbols to int’s and vice versa in local (non-static) dat...
Definition: symbol-table.H:185
virtual void Clear()
Clears all symbols from this symbol table.
Definition: symbol-table.H:154
virtual const_iterator end()
Definition: symbol-table.H:123
virtual void SetIndex(const string &symbol, int index)
Definition: symbol-table.H:201
virtual const string & GetSymbol(int index) const =0
Returns the unique symbol for the specified index, or the empty string if the specified index does no...
virtual void Clear()
Clears all symbols from this symbol table.
Definition: symbol-table.H:216
A converter from symbols (strings) to int indices.
Definition: symbol-table.H:120
virtual const_iterator begin()
Definition: symbol-table.H:187
unordered_map< string, int >::const_iterator const_iterator
Definition: symbol-table.H:60
static string null_symbol
Definition: symbol-table.H:106
virtual int GetIndex(const string &symbol)=0
Converts the specified symbol to a unique integer.
virtual size_t size() const
Returns the number of symbols in this table.
Definition: symbol-table.H:126
virtual ostream & Output(ostream &os)=0
Outputs the symbol table to the specified output stream in a simple format, one symbol-to-index mappi...
An interface specifying a converter from symbols (strings) to int indices.
Definition: symbol-table.H:57
virtual ostream & Output(ostream &os)
Outputs the symbol table to the specified output stream in a simple format, one symbol-to-index mappi...
Definition: symbol-table.H:165
virtual const string & GetSymbol(int index) const
Returns the unique symbol for the specified index, or the empty string if the specified index does no...
Definition: symbol-table.C:67
virtual ~Symbols()
Definition: symbol-table.H:59
virtual const_iterator begin()=0
virtual int GetIndex(const string &symbol)
Converts the specified symbol to a unique integer.
Definition: symbol-table.C:54
virtual void SetIndex(const string &symbol, int index)=0
virtual const string & GetSymbol(int index) const
Returns the unique symbol for the specified index, or the empty string if the specified index does no...
Definition: symbol-table.C:87
virtual size_t size() const
Returns the number of symbols in this table.
Definition: symbol-table.H:191