From: ram@cs.umb.edu (Robert Morris)
Newsgroups: comp.text.interleaf
Subject: Re: Wordcount??
Date: 8 Oct 92 15:45:57 GMT
Article-I.D.: claude.RAM.92Oct8104557
Reply-To: ram@cs.umb.edu
Organization: University of Massachusetts at Boston

In article <wargopl.718484869@sun.soe.clarkson.edu> wargopl@sun.soe.clarkson.edu (Peter L. Wargo) writes:

   Is there a LISP script around that will do a wordcount on an Interleaf
   document?  Our foreign translators charge by the word, and I need to
   give them an estimate...

   -Pete

If you have a lot of documents and want to automate this, you may well
find it easier and faster to run unix shell scripts which
	(a). Run Interleaf in batch mode to make the files interleaf ascii
	(b). Run textfilt to strip markup
	(c). Use unix wc program to count words

But here's lisp code based on code we use to count words in linguistic
corpus work.  Note these defects: (a). White space at the end of a
component is miscounted as a word. [This feature inserted for people
paid by the word.... ;-) ] (b). There may/should be some Interleaf
internal lisp functions which do a few of the tasks, perhaps without
bugs. (c). A stick-up interface putting up the word count is probably
not what you want. Probably you want to write the result of word-count
to a file with the current document name. (d). The whole thing was
cobbled up in 20 minutes and might be bizarrely crude. I welcome
anyone's improvement.

To use it, load the lisp (e.g. in your profile) and make a lisp file
containing an invocation into your  Custom->NoSelection cabinet. You
can then invoke it from the desktop whenever you have a document open.
The lisp file for invocation can contain just the lines
	(lisp-set-implementation "Interleaf Lisp" "2.0")
	(show-word-count)


Bob Morris
ram@cs.umb.edu

--------------------
(lisp-set-implementation "Interleaf Lisp" "2.0")

;; Module name: Count.lsp
;; Purpose: word count
;; Notes: confused by whitespace at ends of components
;; Interfaces:

;; Audit:
;; DD-MON-YY USERNAME	COMMENT
;; 07-10-92  ram@cs.umb.edu
;;
;; Copyright 1992 Robert A. Morris 
;; Department of Mathematics and Computer Science
;; UMASS-Boston, Boston, MA 02125

;; This program may be freely copied without further permission under
;; the terms of the Free Software Foundation General Public License,
;; which says among other things that if you provide it 
;; to anyone else you must do so in source form
;; and with the same license conditions.  
;; A copy of the General Public License can be obtained from 
;; the author or the Free Software Foundation.


(defun word-count (mark)
  "Count words from MARK to end of document"
  (let ( (count 0))
    (while (setq mark (forward-word mark))
      (setq count (1+ count)))
    count))

			     

(defun show-word-count ()
"Display a stickup with word count of document"
  (let ((mark (doc-point-marker)))
      ;first find doc beginning. Isn't there an Interleaf function which does this???
    (while (> (tell mark mid:move-by -1 :by :cmpn-beginnings) 0 )) ;;now at beginning
    (stk-open (format nil "~A" (word-count mark)))))




(defun mark-is-at-doc-end (mark)
"Return t if MARK is at the end of the document, else return nil."
  (let ((m1 (tell mark mid:copy))
        (m2 (tell mark mid:copy)))
    (and 
     (> (tell m1 mid:move-by 1 :by :cmpn-endings) 0) ;in last cmpn
     (> (tell m2 mid:move-by 1 ) 0 )))) ;at last token

(defun forward-word (mark)
"Move MARK to the position after the last token in the first word after its
initial position, and return MARK or or nil if there is no word after it"
;;BUG thinks white space at end of cmpn is a word.
;;;;Isn't there an Interleaf function which does this bug free??
;; NOTE SIDE EFFECT ON MARK

(if (> (tell mark mid:move-by 1 :by :word-endings) 0)
    (progn                            ;we reached a component end
      (if (mark-is-at-doc-end mark) 
	  nil
	(tell mark mid:move-by 1 :by :cmpn-beginnings)
	(forward-word mark)))
    mark))
Newsgroups: comp.text.interleaf