1 /* 2 * Copyright (C) 2018 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 NLP_SAFT_COMPONENTS_COMMON_MOBILE_FEL_FEATURE_DESCRIPTORS_H_ 18 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_FEL_FEATURE_DESCRIPTORS_H_ 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "lang_id/common/lite_base/integral-types.h" 25 #include "lang_id/common/lite_base/logging.h" 26 #include "lang_id/common/lite_base/macros.h" 27 #include "absl/strings/string_view.h" 28 29 namespace libtextclassifier3 { 30 namespace mobile { 31 32 // Named feature parameter. 33 class Parameter { 34 public: Parameter()35 Parameter() {} 36 set_name(absl::string_view name)37 void set_name(absl::string_view name) { name_ = std::string(name); } name()38 const std::string &name() const { return name_; } 39 set_value(absl::string_view value)40 void set_value(absl::string_view value) { value_ = std::string(value); } value()41 const std::string &value() const { return value_; } 42 43 private: 44 std::string name_; 45 std::string value_; 46 }; 47 48 // Descriptor for a feature function. Used to store the results of parsing one 49 // feature function. 50 class FeatureFunctionDescriptor { 51 public: FeatureFunctionDescriptor()52 FeatureFunctionDescriptor() {} 53 54 // Accessors for the feature function type. The function type is the string 55 // that the feature extractor code is registered under. set_type(absl::string_view type)56 void set_type(absl::string_view type) { type_ = std::string(type); } 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(absl::string_view name)62 void set_name(absl::string_view name) { name_ = std::string(name); } name()63 const std::string &name() const { return name_; } 64 65 // Accessors for the default (name-less) parameter. set_argument(int32 argument)66 void set_argument(int32 argument) { argument_ = argument; } has_argument()67 bool has_argument() const { 68 // If argument has not been specified, clients should treat it as 0. This 69 // makes the test below correct, without having a separate has_argument_ 70 // bool field. 71 return argument_ != 0; 72 } argument()73 int32 argument() const { return argument_; } 74 75 // Accessors for the named parameters. add_parameter()76 Parameter *add_parameter() { 77 parameters_.emplace_back(); 78 return &(parameters_.back()); 79 } parameter_size()80 int parameter_size() const { return parameters_.size(); } parameter(int i)81 const Parameter ¶meter(int i) const { 82 SAFTM_DCHECK((i >= 0) && (i < parameter_size())); 83 return parameters_[i]; 84 } 85 86 // Accessors for the sub (i.e., nested) features. Nested features: as in 87 // offset(1).label. add_feature()88 FeatureFunctionDescriptor *add_feature() { 89 sub_features_.emplace_back(new FeatureFunctionDescriptor()); 90 return sub_features_.back().get(); 91 } feature_size()92 int feature_size() const { return sub_features_.size(); } feature(int i)93 const FeatureFunctionDescriptor &feature(int i) const { 94 SAFTM_DCHECK((i >= 0) && (i < feature_size())); 95 return *(sub_features_[i].get()); 96 } 97 98 // Returns human-readable representation of this FeatureFunctionDescriptor. 99 std::string DebugString() const; 100 101 private: 102 // See comments for set_type(). 103 std::string type_; 104 105 // See comments for set_name(). 106 std::string name_; 107 108 // See comments for set_argument(). 109 int32 argument_ = 0; 110 111 // See comemnts for add_parameter(). 112 std::vector<Parameter> parameters_; 113 114 // See comments for add_feature(). 115 std::vector<std::unique_ptr<FeatureFunctionDescriptor>> sub_features_; 116 117 SAFTM_DISALLOW_COPY_AND_ASSIGN(FeatureFunctionDescriptor); 118 }; 119 120 // List of FeatureFunctionDescriptors. Used to store the result of parsing the 121 // spec for several feature functions. 122 class FeatureExtractorDescriptor { 123 public: FeatureExtractorDescriptor()124 FeatureExtractorDescriptor() {} 125 feature_size()126 int feature_size() const { return features_.size(); } 127 add_feature()128 FeatureFunctionDescriptor *add_feature() { 129 features_.emplace_back(new FeatureFunctionDescriptor()); 130 return features_.back().get(); 131 } 132 feature(int i)133 const FeatureFunctionDescriptor &feature(int i) const { 134 SAFTM_DCHECK((i >= 0) && (i < feature_size())); 135 return *(features_[i].get()); 136 } 137 138 // Returns human-readable representation of this FeatureExtractorDescriptor. 139 std::string DebugString() const; 140 141 private: 142 std::vector<std::unique_ptr<FeatureFunctionDescriptor>> features_; 143 144 SAFTM_DISALLOW_COPY_AND_ASSIGN(FeatureExtractorDescriptor); 145 }; 146 147 // Appends to |*output| the FEL representation of the top-level feature from 148 // |function|, without diving into the nested features. 149 void ToFELFunction(const FeatureFunctionDescriptor &function, 150 std::string *output); 151 152 // Appends to |*output| the FEL representation of |function|. 153 void ToFEL(const FeatureFunctionDescriptor &function, std::string *output); 154 155 // Appends to |*output| the FEL representation of |extractor|. 156 void ToFEL(const FeatureExtractorDescriptor &extractor, std::string *output); 157 158 } // namespace mobile 159 } // namespace nlp_saft 160 161 #endif // NLP_SAFT_COMPONENTS_COMMON_MOBILE_FEL_FEATURE_DESCRIPTORS_H_ 162