r"""Encodes each sequence of Unicode code points in `input` into a string. `result[i1...iN]` is the string formed by concatenating the Unicode codepoints `input[1...iN, :]`, encoded using `output_encoding`. Args: input: An `N+1` dimensional potentially ragged integer tensor with shape
(input,
output_encoding,
errors="replace",
replacement_char=65533,
name=None)
| 82 | # pylint: disable=redefined-builtin |
| 83 | @tf_export("strings.unicode_encode") |
| 84 | def unicode_encode(input, |
| 85 | output_encoding, |
| 86 | errors="replace", |
| 87 | replacement_char=65533, |
| 88 | name=None): |
| 89 | r"""Encodes each sequence of Unicode code points in `input` into a string. |
| 90 | |
| 91 | `result[i1...iN]` is the string formed by concatenating the Unicode |
| 92 | codepoints `input[1...iN, :]`, encoded using `output_encoding`. |
| 93 | |
| 94 | Args: |
| 95 | input: An `N+1` dimensional potentially ragged integer tensor with shape |
| 96 | `[D1...DN, num_chars]`. |
| 97 | output_encoding: Unicode encoding that should be used to encode each |
| 98 | codepoint sequence. Can be `"UTF-8"`, `"UTF-16-BE"`, or `"UTF-32-BE"`. |
| 99 | errors: Specifies the response when an invalid codepoint is encountered |
| 100 | (optional). One of: |
| 101 | * `'replace'`: Replace invalid codepoint with the |
| 102 | `replacement_char`. (default) |
| 103 | * `'ignore'`: Skip invalid codepoints. |
| 104 | * `'strict'`: Raise an exception for any invalid codepoint. |
| 105 | replacement_char: The replacement character codepoint to be used in place of |
| 106 | any invalid input when `errors='replace'`. Any valid unicode codepoint may |
| 107 | be used. The default value is the default unicode replacement character |
| 108 | which is 0xFFFD (U+65533). |
| 109 | name: A name for the operation (optional). |
| 110 | |
| 111 | Returns: |
| 112 | A `N` dimensional `string` tensor with shape `[D1...DN]`. |
| 113 | |
| 114 | #### Example: |
| 115 | ```python |
| 116 | >>> input = [[71, 246, 246, 100, 110, 105, 103, 104, 116], [128522]] |
| 117 | >>> unicode_encode(input, 'UTF-8') |
| 118 | ['G\xc3\xb6\xc3\xb6dnight', '\xf0\x9f\x98\x8a'] |
| 119 | ``` |
| 120 | """ |
| 121 | with ops.name_scope(name, "UnicodeEncode", [input]): |
| 122 | input_tensor = ragged_tensor.convert_to_tensor_or_ragged_tensor(input) |
| 123 | if input_tensor.shape.ndims is None: |
| 124 | raise ValueError("Rank of input_tensor must be statically known.") |
| 125 | if ragged_tensor.is_ragged(input_tensor): |
| 126 | if input_tensor.flat_values.shape.ndims > 1: |
| 127 | # If the flat_values of our ragged tensor is multi-dimensional, we can |
| 128 | # process it separately and our output will have the same nested splits |
| 129 | # as our input. |
| 130 | return input_tensor.with_flat_values( |
| 131 | unicode_encode(input_tensor.flat_values, output_encoding, errors, |
| 132 | replacement_char)) |
| 133 | elif input_tensor.ragged_rank > 1: |
| 134 | # Recursively process the values of the ragged tensor. |
| 135 | return input_tensor.with_values( |
| 136 | unicode_encode(input_tensor.values, output_encoding, errors, |
| 137 | replacement_char)) |
| 138 | else: |
| 139 | # Our ragged tensor is of the correct shape (rank 1 flat_values tensor |
| 140 | # with ragged_rank of 1) so we can process it as normal. |
| 141 | return gen_string_ops.unicode_encode( |
no test coverage detected