home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mep1 / Samples / Programs / concordance.icn < prev    next >
Encoding:
Text File  |  1990-11-23  |  2.8 KB  |  95 lines  |  [TEXT/PICN]

  1. ############################################################################
  2. #
  3. #     This program produces a simple concordance to standard
  4. #  output. Words less than three chracters long are ignored.
  5. #
  6. #     There are two options:
  7. #
  8. #    -l n    set maximum line length to n (default 72), starts new line
  9. #    -w n    set maximum width for word to n (default to 12), truncates
  10. #
  11. #     There are lots of possibilities for improving this program and adding
  12. #  functionality to it. For example, a list of words to be ignored could be
  13. #  provided.  The formatting could be made more flexible, and so on.
  14. #
  15. ############################################################################
  16. #
  17. #     This program uses a table whose keys are strings and whose values are
  18. #  tables. In these tables, keys are line numbers and values are counts.
  19. #  This is a little elaborate, but it does the job.
  20. #
  21. #     Note that the program is organized to make it easy (via item()) to
  22. #  handle other kinds of tabulations.
  23. #
  24. ############################################################################
  25. #
  26. #  Caution: The programming technqiue used in this program requires a
  27. #           lot of memory. Do not attempt to use this program to produce
  28. #           a concordance of a large file.
  29. #############################################################################
  30. #
  31. #  Links: getopt
  32. #
  33. ############################################################################
  34.  
  35. link getopt
  36.  
  37. global uses, colmax, namewidth, lineno
  38.  
  39. procedure main(args)
  40.  
  41.    opts := getopt(args,"l+w+")[1]
  42.    colmax := \opts["l"] | 72
  43.    namewidth := \opts["w"] | 12
  44.  
  45.    uses := table()
  46.    lineno := 0
  47.  
  48.    every tabulate(item(line),lineno)
  49.  
  50.    uselist := sort(uses,3)        # sort by uses
  51.    while name := get(uselist) do {
  52.       line := left(name,namewidth)    # build up line numbers
  53.       numbers := sort(get(uselist),3)
  54.       while line ||:= get(numbers) do {
  55.          count := get(numbers)
  56.          if count = 1 then line ||:= ", "
  57.             else line ||:= "(" || count || "), "
  58.          }
  59.       format(line)    # output results in desired format
  60.       }
  61. end
  62.  
  63. procedure tabulate(name,lineno)
  64.    /uses[name] := table(0)
  65.    uses[name][lineno] +:= 1
  66. end
  67.  
  68. procedure format(line)
  69.    while *line > colmax + 2 do {
  70.       i := colmax + 2
  71.       until line[i -:= 1] == " "        # back off to break point
  72.       write(line[1:i])
  73.       line := repl(" ",namewidth) || line[i + 1:0]
  74.       }
  75.    write(line[1:-2])
  76. end
  77.  
  78. procedure item()
  79.  
  80.    name := getfile("File for concordance?") | fail
  81.    infile := open(name) | stop("*** cannot open ",name)
  82.    while line := read(infile) do {
  83.       lineno +:= 1
  84.       write(right(lineno,6),"  ",line)
  85.       line := map(line)                # fold to lowercase
  86.       i := 1
  87.       line ? while tab(upto(&letters)) do {
  88.          word := tab(many(&letters))
  89.          if *word < 3 then next            # skip short words
  90.          suspend word
  91.          }
  92.       }
  93. end
  94.