C++程序  |  49行  |  1.91 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.
 */

#ifndef LIBTEXTCLASSIFIER_UTILS_REGEX_MATCH_H_
#define LIBTEXTCLASSIFIER_UTILS_REGEX_MATCH_H_

#include "utils/flatbuffers.h"
#include "utils/flatbuffers_generated.h"
#include "utils/utf8/unilib.h"

namespace libtextclassifier3 {
// Sets a field in the flatbuffer from a regex match group.
// Returns true if successful, and false if the field couldn't be set.
bool SetFieldFromCapturingGroup(const int group_id,
                                const FlatbufferFieldPath* field_path,
                                const UniLib::RegexMatcher* matcher,
                                ReflectiveFlatbuffer* flatbuffer);

// Post-checks a regular expression match with a lua verifier script.
// The verifier can access:
//   * `context`: The context as a string.
//   * `match`: The groups of the regex match as an array, each group gives
//       * `begin`: span start
//       * `end`: span end
//       * `text`: the text
// The verifier is expected to return a boolean, indicating whether the
// verification succeeded or not.
// Returns true if the verification was successful, false if not.
bool VerifyMatch(const std::string& context,
                 const UniLib::RegexMatcher* matcher,
                 const std::string& lua_verifier_code);

}  // namespace libtextclassifier3

#endif  // LIBTEXTCLASSIFIER_UTILS_REGEX_MATCH_H_