Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StringLower/StringUpper op to support lower/upper() for string operations #25859

Merged
merged 10 commits into from
May 3, 2019
3 changes: 3 additions & 0 deletions tensorflow/core/api_def/base_api/api_def_StringLower.pbtxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
op {
graph_op_name: "StringLower"
}
3 changes: 3 additions & 0 deletions tensorflow/core/api_def/base_api/api_def_StringUpper.pbtxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
op {
graph_op_name: "StringUpper"
}
6 changes: 6 additions & 0 deletions tensorflow/core/api_def/python_api/api_def_StringLower.pbtxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
op {
graph_op_name: "StringLower"
endpoint {
name: "strings.lower"
}
}
6 changes: 6 additions & 0 deletions tensorflow/core/api_def/python_api/api_def_StringUpper.pbtxt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
op {
graph_op_name: "StringUpper"
endpoint {
name: "strings.upper"
}
}
14 changes: 14 additions & 0 deletions tensorflow/core/kernels/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4920,9 +4920,11 @@ cc_library(
":string_format_op",
":string_join_op",
":string_length_op",
":string_lower_op",
":string_split_op",
":string_strip_op",
":string_to_hash_bucket_op",
":string_upper_op",
":substr_op",
":unicode_ops",
":unicode_script_op",
Expand Down Expand Up @@ -5058,6 +5060,18 @@ tf_kernel_library(
deps = STRING_DEPS,
)

tf_kernel_library(
name = "string_lower_op",
prefix = "string_lower_op",
deps = STRING_DEPS,
)

tf_kernel_library(
name = "string_upper_op",
prefix = "string_upper_op",
deps = STRING_DEPS,
)

tf_kernel_library(
name = "substr_op",
prefix = "substr_op",
Expand Down
70 changes: 70 additions & 0 deletions tensorflow/core/kernels/string_lower_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2019


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.
==============================================================================*/

// See docs in ../ops/string_ops.cc.

#include <string>

#include "unicode/unistr.h" // TF:icu

#include "tensorflow/core/framework/kernel_def_builder.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/strings/str_util.h"

namespace tensorflow {

class StringLowerOp : public OpKernel {
public:
explicit StringLowerOp(OpKernelConstruction* context) : OpKernel(context) {
OP_REQUIRES_OK(context, context->GetAttr("encoding", &encoding_));
OP_REQUIRES(
context, encoding_ != "" || encoding_ != "utf-8",
errors::InvalidArgument("only utf-8 or '' (no encoding) is supported, received ", encoding_));
}

void Compute(OpKernelContext* ctx) override {
const Tensor* input_tensor;
OP_REQUIRES_OK(ctx, ctx->input("input", &input_tensor));
Tensor* output_tensor;
OP_REQUIRES_OK(
ctx, ctx->allocate_output(0, input_tensor->shape(), &output_tensor));

const auto input = input_tensor->flat<string>();
auto output = output_tensor->flat<string>();

if (encoding_ == "") {
for (int64 i = 0; i < input.size(); ++i) {
StringPiece entry(input(i));
output(i) = str_util::Lowercase(entry);
}
} else {
// The validation of utf-8 has already been done in GetAttr above.
for (int64 i = 0; i < input.size(); ++i) {
icu::UnicodeString us(input(i).c_str(), "UTF-8");
us.toLower();
us.toUTF8String(output(i));
}
}
}
private:
string encoding_;
};

REGISTER_KERNEL_BUILDER(Name("StringLower").Device(DEVICE_CPU), StringLowerOp);

} // namespace tensorflow
69 changes: 69 additions & 0 deletions tensorflow/core/kernels/string_upper_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/

// See docs in ../ops/string_ops.cc.

#include <string>

#include "unicode/unistr.h" // TF:icu

#include "tensorflow/core/framework/kernel_def_builder.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/strings/str_util.h"

namespace tensorflow {

class StringUpperOp : public OpKernel {
public:
explicit StringUpperOp(OpKernelConstruction* context) : OpKernel(context) {
OP_REQUIRES_OK(context, context->GetAttr("encoding", &encoding_));
OP_REQUIRES(
context, encoding_ != "" || encoding_ != "utf-8",
errors::InvalidArgument("only utf-8 or '' (no encoding) is supported, received ", encoding_));
}

void Compute(OpKernelContext* ctx) override {
const Tensor* input_tensor;
OP_REQUIRES_OK(ctx, ctx->input("input", &input_tensor));
Tensor* output_tensor;
OP_REQUIRES_OK(
ctx, ctx->allocate_output(0, input_tensor->shape(), &output_tensor));

const auto input = input_tensor->flat<string>();
auto output = output_tensor->flat<string>();
if (encoding_ == "") {
for (int64 i = 0; i < input.size(); ++i) {
StringPiece entry(input(i));
output(i) = str_util::Uppercase(entry);
}
} else {
// The validation of utf-8 has already been done in GetAttr above.
for (int64 i = 0; i < input.size(); ++i) {
icu::UnicodeString us(input(i).c_str(), "UTF-8");
us.toUpper();
us.toUTF8String(output(i));
}
}
}
private:
string encoding_;
};

REGISTER_KERNEL_BUILDER(Name("StringUpper").Device(DEVICE_CPU), StringUpperOp);

} // namespace tensorflow
12 changes: 12 additions & 0 deletions tensorflow/core/ops/string_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ REGISTER_OP("StringSplitV2")
return Status::OK();
});

REGISTER_OP("StringLower")
.Input("input: string")
.Output("output: string")
.Attr("encoding: string =''")
.SetShapeFn(shape_inference::UnchangedShape);

