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/fel/feature-descriptors.h"
18
19 #include "lang_id/common/lite_strings/str-cat.h"
20
21 namespace libtextclassifier3 {
22 namespace mobile {
23
ToFELFunction(const FeatureFunctionDescriptor & function,string * output)24 void ToFELFunction(const FeatureFunctionDescriptor &function, string *output) {
25 LiteStrAppend(output, function.type());
26 if (function.argument() != 0 || function.parameter_size() > 0) {
27 LiteStrAppend(output, "(");
28 bool first = true;
29 if (function.argument() != 0) {
30 LiteStrAppend(output, function.argument());
31 first = false;
32 }
33 for (int i = 0; i < function.parameter_size(); ++i) {
34 if (!first) LiteStrAppend(output, ",");
35 LiteStrAppend(output, function.parameter(i).name(), "=\"",
36 function.parameter(i).value(), "\"");
37 first = false;
38 }
39 LiteStrAppend(output, ")");
40 }
41 }
42
ToFEL(const FeatureFunctionDescriptor & function,string * output)43 void ToFEL(const FeatureFunctionDescriptor &function, string *output) {
44 ToFELFunction(function, output);
45 if (function.feature_size() == 1) {
46 LiteStrAppend(output, ".");
47 ToFEL(function.feature(0), output);
48 } else if (function.feature_size() > 1) {
49 LiteStrAppend(output, " { ");
50 for (int i = 0; i < function.feature_size(); ++i) {
51 if (i > 0) LiteStrAppend(output, " ");
52 ToFEL(function.feature(i), output);
53 }
54 LiteStrAppend(output, " } ");
55 }
56 }
57
ToFEL(const FeatureExtractorDescriptor & extractor,string * output)58 void ToFEL(const FeatureExtractorDescriptor &extractor, string *output) {
59 for (int i = 0; i < extractor.feature_size(); ++i) {
60 ToFEL(extractor.feature(i), output);
61 LiteStrAppend(output, "\n");
62 }
63 }
64
DebugString() const65 string FeatureFunctionDescriptor::DebugString() const {
66 string str;
67 ToFEL(*this, &str);
68 return str;
69 }
70
DebugString() const71 string FeatureExtractorDescriptor::DebugString() const {
72 string str;
73 ToFEL(*this, &str);
74 return str;
75 }
76
77 } // namespace mobile
78 } // namespace nlp_saft
79