Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string-canonicalizer.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 //
35 
36 #ifndef RERANKER_STRING_CANONICALIZER_H_
37 #define RERANKER_STRING_CANONICALIZER_H_
38 
39 #include <iostream>
40 #include <string>
41 #include <unordered_set>
42 
43 #define STR_CANON_DEBUG 1
44 
45 namespace reranker {
46 
47 using std::cerr;
48 using std::endl;
49 using std::string;
50 using std::unordered_set;
51 
57  public:
59  static const string &Get(const string &s) {
60  unordered_set<string>::const_iterator it = canonical_.find(s);
61  if (it == canonical_.end()) {
62  canonical_.insert(s);
63  return s;
64  } else {
65  return *it;
66  }
67  }
68 
70  static void Clear() {
71  if (STR_CANON_DEBUG) {
72  cerr << "StringCanonicalizer: clearing out canonical map of size "
73  << canonical_.size() << endl;
74  }
75  canonical_.clear();
76  }
77  private:
78  // data members
79  static unordered_set<string> canonical_;
80 };
81 
82 } // namespace reranker
83 
84 #endif
#define STR_CANON_DEBUG
static const string & Get(const string &s)
Returns the canonical string for the specified string.
A class that stores a canonical version of string objects in a static data structure.
static void Clear()
Clears the strings from the internal data structure.