REGISTER_OP("StringUpper")
.Input("input: string")
.Output("output: string")
.Attr("encoding: string =''")
.SetShapeFn(shape_inference::UnchangedShape);

REGISTER_OP("StringStrip")
.Input("input: string")
.Output("output: string")
Expand Down
28 changes: 28 additions & 0 deletions tensorflow/python/kernel_tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,34 @@ tf_py_test(
],
)

tf_py_test(
name = "string_lower_op_test",
size = "small",
srcs = ["string_lower_op_test.py"],
additional_deps = [
"//third_party/py/numpy",
"//tensorflow/python:array_ops",
"//tensorflow/python:client_testlib",
"//tensorflow/python:errors",
"//tensorflow/python:framework_for_generated_wrappers",
"//tensorflow/python:string_ops",
],
)

tf_py_test(
name = "string_upper_op_test",
size = "small",
srcs = ["string_upper_op_test.py"],
additional_deps = [
"//third_party/py/numpy",
"//tensorflow/python:array_ops",
"//tensorflow/python:client_testlib",
"//tensorflow/python:errors",
"//tensorflow/python:framework_for_generated_wrappers",
"//tensorflow/python:string_ops",
],
)

tf_py_test(
name = "substr_op_test",
size = "small",
Expand Down
57 changes: 57 additions & 0 deletions tensorflow/python/kernel_tests/string_lower_op_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for string_lower_op."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.ops import string_ops
from tensorflow.python.platform import test


class StringLowerOpTest(test.TestCase):
""" Test cases for tf.strings.lower."""

def test_string_lower(self):
strings = ["Pigs on The Wing", "aNimals"]

with self.cached_session() as sess:
output = string_ops.string_lower(strings)
output = self.evaluate(output)
self.assertAllEqual(output, [b"pigs on the wing", b"animals"])

def test_string_lower_2d(self):
strings = [["pigS on THE wIng", "aniMals"],
[" hello ", "\n\tWorld! \r \n"]]

with self.cached_session() as sess:
output = string_ops.string_lower(strings)
output = self.evaluate(output)
self.assertAllEqual(output, [[b"pigs on the wing", b"animals"],
[b" hello ", b"\n\tworld! \r \n"]])

def test_string_upper_unicode(self):
strings = [["ÓÓSSCHLOË"]]
with self.cached_session() as sess:
output = string_ops.string_lower(strings, encoding='utf-8')
output = self.evaluate(output)
# output: "óósschloë"
self.assertAllEqual(output, [[b"\xc3\xb3\xc3\xb3sschlo\xc3\xab"]])


if __name__ == "__main__":
test.main()
57 changes: 57 additions & 0 deletions tensorflow/python/kernel_tests/string_upper_op_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for string_upper_op."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow.python.ops import string_ops
from tensorflow.python.platform import test


class StringUpperOpTest(test.TestCase):
""" Test cases for tf.strings.upper."""

def test_string_upper(self):
strings = ["Pigs on The Wing", "aNimals"]

with self.cached_session() as sess:
output = string_ops.string_upper(strings)
output = self.evaluate(output)
self.assertAllEqual(output, [b"PIGS ON THE WING", b"ANIMALS"])

def test_string_upper_2d(self):
strings = [["pigS on THE wIng", "aniMals"],
[" hello ", "\n\tWorld! \r \n"]]

with self.cached_session() as sess:
output = string_ops.string_upper(strings)
output = self.evaluate(output)
self.assertAllEqual(output, [[b"PIGS ON THE WING", b"ANIMALS"],
[b" HELLO ", b"\n\tWORLD! \r \n"]])

def test_string_upper_unicode(self):
strings = [["óósschloë"]]
with self.cached_session() as sess:
output = string_ops.string_upper(strings, encoding='utf-8')
output = self.evaluate(output)
# output: "ÓÓSSCHLOË"
self.assertAllEqual(output, [[b"\xc3\x93\xc3\x93SSCHLO\xc3\x8b"]])


if __name__ == "__main__":
test.main()
8 changes: 8 additions & 0 deletions tensorflow/tools/api/golden/v1/tensorflow.raw_ops.pbtxt
Original file line number Diff line number Diff line change
Expand Up @@ -3788,6 +3788,10 @@ tf_module {
name: "StringLength"
argspec: "args=[\'input\', \'unit\', \'name\'], varargs=None, keywords=None, defaults=[\'BYTE\', \'None\'], "
}
member_method {
name: "StringLower"
argspec: "args=[\'input\', \'encoding\', \'name\'], varargs=None, keywords=None, defaults=[\'\', \'None\'], "
}
member_method {
name: "StringSplit"
argspec: "args=[\'input\', \'delimiter\', \'skip_empty\', \'name\'], varargs=None, keywords=None, defaults=[\'True\', \'None\'], "
Expand Down Expand Up @@ -3816,6 +3820,10 @@ tf_module {
name: "StringToNumber"
argspec: "args=[\'string_tensor\', \'out_type\', \'name\'], varargs=None, keywords=None, defaults=[\"<dtype: \'float32\'>\", \'None\'], "
}
member_method {
name: "StringUpper"
argspec: "args=[\'input\', \'encoding\', \'name\'], varargs=None, keywords=None, defaults=[\'\', \'None\'], "
}
member_method {
name: "Sub"
argspec: "args=[\'x\', \'y\', \'name\'], varargs=None, keywords=None, defaults=[\'None\'], "
Expand Down