Posted to tcl by dandyn at Sun Nov 24 20:49:38 GMT 2024view raw
- namespace import ::tcl::prefix
- set histBuf {trump Trump trump Trump apple trump trumps apple trumps trump trumps trump hello hello hello hello helloo apple helloo helloo}
- 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] {
- if {$word in "trumps helloo"} {
- switch [prefix match {trumps helloo} $word] {
- trumps {incr tmp([string tolower trump]) ; continue}
- helloo {incr tmp([string tolower hello]) ; continue}
- }
- }
- 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
Add a comment