forked from microsoft/onnxruntime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2efab54
commit 4f27bde
Showing
8 changed files
with
85 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "regex_full_match.h" | ||
#include "core/common/common.h" | ||
|
||
namespace onnxruntime { | ||
ONNX_CPU_OPERATOR_KERNEL( | ||
RegexFullMatch, | ||
20, | ||
KernelDefBuilder() | ||
.TypeConstraint("T1", DataTypeImpl::GetTensorType<std::string>()) | ||
.TypeConstraint("T2", DataTypeImpl::GetTensorType<bool>()), | ||
RegexFullMatch); | ||
|
||
RegexFullMatch::RegexFullMatch(const OpKernelInfo& info) : OpKernel(info) { | ||
ORT_ENFORCE(info.GetAttr<std::string>("pattern", &pattern_).IsOK()); | ||
ORT_ENFORCE(RE2(pattern_).ok(), "Invalid pattern: ", pattern_); | ||
} | ||
|
||
Status RegexFullMatch::Compute(OpKernelContext* context) const { | ||
RE2 re(pattern_); | ||
const auto* input_tensor = context->Input<Tensor>(0); | ||
if (nullptr == input_tensor) { | ||
return Status(common::ONNXRUNTIME, common::FAIL, "Input count mismatch"); | ||
} | ||
auto* output_tensor = context->Output(0, input_tensor->Shape()); | ||
if (nullptr == output_tensor) { | ||
return Status(common::ONNXRUNTIME, common::FAIL, "Output count mismatch"); | ||
} | ||
const auto input_data = input_tensor->template DataAsSpan<std::string>(); | ||
auto output_data = output_tensor->template MutableDataAsSpan<bool>(); | ||
const auto N = input_tensor->Shape().Size(); | ||
for (int64_t i = 0; i < N; ++i) { | ||
output_data[i] = RE2::FullMatch(input_data[i], re); | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include "core/framework/op_kernel.h" | ||
#include "re2/re2.h" | ||
|
||
namespace onnxruntime { | ||
|
||
class RegexFullMatch final : public OpKernel { | ||
public: | ||
explicit RegexFullMatch(const OpKernelInfo& info); | ||
Status Compute(OpKernelContext* context) const override; | ||
|
||
private: | ||
std::string pattern_; | ||
}; | ||
|
||
} // namespace onnxruntime |
24 changes: 24 additions & 0 deletions
24
onnxruntime/test/providers/cpu/nn/regex_full_match_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "gtest/gtest.h" | ||
#include "test/providers/provider_test_utils.h" | ||
|
||
namespace onnxruntime { | ||
namespace test { | ||
|
||
static void RunTest(const std::initializer_list<int64_t>& dims, const std::initializer_list<std::string>& input, const std::string& pattern, const std::initializer_list<bool>& output) { | ||
OpTester test("RegexFullMatch", 20, kOnnxDomain); | ||
test.AddAttribute("pattern", pattern); | ||
test.AddInput<std::string>("Input", dims, input); | ||
test.AddOutput<bool>("Output", dims, output); | ||
test.Run(); | ||
} | ||
|
||
TEST(RegexFullMatch, WebsiteMatch) { | ||
RunTest({3, 1}, {"www.google.com", "www.facebook.com", "www.bbc.co.uk"}, R"(www\.[\w.-]+\.\bcom\b)", {true, true, false}); | ||
} | ||
|
||
TEST(RegexFullMatch, EmailMatch) { | ||
RunTest({2, 2}, {"[email protected]", "[email protected]", "not email", "[email protected]"}, R"((\W|^)[\w.\-]{0,25}@(yahoo|gmail)\.com(\W|$))", {true, false, false, true}); | ||
} | ||
|
||
} // namespace test | ||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters