普通文本  |  79行  |  2.33 KB

/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "lang_id/common/fel/feature-descriptors.h"

#include "lang_id/common/lite_strings/str-cat.h"

namespace libtextclassifier3 {
namespace mobile {

void ToFELFunction(const FeatureFunctionDescriptor &function, string *output) {
  LiteStrAppend(output, function.type());
  if (function.argument() != 0 || function.parameter_size() > 0) {
    LiteStrAppend(output, "(");
    bool first = true;
    if (function.argument() != 0) {
      LiteStrAppend(output, function.argument());
      first = false;
    }
    for (int i = 0; i < function.parameter_size(); ++i) {
      if (!first) LiteStrAppend(output, ",");
      LiteStrAppend(output, function.parameter(i).name(), "=\"",
                    function.parameter(i).value(), "\"");
      first = false;
    }
    LiteStrAppend(output, ")");
  }
}

void ToFEL(const FeatureFunctionDescriptor &function, string *output) {
  ToFELFunction(function, output);
  if (function.feature_size() == 1) {
    LiteStrAppend(output, ".");
    ToFEL(function.feature(0), output);
  } else if (function.feature_size() > 1) {
    LiteStrAppend(output, " { ");
    for (int i = 0; i < function.feature_size(); ++i) {
      if (i > 0) LiteStrAppend(output, " ");
      ToFEL(function.feature(i), output);
    }
    LiteStrAppend(output, " } ");
  }
}

void ToFEL(const FeatureExtractorDescriptor &extractor, string *output) {
  for (int i = 0; i < extractor.feature_size(); ++i) {
    ToFEL(extractor.feature(i), output);
    LiteStrAppend(output, "\n");
  }
}

string FeatureFunctionDescriptor::DebugString() const {
  string str;
  ToFEL(*this, &str);
  return str;
}

string FeatureExtractorDescriptor::DebugString() const {
  string str;
  ToFEL(*this, &str);
  return str;
}

}  // namespace mobile
}  // namespace nlp_saft