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

  1. set histBuf {trump Trump trump Trump trump trumps trumps trump trumps hello hello hello hello hello hello trump}
  2. proc wordcount {nr} {
  3. global histBuf
  4. unset -nocomplain data tmp result word
  5. set data [lrange $histBuf end-$nr end]
  6.  
  7.  
  8. foreach word [regexp -all -inline {\w+} $data] {
  9. incr tmp([string tolower $word])
  10. }
  11.  
  12. # make a list of array..
  13. set result [array get tmp]
  14.  
  15. # First sort by name, as a secondary key
  16. set result [lsort -stride 2 -index 0 $result]
  17. # Then sort by count, descending, as a primary key
  18. set result [lsort -stride 2 -index 1 -integer -decreasing $result]
  19.  
  20. set exclude {
  21. after with that from says over more about will have said their
  22. this they been were into what could first some used than
  23. till lokalt efter säger inte från under vill flera blir alla vara
  24. blev fick över kommer nytt just direkt
  25. mycket måste därför ingen hela utan bakom stora bara igen eller andra allt
  26. aldrig bättre rätt description
  27. }
  28.  
  29. # Print the values
  30. puts "\n[string repeat "=" 3] \[STATS\] [string repeat "=" 23]"
  31.  
  32. foreach {w c} $result {
  33. # word must be at minimum 4 chars long
  34. if {![regexp {^\w{4,}$} $w]} {continue}
  35. # word must accur at least this many times or more
  36. if {$c <= 2} {continue}
  37. if {[regexp $w $exclude match]} {continue}
  38.  
  39. puts [string repeat " " 4][format "%-10s = %s" $w $c]
  40. }
  41. puts "[string repeat "=" 24]"
  42. puts "Last $nr posts..\n"
  43. }
  44.  
  45. wordcount 20