Code: Select all
proc wordMask {text {num {1}}} {
set txtl [expr {[llength $text] -1}]; set rlist [list]
if {$num > [llength $text]} {set num [llength $text]}
while {[llength $rlist] < $num} {
set x [expr {round(rand()*$txtl)}]
lappend rlist $x; set rlist [lsort -unique $rlist]
}
set outs [split $text]; foreach idx $rlist {
set outs [lreplace $outs $idx $idx [regsub -all -- {[[:alnum:]]} [set rep [lindex $outs $idx]] {*}]]
lappend reps [regsub -all -- {[\!\.\?\;\:\"]} $rep {}]
}; return [list [join $outs] [join $reps]]
}
Code: Select all
foreach {mask reps} [wordMask $text 5] {}
Code: Select all
puts "Masked Text -> $mask"
Code: Select all
puts "Replaced Words -> $reps"
That's it...pretty simple and if you read the code in the proc declaration you can see the script will default to replacing 1 word in the passed text, but any other number can be specified if desired.