home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / wordcount < prev    next >
Text File  |  2020-01-01  |  3KB  |  93 lines

  1. #!/usr/local/bin/wermit +
  2. ;
  3. ; From: Dat Thuc Nguyen
  4. ; Date: 27 May 1999
  5. ; Subject: Incoming Script: Word Frequency
  6. ; URL: http://www.smalltickle.com
  7. ;
  8. ; WORD FREQUENCY COUNTING IS THE EXCEL OF SCRIPTING
  9. ; LANGUAGES SUCH AS AWK AND PERL. WITH OOP, C-KERMIT ALSO
  10. ; HANDLES THAT TRIVIAL TASK COMFORTABLY.
  11. ; THE FOLLOWING SCRIPT DEFINES AND USES THE CLASS WORDS
  12. ; TO COUNT THE OCCURRENCES OF UNIQUE WORDS OF A REGULAR
  13. ; TEXT FILE.
  14. ; THE CLASS WORDS SHIELDS THE IMPLEMENTATION DETAILS AND
  15. ; PROMOTES REUSE, THE FLAGSHIP OF OOP.
  16. ;
  17. ; Invoke with argument 1 = Name of text file.
  18. ; If no name is given, it is prompted for.
  19. ;
  20. ; Requires C-Kermit 7.0 Beta.07 or later.
  21.  
  22. ;**************************************************
  23. ;*   DEFINITION OF THE CLASS WORDS                *
  24. ;**************************************************
  25. define Words {
  26. ; \%1 name of the new Words
  27.      _assign Words::\%1.words \x02
  28.      _define \%1 {
  29.           if define \m(Words::\%1) {
  30.                Words::\%1 \v(macro) {\%2} {\%3} {\%4}
  31.                if FAIL END \v(return)
  32.                return \v(return)
  33.           } else {
  34.                END -9999 doesNotUnderstand
  35.           }
  36.      }
  37. }
  38.  
  39. ;**************************************************
  40. ;*   PUBLIC USAGE INTERFACE OF THE CLASS WORDS    *
  41. ;**************************************************
  42. define Words::add {
  43.      local \%f
  44.      assign \%f \find(\x02\%2\x02, \m(Words::\%1.words))
  45.      if \%f {
  46.           _increment Words::\%1.count[\%2]
  47.      } else {
  48.           _assign Words::\%1.words \m(Words::\%1.words)\%2\x02
  49.           _assign Words::\%1.count[\%2] 1
  50.      }
  51. }
  52. define Words::frequency {
  53.      local \&a[] \%i
  54.      for \%i 1 \fsplit(\m(Words::\%1.words),&a,\x02) 1 {
  55.           echo \&a[\%i] => \m(Words::\%1.count[\&a[\%i]])
  56.      }
  57. }
  58. define Words::destroy {
  59.      local \&a[] \%i
  60.      for \%i 1 \fsplit(\m(Words::\%1.words),&a,\x02) 1 {
  61.           _define Words::\%1.count[\&a[\%i]]
  62.      }
  63.      _define Words::\%1.words
  64.      _define \%1
  65. }
  66.  
  67. ; AND HERE IS THE WORD FREQUENCY COUNTING SCRIPT word_cnt.ksc
  68.  
  69. while not defined \%1 {
  70.     ask \%1 { Filename: }
  71. }
  72. open read \%1
  73. if fail exit 1 Can't open \%1
  74.  
  75. words abc                ; create a dictionary abc
  76. local \%l \%i
  77.  
  78. while true {
  79.     read \%l                ; read a line
  80.     if fail break            ; until EOF
  81.     if > \flength(\%l) 0 {        ; skip empty line
  82.         declare \&a[]            ; allocate a dynamic array
  83.         for \%i 1 \fsplit(\%l,&a) 1 {    ; split line on nonalphanumeric
  84.             abc add \flower(\&a[\%i])    ; add word in lower case to dictionary
  85.         }
  86.     }
  87. }
  88.  
  89. abc frequency                ; display each words and its count
  90. abc destroy                ; get rid of the dictionary
  91.  
  92. exit
  93.