1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_TEXT_KERNELS_REGEX_REPLACE_OP_H_ 17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_TEXT_KERNELS_REGEX_REPLACE_OP_H_ 18 #include <memory> 19 #include <string> 20 21 #include "unicode/regex.h" 22 #include "unicode/errorcode.h" 23 #include "unicode/utypes.h" 24 25 #include "minddata/dataset/core/tensor.h" 26 #include "minddata/dataset/kernels/tensor_op.h" 27 #include "minddata/dataset/text/kernels/whitespace_tokenizer_op.h" 28 #include "minddata/dataset/util/status.h" 29 30 namespace mindspore { 31 namespace dataset { 32 33 class RegexReplaceOp : public TensorOp { 34 public: 35 RegexReplaceOp(const std::string &pattern, const std::string &replace, bool replace_all = true) pattern_(icu::UnicodeString::fromUTF8 (pattern))36 : pattern_(icu::UnicodeString::fromUTF8(pattern)), 37 replace_(icu::UnicodeString::fromUTF8(replace)), 38 replace_all_(replace_all) {} 39 40 ~RegexReplaceOp() override = default; 41 42 Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override; 43 Name()44 std::string Name() const override { return kRegexReplaceOp; } 45 46 protected: 47 Status RegexReplace(icu::RegexMatcher *const matcher, const std::string_view &text, std::string *out) const; 48 49 private: 50 const icu::UnicodeString pattern_; 51 const icu::UnicodeString replace_; 52 const bool replace_all_; 53 }; 54 } // namespace dataset 55 } // namespace mindspore 56 #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_TEXT_KERNELS_REGEX_REPLACE_OP_H_ 57