比较与torchtext.data.functional.custom_replace的差异
torchtext.data.functional.custom_replace
torchtext.data.functional.custom_replace(replace_pattern)
mindspore.dataset.text.RegexReplace
class mindspore.dataset.text.RegexReplace(pattern, replace, replace_all=True)
使用方式
PyTorch:根据正则表达式对字符串内容进行正则替换。
MindSpore:根据正则表达式对字符串内容进行正则替换。
| 分类 | 子类 | PyTorch | MindSpore | 差异 | 
|---|---|---|---|---|
| 参数 | 参数1 | replace_pattern | - | 以tuple形式表达的正则表达式的模式以及被替换的字符 | 
| 参数2 | - | pattern | 正则表达式的模式 | |
| 参数3 | - | replace | 替换匹配元素的字符串 | |
| 参数4 | - | replace_all | 是否只替换第一个匹配的元素 | 
代码示例
# 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']