Posted to tcl by dandyn at Sun Nov 24 20:11:41 GMT 2024view pretty

set histBuf {trump Trump trump Trump trump trumps trumps trump trumps hello hello hello hello hello hello trump}
proc wordcount {nr} {
   global histBuf
   unset -nocomplain data tmp result word
   set data [lrange $histBuf end-$nr end]

 
   foreach word [regexp -all -inline {\w+} $data] {
     incr tmp([string tolower $word])
   }
 
   # make a list of array..
   set result [array get tmp]
 
   # First sort by name, as a secondary key
   set result [lsort -stride 2 -index 0 $result]
   # Then sort by count, descending, as a primary key
   set result [lsort -stride 2 -index 1 -integer -decreasing $result]
 
   set exclude {
     after with that from says over more about will have said their
     this they been were into what could first some used than
     till lokalt efter säger inte från under vill flera blir alla vara
     blev fick över kommer nytt just direkt
     mycket måste därför ingen hela utan bakom stora bara igen eller andra allt
     aldrig bättre rätt description
   }
 
   # Print the values
   puts "\n[string repeat "=" 3] \[STATS\] [string repeat "=" 23]"
 
   foreach {w c} $result {
     # word must be at minimum 4 chars long
     if {![regexp {^\w{4,}$} $w]} {continue}
     # word must accur at least this many times or more
     if {$c <= 2} {continue}
     if {[regexp $w $exclude match]} {continue}
 
     puts [string repeat " " 4][format "%-10s = %s" $w $c]
   }
   puts "[string repeat "=" 24]"
   puts "Last $nr posts..\n"
}

wordcount 20