Extracting string with regex from a tf.Tensor in Tensorflow 2?

Viewed 643

I am saving my tf.keras model with a signature in TF2 to serve it with TFServing. In the signature function I would like to extract some entities with regex expressions.

My input is a Tensor with datatype tf.string. I cannot use numpy() within it, resulting in "Tensor object has no attribute numpy". tf.py_function() is unavailable in TFServing as well.

So I am left with tensorflow operations. How would I extract a substring with a pattern?

@tf.function
def serve_fn(input):
    # Returns Today's date is  . Tomorrow is another day. But I need 11/2020
    output = tf.strings.regex_replace("Today's date is 11/2020. Tomorrow is another day.", pattern=r'[\d]{2}/[\d]{4}', rewrite=" ")
    
    # model inference ...

    return {'output': output}

That would return the a tensor with content "Today's date . Tomorrow is another day."

How would a pattern look like, which returns just the date? If I'm not mistaken, tf.strings.regex_replace uses re2 which does not support lookaheads. Are there maybe other solutions?

Thanks in advance

1 Answers

You can use

 tf.strings.regex_replace("Today's date is 11/2020. Tomorrow is another day.", pattern=r'.*?(\d{2}/\d{4}).*', rewrite=r'\1')

See the RE2 regex demo. Details:

  • .*?(\d{2}/\d{4}).* matches 0 or more chars other than line break chars, as few as possible, (\d{2}/\d{4}) captures into Group 1 any two digits,/ and then any four digits and then just matches the rest of the line with .* (greedily, as many as possible)
  • \1 is the brackreference to the Group 1 value. See regex_replace reference: regex_rewrite "supports backslash-escaped digits (\1 to \9) can be to insert text matching corresponding parenthesized group.".
Related