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.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 //
36 
37 #include <string>
38 #include <unordered_map>
39 
40 #include "symbol-table.H"
41 
42 namespace reranker {
43 
44 using std::string;
45 using std::unordered_map;
46 
47 
48 string Symbols::null_symbol("");
49 
50 unordered_map<string, int> StaticSymbolTable::symbols_;
51 unordered_map<int, string> StaticSymbolTable::indices_to_symbols_;
52 
53 int
54 StaticSymbolTable::GetIndex(const string &symbol) {
55  unordered_map<string, int>::iterator it = symbols_.find(symbol);
56  if (it == symbols_.end()) {
57  size_t new_index = symbols_.size();
58  symbols_[symbol] = new_index;
59  indices_to_symbols_[new_index] = symbol;
60  return new_index;
61  } else {
62  return it->second;
63  }
64 }
65 
66 const string &
67 StaticSymbolTable::GetSymbol(int index) const {
68  unordered_map<int, string>::const_iterator it =
69  indices_to_symbols_.find(index);
70  return it == indices_to_symbols_.end() ? Symbols::null_symbol : it->second;
71 }
72 
73 int
74 LocalSymbolTable::GetIndex(const string &symbol) {
75  unordered_map<string, int>::iterator it = symbols_.find(symbol);
76  if (it == symbols_.end()) {
77  size_t new_index = symbols_.size();
78  symbols_[symbol] = new_index;
79  indices_to_symbols_[new_index] = symbol;
80  return new_index;
81  } else {
82  return it->second;
83  }
84 }
85 
86 const string &
87 LocalSymbolTable::GetSymbol(int index) const {
88  unordered_map<int, string>::const_iterator it =
89  indices_to_symbols_.find(index);
90  return it == indices_to_symbols_.end() ? Symbols::null_symbol : it->second;
91 }
92 
93 } // namespace reranker
virtual int GetIndex(const string &symbol)
Returns the unique index for the specified symbol.
Definition: symbol-table.C:74
Provides the reranker::Symbols interface as well as the reranker::StaticSymbolTable implementation...
static string null_symbol
Definition: symbol-table.H:106
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 int GetIndex(const string &symbol)
Converts the specified symbol to a unique integer.
Definition: symbol-table.C:54
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