• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 namespace libtextclassifier3 {
29 namespace mobile {
30 
31 // Named feature parameter.
32 class Parameter {
33  public:
Parameter()34   Parameter() {}
35 
set_name(const string & name)36   void set_name(const string &name) { name_ = name; }
name()37   const string &name() const { return name_; }
38 
set_value(const string & value)39   void set_value(const string &value) { value_ = value; }
value()40   const string &value() const { return value_; }
41 
42  private:
43   string name_;
44   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 string & type)55   void set_type(const string &type) { type_ = type; }
type()56   const string &type() const { return type_; }
57 
58   // Accessors for the feature function name.  The function name (if available)
59   // is used for some log messages.  Otherwise, a more precise, but also more
60   // verbose name based on the feature specification is used.
set_name(const string & name)61   void set_name(const string &name) { name_ = name; }
name()62   const string &name() const { return name_; }
63 
64   // Accessors for the default (name-less) parameter.
set_argument(int32 argument)65   void set_argument(int32 argument) { argument_ = argument; }
has_argument()66   bool has_argument() const {
67     // If argument has not been specified, clients should treat it as 0.  This
68     // makes the test below correct, without having a separate has_argument_
69     // bool field.
70     return argument_ != 0;
71   }
argument()72   int32 argument() const { return argument_; }
73 
74   // Accessors for the named parameters.
add_parameter()75   Parameter *add_parameter() {
76     parameters_.emplace_back();
77     return &(parameters_.back());
78   }
parameter_size()79   int parameter_size() const { return parameters_.size(); }
parameter(int i)80   const Parameter &parameter(int i) const {
81     SAFTM_DCHECK((i >= 0) && (i < parameter_size()));
82     return parameters_[i];
83   }
84 
85   // Accessors for the sub (i.e., nested) features.  Nested features: as in
86   // offset(1).label.
add_feature()87   FeatureFunctionDescriptor *add_feature() {
88     sub_features_.emplace_back(new FeatureFunctionDescriptor());
89     return sub_features_.back().get();
90   }
feature_size()91   int feature_size() const { return sub_features_.size(); }
feature(int i)92   const FeatureFunctionDescriptor &feature(int i) const {
93     SAFTM_DCHECK((i >= 0) && (i < feature_size()));
94     return *(sub_features_[i].get());
95   }
96 
97   // Returns human-readable representation of this FeatureFunctionDescriptor.
98   string DebugString() const;
99 
100  private:
101   // See comments for set_type().
102   string type_;
103 
104   // See comments for set_name().
105   string name_;
106 
107   // See comments for set_argument().
108   int32 argument_ = 0;
109 
110   // See comemnts for add_parameter().
111   std::vector<Parameter> parameters_;
112 
113   // See comments for add_feature().
114   std::vector<std::unique_ptr<FeatureFunctionDescriptor>> sub_features_;
115 
116   SAFTM_DISALLOW_COPY_AND_ASSIGN(FeatureFunctionDescriptor);
117 };
118 
119 // List of FeatureFunctionDescriptors.  Used to store the result of parsing the
120 // spec for several feature functions.
121 class FeatureExtractorDescriptor {
122  public:
FeatureExtractorDescriptor()123   FeatureExtractorDescriptor() {}
124 
feature_size()125   int feature_size() const { return features_.size(); }
126 
add_feature()127   FeatureFunctionDescriptor *add_feature() {
128     features_.emplace_back(new FeatureFunctionDescriptor());
129     return features_.back().get();
130   }
131 
feature(int i)132   const FeatureFunctionDescriptor &feature(int i) const {
133     SAFTM_DCHECK((i >= 0) && (i < feature_size()));
134     return *(features_[i].get());
135   }
136 
137   // Returns human-readable representation of this FeatureExtractorDescriptor.
138   string DebugString() const;
139 
140  private:
141   std::vector<std::unique_ptr<FeatureFunctionDescriptor>> features_;
142 
143   SAFTM_DISALLOW_COPY_AND_ASSIGN(FeatureExtractorDescriptor);
144 };
145 
146 // Appends to |*output| the FEL representation of the top-level feature from
147 // |function|, without diving into the nested features.
148 void ToFELFunction(const FeatureFunctionDescriptor &function, string *output);
149 
150 // Appends to |*output| the FEL representation of |function|.
151 void ToFEL(const FeatureFunctionDescriptor &function, string *output);
152 
153 // Appends to |*output| the FEL representation of |extractor|.
154 void ToFEL(const FeatureExtractorDescriptor &extractor, string *output);
155 
156 }  // namespace mobile
157 }  // namespace nlp_saft
158 
159 #endif  // NLP_SAFT_COMPONENTS_COMMON_MOBILE_FEL_FEATURE_DESCRIPTORS_H_
160