home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-11-23 | 2.8 KB | 95 lines | [TEXT/PICN] |
- ############################################################################
- #
- # This program produces a simple concordance to standard
- # output. Words less than three chracters long are ignored.
- #
- # There are two options:
- #
- # -l n set maximum line length to n (default 72), starts new line
- # -w n set maximum width for word to n (default to 12), truncates
- #
- # There are lots of possibilities for improving this program and adding
- # functionality to it. For example, a list of words to be ignored could be
- # provided. The formatting could be made more flexible, and so on.
- #
- ############################################################################
- #
- # This program uses a table whose keys are strings and whose values are
- # tables. In these tables, keys are line numbers and values are counts.
- # This is a little elaborate, but it does the job.
- #
- # Note that the program is organized to make it easy (via item()) to
- # handle other kinds of tabulations.
- #
- ############################################################################
- #
- # Caution: The programming technqiue used in this program requires a
- # lot of memory. Do not attempt to use this program to produce
- # a concordance of a large file.
- #
- #############################################################################
- #
- # Links: getopt
- #
- ############################################################################
-
- link getopt
-
- global uses, colmax, namewidth, lineno
-
- procedure main(args)
-
- opts := getopt(args,"l+w+")[1]
- colmax := \opts["l"] | 72
- namewidth := \opts["w"] | 12
-
- uses := table()
- lineno := 0
-
- every tabulate(item(line),lineno)
-
- uselist := sort(uses,3) # sort by uses
- while name := get(uselist) do {
- line := left(name,namewidth) # build up line numbers
- numbers := sort(get(uselist),3)
- while line ||:= get(numbers) do {
- count := get(numbers)
- if count = 1 then line ||:= ", "
- else line ||:= "(" || count || "), "
- }
- format(line) # output results in desired format
- }
- end
-
- procedure tabulate(name,lineno)
- /uses[name] := table(0)
- uses[name][lineno] +:= 1
- end
-
- procedure format(line)
- while *line > colmax + 2 do {
- i := colmax + 2
- until line[i -:= 1] == " " # back off to break point
- write(line[1:i])
- line := repl(" ",namewidth) || line[i + 1:0]
- }
- write(line[1:-2])
- end
-
- procedure item()
-
- name := getfile("File for concordance?") | fail
- infile := open(name) | stop("*** cannot open ",name)
- while line := read(infile) do {
- lineno +:= 1
- write(right(lineno,6)," ",line)
- line := map(line) # fold to lowercase
- i := 1
- line ? while tab(upto(&letters)) do {
- word := tab(many(&letters))
- if *word < 3 then next # skip short words
- suspend word
- }
- }
- end
-