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 / kwic.icn < prev    next >
Text File  |  2000-07-29  |  3KB  |  99 lines

  1. ############################################################################
  2. #
  3. #    File:     kwic.icn
  4. #
  5. #    Subject:  Program to produce keywords in context
  6. #
  7. #    Author:   Stephen B. Wampler, modified by Ralph E. Griswold
  8. #
  9. #    Date:     February 15, 1995
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #     This is a simple keyword-in-context (KWIC) program. It reads from
  18. #  standard input and writes to standard output. The "key" words are
  19. #  aligned in column 40, with the text shifted as necessary. Text shifted
  20. #  left is truncated at the left. Tabs and other characters whose "print width"
  21. #  is less than one may not be handled properly.
  22. #
  23. #     If an integer is given on the command line, it overrides the default
  24. #  40.
  25. #
  26. #     Some noise words are omitted (see "exceptions" in the program text).
  27. #  If a file named except.wrd is open and readable in the current directory,
  28. #  the words in it are used instead.
  29. #
  30. #     This program is pretty simple.  Possible extensions include ways
  31. #  of specifying words to be omitted, more flexible output formatting, and
  32. #  so on.  Another "embellisher's delight".
  33. #
  34. ############################################################################
  35.  
  36. global line, loc, exceptions, width
  37.  
  38. procedure main(args)
  39.    local exceptfile
  40.  
  41.    width := integer(args[1]) | 40
  42.  
  43.    if exceptfile := open("except.wrd") then {
  44.       exceptions := set()
  45.       every insert(exceptions, lcword(exceptfile))
  46.       close(exceptfile)
  47.       }
  48.    else
  49.       exceptions := set(["or", "in", "the", "to", "of", "on", "a",
  50.          "an", "at", "and", "i", "it", "by", "for"])
  51.  
  52.    every write(kwic(&input))
  53.  
  54. end
  55.  
  56. procedure kwic(file)
  57.    local index, word
  58.  
  59. #  Each word, in lowercase form, is a key in the table "index".
  60. #  The corresponding values are lists of the positioned lines
  61. #  for that word.  This method may use an impractically large
  62. #  amount of space for large input files.
  63.  
  64.    index := table()
  65.    every word := lcword(file) do {
  66.       if not member(exceptions,word) then {
  67.          /index[word] := []
  68.          index[word] := put(index[word],position())
  69.          }
  70.       }
  71.  
  72. #  Before the new sort options, it was done this way -- the code preserved
  73. #  as an example of "generators in action".
  74.  
  75. #  suspend !((!sort(index,1))[2])
  76.  
  77.    index := sort(index,3)
  78.    while get(index) do
  79.       suspend !get(index)
  80. end
  81.  
  82. procedure lcword(file)
  83.    static chars
  84.    initial chars := &ucase ++ &lcase ++ &digits ++ '\''
  85.    every line := !file do
  86.       line ? while tab(loc := upto(chars)) do
  87.          suspend map(tab(many(chars)) \ 1)
  88. end
  89.  
  90. procedure position()
  91.    local offset
  92.  
  93. #  Note that "line" and ""loc" are global.
  94.  
  95.    offset := width - loc
  96.    if offset >= 0 then return repl(" ",offset) || line
  97.    else return line[-offset + 1:0]
  98. end
  99.