def testSplitWithPaddedOutput(self, texts, expected, ragged_rank=None): input_tensor = ragged.constant_value(_nested_encode(texts, "UTF-8"), ragged_rank=ragged_rank, dtype=bytes) result = ragged.unicode_split(input_tensor, "UTF-8").to_tensor(default_value="") self.assertAllEqual(np.array(expected, dtype=bytes), result)
def testSplitWithSparseOutput(self, texts, expected): input_tensor = np.array(_nested_encode(texts, "UTF-8"), dtype=bytes) result = ragged.unicode_split(input_tensor, "UTF-8").to_sparse() self.assertIsInstance(result, sparse_tensor.SparseTensor) self.assertAllEqual(expected.indices, result.indices) self.assertAllEqual(expected.values, result.values) self.assertAllEqual(expected.dense_shape, result.dense_shape)
def testBasicSplit(self, texts, ragged_rank=None): input_tensor = ragged.constant_value(_nested_encode(texts, "UTF-8"), ragged_rank=ragged_rank, dtype=bytes) result = ragged.unicode_split(input_tensor, "UTF-8") expected = _nested_splitchars(texts, "UTF-8") self.assertRaggedEqual(expected, result)
def testDocstringExamples(self): texts = [s.encode("utf8") for s in [u"G\xf6\xf6dnight", u"\U0001f60a"]] codepoints1 = ragged.unicode_split(texts, "UTF-8") codepoints2, offsets = ragged.unicode_split_with_offsets( texts, "UTF-8") self.assertRaggedEqual(codepoints1, [[ b"G", b"\xc3\xb6", b"\xc3\xb6", b"d", b"n", b"i", b"g", b"h", b"t" ], [b"\xf0\x9f\x98\x8a"]]) self.assertRaggedEqual(codepoints2, [[ b"G", b"\xc3\xb6", b"\xc3\xb6", b"d", b"n", b"i", b"g", b"h", b"t" ], [b"\xf0\x9f\x98\x8a"]]) self.assertRaggedEqual(offsets, [[0, 1, 3, 5, 6, 7, 8, 9, 10], [0]])
def testExceptions(self, exception=None, message=None, **args): with self.assertRaisesRegexp(exception, message): self.evaluate(ragged.unicode_split(**args))
def testSplitWithDifferentEncodings(self, encoding, texts): expected = _nested_splitchars(texts, encoding) input_tensor = constant_op.constant(_nested_encode(texts, encoding)) result = ragged.unicode_split(input_tensor, encoding) self.assertRaggedEqual(expected, result)
def testErrorModes(self, expected=None, **args): result = ragged.unicode_split(**args) self.assertRaggedEqual(expected, result)
def testVectorSplit(self): text = constant_op.constant([u"仅今年前".encode("UTF-8"), b"hello"]) chars = ragged.unicode_split(text, "UTF-8") expected_chars = [[c.encode("UTF-8") for c in u"仅今年前"], [c.encode("UTF-8") for c in u"hello"]] self.assertRaggedEqual(chars, expected_chars)
def testScalarSplit(self): text = constant_op.constant(u"仅今年前".encode("UTF-8")) chars = ragged.unicode_split(text, "UTF-8") self.assertAllEqual(chars, [c.encode("UTF-8") for c in u"仅今年前"])