home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / tablw.icn < prev    next >
Text File  |  2000-07-29  |  3KB  |  97 lines

  1. ############################################################################
  2. #
  3. #    File:     tablw.icn
  4. #
  5. #    Subject:  Program to tabulate words in a file
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     December 27, 1989
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #  
  17. #     This program tabulates words and lists number of times each
  18. #  word occurs. A word is defined to be a string of consecutive
  19. #  upper- and lowercase letters with at most one interior occurrence
  20. #  of a dash or apostrophe.
  21. #  
  22. #  Options: The following options are available:
  23. #  
  24. #       -a   Write the summary in alphabetical order of the words.
  25. #            This is the default.
  26. #  
  27. #       -i   Ignore case distinctions among letters; uppercase
  28. #            letters are mapped into to corresponding lowercase
  29. #            letters on input. The default is to maintain case dis-
  30. #            tinctions.
  31. #  
  32. #       -n   Write the summary in numerical order of the counts.
  33. #  
  34. #       -l n Tabulate only words longer than n characters. The
  35. #            default is to tabulate all words.
  36. #  
  37. #       -u   Write only the words that occur just once.
  38. #  
  39. ############################################################################
  40. #
  41. #  Links: options, usage
  42. #
  43. ############################################################################
  44.  
  45. link options, usage
  46.  
  47. global limit, icase
  48.  
  49. procedure main(args)
  50.    local wcount, unique, order, s, pair, lwidth, rwidth, max, opts, l, i
  51.  
  52.    limit := 0                # lower limit on usage to list
  53.    unique := 0                # switch to list unique usage only
  54.    order := 3                # alphabetical ordering switch
  55.  
  56.    opts := options(args,"ail+nu")
  57.    if \opts["a"] then order := 3
  58.    if \opts["n"] then order := 4
  59.    if \opts["u"] then unique := 1
  60.    if \opts["i"] then icase := 1
  61.    l := \opts["l"] | 1
  62.    if l <= 0 then Usage("-l needs positive parameter")
  63.  
  64.    wcount := table(0)            # table of words
  65.    every wcount[words()] +:= 1
  66.    wcount := sort(wcount,order)
  67.    if unique = 1 then {
  68.       while s := get(wcount) do
  69.          if get(wcount) = 1 then write(s)
  70.       }
  71.    else {
  72.       max := 0
  73.       rwidth := 0
  74.       i := 1
  75.       while i < *wcount do {
  76.          max <:= *wcount[i]
  77.          rwidth <:= *wcount[i +:= 1]
  78.      }
  79.       lwidth := max + 3
  80.       while write(left(get(wcount),lwidth),right(get(wcount),rwidth))
  81.       }
  82. end
  83.  
  84. #  generate words
  85. #
  86. procedure words()
  87.    local line, word
  88.    while line := read() do {
  89.       if \icase then line := map(line)
  90.       line ? while tab(upto(&letters)) do {
  91.          word := tab(many(&letters)) || ((tab(any('-\'')) ||
  92.             tab(many(&letters))) | "")
  93.          if *word > limit then suspend word
  94.          }
  95.       }
  96. end
  97.