home *** CD-ROM | disk | FTP | other *** search
- #!/usr/local/bin/wermit +
- ;
- ; From: Dat Thuc Nguyen
- ; Date: 27 May 1999
- ; Subject: Incoming Script: Word Frequency
- ; URL: http://www.smalltickle.com
- ;
- ; WORD FREQUENCY COUNTING IS THE EXCEL OF SCRIPTING
- ; LANGUAGES SUCH AS AWK AND PERL. WITH OOP, C-KERMIT ALSO
- ; HANDLES THAT TRIVIAL TASK COMFORTABLY.
- ; THE FOLLOWING SCRIPT DEFINES AND USES THE CLASS WORDS
- ; TO COUNT THE OCCURRENCES OF UNIQUE WORDS OF A REGULAR
- ; TEXT FILE.
- ; THE CLASS WORDS SHIELDS THE IMPLEMENTATION DETAILS AND
- ; PROMOTES REUSE, THE FLAGSHIP OF OOP.
- ;
- ; Invoke with argument 1 = Name of text file.
- ; If no name is given, it is prompted for.
- ;
- ; Requires C-Kermit 7.0 Beta.07 or later.
-
- ;**************************************************
- ;* DEFINITION OF THE CLASS WORDS *
- ;**************************************************
- define Words {
- ; \%1 name of the new Words
- _assign Words::\%1.words \x02
- _define \%1 {
- if define \m(Words::\%1) {
- Words::\%1 \v(macro) {\%2} {\%3} {\%4}
- if FAIL END \v(return)
- return \v(return)
- } else {
- END -9999 doesNotUnderstand
- }
- }
- }
-
- ;**************************************************
- ;* PUBLIC USAGE INTERFACE OF THE CLASS WORDS *
- ;**************************************************
- define Words::add {
- local \%f
- assign \%f \find(\x02\%2\x02, \m(Words::\%1.words))
- if \%f {
- _increment Words::\%1.count[\%2]
- } else {
- _assign Words::\%1.words \m(Words::\%1.words)\%2\x02
- _assign Words::\%1.count[\%2] 1
- }
- }
- define Words::frequency {
- local \&a[] \%i
- for \%i 1 \fsplit(\m(Words::\%1.words),&a,\x02) 1 {
- echo \&a[\%i] => \m(Words::\%1.count[\&a[\%i]])
- }
- }
- define Words::destroy {
- local \&a[] \%i
- for \%i 1 \fsplit(\m(Words::\%1.words),&a,\x02) 1 {
- _define Words::\%1.count[\&a[\%i]]
- }
- _define Words::\%1.words
- _define \%1
- }
-
- ; AND HERE IS THE WORD FREQUENCY COUNTING SCRIPT word_cnt.ksc
-
- while not defined \%1 {
- ask \%1 { Filename: }
- }
- open read \%1
- if fail exit 1 Can't open \%1
-
- words abc ; create a dictionary abc
- local \%l \%i
-
- while true {
- read \%l ; read a line
- if fail break ; until EOF
- if > \flength(\%l) 0 { ; skip empty line
- declare \&a[] ; allocate a dynamic array
- for \%i 1 \fsplit(\%l,&a) 1 { ; split line on nonalphanumeric
- abc add \flower(\&a[\%i]) ; add word in lower case to dictionary
- }
- }
- }
-
- abc frequency ; display each words and its count
- abc destroy ; get rid of the dictionary
-
- exit
-