/* Copyright 2016 Google Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ // Common feature types for parser components. #ifndef FEATURE_TYPES_H_ #define FEATURE_TYPES_H_ #include <algorithm> #include <map> #include <string> #include <utility> #include "base.h" namespace chrome_lang_id { // TODO(djweiss) Clean this up as well. // Use the same type for feature values as is used for predicated. Predicate; FeatureValue; // Each feature value in a feature vector has a feature type. The feature type // is used for converting feature type and value pairs to predicate values. The // feature type can also return names for feature values and calculate the size // of the feature value domain. The FeatureType class is abstract and must be // specialized for the concrete feature types. class FeatureType { … }; // Templated generic resource based feature type. This feature type delegates // look up of feature value names to an unknown resource class, which is not // owned. Optionally, this type can also store a mapping of extra values which // are not in the resource. // // Note: this class assumes that Resource->GetFeatureValueName() will return // successfully for values ONLY in the range [0, Resource->NumValues()) Any // feature value not in the extra value map and not in the above range of // Resource will result in a ERROR and return of "<INVALID>". template <class Resource> class ResourceBasedFeatureType : public FeatureType { … }; // Feature type that is defined using an explicit map from FeatureValue to // string values. This can reduce some of the boilerplate when defining // features that generate enum values. Example usage: // // class BeverageSizeFeature : public FeatureFunction<Beverage> // enum FeatureValue { SMALL, MEDIUM, LARGE }; // values for this feature // void Init(TaskContext *context) override { // set_feature_type(new EnumFeatureType("beverage_size", // {{SMALL, "SMALL"}, {MEDIUM, "MEDIUM"}, {LARGE, "LARGE"}}); // } // [...] // }; class EnumFeatureType : public FeatureType { … }; } // namespace chrome_lang_id #endif // FEATURE_TYPES_H_