简体   繁体   中英

Regex Replace of strings of numbers into strings

Having a Tensor of strings of numbers (like "32", "45" and so on), how could I convert it to a tensor that has a symbol repeated as much times as the number indicates.

For instance, if I have a Tensor ["2", "3", "0", "1"], I would like to obtain something like ["aa", "aaa", "", "a"].

I have obtained it using numpy, but now I'm trying to do it in TensorFlow directly because I don't have the session started, so I cannot look for the variable value.

I share here a snippet of the code

import tensorflow as tf

a = tf.Variable(["2", "3", "0", "1"], dtype=tf.dtypes.string)
res = tf.strings.regex_replace(a, "([0-9]+)", r"a" * int("\\1"))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(res)) # It should show ["aa", "aaa", "", "a"]

But int("\\1") doesn't return the number, but a ValueError:

ValueError: invalid literal for int() with base 10: '\\1'

I don't think you can achieve that with a regex in TensorFlow. Here is one way you can do it:

import tensorflow as tf

def repeat_symbol(nums, symbol):
    nums = tf.convert_to_tensor(nums)
    symbol = tf.convert_to_tensor(symbol)
    # Make sequence mask from numbers
    mask = tf.sequence_mask(nums)
    # Use sequence mask to pick either the symbol or an empty string
    symbol_rep = tf.gather(tf.stack(["", symbol]), tf.cast(mask, tf.int32))
    # Join result
    return tf.strings.reduce_join(symbol_rep, axis=-1)

with tf.Graph().as_default(), tf.Session() as sess:
    a = tf.constant(["2", "3", "0", "1"], dtype=tf.string)
    # Convert strings to numbers
    a_nums = tf.strings.to_number(a, out_type=tf.int32)
    result = repeat_symbol(a_nums, "a")
    print(sess.run(result))
    # [b'aa' b'aaa' b'' b'a']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM