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 #include "lang_id/common/embedding-feature-extractor.h"
18
19 #include <stddef.h>
20
21 #include <string>
22 #include <vector>
23
24 #include "lang_id/common/fel/feature-extractor.h"
25 #include "lang_id/common/fel/feature-types.h"
26 #include "lang_id/common/fel/task-context.h"
27 #include "lang_id/common/lite_base/integral-types.h"
28 #include "lang_id/common/lite_base/logging.h"
29 #include "lang_id/common/lite_strings/numbers.h"
30 #include "lang_id/common/lite_strings/str-split.h"
31 #include "lang_id/common/lite_strings/stringpiece.h"
32
33 namespace libtextclassifier3 {
34 namespace mobile {
35
Setup(TaskContext * context)36 bool GenericEmbeddingFeatureExtractor::Setup(TaskContext *context) {
37 // Don't use version to determine how to get feature FML.
38 const string features = context->Get(GetParamName("features"), "");
39 const string embedding_names =
40 context->Get(GetParamName("embedding_names"), "");
41 const string embedding_dims =
42 context->Get(GetParamName("embedding_dims"), "");
43
44 // NOTE: unfortunately, LiteStrSplit returns a vector of StringPieces pointing
45 // to the original string, in this case |features|, which is local to this
46 // method. We need to explicitly create new strings.
47 for (StringPiece sp : LiteStrSplit(features, ';')) {
48 embedding_fml_.emplace_back(sp);
49 }
50
51 // Same here.
52 for (StringPiece sp : LiteStrSplit(embedding_names, ';')) {
53 embedding_names_.emplace_back(sp);
54 }
55
56 std::vector<StringPiece> dim_strs = LiteStrSplit(embedding_dims, ';');
57 for (const auto &dim_str : dim_strs) {
58 int dim = 0;
59 if (!LiteAtoi(dim_str, &dim)) {
60 SAFTM_LOG(ERROR) << "Unable to parse " << dim_str;
61 return false;
62 }
63 embedding_dims_.push_back(dim);
64 }
65 return true;
66 }
67
Init(TaskContext * context)68 bool GenericEmbeddingFeatureExtractor::Init(TaskContext *context) {
69 return true;
70 }
71
72 } // namespace mobile
73 } // namespace nlp_saft
74