• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "common/embedding-feature-extractor.h"
18 
19 #include <stddef.h>
20 
21 #include <vector>
22 
23 #include "common/feature-extractor.h"
24 #include "common/feature-types.h"
25 #include "common/task-context.h"
26 #include "util/base/integral_types.h"
27 #include "util/base/logging.h"
28 #include "util/strings/numbers.h"
29 #include "util/strings/split.h"
30 
31 namespace libtextclassifier {
32 namespace nlp_core {
33 
Init(TaskContext * context)34 bool GenericEmbeddingFeatureExtractor::Init(TaskContext *context) {
35   // Don't use version to determine how to get feature FML.
36   const std::string features = context->Get(GetParamName("features"), "");
37   TC_LOG(INFO) << "Features: " << features;
38 
39   const std::string embedding_names =
40       context->Get(GetParamName("embedding_names"), "");
41   TC_LOG(INFO) << "Embedding names: " << embedding_names;
42 
43   const std::string embedding_dims =
44       context->Get(GetParamName("embedding_dims"), "");
45   TC_LOG(INFO) << "Embedding dims: " << embedding_dims;
46 
47   embedding_fml_ = strings::Split(features, ';');
48   embedding_names_ = strings::Split(embedding_names, ';');
49   for (const std::string &dim : strings::Split(embedding_dims, ';')) {
50     int32 parsed_dim = 0;
51     if (!ParseInt32(dim.c_str(), &parsed_dim)) {
52       TC_LOG(ERROR) << "Unable to parse dim " << dim;
53       return false;
54     }
55     embedding_dims_.push_back(parsed_dim);
56   }
57   if ((embedding_fml_.size() != embedding_names_.size()) ||
58       (embedding_fml_.size() != embedding_dims_.size())) {
59     TC_LOG(ERROR) << "Mismatch: #fml specs = " << embedding_fml_.size()
60                   << "; #names = " << embedding_names_.size()
61                   << "; #dims = " << embedding_dims_.size();
62     return false;
63   }
64   return true;
65 }
66 
67 }  // namespace nlp_core
68 }  // namespace libtextclassifier
69