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