-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into stringsplit
- Loading branch information
Showing
6 changed files
with
177 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#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), re_{info.GetAttr<std::string>("pattern")} { | ||
ORT_ENFORCE(re_.ok(), "Invalid regex pattern: ", re_.pattern()); | ||
} | ||
|
||
Status RegexFullMatch::Compute(OpKernelContext* context) const { | ||
const auto* input_tensor = context->Input<Tensor>(0); | ||
const auto input_data = input_tensor->template DataAsSpan<std::string>(); | ||
auto* output_tensor = context->Output(0, input_tensor->Shape()); | ||
auto output_data = output_tensor->template MutableDataAsSpan<bool>(); | ||
auto output_iter = output_data.begin(); | ||
auto input_iter = input_data.begin(); | ||
while (input_iter != input_data.end()) { | ||
*output_iter = RE2::FullMatch(*input_iter, re_); | ||
input_iter++; | ||
output_iter++; | ||
} | ||
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,20 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#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: | ||
RE2 re_; | ||
}; | ||
|
||
} // namespace onnxruntime |
119 changes: 119 additions & 0 deletions
119
onnxruntime/test/providers/cpu/text/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,119 @@ | ||
#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}); | ||
} | ||
|
||
TEST(RegexFullMatch, MultibyteMatch) { | ||
RunTest({1, 2}, {"ä", "a"}, "ä", {true, false}); | ||
RunTest({ | ||
1, | ||
}, | ||
{"une cédille like in Besançon"}, R"(.*Besançon.*)", { | ||
true, | ||
}); | ||
RunTest({ | ||
1, | ||
}, | ||
{"une cédille like in Besançon"}, R"(.*Besancon.*)", { | ||
false, | ||
}); | ||
RunTest({ | ||
1, | ||
}, | ||
{"Mit freundlichen Grüßen"}, R"(.*Grüßen$)", { | ||
true, | ||
}); | ||
RunTest({ | ||
1, | ||
}, | ||
{"Mit freundlichen Grüßen"}, R"(.*Grußen$)", { | ||
false, | ||
}); | ||
RunTest({ | ||
3, | ||
}, | ||
{"HПонедельник", "Понедельник", "недельник"}, R"(^Понед.*)", { | ||
false, | ||
true, | ||
false, | ||
}); | ||
RunTest({ | ||
3, | ||
}, | ||
{"thank you", "どうもありがとうございます", "こんにちは世界"}, R"(^こんにちは世界.*)", { | ||
false, | ||
false, | ||
true, | ||
}); | ||
RunTest({ | ||
3, | ||
}, | ||
{"नमस्ते, आपसे मिलकर अच्छा लगा", "नमस्ते", "स्वागत एवं नमस्ते"}, R"(.+नमस्ते$)", { | ||
false, | ||
false, | ||
true, | ||
}); | ||
RunTest({ | ||
3, | ||
}, | ||
{"你好,你好吗?", "你好呀", "你好呀!"}, R"(^你好.*\?$)", { | ||
true, | ||
false, | ||
false, | ||
}); | ||
} | ||
|
||
TEST(RegexFullMatch, InvalidPattern) { | ||
OpTester test("RegexFullMatch", 20, kOnnxDomain); | ||
test.AddAttribute("pattern", R"([a-z)"); | ||
test.AddInput<std::string>("Input", { | ||
1, | ||
}, | ||
{ | ||
"abcdef", | ||
}); | ||
test.AddOutput<bool>("Output", { | ||
1, | ||
}, | ||
{ | ||
false, | ||
}); | ||
test.Run(BaseTester::ExpectResult::kExpectFailure, "Invalid regex pattern: [a-z"); | ||
} | ||
|
||
TEST(RegexFullMatch, NonUtf8Pattern) { | ||
uint8_t invalid_bytes[] = {0xC0, 0xC1, 0x41, 0x42, 0xC3, 0x80, 0xC2, 0x80, 0xC2, 0xC3, 0xC4, 0x00}; | ||
OpTester test("RegexFullMatch", 20, kOnnxDomain); | ||
test.AddAttribute("pattern", std::string((char*)invalid_bytes, sizeof(invalid_bytes))); | ||
test.AddInput<std::string>("Input", { | ||
1, | ||
}, | ||
{ | ||
"abcd", | ||
}); | ||
test.AddOutput<bool>("Output", { | ||
1, | ||
}, | ||
{ | ||
false, | ||
}); | ||
test.Run(BaseTester::ExpectResult::kExpectFailure, "Invalid regex pattern"); | ||
} | ||
} // 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