Genereate a histogram of how many times each unique word is used in that test

来源:互联网 发布:哈曼卡顿和惠威 知乎 编辑:程序博客网 时间:2024/05/22 13:17
#! /usr/bin/expect --


# Genereate a histogram of how many times each unique word is used in that test.
proc hWord {sText} {


    set debug 0


    # Print the primary string text
    if {$debug == 1} {
        send_user "The text is $sText\n"
    }
    # Backup the string text
    set bText $sText


    # Copy the unique word into a list
    set sList ""
    for {set i 0} {$i < [llength $sText]} {incr i} {
        set sTmp [lindex $sText $i]
        if {[lsearch $sList $sTmp] == -1} {
            lappend sList $sTmp
        }
    }
    if {$debug == 1} {
        send_user "The list with unique word is $sList\n"
    }


    # Assign the default counter value of different unique word
    for {set i 0} {$i < [llength $sList]} {incr i} {
        set cValue($i) 0
    }


    # Do the unique word counter
    for {set i 0} {$i < [llength $sList]} {incr i} {
        set sTmp [lindex $sList $i]
        set cTmp [lsearch -all $sText $sTmp]
        set cValue($i) [llength $cTmp]
    }


    # Print the unique word counter
    puts [format "%-10s %-10s" "Word" "Times"]
    for {set i 0} {$i < [llength $sList]} {incr i} {
        set sTmp [lindex $sList $i]
        puts [format "%-10s %-10d" $sTmp $cValue($i)]
    }


    return
}


set sText [lindex $argv 0]
if {$sText == ""} {
    puts "Please input string with multi words, (eg, nihao nihao 123)"
    exit
} else {
    hWord $sText
}
0 0