Differences with torchtext.data.functional.custom_replace

View Source On Gitee

torchtext.data.functional.custom_replace

torchtext.data.functional.custom_replace(replace_pattern)

For more information, see torchtext.data.functional.custom_replace.

mindspore.dataset.text.RegexReplace

class mindspore.dataset.text.RegexReplace(pattern, replace, replace_all=True)

For more information, see mindspore.dataset.text.RegexReplace.

Differences

PyTorch: Replace a part of string with given text according to regular expressions.

MindSpore: Replace a part of string with given text according to regular expressions.

Categories

Subcategories

PyTorch

MindSpore

Difference

Parameter

Parameter1

replace_pattern

-

Regex expression and string to replace stored in tuple

Parameter2

-

pattern

Regex expression patterns

Parameter3

-

replace

String to replace matched element.

Parameter4

-

replace_all

If only replace first matched element

Code Example

# PyTorch
from torchtext.data.functional import custom_replace

input_str = ["Sentencepiece encode  aS  pieces"]
custom_replace_transform = custom_replace([(r'S', 's')])
print(list(custom_replace_transform(input_str)))
# Out: ['sentencepiece encode  as  pieces']

# MindSpore
import mindspore.dataset.text as text

input_str = ["Sentencepiece encode  aS  pieces"]
transform = text.RegexReplace(pattern=r'S', replace='s')
print(transform(input_str))
# Out: ['sentencepiece encode  as  pieces']