diff options
author | 2018-09-25 11:56:33 -0700 | |
---|---|---|
committer | 2018-09-25 12:07:36 -0700 | |
commit | d5c5df164cedcd8ae43fff41256592818bc6c2de (patch) | |
tree | 0b07685ff599ad9bfe93a58386c67adb94e57c8e /tensorflow/python/kernel_tests | |
parent | df930015230c1195065e2fd01c61f527b8662efb (diff) |
Add "encoding" attribute to string length op, which controls how "string length" is defined:
* BYTE: The number of bytes in each string. (Default)
* UTF8: The number of UTF-8 encoded Unicode code points in each string.
RELNOTES: Add option to calculate string length in Unicode characters
PiperOrigin-RevId: 214478470
Diffstat (limited to 'tensorflow/python/kernel_tests')
-rw-r--r-- | tensorflow/python/kernel_tests/string_length_op_test.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/tensorflow/python/kernel_tests/string_length_op_test.py b/tensorflow/python/kernel_tests/string_length_op_test.py index 9f013c2c7e..4afe3ad3f4 100644 --- a/tensorflow/python/kernel_tests/string_length_op_test.py +++ b/tensorflow/python/kernel_tests/string_length_op_test.py @@ -32,6 +32,33 @@ class StringLengthOpTest(test.TestCase): values = sess.run(lengths) self.assertAllEqual(values, [[[1, 2], [3, 4], [5, 6]]]) + def testUnit(self): + unicode_strings = [u"H\xc3llo", u"\U0001f604"] + utf8_strings = [s.encode("utf-8") for s in unicode_strings] + expected_utf8_byte_lengths = [6, 4] + expected_utf8_char_lengths = [5, 1] + + with self.test_session() as sess: + utf8_byte_lengths = string_ops.string_length(utf8_strings, unit="BYTE") + utf8_char_lengths = string_ops.string_length( + utf8_strings, unit="UTF8_CHAR") + self.assertAllEqual( + sess.run(utf8_byte_lengths), expected_utf8_byte_lengths) + self.assertAllEqual( + sess.run(utf8_char_lengths), expected_utf8_char_lengths) + with self.assertRaisesRegexp( + ValueError, "Attr 'unit' of 'StringLength' Op passed string 'XYZ' " + 'not in: "BYTE", "UTF8_CHAR"'): + string_ops.string_length(utf8_strings, unit="XYZ") + + def testLegacyPositionalName(self): + # Code that predates the 'unit' parameter may have used a positional + # argument for the 'name' parameter. Check that we don't break such code. + strings = [[["1", "12"], ["123", "1234"], ["12345", "123456"]]] + lengths = string_ops.string_length(strings, "some_name") + with self.test_session(): + self.assertAllEqual(lengths.eval(), [[[1, 2], [3, 4], [5, 6]]]) + if __name__ == "__main__": test.main() |