string_replace
StringReplaceLayer ¤
StringReplaceLayer(
string_match_constant=None,
string_replace_constant=None,
regex=False,
name=None,
input_dtype=None,
output_dtype=None,
**kwargs
)
Bases: BaseLayer
StringReplaceLayer layer for TensorFlow.
Initialises the StringReplaceLayer layer.
WARNING: While it works, the use of tensors in matching/replacement is not recommended due to the complexity of the regex matching which requires use of a map_fn. This will be comparatively VERY slow and may not be suitable for inference use-cases. If you know where in the string the match is, you will be much better off slicing the string and checking for equality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string_match_constant
|
Optional[str]
|
The string to match against and replace. Defaults to |
None
|
string_replace_constant
|
Optional[str]
|
The string to replace the matched string with. Defaults to |
None
|
regex
|
bool
|
Whether to treat the string match as a regular expression. Defaults to |
False
|
name
|
Optional[str]
|
The name of the layer. Defaults to |
None
|
input_dtype
|
Optional[str]
|
The dtype to cast the input to. Defaults to |
None
|
output_dtype
|
Optional[str]
|
The dtype to cast the output to. Defaults to |
None
|
Source code in src/kamae/tensorflow/layers/string_replace.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
compatible_dtypes
property
¤
compatible_dtypes
_call ¤
_call(inputs, **kwargs)
Checks for the existence of a substring/pattern within a tensor and replaces if there is a match.
KNOWN ISSUE: when replacing with a string that contains a backslash, the backslash must be double escaped (\) in order to be added properly. This is consistent in both spark and tensorflow components.
WARNING: While it works, the use of tensors in matching/replacement is not recommended due to the complexity of the regex matching which requires use of a map_fn. This will be comparatively VERY slow and may not be suitable for inference use-cases. If you know where in the string the match is, you will be much better off slicing the string and checking for equality.
Decorated with @allow_single_or_multiple_tensor_input to ensure that the input
is either a single tensor or an iterable of tensors. Returns this result as a
list of tensors for easier use here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Union[Tensor, Iterable[Tensor]]
|
A string tensor or iterable of up to three string tensors. In the case multiple tensors are passed, require that the order of inputs is [string input, {string match tensor}, {string replace tensor}]. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A string tensor of regex replaced strings. |
Source code in src/kamae/tensorflow/layers/string_replace.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
_escape_special_characters ¤
_escape_special_characters(string_to_escape)
Escapes special characters in a string so they are not parsed as regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string_to_escape
|
Union[str, Tensor]
|
The string or string tensor to escape special characters in. |
required |
Returns:
| Type | Description |
|---|---|
Union[str, Tensor]
|
The escaped string or string tensor. |
Source code in src/kamae/tensorflow/layers/string_replace.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
get_config ¤
get_config()
Gets the configuration of the StringReplace layer. Used for saving and loading the layer from disk.
Specifically, regex, string_match_constant and string_replace_constant
are added to the config.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary configuration of the layer. |
Source code in src/kamae/tensorflow/layers/string_replace.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |