1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LIBTEXTCLASSIFIER_COMMON_FEATURE_DESCRIPTORS_H_ 18 #define LIBTEXTCLASSIFIER_COMMON_FEATURE_DESCRIPTORS_H_ 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "util/base/integral_types.h" 25 #include "util/base/logging.h" 26 #include "util/base/macros.h" 27 28 namespace libtextclassifier { 29 namespace nlp_core { 30 31 // Named feature parameter. 32 class Parameter { 33 public: Parameter()34 Parameter() {} 35 set_name(const std::string & name)36 void set_name(const std::string &name) { name_ = name; } name()37 const std::string &name() const { return name_; } 38 set_value(const std::string & value)39 void set_value(const std::string &value) { value_ = value; } value()40 const std::string &value() const { return value_; } 41 42 private: 43 std::string name_; 44 std::string value_; 45 }; 46 47 // Descriptor for a feature function. Used to store the results of parsing one 48 // feature function. 49 class FeatureFunctionDescriptor { 50 public: FeatureFunctionDescriptor()51 FeatureFunctionDescriptor() {} 52 53 // Accessors for the feature function type. The function type is the string 54 // that the feature extractor code is registered under. set_type(const std::string & type)55 void set_type(const std::string &type) { type_ = type; } has_type()56 bool has_type() const { return !type_.empty(); } type()57 const std::string &type() const { return type_; } 58 59 // Accessors for the feature function name. The function name (if available) 60 // is used for some log messages. Otherwise, a more precise, but also more 61 // verbose name based on the feature specification is used. set_name(const std::string & name)62 void set_name(const std::string &name) { name_ = name; } has_name()63 bool has_name() const { return !name_.empty(); } name()64 const std::string &name() { return name_; } 65 66 // Accessors for the default (name-less) parameter. set_argument(int32 argument)67 void set_argument(int32 argument) { argument_ = argument; } has_argument()68 bool has_argument() const { 69 // If argument has not been specified, clients should treat it as 0. This 70 // makes the test below correct, without having a separate has_argument_ 71 // bool field. 72 return argument_ != 0; 73 } argument()74 int32 argument() const { return argument_; } 75 76 // Accessors for the named parameters. add_parameter()77 Parameter *add_parameter() { 78 parameters_.emplace_back(); 79 return &(parameters_.back()); 80 } parameter_size()81 int parameter_size() const { return parameters_.size(); } parameter(int i)82 const Parameter ¶meter(int i) const { 83 TC_DCHECK((i >= 0) && (i < parameter_size())); 84 return parameters_[i]; 85 } 86 87 // Accessors for the sub (i.e., nested) features. Nested features: as in 88 // offset(1).label. add_feature()89 FeatureFunctionDescriptor *add_feature() { 90 sub_features_.emplace_back(new FeatureFunctionDescriptor()); 91 return sub_features_.back().get(); 92 } feature_size()93 int feature_size() const { return sub_features_.size(); } feature(int i)94 const FeatureFunctionDescriptor &feature(int i) const { 95 TC_DCHECK((i >= 0) && (i < feature_size())); 96 return *(sub_features_[i].get()); 97 } mutable_feature(int i)98 FeatureFunctionDescriptor *mutable_feature(int i) { 99 TC_DCHECK((i >= 0) && (i < feature_size())); 100 return sub_features_[i].get(); 101 } 102 103 private: 104 // See comments for set_type(). 105 std::string type_; 106 107 // See comments for set_name(). 108 std::string name_; 109 110 // See comments for set_argument(). 111 int32 argument_ = 0; 112 113 // See comemnts for add_parameter(). 114 std::vector<Parameter> parameters_; 115 116 // See comments for add_feature(). 117 std::vector<std::unique_ptr<FeatureFunctionDescriptor>> sub_features_; 118 119 TC_DISALLOW_COPY_AND_ASSIGN(FeatureFunctionDescriptor); 120 }; 121 122 // List of FeatureFunctionDescriptors. Used to store the result of parsing the 123 // spec for several feature functions. 124 class FeatureExtractorDescriptor { 125 public: FeatureExtractorDescriptor()126 FeatureExtractorDescriptor() {} 127 feature_size()128 int feature_size() const { return features_.size(); } 129 add_feature()130 FeatureFunctionDescriptor *add_feature() { 131 features_.emplace_back(new FeatureFunctionDescriptor()); 132 return features_.back().get(); 133 } 134 feature(int i)135 const FeatureFunctionDescriptor &feature(int i) const { 136 TC_DCHECK((i >= 0) && (i < feature_size())); 137 return *(features_[i].get()); 138 } 139 mutable_feature(int i)140 FeatureFunctionDescriptor *mutable_feature(int i) { 141 TC_DCHECK((i >= 0) && (i < feature_size())); 142 return features_[i].get(); 143 } 144 145 private: 146 std::vector<std::unique_ptr<FeatureFunctionDescriptor>> features_; 147 148 TC_DISALLOW_COPY_AND_ASSIGN(FeatureExtractorDescriptor); 149 }; 150 151 } // namespace nlp_core 152 } // namespace libtextclassifier 153 154 #endif // LIBTEXTCLASSIFIER_COMMON_FEATURE_DESCRIPTORS_H_ 155