Version 2 of Mispelling Generator

Updated 2011-07-01 17:16:03 by snichols

snichols Below is some code for a misspelling generator. It will misspell a word 5 different ways, but only one letter. This is the opposite of the little spell checker program: http://wiki.tcl.tk/882 .


proc cipherVowel {word} {
        set vowels "a e i o u"
        set i [expr {round(rand() * ([llength $vowels] -1))}]
        set mispelling $word
        foreach l $vowels {
                set j [string first $l $word]
                if {$j > -1} {
                        return [string replace $word $j $j [lindex $vowels $i]]
                }
                
        }
        return $mispelling
}

proc skip {word} {
        set i [expr {round(rand() * ([string length $word] -1))}]
        return [string replace $word $i $i ""]
}

proc doubleLetter {word} {
        set i [expr {round(rand() * ([string length $word] -1))}]
        return [string replace $word $i $i [string index $word $i][string index $word $i]]
}

proc reverseLetter {word} {
        set i [expr {round(rand() * ([string length $word] -1))}]
        if { $i == [string length $word] -1} {
                set i [expr [string length $word] -2]
        }
        return [string replace $word $i $i [string index $word [expr $i + 1]]]
}

proc skipSpace {word} {
        set i [string first " " $word]
        if { $i > -1} {
                return [string replace $word $i $i ""]
        } else {
                return $word
        }
}

proc insert {word} {
        set i [expr {round(rand() * ([string length $word] -1))}]
        return [string replace $word $i $i ""]
}


lappend data "this is a test."
lappend data "fall"
lappend data "summer"
lappend data "spring"
lappend data "winter"

puts "correct|incorrect"

foreach word $data {
        puts "$word|[cipherVowel $word]"
        puts "$word|[skip $word]"
        puts "$word|[reverseLetter $word]"
        puts "$word|[skipSpace $word]"
        puts "$word|[insert $word]"
}