Home | History | Annotate | Download | only in common
      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:
     34   Parameter() {}
     35 
     36   void set_name(const std::string &name) { name_ = name; }
     37   const std::string &name() const { return name_; }
     38 
     39   void set_value(const std::string &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:
     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.
     55   void set_type(const std::string &type) { type_ = type; }
     56   bool has_type() const { return !type_.empty(); }
     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.
     62   void set_name(const std::string &name) { name_ = name; }
     63   bool has_name() const { return !name_.empty(); }
     64   const std::string &name() { return name_; }
     65 
     66   // Accessors for the default (name-less) parameter.
     67   void set_argument(int32 argument) { argument_ = 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   }
     74   int32 argument() const { return argument_; }
     75 
     76   // Accessors for the named parameters.
     77   Parameter *add_parameter() {
     78     parameters_.emplace_back();
     79     return &(parameters_.back());
     80   }
     81   int parameter_size() const { return parameters_.size(); }
     82   const Parameter &parameter(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.
     89   FeatureFunctionDescriptor *add_feature() {
     90     sub_features_.emplace_back(new FeatureFunctionDescriptor());
     91     return sub_features_.back().get();
     92   }
     93   int feature_size() const { return sub_features_.size(); }
     94   const FeatureFunctionDescriptor &feature(int i) const {
     95     TC_DCHECK((i >= 0) && (i < feature_size()));
     96     return *(sub_features_[i].get());
     97   }
     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:
    126   FeatureExtractorDescriptor() {}
    127 
    128   int feature_size() const { return features_.size(); }
    129 
    130   FeatureFunctionDescriptor *add_feature() {
    131     features_.emplace_back(new FeatureFunctionDescriptor());
    132     return features_.back().get();
    133   }
    134 
    135   const FeatureFunctionDescriptor &feature(int i) const {
    136     TC_DCHECK((i >= 0) && (i < feature_size()));
    137     return *(features_[i].get());
    138   }
    139 
    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