Skip to content

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.

None
string_replace_constant Optional[str]

The string to replace the matched string with. Defaults to None.

None
regex bool

Whether to treat the string match as a regular expression. Defaults to False. In the case regex is enabled, the string_match_constant or second input tensor elements are treated as a regex pattern. Please be aware that while testing has tried to catch corner cases, this is not guaranteed to be bug-free due to slight differences in the regex implementations between Spark and TensorFlow.

False
name Optional[str]

The name of the layer. Defaults to None.

None
input_dtype Optional[str]

The dtype to cast the input to. Defaults to None.

None
output_dtype Optional[str]

The dtype to cast the output to. Defaults to None.

None
Source code in src/kamae/keras/tensorflow/layers/string_replace.py
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
72
73
74
75
def __init__(
    self,
    string_match_constant: Optional[str] = None,
    string_replace_constant: Optional[str] = None,
    regex: bool = False,
    name: Optional[str] = None,
    input_dtype: Optional[str] = None,
    output_dtype: Optional[str] = None,
    **kwargs: Any,
) -> None:
    """
    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.

    :param string_match_constant: The string to match against and replace.
        Defaults to `None`.
    :param string_replace_constant: The string to replace the matched string with.
        Defaults to `None`.
    :param regex: Whether to treat the string match as a regular expression.
        Defaults to `False`. In the case regex is enabled, the string_match_constant
        or second input tensor elements are treated as a regex pattern. Please be
        aware that while testing has tried to catch corner cases, this is not
        guaranteed to be bug-free due to slight differences in the regex
        implementations between Spark and TensorFlow.
    :param name: The name of the layer. Defaults to `None`.
    :param input_dtype: The dtype to cast the input to. Defaults to `None`.
    :param output_dtype: The dtype to cast the output to. Defaults to `None`.
    """
    super().__init__(
        name=name, input_dtype=input_dtype, output_dtype=output_dtype, **kwargs
    )
    self.string_match_constant = string_match_constant
    self.string_replace_constant = string_replace_constant
    self.regex = regex

compatible_dtypes property ¤

compatible_dtypes

Returns the compatible dtypes of the layer.

Returns:

Type Description
Optional[List[str]]

The compatible dtypes of the layer.

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/keras/tensorflow/layers/string_replace.py
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def get_config(self) -> Dict[str, Any]:
    """
    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: Dictionary configuration of the layer.
    """
    config = super().get_config()
    config.update(
        {
            "regex": self.regex,
            "string_match_constant": self.string_match_constant,
            "string_replace_constant": self.string_replace_constant,
        }
    )
    return config