All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
intfeaturedist.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 // Author: rays@google.com (Ray Smith)
4 // File: intfeaturedist.h
5 // Description: Fast set-difference-based feature distance calculator.
6 // Created: Thu Sep 01 12:14:30 PDT 2011
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 #ifndef TESSERACT_CLASSIFY_INTFEATUREDIST_H_
21 #define TESSERACT_CLASSIFY_INTFEATUREDIST_H_
22 
23 #include "genericvector.h"
24 
25 namespace tesseract {
26 
27 class IntFeatureMap;
28 
29 // Feature distance calculator designed to provide a fast distance calculation
30 // based on set difference between a given feature set and many other feature
31 // sets in turn.
32 // Representation of a feature set as an array of bools that are sparsely
33 // true, and companion arrays that allow fast feature set distance
34 // calculations with allowance of offsets in position.
35 // Init is expensive, so for greatest efficiency, to re-initialize for a new
36 // feature set, use Set(..., false) on the SAME feature set as was used to
37 // setup with Set(..., true), to return to its initialized state before
38 // reuse with Set(..., true) on a new feature set.
40  public:
43 
44  // Initialize the bool array to the given size of feature space.
45  // The feature_map is just borrowed, and must exist for the entire
46  // lifetime of the IntFeatureDist.
47  void Init(const IntFeatureMap* feature_map);
48 
49  // Setup the map for the given indexed_features that have been indexed by
50  // feature_map. After use, use Set(..., false) to reset to the initial state
51  // as this is faster than calling Init for sparse spaces.
52  void Set(const GenericVector<int>& indexed_features,
53  int canonical_count, bool value);
54 
55  // Compute the distance between the given feature vector and the last
56  // Set feature vector.
57  double FeatureDistance(const GenericVector<int>& features) const;
58  double DebugFeatureDistance(const GenericVector<int>& features) const;
59 
60  private:
61  // Clear all data.
62  void Clear();
63 
64  // Size of the indexed feature space.
65  int size_;
66  // Total weight of features currently stored in the maps.
67  double total_feature_weight_;
68  // Pointer to IntFeatureMap given at Init to find offset features.
69  const IntFeatureMap* feature_map_;
70  // Array of bools indicating presence of a feature.
71  bool* features_;
72  // Array indicating the presence of a feature offset by one unit.
73  bool* features_delta_one_;
74  // Array indicating the presence of a feature offset by two units.
75  bool* features_delta_two_;
76 };
77 
78 } // namespace tesseract
79 
80 #endif // TESSERACT_CLASSIFY_INTFEATUREDIST_H_
void Init(const IntFeatureMap *feature_map)
double FeatureDistance(const GenericVector< int > &features) const
double DebugFeatureDistance(const GenericVector< int > &features) const
void Set(const GenericVector< int > &indexed_features, int canonical_count, bool value)